Skip to content

Commit

Permalink
fix related resource ignored in permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-medvedev-codefresh committed Jul 10, 2024
1 parent 205d0ec commit e63d5a7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
4 changes: 2 additions & 2 deletions codefresh/cfclient/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Permission struct {
ID string `json:"id,omitempty"`
Team string `json:"role,omitempty"`
Resource string `json:"resource,omitempty"`
RelatedResource string `json:"related_resource,omitempty"`
RelatedResource string `json:"relatedResource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"attributes,omitempty"`
Expand All @@ -20,7 +20,7 @@ type NewPermission struct {
ID string `json:"_id,omitempty"`
Team string `json:"team,omitempty"`
Resource string `json:"resource,omitempty"`
RelatedResource string `json:"related_resource,omitempty"`
RelatedResource string `json:"relatedResource,omitempty"`
Action string `json:"action,omitempty"`
Account string `json:"account,omitempty"`
Tags []string `json:"tags,omitempty"`
Expand Down
6 changes: 4 additions & 2 deletions codefresh/cfclient/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func (client *Client) GetAllUsers() (*[]User, error) {
var allUsers []User

for !bIsDone {
var userPaginatedResp struct{Docs []User `json:"docs"`}
var userPaginatedResp struct {
Docs []User `json:"docs"`
}

opts := RequestOptions{
Path: fmt.Sprintf("/admin/user?limit=%d&page=%d", limitPerQuery, nPageIndex),
Expand All @@ -248,7 +250,7 @@ func (client *Client) GetAllUsers() (*[]User, error) {
}

if len(userPaginatedResp.Docs) > 0 {
allUsers = append(allUsers,userPaginatedResp.Docs...)
allUsers = append(allUsers, userPaginatedResp.Docs...)
nPageIndex++
} else {
bIsDone = true
Expand Down
20 changes: 10 additions & 10 deletions codefresh/data_account_gitops_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ func dataSourceAccountGitopsSettings() *schema.Resource {
Read: dataSourceAccountGitopsSettingsRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Type: schema.TypeString,
Description: "Account Id",
Computed: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Account name for active account",
},
"git_provider": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Git provider name",
},
"git_provider_api_url": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Git provider API url",
},
"shared_config_repository": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Description: "Shared config repository url",
},
"admins": {
Expand Down
4 changes: 2 additions & 2 deletions codefresh/resource_account_gitops_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func resourceAccountGitopsSettings() *schema.Resource {
Delete: resourceAccountGitopsSettingsDelete,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Type: schema.TypeString,
Description: "Account Id",
Computed: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Expand Down
24 changes: 18 additions & 6 deletions codefresh/resource_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
funk "github.com/thoas/go-funk"
Expand Down Expand Up @@ -96,7 +97,12 @@ The tags for which to apply the permission. Supports two custom tags:
},
},
},
CustomizeDiff: resourcePermissionCustomDiff,
CustomizeDiff: customdiff.All(
resourcePermissionCustomDiff,
customdiff.ForceNewIfChange("related_resource", func(ctx context.Context, oldValue, newValue, meta interface{}) bool {
return true
}),
),
}
}

Expand Down Expand Up @@ -206,6 +212,11 @@ func mapPermissionToResource(permission *cfclient.Permission, d *schema.Resource
return err
}

err = d.Set("related_resource", permission.RelatedResource)
if err != nil {
return err
}

err = d.Set("tags", permission.Tags)
if err != nil {
return err
Expand All @@ -224,11 +235,12 @@ func mapResourceToPermission(d *schema.ResourceData) *cfclient.Permission {
tags = []string{"*", "untagged"}
}
permission := &cfclient.Permission{
ID: d.Id(),
Team: d.Get("team").(string),
Action: d.Get("action").(string),
Resource: d.Get("resource").(string),
Tags: tags,
ID: d.Id(),
Team: d.Get("team").(string),
Action: d.Get("action").(string),
Resource: d.Get("resource").(string),
RelatedResource: d.Get("related_resource").(string),
Tags: tags,
}

return permission
Expand Down
12 changes: 12 additions & 0 deletions codefresh/resource_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "action", "create"),
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
resource.TestCheckResourceAttr(resourceName, "related_resource",""),
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
),
},
{
Config: testAccCodefreshPermissionConfig("create", "pipeline", "project", []string{"production", "*"}),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodefreshPermissionExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "action", "create"),
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
resource.TestCheckResourceAttr(resourceName, "related_resource", "project"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
),
},
Expand Down

0 comments on commit e63d5a7

Please sign in to comment.