-
Notifications
You must be signed in to change notification settings - Fork 15
/
data_source_volume.go
134 lines (111 loc) · 3.72 KB
/
data_source_volume.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package blockstorage
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/clients"
"github.com/vk-cs/terraform-provider-vkcs/vkcs/internal/util"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
)
func DataSourceBlockStorageVolume() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceBlockStorageVolumeRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The region in which to obtain the Block Storage client. If omitted, the `region` argument of the provider is used.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the volume.",
},
"status": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The status of the volume.",
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Description: "Metadata key/value pairs associated with the volume.",
},
// Computed values
"bootable": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Indicates if the volume is bootable.",
},
"volume_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The type of the volume.",
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the volume in GBs.",
},
"source_volume_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the volume from which the current volume was created.",
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the availability zone of the volume.",
},
},
Description: "Use this data source to get information about an existing volume.",
}
}
func dataSourceBlockStorageVolumeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(clients.Config)
client, err := config.BlockStorageV3Client(util.GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating VKCS block storage client: %s", err)
}
listOpts := volumes.ListOpts{
Metadata: util.ExpandToMapStringString(d.Get("metadata").(map[string]interface{})),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
}
allPages, err := volumes.List(client, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to query vkcs_blockstorage_volume: %s", err)
}
allVolumes, err := volumes.ExtractVolumes(allPages)
if err != nil {
return diag.Errorf("Unable to retrieve vkcs_blockstorage_volume: %s", err)
}
if len(allVolumes) > 1 {
return diag.Errorf("Your vkcs_blockstorage_volume query returned multiple results")
}
if len(allVolumes) < 1 {
return diag.Errorf("Your vkcs_blockstorage_volume query returned no results")
}
dataSourceBlockStorageVolumeAttributes(d, allVolumes[0])
return nil
}
func dataSourceBlockStorageVolumeAttributes(d *schema.ResourceData, volume volumes.Volume) {
d.SetId(volume.ID)
d.Set("name", volume.Name)
d.Set("status", volume.Status)
d.Set("bootable", volume.Bootable)
d.Set("volume_type", volume.VolumeType)
d.Set("size", volume.Size)
d.Set("source_volume_id", volume.SourceVolID)
d.Set("availability_zone", volume.AvailabilityZone)
if err := d.Set("metadata", volume.Metadata); err != nil {
log.Printf("[DEBUG] Unable to set metadata for vkcs_blockstorage_volume %s: %s", volume.ID, err)
}
}