@@ -2,6 +2,7 @@ package identitystore
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"regexp"
6
7
7
8
"github.com/aws/aws-sdk-go-v2/aws"
@@ -12,6 +13,7 @@ import (
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
14
"github.com/hashicorp/terraform-provider-aws/internal/conns"
14
15
"github.com/hashicorp/terraform-provider-aws/internal/create"
16
+ "github.com/hashicorp/terraform-provider-aws/internal/tfresource"
15
17
"github.com/hashicorp/terraform-provider-aws/names"
16
18
)
17
19
@@ -20,14 +22,83 @@ func DataSourceGroup() *schema.Resource {
20
22
ReadContext : dataSourceGroupRead ,
21
23
22
24
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
+ },
23
75
"display_name" : {
24
76
Type : schema .TypeString ,
25
77
Computed : true ,
26
78
},
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
+ },
28
95
"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" },
31
102
Elem : & schema.Resource {
32
103
Schema : map [string ]* schema.Schema {
33
104
"attribute_path" : {
@@ -41,17 +112,17 @@ func DataSourceGroup() *schema.Resource {
41
112
},
42
113
},
43
114
},
44
-
45
115
"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" },
49
121
ValidateFunc : validation .All (
50
122
validation .StringLenBetween (1 , 47 ),
51
123
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}" ),
52
124
),
53
125
},
54
-
55
126
"identity_store_id" : {
56
127
Type : schema .TypeString ,
57
128
Required : true ,
@@ -73,76 +144,67 @@ func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta inter
73
144
74
145
identityStoreId := d .Get ("identity_store_id" ).(string )
75
146
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
79
148
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
+ }
83
161
}
84
162
85
- var results []types.Group
86
-
87
- paginator := identitystore .NewListGroupsPaginator (conn , input )
163
+ var groupId string
88
164
89
- for paginator . HasMorePages () {
90
- page , err := paginator . NextPage (ctx )
165
+ if getGroupIdInput != nil {
166
+ output , err := conn . GetGroupId (ctx , getGroupIdInput )
91
167
92
168
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 )
99
174
}
100
-
101
- results = append (results , group )
102
175
}
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
- }
108
176
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 )
111
178
}
112
179
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
+ }
121
185
122
- func expandFilters (l []interface {}) []types.Filter {
123
- if len (l ) == 0 || l [0 ] == nil {
124
- return nil
186
+ groupId = v .(string )
125
187
}
126
188
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" )
132
194
}
133
195
134
- filter := types.Filter {}
196
+ return create .DiagError (names .IdentityStore , create .ErrActionReading , DSNameGroup , identityStoreId , err )
197
+ }
135
198
136
- if v , ok := tfMap ["attribute_path" ].(string ); ok && v != "" {
137
- filter .AttributePath = aws .String (v )
138
- }
199
+ d .SetId (aws .ToString (group .GroupId ))
139
200
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 )
143
204
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 )
145
207
}
146
208
147
- return filters
209
+ return nil
148
210
}
0 commit comments