@@ -18,42 +18,52 @@ func resourceSysdigZone() *schema.Resource {
1818 DeleteContext : resourceSysdigZoneDelete ,
1919
2020 Schema : map [string ]* schema.Schema {
21- "name" : {
21+ SchemaNameKey : {
2222 Type : schema .TypeString ,
2323 Required : true ,
2424 },
25- "description" : {
25+ SchemaDescriptionKey : {
2626 Type : schema .TypeString ,
2727 Optional : true ,
2828 },
29- "is_system" : {
29+ SchemaIsSystemKey : {
3030 Type : schema .TypeBool ,
3131 Computed : true ,
3232 },
33- "author" : {
33+ SchemaAuthorKey : {
3434 Type : schema .TypeString ,
3535 Computed : true ,
3636 },
37- "last_modified_by" : {
37+ SchemaLastModifiedBy : {
3838 Type : schema .TypeString ,
3939 Computed : true ,
4040 },
41- "last_updated" : {
41+ SchemaLastUpdated : {
4242 Type : schema .TypeString ,
4343 Computed : true ,
4444 },
45- "scopes" : {
46- Type : schema .TypeList ,
47- Required : true ,
45+ SchemaScopesKey : {
46+ Optional : true ,
47+ Type : schema .TypeSet ,
48+ MaxItems : 1 ,
4849 Elem : & schema.Resource {
4950 Schema : map [string ]* schema.Schema {
50- "target_type" : {
51- Type : schema .TypeString ,
52- Required : true ,
53- },
54- "rules" : {
55- Type : schema .TypeString ,
51+ SchemaScopeKey : {
52+ Type : schema .TypeSet ,
53+ MinItems : 1 ,
5654 Required : true ,
55+ Elem : & schema.Resource {
56+ Schema : map [string ]* schema.Schema {
57+ SchemaTargetTypeKey : {
58+ Type : schema .TypeString ,
59+ Required : true ,
60+ },
61+ SchemaRulesKey : {
62+ Type : schema .TypeString ,
63+ Optional : true ,
64+ },
65+ },
66+ },
5767 },
5868 },
5969 },
@@ -86,6 +96,7 @@ func resourceSysdigZoneRead(ctx context.Context, d *schema.ResourceData, m inter
8696 }
8797
8898 id , _ := strconv .Atoi (d .Id ())
99+
89100 zone , err := client .GetZoneById (ctx , id )
90101 if err != nil {
91102 d .SetId ("" )
@@ -94,7 +105,7 @@ func resourceSysdigZoneRead(ctx context.Context, d *schema.ResourceData, m inter
94105
95106 _ = d .Set ("name" , zone .Name )
96107 _ = d .Set ("description" , zone .Description )
97- _ = d .Set ("scopes" , flattenZoneScopes (zone .Scopes ))
108+ _ = d .Set ("scopes" , fromZoneScopesResponse (zone .Scopes ))
98109 _ = d .Set ("is_system" , zone .IsSystem )
99110 _ = d .Set ("author" , zone .Author )
100111 _ = d .Set ("last_modified_by" , zone .LastModifiedBy )
@@ -136,35 +147,64 @@ func resourceSysdigZoneDelete(ctx context.Context, d *schema.ResourceData, m int
136147}
137148
138149func zoneRequestFromResourceData (d * schema.ResourceData ) * v2.ZoneRequest {
139- return & v2.ZoneRequest {
140- ID : d .Get ("id" ).(int ),
150+ zoneRequest := & v2.ZoneRequest {
141151 Name : d .Get ("name" ).(string ),
142152 Description : d .Get ("description" ).(string ),
143- Scopes : expandZoneScopes (d .Get ("scopes" ).([]interface {})),
153+ Scopes : toZoneScopesRequest (d .Get ("scopes" ).(* schema.Set )),
154+ }
155+
156+ if d .Id () != "" {
157+ id , err := strconv .Atoi (d .Id ())
158+ if err == nil {
159+ zoneRequest .ID = id
160+ }
144161 }
162+
163+ return zoneRequest
145164}
146165
147- func expandZoneScopes (scopes [] interface {} ) []v2.ZoneScope {
166+ func toZoneScopesRequest (scopes * schema. Set ) []v2.ZoneScope {
148167 var zoneScopes []v2.ZoneScope
149- for _ , scope := range scopes {
150- scopeMap := scope .(map [string ]interface {})
151- zoneScopes = append (zoneScopes , v2.ZoneScope {
152- TargetType : scopeMap ["target_type" ].(string ),
153- Rules : scopeMap ["rules" ].(string ),
154- })
168+ for _ , scopeData := range scopes .List () {
169+ scopeMap := scopeData .(map [string ]interface {})
170+ scopeSet := scopeMap [SchemaScopeKey ].(* schema.Set )
171+ for _ , attr := range scopeSet .List () {
172+ s := attr .(map [string ]interface {})
173+ zoneScopes = append (zoneScopes , v2.ZoneScope {
174+ TargetType : s [SchemaTargetTypeKey ].(string ),
175+ Rules : s [SchemaRulesKey ].(string ),
176+ })
177+ }
155178 }
156179 return zoneScopes
157180}
158181
159- func flattenZoneScopes (scopes []v2.ZoneScope ) []interface {} {
182+ func fromZoneScopesResponse (scopes []v2.ZoneScope ) []interface {} {
160183 var flattenedScopes []interface {}
161184 for _ , scope := range scopes {
162185 flattenedScopes = append (flattenedScopes , map [string ]interface {}{
163- "target_type" : scope .TargetType ,
164- "rules" : scope .Rules ,
186+ SchemaTargetTypeKey : scope .TargetType ,
187+ SchemaRulesKey : scope .Rules ,
165188 })
166189 }
167- return flattenedScopes
190+ response := []interface {}{
191+ map [string ]interface {}{
192+ SchemaScopeKey : schema .NewSet (schema .HashResource (& schema.Resource {
193+ Schema : map [string ]* schema.Schema {
194+ SchemaTargetTypeKey : {
195+ Type : schema .TypeString ,
196+ Required : true ,
197+ },
198+ SchemaRulesKey : {
199+ Type : schema .TypeString ,
200+ Optional : true ,
201+ },
202+ },
203+ }), flattenedScopes ),
204+ },
205+ }
206+
207+ return response
168208}
169209
170210func getZoneClient (clients SysdigClients ) (v2.ZoneInterface , error ) {
0 commit comments