-
Notifications
You must be signed in to change notification settings - Fork 15
/
data_source_ecl_storage_virtualstorage_v1.go
152 lines (134 loc) · 3.89 KB
/
data_source_ecl_storage_virtualstorage_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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ecl
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/nttcom/eclcloud/ecl/storage/v1/virtualstorages"
"log"
)
func dataSourceStorageVirtualStorageV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceStorageVirtualStorageV1Read,
Schema: map[string]*schema.Schema{
"virtual_storage_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"network_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"volume_type_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ip_addr_pool": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"end": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"host_routes": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"nexthop": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func virtualStorageSchemaSet(d *schema.ResourceData, vs *virtualstorages.VirtualStorage) error {
log.Printf("[DEBUG] Retrieved Virtual Storage %s: %+v", vs.ID, vs)
d.SetId(vs.ID)
d.Set("name", vs.Name)
d.Set("api_error_message", vs.APIErrorMessage)
d.Set("network_id", vs.NetworkID)
d.Set("subnet_id", vs.SubnetID)
d.Set("ip_addr_pool", vs.IPAddrPool)
d.Set("host_routes", vs.HostRoutes)
d.Set("name", vs.Name)
d.Set("description", vs.Description)
d.Set("volume_type_id", vs.VolumeTypeID)
d.Set("host_routes", getHostRoutesFromVirtualStorage(vs))
d.Set("ip_addr_pool", getIPAddrPoolFromVirtualStorage(vs))
return nil
}
func dataSourceStorageVirtualStorageV1Read(d *schema.ResourceData, meta interface{}) error {
var listOpts virtualstorages.ListOptsBuilder
config := meta.(*Config)
client, err := config.storageV1Client(GetRegion(d, config))
noResultMessage := "Your query returned no results. Please change your search criteria and try again"
if _, ok := d.GetOk("virtual_storage_id"); ok {
vs, err := virtualstorages.Get(client, d.Get("virtual_storage_id").(string)).Extract()
if err != nil {
return fmt.Errorf(noResultMessage)
}
return virtualStorageSchemaSet(d, vs)
}
pages, err := virtualstorages.List(client, listOpts).AllPages()
if err != nil {
return err
}
tmpAllVirtualStorages, err := virtualstorages.ExtractVirtualStorages(pages)
if err != nil {
return err
}
if len(tmpAllVirtualStorages) < 1 {
return fmt.Errorf(noResultMessage)
}
var allVirtualStorages []virtualstorages.VirtualStorage
err = virtualstorages.ExtractVirtualStoragesInto(pages, &allVirtualStorages)
if err != nil {
return fmt.Errorf("Unable to retrieve virtual storages: %s", err)
}
var refinedVirtualStorages []virtualstorages.VirtualStorage
if name := d.Get("name").(string); name != "" {
for _, vs := range allVirtualStorages {
if vs.Name == name {
refinedVirtualStorages = append(refinedVirtualStorages, vs)
}
}
} else {
refinedVirtualStorages = allVirtualStorages
}
if len(refinedVirtualStorages) < 1 {
return fmt.Errorf(noResultMessage)
}
if len(refinedVirtualStorages) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
vs := refinedVirtualStorages[0]
return virtualStorageSchemaSet(d, &vs)
}