Skip to content

Commit aa5f1b8

Browse files
authored
Merge pull request #27762 from roberth-k/fd-aws_identitystore_group-all-attributes
d/aws_identitystore_group: expose all group attributes
2 parents 0159951 + d53969e commit aa5f1b8

File tree

4 files changed

+504
-102
lines changed

4 files changed

+504
-102
lines changed

.changelog/27762.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
data-source/aws_identitystore_group: Add `alternate_identifier` argument and `description` attribute
3+
```
4+
5+
```release-note:note
6+
data-source/aws_identitystore_group: The `filter` argument has been deprecated. Use the `alternate_identifier` argument instead
7+
```

internal/service/identitystore/group_data_source.go

+121-59
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package identitystore
22

33
import (
44
"context"
5+
"errors"
56
"regexp"
67

78
"github.com/aws/aws-sdk-go-v2/aws"
@@ -12,6 +13,7 @@ import (
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1314
"github.com/hashicorp/terraform-provider-aws/internal/conns"
1415
"github.com/hashicorp/terraform-provider-aws/internal/create"
16+
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
1517
"github.com/hashicorp/terraform-provider-aws/names"
1618
)
1719

@@ -20,14 +22,83 @@ func DataSourceGroup() *schema.Resource {
2022
ReadContext: dataSourceGroupRead,
2123

2224
Schema: map[string]*schema.Schema{
25+
"alternate_identifier": {
26+
Type: schema.TypeList,
27+
Optional: true,
28+
MaxItems: 1,
29+
ConflictsWith: []string{"filter", "group_id"},
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"external_id": {
33+
Type: schema.TypeList,
34+
Optional: true,
35+
MaxItems: 1,
36+
ExactlyOneOf: []string{"alternate_identifier.0.external_id", "alternate_identifier.0.unique_attribute"},
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"id": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
},
43+
"issuer": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
},
47+
},
48+
},
49+
},
50+
"unique_attribute": {
51+
Type: schema.TypeList,
52+
Optional: true,
53+
MaxItems: 1,
54+
ExactlyOneOf: []string{"alternate_identifier.0.external_id", "alternate_identifier.0.unique_attribute"},
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
"attribute_path": {
58+
Type: schema.TypeString,
59+
Required: true,
60+
},
61+
"attribute_value": {
62+
Type: schema.TypeString,
63+
Required: true,
64+
},
65+
},
66+
},
67+
},
68+
},
69+
},
70+
},
71+
"description": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
2375
"display_name": {
2476
Type: schema.TypeString,
2577
Computed: true,
2678
},
27-
79+
"external_ids": {
80+
Type: schema.TypeList,
81+
Computed: true,
82+
Elem: &schema.Resource{
83+
Schema: map[string]*schema.Schema{
84+
"id": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
},
88+
"issuer": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
},
92+
},
93+
},
94+
},
2895
"filter": {
29-
Type: schema.TypeSet,
30-
Required: true,
96+
Deprecated: "Use the alternate_identifier attribute instead.",
97+
Type: schema.TypeList,
98+
Optional: true,
99+
MaxItems: 1,
100+
AtLeastOneOf: []string{"alternate_identifier", "filter", "group_id"},
101+
ConflictsWith: []string{"alternate_identifier"},
31102
Elem: &schema.Resource{
32103
Schema: map[string]*schema.Schema{
33104
"attribute_path": {
@@ -41,17 +112,17 @@ func DataSourceGroup() *schema.Resource {
41112
},
42113
},
43114
},
44-
45115
"group_id": {
46-
Type: schema.TypeString,
47-
Optional: true,
48-
Computed: true,
116+
Type: schema.TypeString,
117+
Optional: true,
118+
Computed: true,
119+
AtLeastOneOf: []string{"alternate_identifier", "filter", "group_id"},
120+
ConflictsWith: []string{"alternate_identifier"},
49121
ValidateFunc: validation.All(
50122
validation.StringLenBetween(1, 47),
51123
validation.StringMatch(regexp.MustCompile(`^([0-9a-f]{10}-|)[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$`), "must match ([0-9a-f]{10}-|)[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}"),
52124
),
53125
},
54-
55126
"identity_store_id": {
56127
Type: schema.TypeString,
57128
Required: true,
@@ -73,76 +144,67 @@ func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta inter
73144

74145
identityStoreId := d.Get("identity_store_id").(string)
75146

76-
// Filters has been marked as deprecated in favour of GetGroupId, which
77-
// allows only a single filter. Keep using it to maintain backwards
78-
// compatibility of the data source.
147+
var getGroupIdInput *identitystore.GetGroupIdInput
79148

80-
input := &identitystore.ListGroupsInput{
81-
IdentityStoreId: aws.String(identityStoreId),
82-
Filters: expandFilters(d.Get("filter").(*schema.Set).List()),
149+
if v, ok := d.GetOk("alternate_identifier"); ok && len(v.([]interface{})) > 0 {
150+
getGroupIdInput = &identitystore.GetGroupIdInput{
151+
AlternateIdentifier: expandAlternateIdentifier(v.([]interface{})[0].(map[string]interface{})),
152+
IdentityStoreId: aws.String(identityStoreId),
153+
}
154+
} else if v, ok := d.GetOk("filter"); ok && len(v.([]interface{})) > 0 {
155+
getGroupIdInput = &identitystore.GetGroupIdInput{
156+
AlternateIdentifier: &types.AlternateIdentifierMemberUniqueAttribute{
157+
Value: *expandUniqueAttribute(v.([]interface{})[0].(map[string]interface{})),
158+
},
159+
IdentityStoreId: aws.String(identityStoreId),
160+
}
83161
}
84162

85-
var results []types.Group
86-
87-
paginator := identitystore.NewListGroupsPaginator(conn, input)
163+
var groupId string
88164

89-
for paginator.HasMorePages() {
90-
page, err := paginator.NextPage(ctx)
165+
if getGroupIdInput != nil {
166+
output, err := conn.GetGroupId(ctx, getGroupIdInput)
91167

92168
if err != nil {
93-
return create.DiagError(names.IdentityStore, create.ErrActionReading, DSNameGroup, identityStoreId, err)
94-
}
95-
96-
for _, group := range page.Groups {
97-
if v, ok := d.GetOk("group_id"); ok && v.(string) != aws.ToString(group.GroupId) {
98-
continue
169+
var e *types.ResourceNotFoundException
170+
if errors.As(err, &e) {
171+
return diag.Errorf("no Identity Store Group found matching criteria; try different search")
172+
} else {
173+
return create.DiagError(names.IdentityStore, create.ErrActionReading, DSNameGroup, identityStoreId, err)
99174
}
100-
101-
results = append(results, group)
102175
}
103-
}
104-
105-
if len(results) == 0 {
106-
return diag.Errorf("no Identity Store Group found matching criteria\n%v; try different search", input.Filters)
107-
}
108176

109-
if len(results) > 1 {
110-
return diag.Errorf("multiple Identity Store Groups found matching criteria\n%v; try different search", input.Filters)
177+
groupId = aws.ToString(output.GroupId)
111178
}
112179

113-
group := results[0]
114-
115-
d.SetId(aws.ToString(group.GroupId))
116-
d.Set("display_name", group.DisplayName)
117-
d.Set("group_id", group.GroupId)
118-
119-
return nil
120-
}
180+
if v, ok := d.GetOk("group_id"); ok && v.(string) != "" {
181+
if groupId != "" && groupId != v.(string) {
182+
// We were given a filter, and it found a group different to this one.
183+
return diag.Errorf("no Identity Store Group found matching criteria; try different search")
184+
}
121185

122-
func expandFilters(l []interface{}) []types.Filter {
123-
if len(l) == 0 || l[0] == nil {
124-
return nil
186+
groupId = v.(string)
125187
}
126188

127-
filters := make([]types.Filter, 0, len(l))
128-
for _, v := range l {
129-
tfMap, ok := v.(map[string]interface{})
130-
if !ok {
131-
continue
189+
group, err := findGroupByID(ctx, conn, identityStoreId, groupId)
190+
191+
if err != nil {
192+
if tfresource.NotFound(err) {
193+
return diag.Errorf("no Identity Store Group found matching criteria; try different search")
132194
}
133195

134-
filter := types.Filter{}
196+
return create.DiagError(names.IdentityStore, create.ErrActionReading, DSNameGroup, identityStoreId, err)
197+
}
135198

136-
if v, ok := tfMap["attribute_path"].(string); ok && v != "" {
137-
filter.AttributePath = aws.String(v)
138-
}
199+
d.SetId(aws.ToString(group.GroupId))
139200

140-
if v, ok := tfMap["attribute_value"].(string); ok && v != "" {
141-
filter.AttributeValue = aws.String(v)
142-
}
201+
d.Set("description", group.Description)
202+
d.Set("display_name", group.DisplayName)
203+
d.Set("group_id", group.GroupId)
143204

144-
filters = append(filters, filter)
205+
if err := d.Set("external_ids", flattenExternalIds(group.ExternalIds)); err != nil {
206+
return create.DiagError(names.IdentityStore, create.ErrActionSetting, DSNameGroup, d.Id(), err)
145207
}
146208

147-
return filters
209+
return nil
148210
}

0 commit comments

Comments
 (0)