@@ -18,13 +18,16 @@ func identityGroupResource() *schema.Resource {
1818 Read : identityGroupRead ,
1919 Delete : identityGroupDelete ,
2020 Exists : identityGroupExists ,
21+ Importer : & schema.ResourceImporter {
22+ State : schema .ImportStatePassthrough ,
23+ },
2124
2225 Schema : map [string ]* schema.Schema {
2326 "name" : {
2427 Type : schema .TypeString ,
25- Required : true ,
2628 Description : "Name of the group." ,
27- ForceNew : true ,
29+ Optional : true ,
30+ Computed : true ,
2831 },
2932
3033 "type" : {
@@ -51,6 +54,16 @@ func identityGroupResource() *schema.Resource {
5154 Type : schema .TypeString ,
5255 },
5356 Description : "Policies to be tied to the group." ,
57+ DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool {
58+ return d .Get ("external_policies" ).(bool )
59+ },
60+ },
61+
62+ "external_policies" : {
63+ Type : schema .TypeBool ,
64+ Optional : true ,
65+ Default : false ,
66+ Description : "Manage policies externally through `vault_identity_group_policies`, allows using group ID in assigned policies." ,
5467 },
5568
5669 "member_group_ids" : {
@@ -79,19 +92,19 @@ func identityGroupResource() *schema.Resource {
7992 return false
8093 },
8194 },
82-
83- "id" : {
84- Type : schema .TypeString ,
85- Computed : true ,
86- Description : "ID of the group." ,
87- },
8895 },
8996 }
9097}
9198
9299func identityGroupUpdateFields (d * schema.ResourceData , data map [string ]interface {}) error {
93- if policies , ok := d .GetOk ("policies" ); ok {
94- data ["policies" ] = policies .(* schema.Set ).List ()
100+ if name , ok := d .GetOk ("name" ); ok {
101+ data ["name" ] = name
102+ }
103+
104+ if externalPolicies , ok := d .GetOk ("external_policies" ); ! (ok && externalPolicies .(bool )) {
105+ if policies , ok := d .GetOk ("policies" ); ok {
106+ data ["policies" ] = policies .(* schema.Set ).List ()
107+ }
95108 }
96109
97110 if memberEntityIDs , ok := d .GetOk ("member_entity_ids" ); ok && d .Get ("type" ).(string ) == "internal" {
@@ -133,8 +146,6 @@ func identityGroupCreate(d *schema.ResourceData, meta interface{}) error {
133146 }
134147 log .Printf ("[DEBUG] Wrote IdentityGroup %q" , name )
135148
136- d .Set ("id" , resp .Data ["id" ])
137-
138149 d .SetId (resp .Data ["id" ].(string ))
139150
140151 return identityGroupRead (d , meta )
@@ -167,16 +178,13 @@ func identityGroupRead(d *schema.ResourceData, meta interface{}) error {
167178 client := meta .(* api.Client )
168179 id := d .Id ()
169180
170- path := identityGroupIDPath (id )
171-
172- log .Printf ("[DEBUG] Reading IdentityGroup %s from %q" , id , path )
173- resp , err := client .Logical ().Read (path )
181+ resp , err := readIdentityGroup (client , id )
174182 if err != nil {
175183 // We need to check if the secret_id has expired
176184 if util .IsExpiredTokenErr (err ) {
177185 return nil
178186 }
179- return fmt .Errorf ("error reading AppRole auth backend role SecretID %q: %s" , id , err )
187+ return fmt .Errorf ("error reading IdentityGroup %q: %s" , id , err )
180188 }
181189 log .Printf ("[DEBUG] Read IdentityGroup %s" , id )
182190 if resp == nil {
@@ -185,11 +193,11 @@ func identityGroupRead(d *schema.ResourceData, meta interface{}) error {
185193 return nil
186194 }
187195
188- for _ , k := range []string {"name" , "type" , "metadata" , "policies " , "member_entity_ids " , "member_group_ids" } {
189- if v , ok := resp . Data [ k ]; ok {
190- if err := d . Set ( k , v ); err != nil {
191- return fmt . Errorf ( "error reading %s for Identity Group %q: %q" , k , path , err )
192- }
196+ readFields := []string {"name" , "type" , "metadata" , "member_entity_ids " , "member_group_ids " , "policies" }
197+
198+ for _ , k := range readFields {
199+ if err := d . Set ( k , resp . Data [ k ]); err != nil {
200+ return fmt . Errorf ( "error setting state key \" %s \" on IdentityGroup %q: %s" , k , id , err )
193201 }
194202 }
195203 return nil
@@ -214,30 +222,44 @@ func identityGroupDelete(d *schema.ResourceData, meta interface{}) error {
214222func identityGroupExists (d * schema.ResourceData , meta interface {}) (bool , error ) {
215223 client := meta .(* api.Client )
216224 id := d .Id ()
217-
218- path := identityGroupIDPath (id )
219225 key := id
220226
221- // use the name if no ID is set
222227 if len (id ) == 0 {
228+ return false , nil
229+ } else {
223230 key = d .Get ("name" ).(string )
224- path = identityGroupNamePath (key )
225231 }
226232
227233 log .Printf ("[DEBUG] Checking if IdentityGroup %q exists" , key )
228- resp , err := client . Logical (). Read ( path )
234+ resp , err := readIdentityGroup ( client , id )
229235 if err != nil {
230236 return true , fmt .Errorf ("error checking if IdentityGroup %q exists: %s" , key , err )
231237 }
232238 log .Printf ("[DEBUG] Checked if IdentityGroup %q exists" , key )
233-
234239 return resp != nil , nil
235240}
236241
237- func identityGroupNamePath (name string ) string {
238- return fmt .Sprintf ("%s/name/%s" , identityGroupPath , name )
239- }
240-
241242func identityGroupIDPath (id string ) string {
242243 return fmt .Sprintf ("%s/id/%s" , identityGroupPath , id )
243244}
245+
246+ func readIdentityGroupPolicies (client * api.Client , groupId string ) ([]interface {}, error ) {
247+ var presentPolicies []interface {}
248+ if resp , err := readIdentityGroup (client , groupId ); err != nil {
249+ return nil , fmt .Errorf ("error reading IdentityGroup policies %q: %s" , groupId , err )
250+ } else {
251+ presentPolicies = resp .Data ["policies" ].([]interface {})
252+ }
253+ return presentPolicies , nil
254+ }
255+
256+ func readIdentityGroup (client * api.Client , groupId string ) (* api.Secret , error ) {
257+ path := identityGroupIDPath (groupId )
258+ log .Printf ("[DEBUG] Reading IdentityGroup %s from %q" , groupId , path )
259+
260+ if resp , err := client .Logical ().Read (path ); err != nil {
261+ return resp , fmt .Errorf ("failed reading IdentityGroup %s from %s" , groupId , path )
262+ } else {
263+ return resp , nil
264+ }
265+ }
0 commit comments