@@ -21,6 +21,7 @@ func resourceAllocationGroup() *schema.Resource {
2121 ReadContext : resourceAllocationGroupRead ,
2222 UpdateContext : resourceAllocationGroupUpdate ,
2323 DeleteContext : resourceAllocationGroupDelete ,
24+ CustomizeDiff : resourceAllocationGroupDiff ,
2425 Description : "Manage allocation group. Allocation group [reference](https://docs.cast.ai/docs/allocation-groups)" ,
2526 Importer : & schema.ResourceImporter {
2627 StateContext : allocationGroupImporter ,
@@ -33,7 +34,7 @@ func resourceAllocationGroup() *schema.Resource {
3334 },
3435 "cluster_ids" : {
3536 Type : schema .TypeSet ,
36- Required : true ,
37+ Optional : true ,
3738 Description : "List of CAST AI cluster ids" ,
3839 Elem : & schema.Schema {
3940 Type : schema .TypeString ,
@@ -75,6 +76,24 @@ func resourceAllocationGroup() *schema.Resource {
7576 }
7677}
7778
79+ func resourceAllocationGroupDiff (_ context.Context , d * schema.ResourceDiff , _ any ) error {
80+ var clusterIds []string
81+ if cids , ok := d .GetOk ("cluster_ids" ); ok {
82+ clusterIds = toClusterIds (cids .(* schema.Set ).List ())
83+ }
84+ namespaces := toStringList (d .Get ("namespaces" ).([]interface {}))
85+
86+ var labels []sdk.CostreportV1beta1AllocationGroupFilterLabelValue
87+ if ls , ok := d .GetOk ("labels" ); ok {
88+ labels = toLabels (ls .(map [string ]interface {}))
89+ }
90+
91+ if len (clusterIds ) == 0 && len (namespaces ) == 0 && len (labels ) == 0 {
92+ return errors .New ("allocation group must specify at least one of: cluster_ids, namespaces, or labels" )
93+ }
94+ return nil
95+ }
96+
7897func allocationGroupImporter (ctx context.Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
7998 client := meta .(* ProviderConfig ).api
8099 if _ , err := uuid .ParseUUID (d .Id ()); err != nil {
@@ -133,18 +152,24 @@ func resourceAllocationGroupRead(ctx context.Context, d *schema.ResourceData, me
133152func resourceAllocationGroupCreate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
134153 client := meta .(* ProviderConfig ).api
135154
136- clusterIds := toClusterIds (d )
155+ var clusterIds []string
156+ if cid , ok := d .GetOk ("cluster_ids" ); ok {
157+ clusterIds = toClusterIds (cid .(* schema.Set ).List ())
158+ }
137159
138160 allocationGroupName := d .Get ("name" ).(string )
139161
140162 namespaces := toStringList (d .Get ("namespaces" ).([]interface {}))
141163
142- labels := toLabels (d )
164+ var labels []sdk.CostreportV1beta1AllocationGroupFilterLabelValue
165+ if ls , ok := d .GetOk ("labels" ); ok {
166+ labels = toLabels (ls .(map [string ]interface {}))
167+ }
143168
144169 labelsOperator := toLabelsOperator (d )
145170
146- if len (clusterIds ) == 0 || ( len (namespaces ) == 0 && len (labels ) == 0 ) {
147- return diag .FromErr (errors .New ("allocation group needs to have at least one cluster id and one namespace or label " ))
171+ if len (clusterIds ) == 0 && len (namespaces ) == 0 && len (labels ) == 0 {
172+ return diag .FromErr (errors .New ("allocation group must specify at least one of: cluster_ids, namespaces, or labels " ))
148173 }
149174
150175 body := sdk.AllocationGroupAPICreateAllocationGroupJSONRequestBody {
@@ -184,12 +209,21 @@ func resourceAllocationGroupUpdate(ctx context.Context, d *schema.ResourceData,
184209 client := meta .(* ProviderConfig ).api
185210
186211 allocationGroupName := d .Get ("name" ).(string )
187- clusterIds := toClusterIds (d )
188- labels := toLabels (d )
212+
213+ var clusterIds []string
214+ if cids , ok := d .GetOk ("cluster_ids" ); ok {
215+ clusterIds = toClusterIds (cids .(* schema.Set ).List ())
216+ }
217+
218+ var labels []sdk.CostreportV1beta1AllocationGroupFilterLabelValue
219+ if ls , ok := d .GetOk ("labels" ); ok {
220+ labels = toLabels (ls .(map [string ]interface {}))
221+ }
222+
189223 namespaces := toStringList (d .Get ("namespaces" ).([]interface {}))
190224
191- if len (clusterIds ) == 0 || ( len (namespaces ) == 0 && len (labels ) == 0 ) {
192- return diag .FromErr (errors .New ("allocation group needs to have at least one cluster id and one namespace or label " ))
225+ if len (clusterIds ) == 0 && len (namespaces ) == 0 && len (labels ) == 0 {
226+ return diag .FromErr (errors .New ("allocation group must specify at least one of: cluster_ids, namespaces, or labels " ))
193227 }
194228
195229 req := sdk.AllocationGroupAPIUpdateAllocationGroupJSONRequestBody {
@@ -234,11 +268,9 @@ func toLabelsOperator(d *schema.ResourceData) *sdk.CostreportV1beta1FilterOperat
234268 return & defaultLabelOperator
235269}
236270
237- func toClusterIds (d * schema.ResourceData ) []string {
238- if v , ok := d .GetOk ("cluster_ids" ); ok {
239- if lv := v .(* schema.Set ).List (); len (lv ) > 0 {
240- return toStringList (lv )
241- }
271+ func toClusterIds (lv []interface {}) []string {
272+ if len (lv ) > 0 {
273+ return toStringList (lv )
242274 }
243275 return nil
244276}
@@ -251,25 +283,23 @@ func fromLabels(labels []sdk.CostreportV1beta1AllocationGroupFilterLabelValue) m
251283 return result
252284}
253285
254- func toLabels (d * schema.ResourceData ) []sdk.CostreportV1beta1AllocationGroupFilterLabelValue {
255- if v , ok := d .GetOk ("labels" ); ok {
256- if lv := v .(map [string ]interface {}); len (lv ) > 0 {
257- labelsStringMap := toStringMap (lv )
258-
259- operator := sdk .CostreportV1beta1AllocationGroupFilterLabelValueOperatorEqual
260-
261- if len (labelsStringMap ) > 0 {
262- labels := make ([]sdk.CostreportV1beta1AllocationGroupFilterLabelValue , 0 , len (labelsStringMap ))
263- for labelKey , labelValue := range labelsStringMap {
264- label := sdk.CostreportV1beta1AllocationGroupFilterLabelValue {
265- Label : & labelKey ,
266- Value : & labelValue ,
267- Operator : & operator ,
268- }
269- labels = append (labels , label )
286+ func toLabels (lv map [string ]interface {}) []sdk.CostreportV1beta1AllocationGroupFilterLabelValue {
287+ if len (lv ) > 0 {
288+ labelsStringMap := toStringMap (lv )
289+
290+ operator := sdk .CostreportV1beta1AllocationGroupFilterLabelValueOperatorEqual
291+
292+ if len (labelsStringMap ) > 0 {
293+ labels := make ([]sdk.CostreportV1beta1AllocationGroupFilterLabelValue , 0 , len (labelsStringMap ))
294+ for labelKey , labelValue := range labelsStringMap {
295+ label := sdk.CostreportV1beta1AllocationGroupFilterLabelValue {
296+ Label : & labelKey ,
297+ Value : & labelValue ,
298+ Operator : & operator ,
270299 }
271- return labels
300+ labels = append ( labels , label )
272301 }
302+ return labels
273303 }
274304 }
275305 return nil
0 commit comments