-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathdata_source_cloudstack_vpc_offering.go
More file actions
233 lines (211 loc) · 6.28 KB
/
Copy pathdata_source_cloudstack_vpc_offering.go
File metadata and controls
233 lines (211 loc) · 6.28 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package cloudstack
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCloudstackVPCOffering() *schema.Resource {
return &schema.Resource{
Read: datasourceCloudStackVPCOfferingRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
//Computed values
"name": {
Type: schema.TypeString,
Computed: true,
},
"display_text": {
Type: schema.TypeString,
Computed: true,
},
"enable": {
Type: schema.TypeBool,
Computed: true,
},
"for_nsx": {
Type: schema.TypeBool,
Computed: true,
},
"internet_protocol": {
Type: schema.TypeString,
Computed: true,
},
"network_mode": {
Type: schema.TypeString,
Computed: true,
},
"routing_mode": {
Type: schema.TypeString,
Computed: true,
},
"specify_as_number": {
Type: schema.TypeBool,
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
},
"supported_services": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"service_provider_list": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"service_capability_list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Computed: true,
},
"capability_type": {
Type: schema.TypeString,
Computed: true,
},
"capability_value": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func datasourceCloudStackVPCOfferingRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
p := cs.VPC.NewListVPCOfferingsParams()
csVPCOfferings, err := cs.VPC.ListVPCOfferings(p)
if err != nil {
return fmt.Errorf("Failed to list VPC offerings: %s", err)
}
filters := d.Get("filter")
var vpcOfferings []*cloudstack.VPCOffering
for _, o := range csVPCOfferings.VPCOfferings {
match, err := applyVPCOfferingFilters(o, filters.(*schema.Set))
if err != nil {
return err
}
if match {
vpcOfferings = append(vpcOfferings, o)
}
}
if len(vpcOfferings) == 0 {
return fmt.Errorf("No VPC offering is matching with the specified regex")
}
//return the latest VPC offering from the list of filtered VPC offerings according
//to its creation date
vpcOffering, err := latestVPCOffering(vpcOfferings)
if err != nil {
return err
}
log.Printf("[DEBUG] Selected VPC offering: %s\n", vpcOffering.Displaytext)
fullVPCOffering, _, err := cs.VPC.GetVPCOfferingByID(vpcOffering.Id)
if err != nil {
return fmt.Errorf("Error retrieving full VPC offering details: %s", err)
}
return vpcOfferingDescriptionAttributes(d, fullVPCOffering)
}
func vpcOfferingDescriptionAttributes(d *schema.ResourceData, vpcOffering *cloudstack.VPCOffering) error {
d.SetId(vpcOffering.Id)
d.Set("name", vpcOffering.Name)
d.Set("display_text", vpcOffering.Displaytext)
d.Set("enable", vpcOffering.State == "Enabled")
d.Set("for_nsx", vpcOffering.Fornsx)
d.Set("internet_protocol", vpcOffering.Internetprotocol)
d.Set("network_mode", vpcOffering.Networkmode)
d.Set("routing_mode", vpcOffering.Routingmode)
d.Set("specify_as_number", vpcOffering.Specifyasnumber)
d.Set("is_default", vpcOffering.Isdefault)
if len(vpcOffering.Service) > 0 {
services := make([]string, len(vpcOffering.Service))
serviceProviders := make(map[string]string)
var serviceCapabilities []map[string]interface{}
for i, service := range vpcOffering.Service {
services[i] = service.Name
if len(service.Provider) > 0 {
serviceProviders[service.Name] = service.Provider[0].Name
}
for _, capability := range service.Capability {
serviceCapabilities = append(serviceCapabilities, map[string]interface{}{
"service": service.Name,
"capability_type": capability.Name,
"capability_value": capability.Value,
})
}
}
d.Set("supported_services", services)
d.Set("service_provider_list", serviceProviders)
d.Set("service_capability_list", serviceCapabilities)
}
return nil
}
func latestVPCOffering(vpcOfferings []*cloudstack.VPCOffering) (*cloudstack.VPCOffering, error) {
var latest time.Time
var vpcOffering *cloudstack.VPCOffering
for _, o := range vpcOfferings {
created, err := time.Parse("2006-01-02T15:04:05-0700", o.Created)
if err != nil {
return nil, fmt.Errorf("Failed to parse creation date of a VPC offering: %s", err)
}
if created.After(latest) {
latest = created
vpcOffering = o
}
}
return vpcOffering, nil
}
func applyVPCOfferingFilters(vpcOffering *cloudstack.VPCOffering, filters *schema.Set) (bool, error) {
var vpcOfferingJSON map[string]interface{}
k, _ := json.Marshal(vpcOffering)
err := json.Unmarshal(k, &vpcOfferingJSON)
if err != nil {
return false, err
}
for _, f := range filters.List() {
m := f.(map[string]interface{})
r, err := regexp.Compile(m["value"].(string))
if err != nil {
return false, fmt.Errorf("Invalid regex: %s", err)
}
updatedName := strings.ReplaceAll(m["name"].(string), "_", "")
vpcOfferingField, ok := vpcOfferingJSON[updatedName].(string)
if !ok {
continue
}
if !r.MatchString(vpcOfferingField) {
return false, nil
}
}
return true, nil
}