Skip to content

Commit 40cd81a

Browse files
authored
Adding attach volume resource (#76)
1 parent e1983e9 commit 40cd81a

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed

cloudstack/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func Provider() terraform.ResourceProvider {
9393

9494
ResourcesMap: map[string]*schema.Resource{
9595
"cloudstack_affinity_group": resourceCloudStackAffinityGroup(),
96+
"cloudstack_attach_volume": resourceCloudStackAttachVolume(),
9697
"cloudstack_autoscale_vm_profile": resourceCloudStackAutoScaleVMProfile(),
9798
"cloudstack_disk": resourceCloudStackDisk(),
9899
"cloudstack_egress_firewall": resourceCloudStackEgressFirewall(),
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for Attachitional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"github.com/apache/cloudstack-go/v2/cloudstack"
24+
"github.com/hashicorp/terraform/helper/schema"
25+
)
26+
27+
func resourceCloudStackAttachVolume() *schema.Resource {
28+
return &schema.Resource{
29+
Read: resourceCloudStackAttachVolumeRead,
30+
Create: resourceCloudStackAttachVolumeCreate,
31+
Delete: resourceCloudStackAttachVolumeDelete,
32+
Schema: map[string]*schema.Schema{
33+
"volume_id": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
ForceNew: true,
37+
Description: "the ID of the disk volume",
38+
},
39+
"virtual_machine_id": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
ForceNew: true,
43+
Description: "the ID of the virtual machine",
44+
},
45+
"device_id": {
46+
Type: schema.TypeInt,
47+
Optional: true,
48+
Computed: true,
49+
ForceNew: true,
50+
Description: "The ID of the device to map the volume to the guest OS. ",
51+
},
52+
"attached": {
53+
Type: schema.TypeString,
54+
Required: false,
55+
Computed: true,
56+
Description: "the date the volume was attached to a VM instance",
57+
},
58+
},
59+
}
60+
}
61+
62+
func resourceCloudStackAttachVolumeCreate(d *schema.ResourceData, meta interface{}) error {
63+
cs := meta.(*cloudstack.CloudStackClient)
64+
65+
p := cs.Volume.NewAttachVolumeParams(d.Get("volume_id").(string), d.Get("virtual_machine_id").(string))
66+
if v, ok := d.GetOk("device_id"); ok {
67+
p.SetDeviceid(v.(int64))
68+
}
69+
70+
r, err := cs.Volume.AttachVolume(p)
71+
if err != nil {
72+
return err
73+
}
74+
75+
d.SetId(r.Id)
76+
77+
return resourceCloudStackAttachVolumeRead(d, meta)
78+
}
79+
80+
func resourceCloudStackAttachVolumeRead(d *schema.ResourceData, meta interface{}) error {
81+
cs := meta.(*cloudstack.CloudStackClient)
82+
83+
r, _, err := cs.Volume.GetVolumeByID(d.Id())
84+
if err != nil {
85+
return err
86+
}
87+
88+
d.Set("volume_id", r.Id)
89+
d.Set("virtual_machine_id", r.Virtualmachineid)
90+
d.Set("device_id", r.Deviceid)
91+
d.Set("attached", r.Attached)
92+
93+
return nil
94+
}
95+
96+
func resourceCloudStackAttachVolumeDelete(d *schema.ResourceData, meta interface{}) error {
97+
cs := meta.(*cloudstack.CloudStackClient)
98+
99+
p := cs.Volume.NewDetachVolumeParams()
100+
p.SetId(d.Id())
101+
_, err := cs.Volume.DetachVolume(p)
102+
if err != nil {
103+
return err
104+
}
105+
106+
return nil
107+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package cloudstack
21+
22+
import (
23+
"testing"
24+
25+
"github.com/hashicorp/terraform/helper/resource"
26+
)
27+
28+
func TestAccCloudstackAttachVolume_basic(t *testing.T) {
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheck(t) },
31+
Providers: testAccProviders,
32+
Steps: []resource.TestStep{
33+
{
34+
Config: testAccCloudstackAttachVolume_basic,
35+
Check: resource.ComposeTestCheckFunc(
36+
resource.TestCheckResourceAttr("cloudstack_attach_volume.foo", "device_id", "1"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
const testAccCloudstackAttachVolume_basic = `
44+
resource "cloudstack_network" "foo" {
45+
name = "terraform-network"
46+
cidr = "10.1.1.0/24"
47+
network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
48+
zone = "Sandbox-simulator"
49+
}
50+
51+
resource "cloudstack_instance" "foobar" {
52+
name = "terraform-test"
53+
display_name = "terraform"
54+
service_offering= "Small Instance"
55+
network_id = "${cloudstack_network.foo.id}"
56+
template = "CentOS 5.6 (64-bit) no GUI (Simulator)"
57+
zone = "${cloudstack_network.foo.zone}"
58+
expunge = true
59+
}
60+
61+
resource "cloudstack_disk" "foo" {
62+
name = "terraform-disk"
63+
disk_offering = "Small"
64+
zone = "${cloudstack_instance.foobar.zone}"
65+
}
66+
67+
resource "cloudstack_attach_volume" "foo" {
68+
volume_id = cloudstack_disk.foo.id
69+
virtual_machine_id = cloudstack_instance.foobar.id
70+
}
71+
`

cloudstack/resource_cloudstack_disk.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func resourceCloudStackDisk() *schema.Resource {
4747
"attach": {
4848
Type: schema.TypeBool,
4949
Optional: true,
50-
Default: false,
50+
Computed: true,
5151
},
5252

5353
"device_id": {
@@ -76,6 +76,7 @@ func resourceCloudStackDisk() *schema.Resource {
7676
"virtual_machine_id": {
7777
Type: schema.TypeString,
7878
Optional: true,
79+
Computed: true,
7980
},
8081

8182
"project": {

0 commit comments

Comments
 (0)