diff --git a/cloudstack/resource_cloudstack_attach_volume.go b/cloudstack/resource_cloudstack_attach_volume.go index 5880c912..e5916cc9 100644 --- a/cloudstack/resource_cloudstack_attach_volume.go +++ b/cloudstack/resource_cloudstack_attach_volume.go @@ -64,7 +64,7 @@ func resourceCloudStackAttachVolumeCreate(d *schema.ResourceData, meta interface p := cs.Volume.NewAttachVolumeParams(d.Get("volume_id").(string), d.Get("virtual_machine_id").(string)) if v, ok := d.GetOk("device_id"); ok { - p.SetDeviceid(v.(int64)) + p.SetDeviceid(int64(v.(int))) } r, err := cs.Volume.AttachVolume(p) diff --git a/cloudstack/resource_cloudstack_attach_volume_unit_test.go b/cloudstack/resource_cloudstack_attach_volume_unit_test.go new file mode 100644 index 00000000..5798d02f --- /dev/null +++ b/cloudstack/resource_cloudstack_attach_volume_unit_test.go @@ -0,0 +1,46 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package cloudstack + +import ( + "testing" + + "github.com/apache/cloudstack-go/v2/cloudstack" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// device_id is a schema.TypeInt, which the SDK returns as a Go int. Asserting +// v.(int64) on it panics with "interface conversion: interface {} is int, not +// int64" whenever a volume is attached with an explicit device_id. The create +// function must reach the API call (and return its error) rather than panic. +func TestAttachVolumeDeviceIdDoesNotPanic(t *testing.T) { + cs := cloudstack.NewClient("http://127.0.0.1:1", "key", "secret", false) + + d := schema.TestResourceDataRaw(t, resourceCloudStackAttachVolume().Schema, map[string]interface{}{ + "volume_id": "vol-1", + "virtual_machine_id": "vm-1", + "device_id": 5, + }) + + err := resourceCloudStackAttachVolumeCreate(d, cs) + if err == nil { + t.Fatal("expected an API error from the unreachable endpoint, got nil") + } +}