diff --git a/sonarqube/resource_sonarqube_permissions.go b/sonarqube/resource_sonarqube_permissions.go index 3e9b3d9b..6c83ac5b 100644 --- a/sonarqube/resource_sonarqube_permissions.go +++ b/sonarqube/resource_sonarqube_permissions.go @@ -101,6 +101,10 @@ func resourceSonarqubePermissionsCreate(d *schema.ResourceData, m interface{}) e // template user permission sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/add_user_to_template" RawQuery.Add("templateId", templateID.(string)) + // name provide instead of id + } else if templateName, ok := d.GetOk("template_name"); ok { + sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/add_user_to_template" + RawQuery.Add("templateName", templateName.(string)) } else { // direct user permission sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/add_user" diff --git a/sonarqube/resource_sonarqube_permissions_test.go b/sonarqube/resource_sonarqube_permissions_test.go index 646fa31d..ce7a7f89 100644 --- a/sonarqube/resource_sonarqube_permissions_test.go +++ b/sonarqube/resource_sonarqube_permissions_test.go @@ -67,7 +67,7 @@ func testAccSonarqubePermissionLoginNameConfig(id string, loginName string, perm name = "%[2]s" password = "secret-sauce37!" } - + resource "sonarqube_permissions" "%[1]s" { login_name = sonarqube_user.%[1]s.name permissions = %[3]s @@ -94,3 +94,42 @@ func TestAccSonarqubePermissionLoginName(t *testing.T) { }, }) } + +func testAccSonarqubePermissionLoginNameTemplateNameConfig(id string, loginName string, permissions []string) string { + formattedPermissions := generateHCLList(permissions) + return fmt.Sprintf(` + resource "sonarqube_user" "%[1]s" { + login_name = "%[2]s" + name = "%[2]s" + password = "secret-sauce37!" + } + + resource "sonarqube_permission_template" "this" { + name = "foo" + } + resource "sonarqube_permissions" "%[1]s" { + login_name = sonarqube_user.%[1]s.name + template_name = sonarqube_permission_template.this.name + permissions = %[3]s + } + `, id, loginName, formattedPermissions) +} + +func TestAccSonarqubePermissionLoginNameTemplateName(t *testing.T) { + rnd := generateRandomResourceName() + name := "sonarqube_permissions." + rnd + permissions := []string{"codeviewer", "scan"} + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccSonarqubePermissionLoginNameTemplateNameConfig(rnd, "testAccSonarqubePermissions", permissions), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(name, "login_name", "testAccSonarqubePermissions"), + ), + }, + }, + }) +}