|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package cloudstack |
| 21 | + |
| 22 | +import ( |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "log" |
| 26 | + "regexp" |
| 27 | + "strings" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/apache/cloudstack-go/v2/cloudstack" |
| 31 | + "github.com/hashicorp/terraform/helper/schema" |
| 32 | +) |
| 33 | + |
| 34 | +func dataSourceCloudstackInstance() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Read: dataSourceCloudstackInstanceRead, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "filter": dataSourceFiltersSchema(), |
| 39 | + |
| 40 | + //Computed values |
| 41 | + "instance_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + |
| 46 | + "account": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + |
| 51 | + "display_name": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + |
| 56 | + "state": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + |
| 61 | + "host_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + |
| 66 | + "zone_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + |
| 71 | + "created": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + }, |
| 75 | + |
| 76 | + "tags": tagsSchema(), |
| 77 | + |
| 78 | + "nic": { |
| 79 | + Type: schema.TypeList, |
| 80 | + Computed: true, |
| 81 | + Optional: true, |
| 82 | + Elem: &schema.Resource{ |
| 83 | + Schema: map[string]*schema.Schema{ |
| 84 | + "ip_address": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Optional: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func dataSourceCloudstackInstanceRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + log.Printf("Instance Data Source Read Started") |
| 98 | + |
| 99 | + cs := meta.(*cloudstack.CloudStackClient) |
| 100 | + p := cs.VirtualMachine.NewListVirtualMachinesParams() |
| 101 | + csInstances, err := cs.VirtualMachine.ListVirtualMachines(p) |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("Failed to list instances: %s", err) |
| 105 | + } |
| 106 | + |
| 107 | + filters := d.Get("filter") |
| 108 | + nic := d.Get("nic").([]interface{}) |
| 109 | + var instances []*cloudstack.VirtualMachine |
| 110 | + |
| 111 | + //the if-else block to check whether to filter the data source by an IP address |
| 112 | + // or by any other exported attributes |
| 113 | + if len(nic) != 0 { |
| 114 | + ip_address := nic[0].(map[string]interface{})["ip_address"] |
| 115 | + for _, i := range csInstances.VirtualMachines { |
| 116 | + if ip_address == i.Nic[0].Ipaddress { |
| 117 | + instances = append(instances, i) |
| 118 | + } |
| 119 | + } |
| 120 | + } else { |
| 121 | + for _, i := range csInstances.VirtualMachines { |
| 122 | + match, err := applyInstanceFilters(i, filters.(*schema.Set)) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + if match { |
| 128 | + instances = append(instances, i) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if len(instances) == 0 { |
| 134 | + return fmt.Errorf("No instance is matching with the specified regex") |
| 135 | + } |
| 136 | + //return the latest instance from the list of filtered instances according |
| 137 | + //to its creation date |
| 138 | + instance, err := latestInstance(instances) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + log.Printf("[DEBUG] Selected instances: %s\n", instance.Displayname) |
| 143 | + |
| 144 | + return instanceDescriptionAttributes(d, instance) |
| 145 | +} |
| 146 | + |
| 147 | +func instanceDescriptionAttributes(d *schema.ResourceData, instance *cloudstack.VirtualMachine) error { |
| 148 | + d.SetId(instance.Id) |
| 149 | + d.Set("instance_id", instance.Id) |
| 150 | + d.Set("account", instance.Account) |
| 151 | + d.Set("created", instance.Created) |
| 152 | + d.Set("display_name", instance.Displayname) |
| 153 | + d.Set("state", instance.State) |
| 154 | + d.Set("host_id", instance.Hostid) |
| 155 | + d.Set("zone_id", instance.Zoneid) |
| 156 | + d.Set("nic", []interface{}{map[string]string{"ip_address": instance.Nic[0].Ipaddress}}) |
| 157 | + |
| 158 | + tags := make(map[string]interface{}) |
| 159 | + for _, tag := range instance.Tags { |
| 160 | + tags[tag.Key] = tag.Value |
| 161 | + } |
| 162 | + d.Set("tags", tags) |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func latestInstance(instances []*cloudstack.VirtualMachine) (*cloudstack.VirtualMachine, error) { |
| 168 | + var latest time.Time |
| 169 | + var instance *cloudstack.VirtualMachine |
| 170 | + |
| 171 | + for _, i := range instances { |
| 172 | + created, err := time.Parse("2006-01-02T15:04:05-0700", i.Created) |
| 173 | + if err != nil { |
| 174 | + return nil, fmt.Errorf("Failed to parse creation date of an instance: %s", err) |
| 175 | + } |
| 176 | + |
| 177 | + if created.After(latest) { |
| 178 | + latest = created |
| 179 | + instance = i |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return instance, nil |
| 184 | +} |
| 185 | + |
| 186 | +func applyInstanceFilters(instance *cloudstack.VirtualMachine, filters *schema.Set) (bool, error) { |
| 187 | + var instanceJSON map[string]interface{} |
| 188 | + i, _ := json.Marshal(instance) |
| 189 | + err := json.Unmarshal(i, &instanceJSON) |
| 190 | + if err != nil { |
| 191 | + return false, err |
| 192 | + } |
| 193 | + |
| 194 | + for _, f := range filters.List() { |
| 195 | + m := f.(map[string]interface{}) |
| 196 | + r, err := regexp.Compile(m["value"].(string)) |
| 197 | + if err != nil { |
| 198 | + return false, fmt.Errorf("Invalid regex: %s", err) |
| 199 | + } |
| 200 | + updatedName := strings.ReplaceAll(m["name"].(string), "_", "") |
| 201 | + instanceField := instanceJSON[updatedName].(string) |
| 202 | + if !r.MatchString(instanceField) { |
| 203 | + return false, nil |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + return true, nil |
| 208 | +} |
0 commit comments