|
| 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 | +} |
0 commit comments