forked from apache/cloudstack-terraform-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_cloudstack_instance.go
More file actions
208 lines (177 loc) · 5.15 KB
/
Copy pathdata_source_cloudstack_instance.go
File metadata and controls
208 lines (177 loc) · 5.15 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
//
// 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/helper/schema"
)
func dataSourceCloudstackInstance() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudstackInstanceRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
//Computed values
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
"account": {
Type: schema.TypeString,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"host_id": {
Type: schema.TypeString,
Computed: true,
},
"zone_id": {
Type: schema.TypeString,
Computed: true,
},
"created": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
"nic": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip_address": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
},
},
},
}
}
func dataSourceCloudstackInstanceRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Instance Data Source Read Started")
cs := meta.(*cloudstack.CloudStackClient)
p := cs.VirtualMachine.NewListVirtualMachinesParams()
csInstances, err := cs.VirtualMachine.ListVirtualMachines(p)
if err != nil {
return fmt.Errorf("Failed to list instances: %s", err)
}
filters := d.Get("filter")
nic := d.Get("nic").([]interface{})
var instances []*cloudstack.VirtualMachine
//the if-else block to check whether to filter the data source by an IP address
// or by any other exported attributes
if len(nic) != 0 {
ip_address := nic[0].(map[string]interface{})["ip_address"]
for _, i := range csInstances.VirtualMachines {
if ip_address == i.Nic[0].Ipaddress {
instances = append(instances, i)
}
}
} else {
for _, i := range csInstances.VirtualMachines {
match, err := applyInstanceFilters(i, filters.(*schema.Set))
if err != nil {
return err
}
if match {
instances = append(instances, i)
}
}
}
if len(instances) == 0 {
return fmt.Errorf("No instance is matching with the specified regex")
}
//return the latest instance from the list of filtered instances according
//to its creation date
instance, err := latestInstance(instances)
if err != nil {
return err
}
log.Printf("[DEBUG] Selected instances: %s\n", instance.Displayname)
return instanceDescriptionAttributes(d, instance)
}
func instanceDescriptionAttributes(d *schema.ResourceData, instance *cloudstack.VirtualMachine) error {
d.SetId(instance.Id)
d.Set("instance_id", instance.Id)
d.Set("account", instance.Account)
d.Set("created", instance.Created)
d.Set("display_name", instance.Displayname)
d.Set("state", instance.State)
d.Set("host_id", instance.Hostid)
d.Set("zone_id", instance.Zoneid)
d.Set("nic", []interface{}{map[string]string{"ip_address": instance.Nic[0].Ipaddress}})
tags := make(map[string]interface{})
for _, tag := range instance.Tags {
tags[tag.Key] = tag.Value
}
d.Set("tags", tags)
return nil
}
func latestInstance(instances []*cloudstack.VirtualMachine) (*cloudstack.VirtualMachine, error) {
var latest time.Time
var instance *cloudstack.VirtualMachine
for _, i := range instances {
created, err := time.Parse("2006-01-02T15:04:05-0700", i.Created)
if err != nil {
return nil, fmt.Errorf("Failed to parse creation date of an instance: %s", err)
}
if created.After(latest) {
latest = created
instance = i
}
}
return instance, nil
}
func applyInstanceFilters(instance *cloudstack.VirtualMachine, filters *schema.Set) (bool, error) {
var instanceJSON map[string]interface{}
i, _ := json.Marshal(instance)
err := json.Unmarshal(i, &instanceJSON)
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), "_", "")
instanceField := instanceJSON[updatedName].(string)
if !r.MatchString(instanceField) {
return false, nil
}
}
return true, nil
}