This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch main
in repository
https://gitbox.apache.org/repos/asf/cloudstack-terraform-provider.git
The following commit(s) were added to refs/heads/main by this push:
new 40cd81a Adding attach volume resource (#76)
40cd81a is described below
commit 40cd81a6d337a06248d17acbb54ed8a0b4d73b91
Author: poddm <[email protected]>
AuthorDate: Mon Feb 19 12:11:48 2024 -0800
Adding attach volume resource (#76)
---
cloudstack/provider.go | 1 +
cloudstack/resource_cloudstack_attach_volume.go | 107 +++++++++++++++++++++
.../resource_cloudstack_attach_volume_test.go | 71 ++++++++++++++
cloudstack/resource_cloudstack_disk.go | 3 +-
4 files changed, 181 insertions(+), 1 deletion(-)
diff --git a/cloudstack/provider.go b/cloudstack/provider.go
index c39914e..3def7c9 100644
--- a/cloudstack/provider.go
+++ b/cloudstack/provider.go
@@ -93,6 +93,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"cloudstack_affinity_group":
resourceCloudStackAffinityGroup(),
+ "cloudstack_attach_volume":
resourceCloudStackAttachVolume(),
"cloudstack_autoscale_vm_profile":
resourceCloudStackAutoScaleVMProfile(),
"cloudstack_disk":
resourceCloudStackDisk(),
"cloudstack_egress_firewall":
resourceCloudStackEgressFirewall(),
diff --git a/cloudstack/resource_cloudstack_attach_volume.go
b/cloudstack/resource_cloudstack_attach_volume.go
new file mode 100644
index 0000000..07892f2
--- /dev/null
+++ b/cloudstack/resource_cloudstack_attach_volume.go
@@ -0,0 +1,107 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for Attachitional 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 (
+ "github.com/apache/cloudstack-go/v2/cloudstack"
+ "github.com/hashicorp/terraform/helper/schema"
+)
+
+func resourceCloudStackAttachVolume() *schema.Resource {
+ return &schema.Resource{
+ Read: resourceCloudStackAttachVolumeRead,
+ Create: resourceCloudStackAttachVolumeCreate,
+ Delete: resourceCloudStackAttachVolumeDelete,
+ Schema: map[string]*schema.Schema{
+ "volume_id": {
+ Type: schema.TypeString,
+ Required: true,
+ ForceNew: true,
+ Description: "the ID of the disk volume",
+ },
+ "virtual_machine_id": {
+ Type: schema.TypeString,
+ Required: true,
+ ForceNew: true,
+ Description: "the ID of the virtual machine",
+ },
+ "device_id": {
+ Type: schema.TypeInt,
+ Optional: true,
+ Computed: true,
+ ForceNew: true,
+ Description: "The ID of the device to map the
volume to the guest OS. ",
+ },
+ "attached": {
+ Type: schema.TypeString,
+ Required: false,
+ Computed: true,
+ Description: "the date the volume was attached
to a VM instance",
+ },
+ },
+ }
+}
+
+func resourceCloudStackAttachVolumeCreate(d *schema.ResourceData, meta
interface{}) error {
+ cs := meta.(*cloudstack.CloudStackClient)
+
+ 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))
+ }
+
+ r, err := cs.Volume.AttachVolume(p)
+ if err != nil {
+ return err
+ }
+
+ d.SetId(r.Id)
+
+ return resourceCloudStackAttachVolumeRead(d, meta)
+}
+
+func resourceCloudStackAttachVolumeRead(d *schema.ResourceData, meta
interface{}) error {
+ cs := meta.(*cloudstack.CloudStackClient)
+
+ r, _, err := cs.Volume.GetVolumeByID(d.Id())
+ if err != nil {
+ return err
+ }
+
+ d.Set("volume_id", r.Id)
+ d.Set("virtual_machine_id", r.Virtualmachineid)
+ d.Set("device_id", r.Deviceid)
+ d.Set("attached", r.Attached)
+
+ return nil
+}
+
+func resourceCloudStackAttachVolumeDelete(d *schema.ResourceData, meta
interface{}) error {
+ cs := meta.(*cloudstack.CloudStackClient)
+
+ p := cs.Volume.NewDetachVolumeParams()
+ p.SetId(d.Id())
+ _, err := cs.Volume.DetachVolume(p)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/cloudstack/resource_cloudstack_attach_volume_test.go
b/cloudstack/resource_cloudstack_attach_volume_test.go
new file mode 100644
index 0000000..cb79a34
--- /dev/null
+++ b/cloudstack/resource_cloudstack_attach_volume_test.go
@@ -0,0 +1,71 @@
+//
+// 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/hashicorp/terraform/helper/resource"
+)
+
+func TestAccCloudstackAttachVolume_basic(t *testing.T) {
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ Steps: []resource.TestStep{
+ {
+ Config: testAccCloudstackAttachVolume_basic,
+ Check: resource.ComposeTestCheckFunc(
+
resource.TestCheckResourceAttr("cloudstack_attach_volume.foo", "device_id",
"1"),
+ ),
+ },
+ },
+ })
+}
+
+const testAccCloudstackAttachVolume_basic = `
+resource "cloudstack_network" "foo" {
+ name = "terraform-network"
+ cidr = "10.1.1.0/24"
+ network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
+ zone = "Sandbox-simulator"
+}
+
+ resource "cloudstack_instance" "foobar" {
+ name = "terraform-test"
+ display_name = "terraform"
+ service_offering= "Small Instance"
+ network_id = "${cloudstack_network.foo.id}"
+ template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
+ zone = "${cloudstack_network.foo.zone}"
+ expunge = true
+}
+
+ resource "cloudstack_disk" "foo" {
+ name = "terraform-disk"
+ disk_offering = "Small"
+ zone = "${cloudstack_instance.foobar.zone}"
+}
+
+resource "cloudstack_attach_volume" "foo" {
+ volume_id = cloudstack_disk.foo.id
+ virtual_machine_id = cloudstack_instance.foobar.id
+}
+`
diff --git a/cloudstack/resource_cloudstack_disk.go
b/cloudstack/resource_cloudstack_disk.go
index 5184a78..6127633 100644
--- a/cloudstack/resource_cloudstack_disk.go
+++ b/cloudstack/resource_cloudstack_disk.go
@@ -47,7 +47,7 @@ func resourceCloudStackDisk() *schema.Resource {
"attach": {
Type: schema.TypeBool,
Optional: true,
- Default: false,
+ Computed: true,
},
"device_id": {
@@ -76,6 +76,7 @@ func resourceCloudStackDisk() *schema.Resource {
"virtual_machine_id": {
Type: schema.TypeString,
Optional: true,
+ Computed: true,
},
"project": {