Skip to content

Commit 68ee84d

Browse files
authored
[BUG] github_organization_role Ensure role_id is set after Create (#3011)
* Set `role_id` also in `resourceGithubOrganizationRoleCreate`` Since it's a computed value, it should be set in Create and Read Signed-off-by: Timo Sand <timo.sand@f-secure.com> * API doesn't accept `base_role = none` Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Revert unnecessary change Signed-off-by: Timo Sand <timo.sand@f-secure.com> --------- Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 972ef3b commit 68ee84d

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

github/resource_github_organization_role.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,26 @@ func resourceGithubOrganizationRoleCreate(ctx context.Context, d *schema.Resourc
7373
permissionsStr[i] = v.(string)
7474
}
7575

76-
role, _, err := client.Organizations.CreateCustomOrgRole(ctx, orgName, &github.CreateOrUpdateOrgRoleOptions{
76+
createOrUpdateOrgRoleOptions := &github.CreateOrUpdateOrgRoleOptions{
7777
Name: github.String(d.Get("name").(string)),
7878
Description: github.String(d.Get("description").(string)),
79-
BaseRole: github.String(d.Get("base_role").(string)),
8079
Permissions: permissionsStr,
81-
})
80+
}
81+
82+
baseRole := d.Get("base_role").(string)
83+
if baseRole != "none" {
84+
createOrUpdateOrgRoleOptions.BaseRole = github.String(baseRole)
85+
}
86+
87+
role, _, err := client.Organizations.CreateCustomOrgRole(ctx, orgName, createOrUpdateOrgRoleOptions)
8288
if err != nil {
8389
return diag.FromErr(fmt.Errorf("error creating organization role (%s/%s): %w", orgName, d.Get("name").(string), err))
8490
}
8591

8692
d.SetId(fmt.Sprint(role.GetID()))
93+
if err = d.Set("role_id", role.GetID()); err != nil {
94+
return diag.FromErr(err)
95+
}
8796
return nil
8897
}
8998

@@ -123,8 +132,14 @@ func resourceGithubOrganizationRoleRead(ctx context.Context, d *schema.ResourceD
123132
if err = d.Set("description", role.Description); err != nil {
124133
return diag.FromErr(err)
125134
}
126-
if err = d.Set("base_role", role.BaseRole); err != nil {
127-
return diag.FromErr(err)
135+
if role.BaseRole != nil {
136+
if err = d.Set("base_role", role.BaseRole); err != nil {
137+
return diag.FromErr(err)
138+
}
139+
} else {
140+
if err = d.Set("base_role", "none"); err != nil {
141+
return diag.FromErr(err)
142+
}
128143
}
129144
if err = d.Set("permissions", role.Permissions); err != nil {
130145
return diag.FromErr(err)

github/resource_github_organization_role_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ func TestAccGithubOrganizationRole(t *testing.T) {
2525
Steps: []resource.TestStep{
2626
{
2727
Config: config,
28-
Check: resource.ComposeTestCheckFunc(
28+
Check: resource.ComposeAggregateTestCheckFunc(
2929
resource.TestCheckResourceAttrSet("github_organization_role.test", "id"),
3030
resource.TestCheckResourceAttrSet("github_organization_role.test", "role_id"),
3131
resource.TestCheckResourceAttr("github_organization_role.test", "name", name),
3232
resource.TestCheckResourceAttr("github_organization_role.test", "base_role", "none"),
33-
resource.TestCheckResourceAttrSet("github_organization_role.test", "permissions.#"),
33+
resource.TestCheckNoResourceAttr("github_organization_role.test", "permissions"),
3434
resource.TestCheckResourceAttr("github_organization_role.test", "permissions.#", "0"),
3535
),
3636
},
@@ -57,11 +57,11 @@ func TestAccGithubOrganizationRole(t *testing.T) {
5757
Steps: []resource.TestStep{
5858
{
5959
Config: config,
60-
Check: resource.ComposeTestCheckFunc(
60+
Check: resource.ComposeAggregateTestCheckFunc(
6161
resource.TestCheckResourceAttrSet("github_organization_role.test", "id"),
6262
resource.TestCheckResourceAttr("github_organization_role.test", "name", name),
6363
resource.TestCheckResourceAttr("github_organization_role.test", "base_role", baseRole),
64-
resource.TestCheckResourceAttrSet("github_organization_role.test", "permissions.#"),
64+
resource.TestCheckNoResourceAttr("github_organization_role.test", "permissions"),
6565
resource.TestCheckResourceAttr("github_organization_role.test", "permissions.#", "0"),
6666
),
6767
},
@@ -167,16 +167,16 @@ func TestAccGithubOrganizationRole(t *testing.T) {
167167
Steps: []resource.TestStep{
168168
{
169169
Config: config,
170-
Check: resource.ComposeTestCheckFunc(
170+
Check: resource.ComposeAggregateTestCheckFunc(
171171
resource.TestCheckResourceAttrSet("github_organization_role.test", "id"),
172172
resource.TestCheckResourceAttrSet("github_organization_role.test", "role_id"),
173173
resource.TestCheckResourceAttr("github_organization_role.test", "name", name),
174174
resource.TestCheckResourceAttr("github_organization_role.test", "description", description),
175175
resource.TestCheckResourceAttr("github_organization_role.test", "base_role", baseRole),
176176
resource.TestCheckResourceAttrSet("github_organization_role.test", "permissions.#"),
177177
resource.TestCheckResourceAttr("github_organization_role.test", "permissions.#", "2"),
178-
resource.TestCheckResourceAttr("github_organization_role.test", "permissions.0", permission0),
179-
resource.TestCheckResourceAttr("github_organization_role.test", "permissions.1", permission1),
178+
resource.TestCheckTypeSetElemAttr("github_organization_role.test", "permissions.*", permission0),
179+
resource.TestCheckTypeSetElemAttr("github_organization_role.test", "permissions.*", permission1),
180180
),
181181
},
182182
},

0 commit comments

Comments
 (0)