Skip to content

Commit c078b76

Browse files
committed
chore: update API client generator and API client
1 parent 01cf99b commit c078b76

18 files changed

+2391
-3336
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ jobs:
5959
SSO_CLIENT_ID: ${{ secrets.SSO_CLIENT_ID }}
6060
SSO_CLIENT_SECRET: ${{ secrets.SSO_CLIENT_SECRET }}
6161
SSO_DOMAIN: ${{ secrets.SSO_DOMAIN }}
62+
ACCEPTANCE_TEST_ORGANIZATION_ID: ${{ vars.TF_ACCEPTANCE_TEST_ORGANIZATION_ID }}
6263
run: make testacc
6364

castai/provider_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func testAccPreCheck(t *testing.T) {
5050
t.Fatal("CASTAI_API_TOKEN must be set for acceptance tests")
5151
}
5252

53+
if v := os.Getenv("ACCEPTANCE_TEST_ORGANIZATION_ID"); v == "" {
54+
t.Fatal("ACCEPTANCE_TEST_ORGANIZATION_ID must be set for acceptance tests")
55+
}
56+
5357
if err := testAccProvider.Configure(context.Background(), terraform.NewResourceConfigRaw(nil)); err != nil {
5458
t.Fatal(err)
5559
}

castai/resource_eviction_config.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"github.com/castai/terraform-provider-castai/castai/sdk"
8+
"log"
9+
"time"
10+
911
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214
"github.com/samber/lo"
13-
"log"
14-
"time"
15+
16+
"github.com/castai/terraform-provider-castai/castai/sdk"
1517
)
1618

1719
const (
@@ -124,18 +126,18 @@ func resourceEvictionConfig() *schema.Resource {
124126
},
125127
},
126128
FieldEvictionOptionDisabled: {
127-
Type: schema.TypeBool,
128-
Optional: true,
129+
Type: schema.TypeBool,
130+
Optional: true,
129131
Description: "Mark pods as removal disabled",
130132
},
131133
FieldEvictionOptionAggressive: {
132-
Type: schema.TypeBool,
133-
Optional: true,
134+
Type: schema.TypeBool,
135+
Optional: true,
134136
Description: "Apply Aggressive mode to Evictor",
135137
},
136138
FieldEvictionOptionDisposable: {
137-
Type: schema.TypeBool,
138-
Optional: true,
139+
Type: schema.TypeBool,
140+
Optional: true,
139141
Description: "Mark node as disposable",
140142
},
141143
},
@@ -424,7 +426,7 @@ func toPodSelector(in interface{}) (*sdk.CastaiEvictorV1PodSelector, error) {
424426
return nil, err
425427
}
426428

427-
if mls == nil || len(mls.AdditionalProperties) == 0 {
429+
if mls == nil || len(*mls) == 0 {
428430
continue
429431
}
430432

@@ -474,21 +476,21 @@ func toNodeSelector(in interface{}) (*sdk.CastaiEvictorV1NodeSelector, error) {
474476
return &out, nil
475477
}
476478

477-
func toMatchLabels(in interface{}) (*sdk.CastaiEvictorV1LabelSelector_MatchLabels, error) {
479+
func toMatchLabels(in interface{}) (*map[string]string, error) {
478480
mls, ok := in.(map[string]interface{})
479481
if !ok {
480482
return nil, fmt.Errorf("mapping match_labels expecting map[string]interface, got %T %+v", in, in)
481483
}
482484
if len(mls) == 0 {
483485
return nil, nil
484486
}
485-
out := sdk.CastaiEvictorV1LabelSelector_MatchLabels{AdditionalProperties: map[string]string{}}
487+
out := map[string]string{}
486488
for k, v := range mls {
487489
value, ok := v.(string)
488490
if !ok {
489491
return nil, fmt.Errorf("mapping match_labels expecting string, got %T %+v", v, v)
490492
}
491-
out.AdditionalProperties[k] = value
493+
out[k] = value
492494
}
493495

494496
return &out, nil
@@ -507,7 +509,7 @@ func flattenPodSelector(ps *sdk.CastaiEvictorV1PodSelector) []map[string]any {
507509
}
508510
if ps.LabelSelector != nil {
509511
if ps.LabelSelector.MatchLabels != nil {
510-
out[FieldMatchLabels] = ps.LabelSelector.MatchLabels.AdditionalProperties
512+
out[FieldMatchLabels] = *ps.LabelSelector.MatchLabels
511513
}
512514
if ps.LabelSelector.MatchExpressions != nil {
513515
out[FieldMatchExpressions] = flattenMatchExpressions(*ps.LabelSelector.MatchExpressions)
@@ -522,7 +524,7 @@ func flattenNodeSelector(ns *sdk.CastaiEvictorV1NodeSelector) []map[string]any {
522524
}
523525
out := map[string]any{}
524526
if ns.LabelSelector.MatchLabels != nil {
525-
out[FieldMatchLabels] = ns.LabelSelector.MatchLabels.AdditionalProperties
527+
out[FieldMatchLabels] = *ns.LabelSelector.MatchLabels
526528
}
527529
if ns.LabelSelector.MatchExpressions != nil {
528530
out[FieldMatchExpressions] = flattenMatchExpressions(*ns.LabelSelector.MatchExpressions)

castai/resource_eviction_config_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"github.com/castai/terraform-provider-castai/castai/sdk"
8-
mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock"
7+
"io"
8+
"net/http"
9+
"testing"
10+
911
"github.com/golang/mock/gomock"
1012
"github.com/hashicorp/go-cty/cty"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1416
"github.com/samber/lo"
1517
"github.com/stretchr/testify/require"
16-
"io"
17-
"net/http"
18-
"testing"
18+
19+
"github.com/castai/terraform-provider-castai/castai/sdk"
20+
mock_sdk "github.com/castai/terraform-provider-castai/castai/sdk/mock"
1921
)
2022

2123
func TestEvictionConfig_ReadContext(t *testing.T) {
@@ -299,9 +301,9 @@ func TestEvictionConfig_UpdateContext(t *testing.T) {
299301
PodSelector: &sdk.CastaiEvictorV1PodSelector{
300302
Kind: lo.ToPtr("Job"),
301303
LabelSelector: &sdk.CastaiEvictorV1LabelSelector{
302-
MatchLabels: &sdk.CastaiEvictorV1LabelSelector_MatchLabels{AdditionalProperties: map[string]string{
304+
MatchLabels: &map[string]string{
303305
"key1": "value1",
304-
}}}}}
306+
}}}}
305307

306308
newConfig := sdk.CastaiEvictorV1EvictionConfig{
307309
Settings: sdk.CastaiEvictorV1EvictionSettings{Disposable: &sdk.CastaiEvictorV1EvictionSettingsSettingEnabled{Enabled: true}},

castai/resource_hibernation_schedule.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func resourceHibernationSchedule() *schema.Resource {
110110
},
111111

112112
Schema: map[string]*schema.Schema{
113-
FieldServiceAccountOrganizationID: {
113+
FieldHibernationScheduleOrganizationID: {
114114
Type: schema.TypeString,
115115
Optional: true,
116116
Description: "ID of the organization. If not provided, then will attempt to infer it using CAST AI API client.",
@@ -378,19 +378,24 @@ func resourceHibernationSchedule() *schema.Resource {
378378
}
379379

380380
func hibernationScheduleStateImporter(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
381-
organizationID, err := getHibernationScheduleOrganizationID(ctx, d, meta)
382-
if err != nil {
383-
return nil, err
381+
organizationID, id := parseImportID(d)
382+
if organizationID == "" {
383+
stateOrganizationID, err := getHibernationScheduleOrganizationID(ctx, d, meta)
384+
if err != nil {
385+
return nil, err
386+
}
387+
388+
organizationID = stateOrganizationID
384389
}
385390

386391
// if importing by UUID, nothing to do; if importing by name, fetch schedule ID and set that as resource ID
387-
if _, err := uuid.Parse(d.Id()); err != nil {
392+
if _, err := uuid.Parse(id); err != nil {
388393
tflog.Info(ctx, "provided schedule ID is not a UUID, will import by name")
389-
schedule, err := getHibernationScheduleByName(ctx, meta, organizationID, d.Id())
394+
schedule, err := getHibernationScheduleByName(ctx, meta, organizationID, id)
390395
if err != nil {
391396
return nil, err
392397
} else if schedule == nil {
393-
return nil, fmt.Errorf("could not find schedule by name: %s", d.Id())
398+
return nil, fmt.Errorf("could not find schedule by name: %s", id)
394399
}
395400

396401
d.SetId(lo.FromPtr(schedule.Id))
@@ -422,7 +427,7 @@ func resourceHibernationScheduleUpdate(ctx context.Context, d *schema.ResourceDa
422427

423428
resp, err := client.HibernationSchedulesAPIUpdateHibernationScheduleWithResponse(ctx, organizationID, d.Id(), req)
424429
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
425-
return diag.FromErr(checkErr)
430+
return diag.FromErr(fmt.Errorf("could not update hibernation schedule in organization %s: %v", organizationID, checkErr))
426431
}
427432

428433
return readHibernationScheduleIntoState(ctx, d, meta, organizationID, d.Id())
@@ -448,17 +453,17 @@ func resourceHibernationScheduleCreate(ctx context.Context, d *schema.ResourceDa
448453

449454
organizationID, err := getHibernationScheduleOrganizationID(ctx, d, meta)
450455
if err != nil {
451-
return diag.FromErr(err)
456+
return diag.FromErr(fmt.Errorf("could not determine organization id: %v", err))
452457
}
453458

454459
schedule, err := stateToHibernationSchedule(d)
455460
if err != nil {
456-
return diag.FromErr(err)
461+
return diag.FromErr(fmt.Errorf("could not map state to hibernation schedule: %v", err))
457462
}
458463

459464
resp, err := client.HibernationSchedulesAPICreateHibernationScheduleWithResponse(ctx, organizationID, *schedule)
460465
if checkErr := sdk.CheckOKResponse(resp, err); checkErr != nil {
461-
return diag.FromErr(checkErr)
466+
return diag.FromErr(fmt.Errorf("could not create hibernation schedule in organization %s: %v", organizationID, checkErr))
462467
}
463468

464469
d.SetId(*resp.JSON200.Id)
@@ -478,7 +483,7 @@ func resourceHibernationScheduleRead(ctx context.Context, d *schema.ResourceData
478483
func readHibernationScheduleIntoState(ctx context.Context, d *schema.ResourceData, meta any, organizationID, id string) diag.Diagnostics {
479484
schedule, err := getHibernationScheduleById(ctx, meta, organizationID, id)
480485
if err != nil {
481-
return diag.FromErr(err)
486+
return diag.FromErr(fmt.Errorf("could not retrieve hibernation schedule by id in organization %s: %v", organizationID, err))
482487
}
483488
if !d.IsNewResource() && schedule == nil {
484489
tflog.Warn(ctx, "Hibernation schedule not found, removing from state", map[string]any{"id": d.Id()})
@@ -840,7 +845,7 @@ func sectionToNodeConfig(section map[string]interface{}) cluster_autoscaler.Node
840845
ConfigName: lo.Ternary(configName != "", &configName, nil),
841846
SubnetId: lo.Ternary(subnetId != "", &subnetId, nil),
842847
Zone: lo.Ternary(zone != "", &zone, nil),
843-
KubernetesLabels: lo.Ternary(kubernetesLabels != nil && len(kubernetesLabels) != 0, &kubernetesLabels, nil),
848+
KubernetesLabels: lo.Ternary(len(kubernetesLabels) != 0, &kubernetesLabels, nil),
844849
GpuConfig: nodeConfigSectionToGPUConfig(section),
845850
NodeAffinity: nodeConfigSectionToNodeAffinity(section),
846851
KubernetesTaints: nodeConfigSectionToKubernetesTaints(section),
@@ -951,3 +956,15 @@ func getHibernationScheduleByName(ctx context.Context, meta interface{}, organiz
951956

952957
return nil, nil
953958
}
959+
960+
func parseImportID(d *schema.ResourceData) (string, string) {
961+
id := d.Id()
962+
963+
if strings.Contains(id, "/") {
964+
if parts := strings.Split(id, "/"); len(parts) > 1 {
965+
return parts[0], parts[1]
966+
}
967+
}
968+
969+
return "", id
970+
}

castai/resource_hibernation_schedule_test.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"os"
1011
"testing"
1112

1213
"github.com/golang/mock/gomock"
@@ -227,14 +228,16 @@ func TestHibernationSchedule_CreateContext(t *testing.T) {
227228
func TestAccResourceHibernationSchedule_basic(t *testing.T) {
228229
resourceName := fmt.Sprintf("%v-hibernation-schedule-%v", ResourcePrefix, acctest.RandString(8))
229230
renamedResourceName := fmt.Sprintf("%s %s", resourceName, "renamed")
231+
organizationID := os.Getenv("ACCEPTANCE_TEST_ORGANIZATION_ID")
230232

231233
resource.ParallelTest(t, resource.TestCase{
232234
PreCheck: func() { testAccPreCheck(t) },
233235

234236
ProviderFactories: providerFactories,
235237
Steps: []resource.TestStep{
236238
{
237-
Config: makeInitialHibernationScheduleConfig(resourceName),
239+
// Test creation
240+
Config: makeInitialHibernationScheduleConfig(resourceName, organizationID),
238241
Check: resource.ComposeTestCheckFunc(
239242
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "name", resourceName),
240243
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "enabled", "false"),
@@ -246,10 +249,10 @@ func TestAccResourceHibernationSchedule_basic(t *testing.T) {
246249
),
247250
},
248251
{
249-
// test edits
250-
Config: makeUpdateHibernationScheduleConfig(renamedResourceName),
252+
// Test update
253+
Config: makeUpdateHibernationScheduleConfig(renamedResourceName, organizationID),
251254
Check: resource.ComposeTestCheckFunc(
252-
resource.TestCheckResourceAttr("castai_rebalancing_schedule.test", "name", renamedResourceName),
255+
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "name", renamedResourceName),
253256
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "enabled", "true"),
254257
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "pause_config.0.enabled", "false"),
255258
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "pause_config.0.schedule.0.cron_expression", "1 0 * * *"),
@@ -258,30 +261,23 @@ func TestAccResourceHibernationSchedule_basic(t *testing.T) {
258261
resource.TestCheckResourceAttr("castai_hibernation_schedule.test_hibernation_schedule", "resume_config.0.job_config.0.node_config.0.instance_type", "e2-standard-8"),
259262
),
260263
},
261-
// We keep the ImportState test cases at the end so they will test any newly added fields.
262-
// This way it will also verify that after importing the state the output of `tf plan` is empty.
263264
{
264-
// Import state by ID
265+
// Test import
265266
ImportState: true,
266267
ResourceName: "castai_hibernation_schedule.test_hibernation_schedule",
267-
ImportStateVerify: true,
268-
},
269-
{
270-
// Import state by name
271-
ImportState: true,
272-
ResourceName: "castai_hibernation_schedule.test_hibernation_schedule",
273-
ImportStateId: renamedResourceName,
268+
ImportStateId: fmt.Sprintf("%s/%s", organizationID, renamedResourceName),
274269
ImportStateVerify: true,
275270
},
276271
},
277272
})
278273
}
279274

280-
func makeInitialHibernationScheduleConfig(rName string) string {
275+
func makeInitialHibernationScheduleConfig(rName string, organizationID string) string {
281276
template := `
282277
resource "castai_hibernation_schedule" "test_hibernation_schedule" {
283278
name = %q
284279
enabled = false
280+
organization_id = %q
285281
286282
pause_config {
287283
enabled = true
@@ -308,14 +304,15 @@ resource "castai_hibernation_schedule" "test_hibernation_schedule" {
308304
cluster_assignments {}
309305
}
310306
`
311-
return fmt.Sprintf(template, rName)
307+
return fmt.Sprintf(template, rName, organizationID)
312308
}
313309

314-
func makeUpdateHibernationScheduleConfig(rName string) string {
310+
func makeUpdateHibernationScheduleConfig(rName string, organizationID string) string {
315311
template := `
316312
resource "castai_hibernation_schedule" "test_hibernation_schedule" {
317313
name = %q
318314
enabled = true
315+
organization_id = %q
319316
320317
pause_config {
321318
enabled = false
@@ -342,5 +339,5 @@ resource "castai_hibernation_schedule" "test_hibernation_schedule" {
342339
cluster_assignments {}
343340
}
344341
`
345-
return fmt.Sprintf(template, rName)
342+
return fmt.Sprintf(template, rName, organizationID)
346343
}

castai/resource_node_configuration.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,7 @@ func resourceNodeConfigurationCreate(ctx context.Context, d *schema.ResourceData
645645
req.KubeletConfig = toPtr(m)
646646
}
647647
if v := d.Get(FieldNodeConfigurationTags).(map[string]interface{}); len(v) > 0 {
648-
req.Tags = &sdk.NodeconfigV1NewNodeConfiguration_Tags{
649-
AdditionalProperties: toStringMap(v),
650-
}
648+
req.Tags = lo.ToPtr(toStringMap(v))
651649
}
652650

653651
// Map provider specific configurations.
@@ -720,7 +718,7 @@ func resourceNodeConfigurationRead(ctx context.Context, d *schema.ResourceData,
720718
if err := d.Set(FieldNodeConfigurationContainerRuntime, nodeConfig.ContainerRuntime); err != nil {
721719
return diag.FromErr(fmt.Errorf("setting container runtime: %w", err))
722720
}
723-
if err := d.Set(FieldNodeConfigurationTags, nodeConfig.Tags.AdditionalProperties); err != nil {
721+
if err := d.Set(FieldNodeConfigurationTags, nodeConfig.Tags); err != nil {
724722
return diag.FromErr(fmt.Errorf("setting tags: %w", err))
725723
}
726724

@@ -819,9 +817,7 @@ func resourceNodeConfigurationUpdate(ctx context.Context, d *schema.ResourceData
819817
req.KubeletConfig = toPtr(m)
820818
}
821819
if v := d.Get(FieldNodeConfigurationTags).(map[string]interface{}); len(v) > 0 {
822-
req.Tags = &sdk.NodeconfigV1NodeConfigurationUpdate_Tags{
823-
AdditionalProperties: toStringMap(v),
824-
}
820+
req.Tags = lo.ToPtr(toStringMap(v))
825821
}
826822

827823
// Map provider specific configurations.

0 commit comments

Comments
 (0)