@@ -29,6 +29,9 @@ const (
2929 fieldCommitmentsConfigs = "commitment_configs"
3030 fieldCommitmentsOrganizationId = "organization_id"
3131 fieldCommitmentsImportMode = "import_mode"
32+
33+ importModeOverwrite = "OVERWRITE"
34+ importModeAppend = "APPEND"
3235)
3336
3437var (
@@ -160,9 +163,9 @@ func resourceCommitments() *schema.Resource {
160163 fieldCommitmentsImportMode : {
161164 Type : schema .TypeString ,
162165 Optional : true ,
163- Default : "OVERWRITE" ,
166+ Default : importModeOverwrite ,
164167 Description : "Import mode. OVERWRITE replaces all commitments for the CSP. APPEND upserts without deleting existing ones." ,
165- ValidateDiagFunc : validation .ToDiagFunc (validation .StringInSlice ([]string {"OVERWRITE" , "APPEND" }, false )),
168+ ValidateDiagFunc : validation .ToDiagFunc (validation .StringInSlice ([]string {importModeOverwrite , importModeAppend }, false )),
166169 },
167170 // Input configurations
168171 fieldCommitmentsConfigs : {
@@ -584,7 +587,7 @@ func resourceCastaiCommitmentsUpsert(ctx context.Context, data *schema.ResourceD
584587 return diag .FromErr (err )
585588 }
586589
587- if mode == "APPEND" {
590+ if mode == importModeAppend {
588591 // Build set of reservation IDs from the import input
589592 importReservationIDs := make (map [string ]struct {})
590593 for _ , r := range reservations {
@@ -678,7 +681,7 @@ func importCUDs(ctx context.Context, meta any, imports []sdk.CastaiInventoryV1be
678681 res , err := meta .(* ProviderConfig ).api .CommitmentsAPIImportGCPCommitmentsWithResponse (
679682 ctx ,
680683 & sdk.CommitmentsAPIImportGCPCommitmentsParams {
681- Behaviour : lo.ToPtr [sdk.CommitmentsAPIImportGCPCommitmentsParamsBehaviour ]("OVERWRITE" ),
684+ Behaviour : lo.ToPtr [sdk.CommitmentsAPIImportGCPCommitmentsParamsBehaviour ](importModeOverwrite ),
682685 },
683686 imports ,
684687 )
@@ -688,12 +691,26 @@ func importCUDs(ctx context.Context, meta any, imports []sdk.CastaiInventoryV1be
688691 return nil
689692}
690693
694+ func toAzureImportBehaviour (mode string ) (sdk.CommitmentsAPIImportAzureReservationsParamsBehaviour , error ) {
695+ switch mode {
696+ case importModeOverwrite :
697+ return sdk .CommitmentsAPIImportAzureReservationsParamsBehaviourOVERWRITE , nil
698+ case importModeAppend :
699+ return sdk .CommitmentsAPIImportAzureReservationsParamsBehaviourAPPEND , nil
700+ default :
701+ return "" , fmt .Errorf ("unsupported azure import mode: %q" , mode )
702+ }
703+ }
704+
691705func importReservations (ctx context.Context , meta any , imports []sdk.CastaiInventoryV1beta1AzureReservationImport , mode string ) error {
706+ behaviour , err := toAzureImportBehaviour (mode )
707+ if err != nil {
708+ return err
709+ }
692710 res , err := meta .(* ProviderConfig ).api .CommitmentsAPIImportAzureReservationsWithResponse (
693711 ctx ,
694712 & sdk.CommitmentsAPIImportAzureReservationsParams {
695- Behaviour : lo.ToPtr [sdk.CommitmentsAPIImportAzureReservationsParamsBehaviour ](
696- sdk .CommitmentsAPIImportAzureReservationsParamsBehaviour (mode )),
713+ Behaviour : lo .ToPtr (behaviour ),
697714 },
698715 imports ,
699716 )
@@ -768,7 +785,7 @@ func populateCommitmentsResourceData(ctx context.Context, d *schema.ResourceData
768785 if v , ok := d .GetOk (fieldCommitmentsImportMode ); ok {
769786 mode = v .(string )
770787 }
771- if mode == "APPEND" && reservationsOk {
788+ if mode == importModeAppend && reservationsOk {
772789 // Filter azureResources to only those matching our import identifiers
773790 importIDs := make (map [string ]struct {})
774791 for _ , r := range reservations {
0 commit comments