Skip to content

Commit 0ef223a

Browse files
committed
Roles are now optional for in Groups
Signed-off-by: Andy Lo-A-Foe <andy.loafoe@gmail.com>
1 parent 0785ea8 commit 0ef223a

File tree

3 files changed

+108
-25
lines changed

3 files changed

+108
-25
lines changed

docs/resources/iam_group.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The following arguments are supported:
5353

5454
* `name` - (Required) The name of the group
5555
* `description` - (Required) The description of the group
56-
* `roles` - (Required) The list of role IDS to assign to this group
56+
* `roles` - (Optional) The list of role IDS to assign to this group
5757
* `managing_organization` - (Required) The managing organization ID
5858
* `users` - (Optional) The list of user IDs to include in this group. The provider only manages this list of users. Existing users added by others means to the group by the provider. It is not practical to manage hundreds or thousands of users this way of course.
5959
* `services` - (Optional) The list of service identity IDs to include in this group. See `hsdp_iam_service`

internal/services/iam/group/resource_iam_group.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func ResourceIAMGroup() *schema.Resource {
2323
Importer: &schema.ResourceImporter{
2424
StateContext: schema.ImportStatePassthroughContext,
2525
},
26-
SchemaVersion: 3,
26+
SchemaVersion: 4,
2727
CreateContext: resourceIAMGroupCreate,
2828
ReadContext: resourceIAMGroupRead,
2929
UpdateContext: resourceIAMGroupUpdate,
@@ -44,6 +44,11 @@ func ResourceIAMGroup() *schema.Resource {
4444
Upgrade: patchIAMGroupV2,
4545
Version: 2,
4646
},
47+
{
48+
Type: ResourceIAMGroupV3().CoreConfigSchema().ImpliedType(),
49+
Upgrade: patchIAMGroupV3,
50+
Version: 3,
51+
},
4752
},
4853

4954
Schema: map[string]*schema.Schema{
@@ -69,7 +74,7 @@ func ResourceIAMGroup() *schema.Resource {
6974
"roles": {
7075
Type: schema.TypeSet,
7176
MaxItems: 1000,
72-
Required: true,
77+
Optional: true,
7378
Elem: tools.StringSchema(),
7479
Description: "The list of role IDS to assign to this group.",
7580
},
@@ -100,13 +105,6 @@ func ResourceIAMGroup() *schema.Resource {
100105
Default: true,
101106
Description: "When enabled, the provider will perform additional API calls to determine if any changes were made outside of Terraform to user and service assignments of this group.",
102107
},
103-
"iam_device_bug_workaround": {
104-
Type: schema.TypeBool,
105-
Optional: true,
106-
Default: false,
107-
Deprecated: "This workaround is no longer required and will be removed in the near future.",
108-
Description: "Deprecated, do not use.",
109-
},
110108
},
111109
}
112110
}
@@ -138,28 +136,30 @@ func resourceIAMGroupCreate(ctx context.Context, d *schema.ResourceData, m inter
138136
if err != nil {
139137
return diag.FromErr(err)
140138
}
141-
roles := tools.ExpandStringList(d.Get("roles").(*schema.Set).List())
142139

143140
_ = d.Set("name", createdGroup.Name)
144141
_ = d.Set("description", createdGroup.Description)
145142
_ = d.Set("managing_organization", createdGroup.ManagingOrganization)
146143

144+
roles := tools.ExpandStringList(d.Get("roles").(*schema.Set).List())
147145
// Add roles
148-
for _, r := range roles {
149-
role, _, _ := client.Roles.GetRoleByID(r)
150-
if role != nil {
151-
err = tools.TryHTTPCall(ctx, 10, func() (*http.Response, error) {
152-
_, resp, err := client.Groups.AssignRole(ctx, *createdGroup, *role)
153-
if resp == nil {
154-
return nil, err
146+
if len(roles) > 0 {
147+
for _, r := range roles {
148+
role, _, _ := client.Roles.GetRoleByID(r)
149+
if role != nil {
150+
err = tools.TryHTTPCall(ctx, 10, func() (*http.Response, error) {
151+
_, resp, err := client.Groups.AssignRole(ctx, *createdGroup, *role)
152+
if resp == nil {
153+
return nil, err
154+
}
155+
return resp.Response, err
156+
}, append(tools.StandardRetryOnCodes, http.StatusUnprocessableEntity)...) // Handle intermittent HTTP 422 errors
157+
if err != nil {
158+
// Cleanup
159+
_ = purgeGroupContent(ctx, client, createdGroup.ID, d)
160+
_, _, _ = client.Groups.DeleteGroup(*createdGroup)
161+
return diag.FromErr(fmt.Errorf("error adding roles: %v", err))
155162
}
156-
return resp.Response, err
157-
}, append(tools.StandardRetryOnCodes, http.StatusUnprocessableEntity)...) // Handle intermittent HTTP 422 errors
158-
if err != nil {
159-
// Cleanup
160-
_ = purgeGroupContent(ctx, client, createdGroup.ID, d)
161-
_, _, _ = client.Groups.DeleteGroup(*createdGroup)
162-
return diag.FromErr(fmt.Errorf("error adding roles: %v", err))
163163
}
164164
}
165165
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package group
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/philips-software/terraform-provider-hsdp/internal/tools"
8+
)
9+
10+
// Upgrades an IAM Group resource from v3 to v4
11+
func patchIAMGroupV3(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
12+
if rawState == nil {
13+
rawState = map[string]interface{}{}
14+
}
15+
return rawState, nil
16+
}
17+
18+
func ResourceIAMGroupV3() *schema.Resource {
19+
return &schema.Resource{
20+
Schema: map[string]*schema.Schema{
21+
"name": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
ForceNew: true,
25+
DiffSuppressFunc: tools.SuppressCaseDiffs,
26+
Description: "The group name.",
27+
},
28+
"description": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
DiffSuppressFunc: tools.SuppressWhenGenerated,
32+
Description: "The group description.",
33+
},
34+
"managing_organization": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
Description: "The managing organization ID.",
39+
},
40+
"roles": {
41+
Type: schema.TypeSet,
42+
MaxItems: 1000,
43+
Required: true,
44+
Elem: tools.StringSchema(),
45+
Description: "The list of role IDS to assign to this group.",
46+
},
47+
"users": {
48+
Type: schema.TypeSet,
49+
MaxItems: 2000,
50+
Optional: true,
51+
Elem: tools.StringSchema(),
52+
Description: "The list of user IDs to include in this group. The provider only manages this list of users. Existing users added by others means to the group by the provider. It is not practical to manage hundreds or thousands of users this way of course.",
53+
},
54+
"services": {
55+
Type: schema.TypeSet,
56+
MaxItems: 2000,
57+
Optional: true,
58+
Elem: tools.StringSchema(),
59+
Description: "The list of service identity IDs to include in this group.",
60+
},
61+
"devices": {
62+
Type: schema.TypeSet,
63+
MaxItems: 2000,
64+
Optional: true,
65+
Elem: tools.StringSchema(),
66+
Description: "The list of IAM device identity IDs to include in this group.",
67+
},
68+
"drift_detection": {
69+
Type: schema.TypeBool,
70+
Optional: true,
71+
Default: true,
72+
Description: "When enabled, the provider will perform additional API calls to determine if any changes were made outside of Terraform to user and service assignments of this group.",
73+
},
74+
"iam_device_bug_workaround": {
75+
Type: schema.TypeBool,
76+
Optional: true,
77+
Default: false,
78+
Deprecated: "This workaround is no longer required and will be removed in the near future.",
79+
Description: "Deprecated, do not use.",
80+
},
81+
},
82+
}
83+
}

0 commit comments

Comments
 (0)