Skip to content

Commit

Permalink
Update project settings definition
Browse files Browse the repository at this point in the history
  • Loading branch information
pnag90 committed Oct 14, 2023
1 parent 6d6b213 commit 7f084ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
16 changes: 10 additions & 6 deletions sonarqube/resource_sonarqube_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,22 @@ func resourceSonarqubeProjectRead(d *schema.ResourceData, m interface{}) error {
// Get settings
var projectSettings []Setting
if _, ok := d.GetOk("setting"); ok {
projectSettings, err = getComponentSettings(d.Id(), m, true)
componentSettings := d.Get("setting").([]interface{})
projectSettings, err = getComponentSettings(d.Id(), m)
if err != nil {
return fmt.Errorf("resourceSonarqubeProjectRead: Failed to read project settings: %+v", err)
}

if len(projectSettings) > 0 {
settings := make([]interface{}, len(projectSettings))
for i, s := range projectSettings {
settings[i] = s.ToMap()
settings := make([]interface{}, len(componentSettings))
for i, s := range componentSettings {
key := s.(map[string]interface{})["key"].(string)
for _, apiSetting := range projectSettings {
if key == apiSetting.Key {
settings[i] = apiSetting.ToMap()
}
}
d.Set("setting", settings)
}
d.Set("setting", settings)
}

if len(projectReadResponse.Component.Tags) > 0 {
Expand Down
28 changes: 14 additions & 14 deletions sonarqube/resource_sonarqube_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func testAccSonarqubeProjectTagsConfig(rnd string, name string, project string,
name = "%[2]s"
project = "%[3]s"
visibility = "%[4]s"
tags = %[5]s // Note that the "" should be missing since this is a list
tags = %[5]s // Note that the "" should be missing since this is a list
}
`, rnd, name, project, visibility, formattedTags)
}
Expand All @@ -51,8 +51,8 @@ func testAccSonarqubeProjectSettingsConfig(rnd string, name string, project stri
visibility = "%[4]s"
setting {
key = "sonar.demo"
value = "[email protected]"
key = "sonar.docker.activate"
value = "false"
}
}
`, rnd, name, project, visibility)
Expand All @@ -68,8 +68,8 @@ func testAccSonarqubeProjectSettingsMultiple(rnd string, key string, name string
visibility = "public"
setting {
key = "sonar.demo"
value = "[email protected]"
key = "sonar.terraform.activate"
value = "true"
}
setting {
Expand Down Expand Up @@ -276,8 +276,8 @@ func TestAccSonarqubeProjectSettingsCreate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "project", "testAccSonarqubeProject"),
resource.TestCheckResourceAttr(name, "setting.#", strconv.Itoa(expectedSettings)),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.demo"),
resource.TestCheckResourceAttr(name, "setting.0.value", "[email protected]"),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.docker.activate"),
resource.TestCheckResourceAttr(name, "setting.0.value", "false"),
),
},
},
Expand All @@ -304,8 +304,8 @@ func TestAccSonarqubeProjectSettingsUpdate(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "project", "testAccSonarqubeProject"),
resource.TestCheckResourceAttr(name, "setting.#", "1"),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.demo"),
resource.TestCheckResourceAttr(name, "setting.0.value", "[email protected]"),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.docker.activate"),
resource.TestCheckResourceAttr(name, "setting.0.value", "false"),
),
},
},
Expand All @@ -316,7 +316,7 @@ func TestAccSonarqubeProjectSettingsTypes(t *testing.T) {
rnd := generateRandomResourceName()
name := "sonarqube_project." + rnd
expectedConditions := 3
values := []string{"foo", "bar"}
values := []string{"main", "release"}
fieldValues := map[string]string{"ruleKey": "foo", "resourceKey": "bar"}

resource.Test(t, resource.TestCase{
Expand All @@ -328,11 +328,11 @@ func TestAccSonarqubeProjectSettingsTypes(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "project", "testAccSonarqubeProject"),
resource.TestCheckResourceAttr(name, "setting.#", strconv.Itoa(expectedConditions)),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.demo"),
resource.TestCheckResourceAttr(name, "setting.0.value", "[email protected]"),
resource.TestCheckResourceAttr(name, "setting.0.key", "sonar.terraform.activate"),
resource.TestCheckResourceAttr(name, "setting.0.value", "true"),
resource.TestCheckResourceAttr(name, "setting.1.key", "sonar.dbcleaner.branchesToKeepWhenInactive"),
resource.TestCheckTypeSetElemAttr(name, "setting.1.values.*", "foo"),
resource.TestCheckTypeSetElemAttr(name, "setting.1.values.*", "bar"),
resource.TestCheckTypeSetElemAttr(name, "setting.1.values.*", "main"),
resource.TestCheckTypeSetElemAttr(name, "setting.1.values.*", "release"),
resource.TestCheckResourceAttr(name, "setting.2.key", "sonar.issue.ignore.multicriteria"),
resource.TestCheckTypeSetElemNestedAttrs(name, "setting.2.field_values.*", fieldValues),
),
Expand Down
20 changes: 10 additions & 10 deletions sonarqube/resource_sonarqube_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type GetSettings struct {
func (a Setting) ToMap() map[string]interface{} {
obj := make(map[string]interface{})

obj["key"] = a.Key
obj["value"] = a.Value
if a.Values != nil {
obj["values"] = a.Values
Expand Down Expand Up @@ -221,7 +222,7 @@ func getCreateOrUpdateQueryRawQuery(key []string, d *schema.ResourceData) string
}

/* This content is used for settings parameter in multiple resources ('project', 'portfolio') */
func getComponentSettings(component string, m interface{}, filterInherited bool) ([]Setting, error) {
func getComponentSettings(component string, m interface{}) ([]Setting, error) {
if component == "" {
return []Setting{}, nil
}
Expand Down Expand Up @@ -249,11 +250,10 @@ func getComponentSettings(component string, m interface{}, filterInherited bool)

settingsList := make([]Setting, 0)
// Filter settings by parameter inherited
for _, e := range settingReadResponse.Setting {
if !filterInherited || (filterInherited && !e.Inherited) {
settingsList = append(settingsList, e)
}
}
settingsList = append(settingsList, settingReadResponse.Setting...)
// for _, e := range settingReadResponse.Setting {
// settingsList = append(settingsList, e)
// }

// Make sure the order is always the same for when we are comparing lists of conditions
sort.Slice(settingsList, func(i, j int) bool {
Expand All @@ -268,12 +268,12 @@ func synchronizeSettings(d *schema.ResourceData, m interface{}) (bool, error) {
componentId := d.Id()
componentSettings := d.Get("setting").([]interface{})

apiComponentSettings, _ := getComponentSettings(componentId, m, false)
apiComponentSettings, _ := getComponentSettings(componentId, m)

// Make sure the order is always the same for when we are comparing lists of conditions
sort.Slice(componentSettings, func(i, j int) bool {
return componentSettings[i].(map[string]interface{})["key"].(string) < componentSettings[j].(map[string]interface{})["key"].(string)
})
// sort.Slice(componentSettings, func(i, j int) bool {
// return componentSettings[i].(map[string]interface{})["key"].(string) < componentSettings[j].(map[string]interface{})["key"].(string)
// })

// Determine which conditions have been added or changed and update those
for _, s := range componentSettings {
Expand Down

0 comments on commit 7f084ed

Please sign in to comment.