Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter flatten permissions for project and template #210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions constants/permissions/permissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package permissions

const (
Admin = "admin"
Browse = "user"
ProjectCreation = "provisioning"
ApplicationCreation = "applicationcreator"
PortfolioCreation = "portfoliocreator"
QualityGateAdmin = "gateadmin"
QualityProfileAdmin = "profileadmin"
Scan = "scan"
CodeViewer = "codeviewer"
IssueAdmin = "issueadmin"
SecurityHotspotAdmin = "securityhotspotadmin"
)
19 changes: 19 additions & 0 deletions constants/permissions/permissions_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package permissions

var PERMISSIONS_ORDER_FOR_PROJECT = []string{
Browse,
CodeViewer,
IssueAdmin,
SecurityHotspotAdmin,
Admin,
Scan,
}

var PERMISSIONS_ORDER_GLOBAL = []string{
Admin,
QualityGateAdmin, QualityProfileAdmin,
Scan,
ProjectCreation,
ApplicationCreation,
PortfolioCreation,
}
19 changes: 15 additions & 4 deletions sonarqube/resource_sonarqube_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"fmt"
"net/http"
"net/url"
"slices"

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (lts-community)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (.env.dce.lts.yml)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (lts-developer)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (.env.dce.yml)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (lts-enterprise)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (developer)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)

Check failure on line 8 in sonarqube/resource_sonarqube_permissions.go

View workflow job for this annotation

GitHub Actions / acceptance-tests (enterprise)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/slices)
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
permissionsConst "github.com/jdamata/terraform-provider-sonarqube/constants/permissions"
"github.com/satori/uuid"
)

Expand Down Expand Up @@ -149,6 +151,7 @@

func resourceSonarqubePermissionsRead(d *schema.ResourceData, m interface{}) error {
sonarQubeURL := m.(*ProviderConfiguration).sonarQubeURL
permissionsFilter := permissionsConst.PERMISSIONS_ORDER_FOR_PROJECT

// build the base query
RawQuery := url.Values{
Expand Down Expand Up @@ -176,6 +179,9 @@
sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/template_users"
RawQuery.Add("templateName", templateName.(string))
} else {
if !RawQuery.Has("projectKey") {
permissionsFilter = permissionsConst.PERMISSIONS_ORDER_GLOBAL
}
// direct user permission
sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/users"
}
Expand Down Expand Up @@ -205,7 +211,7 @@
for _, value := range users.Users {
if strings.EqualFold(value.Login, loginName) {
d.Set("login_name", value.Login)
d.Set("permissions", flattenPermissions(&value.Permissions))
d.Set("permissions", flattenPermissions(&value.Permissions, &permissionsFilter))
return nil
}
}
Expand All @@ -220,6 +226,9 @@
sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/template_groups"
RawQuery.Add("templateName", templateName.(string))
} else {
if !RawQuery.Has("projectKey") {
permissionsFilter = permissionsConst.PERMISSIONS_ORDER_GLOBAL
}
// direct group permission
sonarQubeURL.Path = strings.TrimSuffix(sonarQubeURL.Path, "/") + "/api/permissions/groups"
}
Expand Down Expand Up @@ -249,7 +258,7 @@
for _, value := range groups.Groups {
if strings.EqualFold(value.Name, groupName) {
d.Set("group_name", value.Name)
d.Set("permissions", flattenPermissions(&value.Permissions))
d.Set("permissions", flattenPermissions(&value.Permissions, &permissionsFilter))
return nil
}
}
Expand Down Expand Up @@ -339,14 +348,16 @@
return expandedPermissions
}

func flattenPermissions(input *[]string) []interface{} {
func flattenPermissions(input *[]string, filter *[]string) []interface{} {
flatPermissions := make([]interface{}, 0)
if input == nil {
return flatPermissions
}

for _, permission := range *input {
flatPermissions = append(flatPermissions, permission)
if len(*filter) == 0 || slices.Contains(*filter, permission) {
flatPermissions = append(flatPermissions, permission)
}
}

return flatPermissions
Expand Down
Loading