diff --git a/go.mod b/go.mod index 60babebc..acd007d3 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/HewlettPackard/hpegl-vmaas-terraform-resources go 1.21 require ( - github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14 + github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.15-0.20241107103327-b0deabd3fd01 github.com/golang/mock v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/terraform-plugin-docs v0.9.0 diff --git a/go.sum b/go.sum index bc80f5ae..52c90d26 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14 h1:G3i8N70LBGo1tEFXCCBx5fd+cERBvnBCviFheVLlj80= github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.14/go.mod h1:tsKOAAbEQnpXAzqjcZUGzRiVp9QfRViKoTOdIltIMFI= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.15-0.20241107103327-b0deabd3fd01 h1:Ih+LknaNiYFLYUn4D5DZ+yPvy3S1rPdXlXtN+YT5D9o= +github.com/HewlettPackard/hpegl-vmaas-cmp-go-sdk v0.1.15-0.20241107103327-b0deabd3fd01/go.mod h1:tsKOAAbEQnpXAzqjcZUGzRiVp9QfRViKoTOdIltIMFI= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= diff --git a/internal/acceptance_test/data_source_instance_storage_controller_test.go b/internal/acceptance_test/data_source_instance_storage_controller_test.go index 63176a62..afb289a9 100644 --- a/internal/acceptance_test/data_source_instance_storage_controller_test.go +++ b/internal/acceptance_test/data_source_instance_storage_controller_test.go @@ -21,7 +21,7 @@ func TestAccDataSourceInstanceStorageController(t *testing.T) { Cfg: cfg, } - return iClient.GetStorageControllerMount(getAccContext(), toInt(attr["instance_id"]), attr["controller_type"], toInt(attr["bus_number"]), toInt(attr["interface_number"])) + return iClient.GetStorageControllerMount(getAccContext(), toInt64(attr["instance_id"]), attr["controller_type"], toInt(attr["bus_number"]), toInt(attr["interface_number"])) }, } diff --git a/internal/acceptance_test/helper.go b/internal/acceptance_test/helper.go index 3505caee..58f258ac 100644 --- a/internal/acceptance_test/helper.go +++ b/internal/acceptance_test/helper.go @@ -139,6 +139,12 @@ func toInt(s string) int { return i } +func toInt64(s string) int64 { + i, _ := strconv.ParseInt(s, 10, 64) + + return i +} + func getAccContext() context.Context { return context.Background() } diff --git a/internal/acceptance_test/resource_instances_clone_test.go b/internal/acceptance_test/resource_instances_clone_test.go index 4ce3d62a..4ff26aae 100644 --- a/internal/acceptance_test/resource_instances_clone_test.go +++ b/internal/acceptance_test/resource_instances_clone_test.go @@ -29,7 +29,7 @@ func TestAccResourceInstanceCloneCreate(t *testing.T) { Client: cl, Cfg: cfg, } - id := toInt(attr["id"]) + id := toInt64(attr["id"]) return iClient.GetASpecificInstance(getAccContext(), id) }, diff --git a/internal/acceptance_test/resource_instances_test.go b/internal/acceptance_test/resource_instances_test.go index 47f1de56..f5fb1ac7 100644 --- a/internal/acceptance_test/resource_instances_test.go +++ b/internal/acceptance_test/resource_instances_test.go @@ -29,7 +29,7 @@ func TestAccResourceInstanceCreate(t *testing.T) { Client: cl, Cfg: cfg, } - id := toInt(attr["id"]) + id := toInt64(attr["id"]) return iClient.GetASpecificInstance(getAccContext(), id) }, @@ -50,7 +50,7 @@ func TestAccResourceInstanceCreate_templateErr(t *testing.T) { Client: cl, Cfg: cfg, } - id := toInt(attr["id"]) + id := toInt64(attr["id"]) return iClient.GetASpecificInstance(getAccContext(), id) }, diff --git a/internal/cmp/instance_clone.go b/internal/cmp/instance_clone.go index 2494e91c..3b24e830 100644 --- a/internal/cmp/instance_clone.go +++ b/internal/cmp/instance_clone.go @@ -70,7 +70,7 @@ func (i *instanceClone) Create(ctx context.Context, d *utils.Data, meta interfac } // Get source instance - sourceID := d.GetInt("source_instance_id") + sourceID := d.GetInt64("source_instance_id") err := copyInstanceAttribsToClone(ctx, i, &req, d.GetListMap("volume"), sourceID) if err != nil { return err @@ -165,7 +165,7 @@ func (i *instanceClone) Read(ctx context.Context, d *utils.Data, meta interface{ return readInstance(ctx, i.instanceSharedClient, d, meta, true) } -func checkInstanceCloneHistory(ctx context.Context, i *instanceClone, meta interface{}, instanceID int) error { +func checkInstanceCloneHistory(ctx context.Context, i *instanceClone, meta interface{}, instanceID int64) error { errCount := 0 historyRetry := utils.CustomRetry{ InitialDelay: time.Second * 15, @@ -211,7 +211,7 @@ func cloneInstance( i *instanceClone, meta interface{}, req models.CreateInstanceCloneBody, - sourceID int, + sourceID int64, ) error { cloneRetry := &utils.CustomRetry{ Cond: func(response interface{}, ResponseErr error) (bool, error) { @@ -243,7 +243,7 @@ func copyInstanceAttribsToClone( i *instanceClone, req *models.CreateInstanceCloneBody, volumes []map[string]interface{}, - sourceID int, + sourceID int64, ) error { sourceInstance, err := i.iClient.GetASpecificInstance(ctx, sourceID) if err != nil { diff --git a/internal/cmp/instance_helper.go b/internal/cmp/instance_helper.go index 738c032b..530f6f3e 100644 --- a/internal/cmp/instance_helper.go +++ b/internal/cmp/instance_helper.go @@ -23,7 +23,7 @@ type instanceSharedClient struct { } func readInstance(ctx context.Context, sharedClient instanceSharedClient, d *utils.Data, meta interface{}, isClone bool) error { - id := d.GetID() + id := d.GetID64() log.Printf("[INFO] Get instance with ID %d", id) // Precheck @@ -98,7 +98,7 @@ func readInstance(ctx context.Context, sharedClient instanceSharedClient, d *uti func updateInstance(ctx context.Context, sharedClient instanceSharedClient, d *utils.Data) error { log.Printf("[DEBUG] Updating the instance") - id := d.GetID() + id := d.GetID64() if d.HasChanged("name") || d.HasChanged("group_id") || d.HasChanged("tags") || d.HasChanged("labels") || d.HasChanged("environment_code") || d.HasChanged("power_schedule_id") { @@ -168,7 +168,7 @@ func updateInstance(ctx context.Context, sharedClient instanceSharedClient, d *u // Delete instance and set ID as "" func deleteInstance(ctx context.Context, sharedClient instanceSharedClient, d *utils.Data, meta interface{}) error { - id := d.GetID() + id := d.GetID64() log.Printf("[DEBUG] Deleting instance with ID : %d", id) // Precheck @@ -378,7 +378,7 @@ func instanceCompareVolumes(org, new []map[string]interface{}) ([]map[string]int func instanceDoPowerTask( ctx context.Context, sharedClient instanceSharedClient, - instanceID int, + instanceID int64, newOp string) error { var err error @@ -422,7 +422,7 @@ func instanceCloneCompareVolume( func createInstanceSnapshot( ctx context.Context, sharedClient instanceSharedClient, - instanceID int, + instanceID int64, snapshot models.SnapshotBody, ) error { instanceModel, err := sharedClient.iClient.SnapshotAnInstance(ctx, instanceID, &snapshot) @@ -468,7 +468,7 @@ func instanceCheckSnaphotByName(name string, snapshotResp interface{}) int { return -1 } -func instanceWaitUntilCreated(ctx context.Context, sharedClient instanceSharedClient, meta interface{}, instanceID int) error { +func instanceWaitUntilCreated(ctx context.Context, sharedClient instanceSharedClient, meta interface{}, instanceID int64) error { errCount := 0 cRetry := utils.CustomRetry{ Timeout: maxTimeout, @@ -584,7 +584,7 @@ func instanceUpdateNetworkVolumePlan( ctx context.Context, sharedClient instanceSharedClient, d *utils.Data, - instanceID int, + instanceID int64, ) error { var resizeReq models.ResizeInstanceBody if d.HasChanged("volume") { diff --git a/internal/cmp/instance_storage_controller_data_source.go b/internal/cmp/instance_storage_controller_data_source.go index a1efb3a1..8655f1a8 100644 --- a/internal/cmp/instance_storage_controller_data_source.go +++ b/internal/cmp/instance_storage_controller_data_source.go @@ -24,7 +24,7 @@ func newInstanceStorageController(instanceClient *client.InstancesAPIService) *i func (i *instanceStorageController) Read(ctx context.Context, d *utils.Data, meta interface{}) error { setMeta(meta, i.iClient.Client) log.Printf("[DEBUG] Get Instance Storage Controller") - instanceID := d.GetInt("instance_id") + instanceID := d.GetInt64("instance_id") if instanceID == 0 { return nil } diff --git a/internal/utils/data.go b/internal/utils/data.go index 7a6148fb..5ab33393 100644 --- a/internal/utils/data.go +++ b/internal/utils/data.go @@ -122,6 +122,16 @@ func (d *Data) GetID() int { return int(id) } +func (d *Data) GetID64() int64 { + id, err := ParseInt(d.d.Id()) + if err != nil { + d.err("id", ErrInvalidType) + + return NAN + } + + return int64(id) +} // GetIDString returns ID as string func (d *Data) GetIDString() string { @@ -205,6 +215,33 @@ func (d *Data) GetInt(key string, ignore ...bool) int { return valInt } +func (d *Data) GetInt64(key string, ignore ...bool) int64 { + valInter, ok := d.getOk(key, ignore) + if !ok { + return NAN + } + valInt, ok := valInter.(int64) + var err error + if !ok { + valString, ok := valInter.(string) + if ok { + valInt, err = ParseInt(valString) + if err != nil { + d.err(key, ErrInvalidType+valString) + + return NAN + } + + return valInt + } + d.err(key, ErrInvalidType) + + return NAN + } + + return valInt +} + func (d *Data) getOk(key string, ignore []bool) (interface{}, bool) { val, ok := d.d.GetOk(key) if len(ignore) != 0 && !ignore[0] {