Skip to content

Commit e63d5a7

Browse files
fix related resource ignored in permissions
1 parent 205d0ec commit e63d5a7

6 files changed

+48
-22
lines changed

codefresh/cfclient/permission.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Permission struct {
99
ID string `json:"id,omitempty"`
1010
Team string `json:"role,omitempty"`
1111
Resource string `json:"resource,omitempty"`
12-
RelatedResource string `json:"related_resource,omitempty"`
12+
RelatedResource string `json:"relatedResource,omitempty"`
1313
Action string `json:"action,omitempty"`
1414
Account string `json:"account,omitempty"`
1515
Tags []string `json:"attributes,omitempty"`
@@ -20,7 +20,7 @@ type NewPermission struct {
2020
ID string `json:"_id,omitempty"`
2121
Team string `json:"team,omitempty"`
2222
Resource string `json:"resource,omitempty"`
23-
RelatedResource string `json:"related_resource,omitempty"`
23+
RelatedResource string `json:"relatedResource,omitempty"`
2424
Action string `json:"action,omitempty"`
2525
Account string `json:"account,omitempty"`
2626
Tags []string `json:"tags,omitempty"`

codefresh/cfclient/user.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ func (client *Client) GetAllUsers() (*[]User, error) {
228228
var allUsers []User
229229

230230
for !bIsDone {
231-
var userPaginatedResp struct{Docs []User `json:"docs"`}
231+
var userPaginatedResp struct {
232+
Docs []User `json:"docs"`
233+
}
232234

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

250252
if len(userPaginatedResp.Docs) > 0 {
251-
allUsers = append(allUsers,userPaginatedResp.Docs...)
253+
allUsers = append(allUsers, userPaginatedResp.Docs...)
252254
nPageIndex++
253255
} else {
254256
bIsDone = true

codefresh/data_account_gitops_settings.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ func dataSourceAccountGitopsSettings() *schema.Resource {
1313
Read: dataSourceAccountGitopsSettingsRead,
1414
Schema: map[string]*schema.Schema{
1515
"id": {
16-
Type: schema.TypeString,
16+
Type: schema.TypeString,
1717
Description: "Account Id",
18-
Computed: true,
18+
Computed: true,
1919
},
2020
"name": {
21-
Type: schema.TypeString,
22-
Computed: true,
21+
Type: schema.TypeString,
22+
Computed: true,
2323
Description: "Account name for active account",
2424
},
2525
"git_provider": {
26-
Type: schema.TypeString,
27-
Computed: true,
26+
Type: schema.TypeString,
27+
Computed: true,
2828
Description: "Git provider name",
2929
},
3030
"git_provider_api_url": {
31-
Type: schema.TypeString,
32-
Computed: true,
31+
Type: schema.TypeString,
32+
Computed: true,
3333
Description: "Git provider API url",
3434
},
3535
"shared_config_repository": {
36-
Type: schema.TypeString,
37-
Computed: true,
36+
Type: schema.TypeString,
37+
Computed: true,
3838
Description: "Shared config repository url",
3939
},
4040
"admins": {

codefresh/resource_account_gitops_settings.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func resourceAccountGitopsSettings() *schema.Resource {
2424
Delete: resourceAccountGitopsSettingsDelete,
2525
Schema: map[string]*schema.Schema{
2626
"id": {
27-
Type: schema.TypeString,
27+
Type: schema.TypeString,
2828
Description: "Account Id",
29-
Computed: true,
29+
Computed: true,
3030
},
3131
"name": {
3232
Type: schema.TypeString,

codefresh/resource_permission.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
99
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/internal/datautil"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
funk "github.com/thoas/go-funk"
@@ -96,7 +97,12 @@ The tags for which to apply the permission. Supports two custom tags:
9697
},
9798
},
9899
},
99-
CustomizeDiff: resourcePermissionCustomDiff,
100+
CustomizeDiff: customdiff.All(
101+
resourcePermissionCustomDiff,
102+
customdiff.ForceNewIfChange("related_resource", func(ctx context.Context, oldValue, newValue, meta interface{}) bool {
103+
return true
104+
}),
105+
),
100106
}
101107
}
102108

@@ -206,6 +212,11 @@ func mapPermissionToResource(permission *cfclient.Permission, d *schema.Resource
206212
return err
207213
}
208214

215+
err = d.Set("related_resource", permission.RelatedResource)
216+
if err != nil {
217+
return err
218+
}
219+
209220
err = d.Set("tags", permission.Tags)
210221
if err != nil {
211222
return err
@@ -224,11 +235,12 @@ func mapResourceToPermission(d *schema.ResourceData) *cfclient.Permission {
224235
tags = []string{"*", "untagged"}
225236
}
226237
permission := &cfclient.Permission{
227-
ID: d.Id(),
228-
Team: d.Get("team").(string),
229-
Action: d.Get("action").(string),
230-
Resource: d.Get("resource").(string),
231-
Tags: tags,
238+
ID: d.Id(),
239+
Team: d.Get("team").(string),
240+
Action: d.Get("action").(string),
241+
Resource: d.Get("resource").(string),
242+
RelatedResource: d.Get("related_resource").(string),
243+
Tags: tags,
232244
}
233245

234246
return permission

codefresh/resource_permission_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ func TestAccCodefreshPermissionConfig(t *testing.T) {
2727
resource.TestCheckResourceAttr(resourceName, "action", "create"),
2828
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
2929
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
30+
resource.TestCheckResourceAttr(resourceName, "related_resource",""),
31+
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
32+
),
33+
},
34+
{
35+
Config: testAccCodefreshPermissionConfig("create", "pipeline", "project", []string{"production", "*"}),
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckCodefreshPermissionExists(resourceName),
38+
resource.TestCheckResourceAttr(resourceName, "action", "create"),
39+
resource.TestCheckResourceAttr(resourceName, "resource", "pipeline"),
40+
resource.TestCheckResourceAttr(resourceName, "related_resource", "project"),
41+
resource.TestCheckResourceAttr(resourceName, "tags.0", "*"),
3042
resource.TestCheckResourceAttr(resourceName, "tags.1", "production"),
3143
),
3244
},

0 commit comments

Comments
 (0)