@@ -45,7 +45,7 @@ type CloudProjectKubeNodePoolTemplateMetadata struct {
4545
4646type CloudProjectKubeNodePoolTemplateSpec struct {
4747 Taints []Taint `json:"taints"`
48- Unschedulable bool `json:"unschedulable"`
48+ Unschedulable * bool `json:"unschedulable"`
4949}
5050
5151type CloudProjectKubeNodePoolTemplate struct {
@@ -94,90 +94,77 @@ func (opts *CloudProjectKubeNodePoolCreateOpts) FromResource(d *schema.ResourceD
9494}
9595
9696func loadNodelPoolTemplateFromResource (i interface {}) (* CloudProjectKubeNodePoolTemplate , error ) {
97+ // initialize map variables to explicit empty map
9798 template := CloudProjectKubeNodePoolTemplate {
98- Metadata : & CloudProjectKubeNodePoolTemplateMetadata {
99- Annotations : map [string ]string {},
100- Finalizers : []string {},
101- Labels : map [string ]string {},
102- },
103- Spec : & CloudProjectKubeNodePoolTemplateSpec {
104- Taints : []Taint {},
105- Unschedulable : false ,
106- },
99+ Metadata : & CloudProjectKubeNodePoolTemplateMetadata {},
100+ Spec : & CloudProjectKubeNodePoolTemplateSpec {},
107101 }
108102
109103 templateSet := i .(* schema.Set ).List ()
110- if len (templateSet ) == 0 {
111- return nil , nil
112- }
113- templateObject := templateSet [0 ]
114-
115- // when updating the nested object template there is two objects, one is empty, take the not empty one
116- if len (templateSet ) > 1 {
117- for _ , to := range templateSet {
118- metadata := to .(map [string ]interface {})["metadata" ].(* schema.Set ).List ()[0 ]
119- annotations := metadata .(map [string ]interface {})["annotations" ].(map [string ]interface {})
120- labels := metadata .(map [string ]interface {})["labels" ].(map [string ]interface {})
121- finalizers := metadata .(map [string ]interface {})["finalizers" ].([]interface {})
122-
123- spec := templateObject .(map [string ]interface {})["spec" ].(* schema.Set ).List ()[0 ]
124- taints := spec .(map [string ]interface {})["taints" ].([]interface {})
125- unschedulable := spec .(map [string ]interface {})["unschedulable" ].(bool )
126-
127- if len (annotations ) == 0 && len (labels ) == 0 && len (finalizers ) == 0 && len (taints ) == 0 && unschedulable == false {
128- // is empty
129- } else {
130- templateObject = to
131- break
132- }
133- }
134- }
135104 if len (templateSet ) > 2 {
136105 return nil , errors .New ("resource template cannot have more than 2 elements" )
137106 }
138107
139- metadataSet := templateObject .(map [string ]interface {})["metadata" ].(* schema.Set ).List ()
140- for _ , meta := range metadataSet {
141-
142- annotations := meta .(map [string ]interface {})["annotations" ].(map [string ]interface {})
143- template .Metadata .Annotations = make (map [string ]string )
144- for k , v := range annotations {
145- template .Metadata .Annotations [k ] = v .(string )
146- }
147-
148- labels := meta .(map [string ]interface {})["labels" ].(map [string ]interface {})
149- template .Metadata .Labels = make (map [string ]string )
150- for k , v := range labels {
151- template .Metadata .Labels [k ] = v .(string )
152- }
108+ if len (templateSet ) > 0 {
109+ templateObject := templateSet [0 ].(map [string ]interface {})
153110
154- finalizers := meta .(map [string ]interface {})["finalizers" ].([]interface {})
155- for _ , finalizer := range finalizers {
156- template .Metadata .Finalizers = append (template .Metadata .Finalizers , finalizer .(string ))
111+ // metadata
112+ {
113+ metadataSet := templateObject ["metadata" ].(* schema.Set ).List ()
114+ if len (metadataSet ) > 0 {
115+ metadata := metadataSet [0 ].(map [string ]interface {})
116+
117+ // metadata.annotations
118+ annotations := metadata ["annotations" ].(map [string ]interface {})
119+ template .Metadata .Annotations = make (map [string ]string )
120+ for k , v := range annotations {
121+ template .Metadata .Annotations [k ] = v .(string )
122+ }
123+
124+ // metadata.finalizers
125+ finalizers := metadata ["finalizers" ].([]interface {})
126+ template .Metadata .Finalizers = make ([]string , 0 )
127+ for _ , finalizer := range finalizers {
128+ template .Metadata .Finalizers = append (template .Metadata .Finalizers , finalizer .(string ))
129+ }
130+
131+ // metadata.labels
132+ labels := metadata ["labels" ].(map [string ]interface {})
133+ template .Metadata .Labels = make (map [string ]string )
134+ for k , v := range labels {
135+ template .Metadata .Labels [k ] = v .(string )
136+ }
137+ }
157138 }
158139
159- }
160-
161- specSet := templateObject .(map [string ]interface {})["spec" ].(* schema.Set ).List ()
162- for _ , spec := range specSet {
163-
164- taints := spec .(map [string ]interface {})["taints" ].([]interface {})
165- for _ , taint := range taints {
166- effectString := taint .(map [string ]interface {})["effect" ].(string )
167- effect := TaintEffecTypeToID [effectString ]
168- if effect == NotATaint {
169- return nil , errors .New (fmt .Sprintf ("Effect: %s is not a allowable taint %#v" , effectString , TaintEffecTypeToID ))
140+ // spec
141+ {
142+ specSet := templateObject ["spec" ].(* schema.Set ).List ()
143+ if len (specSet ) > 0 {
144+ spec := specSet [0 ].(map [string ]interface {})
145+
146+ // spec.taints
147+ taints := spec ["taints" ].([]interface {})
148+ data := make ([]Taint , 0 )
149+ for _ , taint := range taints {
150+ effectString := taint .(map [string ]interface {})["effect" ].(string )
151+ effect := TaintEffecTypeToID [effectString ]
152+ if effect == NotATaint {
153+ return nil , fmt .Errorf ("effect: %s is not a allowable taint %#v" , effectString , TaintEffecTypeToID )
154+ }
155+
156+ data = append (data , Taint {
157+ Effect : effect ,
158+ Key : taint .(map [string ]interface {})["key" ].(string ),
159+ Value : taint .(map [string ]interface {})["value" ].(string ),
160+ })
161+ }
162+ template .Spec .Taints = data
163+
164+ // spec.unschedulable
165+ template .Spec .Unschedulable = helpers .GetNilBoolPointerFromData (spec , "unschedulable" )
170166 }
171-
172- template .Spec .Taints = append (template .Spec .Taints , Taint {
173- Effect : effect ,
174- Key : taint .(map [string ]interface {})["key" ].(string ),
175- Value : taint .(map [string ]interface {})["value" ].(string ),
176- })
177167 }
178-
179- template .Spec .Unschedulable = spec .(map [string ]interface {})["unschedulable" ].(bool )
180-
181168 }
182169
183170 return & template , nil
@@ -271,64 +258,60 @@ func (v CloudProjectKubeNodePoolResponse) ToMap() map[string]interface{} {
271258 obj ["up_to_date_nodes" ] = v .UpToDateNodes
272259 obj ["updated_at" ] = v .UpdatedAt
273260
274- var taints []map [string ]interface {}
275- for _ , taint := range v .Template .Spec .Taints {
276- t := map [string ]interface {}{
277- "effect" : taint .Effect .String (),
278- "key" : taint .Key ,
279- "value" : taint .Value ,
280- }
261+ if v .Template != nil {
262+ obj ["template" ] = []map [string ]interface {}{{}}
281263
282- taints = append (taints , t )
283- }
264+ // template.metadata
265+ if v .Template .Metadata != nil {
266+ data := make (map [string ]interface {})
267+ if vv := v .Template .Metadata .Finalizers ; vv != nil && len (vv ) > 0 {
268+ data ["finalizers" ] = vv
269+ }
284270
285- obj ["template" ] = []map [string ]interface {}{
286- {
287- "metadata" : []map [string ]interface {}{
288- {
289- "finalizers" : v .Template .Metadata .Finalizers ,
290- "labels" : v .Template .Metadata .Labels ,
291- "annotations" : v .Template .Metadata .Annotations ,
292- },
293- },
294- "spec" : []map [string ]interface {}{
295- {
296- "unschedulable" : v .Template .Spec .Unschedulable ,
297- "taints" : taints ,
298- },
299- },
300- },
301- }
271+ if vv := v .Template .Metadata .Labels ; vv != nil && len (vv ) > 0 {
272+ data ["labels" ] = vv
273+ }
302274
303- if len (obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["finalizers" ].([]string )) == 0 {
304- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["finalizers" ] = nil
305- }
306- if len (obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["labels" ].(map [string ]string )) == 0 {
307- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["labels" ] = nil
308- }
309- if len (obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["annotations" ].(map [string ]string )) == 0 {
310- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["annotations" ] = nil
311- }
312- if obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["finalizers" ] == nil &&
313- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["labels" ] == nil &&
314- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ].([]map [string ]interface {})[0 ]["annotations" ] == nil {
315- obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ] = nil
316- }
317- if obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["unschedulable" ].(bool ) == false {
318- obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["unschedulable" ] = nil
319- }
320- if len (obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["taints" ].([]map [string ]interface {})) == 0 {
321- obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["taints" ] = nil
322- }
275+ if vv := v .Template .Metadata .Annotations ; vv != nil && len (vv ) > 0 {
276+ data ["annotations" ] = vv
277+ }
323278
324- if obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["unschedulable" ] == nil &&
325- obj ["template" ].([]map [string ]interface {})[0 ]["spec" ].([]map [string ]interface {})[0 ]["taints" ] == nil {
326- obj ["template" ].([]map [string ]interface {})[0 ]["spec" ] = nil
279+ if len (data ) > 0 {
280+ obj ["template" ].([]map [string ]interface {})[0 ]["metadata" ] = []map [string ]interface {}{data }
281+ }
282+ }
283+
284+ // template.spec
285+ if v .Template .Spec != nil {
286+ data := make (map [string ]interface {})
287+ if vv := v .Template .Spec .Taints ; vv != nil && len (vv ) > 0 {
288+ var taints []map [string ]interface {}
289+ for _ , taint := range vv {
290+ t := map [string ]interface {}{
291+ "effect" : taint .Effect .String (),
292+ "key" : taint .Key ,
293+ "value" : taint .Value ,
294+ }
295+
296+ taints = append (taints , t )
297+ }
298+
299+ data ["taints" ] = taints
300+ }
301+
302+ if vv := v .Template .Spec .Unschedulable ; vv != nil {
303+ data ["unschedulable" ] = vv
304+ }
305+
306+ if len (data ) > 0 {
307+ obj ["template" ].([]map [string ]interface {})[0 ]["spec" ] = []map [string ]interface {}{data }
308+ }
309+ }
327310 }
328311
329- if obj [ " template" ].([] map [ string ] interface {})[ 0 ][ "metadata" ] == nil &&
330- obj ["template" ].([]map [string ]interface {})[0 ][ "spec" ] == nil {
331- obj [ "template" ] = nil
312+ // Delete the entire template if it's empty
313+ if len ( obj ["template" ].([]map [string ]interface {})[0 ]) == 0 {
314+ delete ( obj , "template" )
332315 }
333316
334317 return obj
0 commit comments