Skip to content

Commit 9f8ab6d

Browse files
committed
REP-1293: corrects PR comments, makes resource follow how the api works
1 parent 391e050 commit 9f8ab6d

4 files changed

Lines changed: 194 additions & 35 deletions

File tree

castai/resource_allocation_group.go

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
7897
func 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
133152
func 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

castai/resource_allocation_group_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"regexp"
78
"testing"
89
"time"
910

@@ -19,6 +20,10 @@ func TestAccResourceAllocationGroup(t *testing.T) {
1920
ProviderFactories: providerFactories,
2021
CheckDestroy: testAccCheckAllocationGroupDestroy,
2122
Steps: []resource.TestStep{
23+
{
24+
Config: invalidAllocationGroupConfig(),
25+
ExpectError: regexp.MustCompile(`allocation group must specify at least one of: cluster_ids, namespaces, or labels`),
26+
},
2227
{
2328
Config: allocationGroupConfig(),
2429
Check: resource.ComposeTestCheckFunc(
@@ -81,6 +86,14 @@ func testAccCheckAllocationGroupDestroy(s *terraform.State) error {
8186
return nil
8287
}
8388

89+
func invalidAllocationGroupConfig() string {
90+
cfg := `
91+
resource "castai_allocation_group" "test" {
92+
name = "Test terraform example"
93+
}`
94+
return ConfigCompose(cfg)
95+
}
96+
8497
func allocationGroupConfig() string {
8598
cfg := `
8699
resource "castai_allocation_group" "test" {

castai/sdk/api.gen.go

Lines changed: 119 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/resources/allocation_group.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)