/
blockstorage_volume_v1.go
54 lines (44 loc) · 1.51 KB
/
blockstorage_volume_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package nhncloud
import (
"bytes"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
"github.com/gophercloud/utils/terraform/hashcode"
)
func flattenBlockStorageVolumeV1Attachments(v []map[string]interface{}) []map[string]interface{} {
attachments := make([]map[string]interface{}, len(v))
for i, attachment := range v {
attachments[i] = make(map[string]interface{})
attachments[i]["id"] = attachment["id"]
attachments[i]["instance_id"] = attachment["server_id"]
attachments[i]["device"] = attachment["device"]
}
return attachments
}
func blockStorageVolumeV1StateRefreshFunc(client *gophercloud.ServiceClient, volumeID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
v, err := volumes.Get(client, volumeID).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return v, "deleted", nil
}
return nil, "", err
}
if v.Status == "error" {
return v, v.Status, fmt.Errorf("The volume is in error status. " +
"Please check with your cloud admin or check the Block Storage " +
"API logs to see why this error occurred.")
}
return v, v.Status, nil
}
}
func blockStorageVolumeV1AttachmentHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
if m["instance_id"] != nil {
buf.WriteString(fmt.Sprintf("%s-", m["instance_id"].(string)))
}
return hashcode.String(buf.String())
}