|
| 1 | +package openstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones" |
| 9 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/tags" |
| 10 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors" |
| 11 | + "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 13 | +) |
| 14 | + |
| 15 | +func dataSourceComputeInstanceV2() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceComputeInstanceV2Read, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "region": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Computed: true, |
| 23 | + }, |
| 24 | + "id": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + }, |
| 28 | + "name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + "image_id": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "flavor_id": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "flavor_name": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "user_data": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + Computed: true, |
| 48 | + // just stash the hash for state & diff comparisons |
| 49 | + }, |
| 50 | + "security_groups": { |
| 51 | + Type: schema.TypeSet, |
| 52 | + Computed: true, |
| 53 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 54 | + Set: schema.HashString, |
| 55 | + }, |
| 56 | + "availability_zone": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "network": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Optional: true, |
| 63 | + Computed: true, |
| 64 | + Elem: &schema.Resource{ |
| 65 | + Schema: map[string]*schema.Schema{ |
| 66 | + "uuid": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "name": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "port": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "fixed_ip_v4": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "fixed_ip_v6": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "mac": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + "access_ip_v4": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + }, |
| 97 | + "access_ip_v6": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + "key_pair": { |
| 102 | + Type: schema.TypeString, |
| 103 | + Computed: true, |
| 104 | + }, |
| 105 | + "metadata": { |
| 106 | + Type: schema.TypeMap, |
| 107 | + Computed: true, |
| 108 | + }, |
| 109 | + "tags": { |
| 110 | + Type: schema.TypeSet, |
| 111 | + Computed: true, |
| 112 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func dataSourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) error { |
| 119 | + config := meta.(*Config) |
| 120 | + log.Print("[DEBUG] Creating compute client") |
| 121 | + computeClient, err := config.ComputeV2Client(GetRegion(d, config)) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("Error creating OpenStack compute client: %s", err) |
| 124 | + } |
| 125 | + |
| 126 | + id := d.Get("id").(string) |
| 127 | + log.Printf("[DEBUG] Attempting to retrieve server %s", id) |
| 128 | + server, err := servers.Get(computeClient, id).Extract() |
| 129 | + if err != nil { |
| 130 | + return CheckDeleted(d, err, "server") |
| 131 | + } |
| 132 | + |
| 133 | + log.Printf("[DEBUG] Retrieved Server %s: %+v", id, server) |
| 134 | + |
| 135 | + d.SetId(server.ID) |
| 136 | + |
| 137 | + d.Set("name", server.Name) |
| 138 | + d.Set("image_id", server.Image["ID"]) |
| 139 | + |
| 140 | + // Get the instance network and address information |
| 141 | + networks, err := flattenInstanceNetworks(d, meta) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + // Determine the best IPv4 and IPv6 addresses to access the instance with |
| 147 | + hostv4, hostv6 := getInstanceAccessAddresses(d, networks) |
| 148 | + |
| 149 | + // AccessIPv4/v6 isn't standard in OpenStack, but there have been reports |
| 150 | + // of them being used in some environments. |
| 151 | + if server.AccessIPv4 != "" && hostv4 == "" { |
| 152 | + hostv4 = server.AccessIPv4 |
| 153 | + } |
| 154 | + |
| 155 | + if server.AccessIPv6 != "" && hostv6 == "" { |
| 156 | + hostv6 = server.AccessIPv6 |
| 157 | + } |
| 158 | + |
| 159 | + log.Printf("[DEBUG] Setting networks: %+v", networks) |
| 160 | + |
| 161 | + d.Set("network", networks) |
| 162 | + d.Set("access_ip_v4", hostv4) |
| 163 | + d.Set("access_ip_v6", hostv6) |
| 164 | + |
| 165 | + d.Set("metadata", server.Metadata) |
| 166 | + |
| 167 | + secGrpNames := []string{} |
| 168 | + for _, sg := range server.SecurityGroups { |
| 169 | + secGrpNames = append(secGrpNames, sg["name"].(string)) |
| 170 | + } |
| 171 | + |
| 172 | + log.Printf("[DEBUG] Setting security groups: %+v", secGrpNames) |
| 173 | + |
| 174 | + d.Set("security_groups", secGrpNames) |
| 175 | + |
| 176 | + flavorId, ok := server.Flavor["id"].(string) |
| 177 | + if !ok { |
| 178 | + return fmt.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor) |
| 179 | + } |
| 180 | + d.Set("flavor_id", flavorId) |
| 181 | + |
| 182 | + d.Set("key_pair", server.KeyName) |
| 183 | + flavor, err := flavors.Get(computeClient, flavorId).Extract() |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + d.Set("flavor_name", flavor.Name) |
| 188 | + |
| 189 | + // Set the instance's image information appropriately |
| 190 | + if err := setImageInformation(computeClient, server, d); err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + // Build a custom struct for the availability zone extension |
| 195 | + var serverWithAZ struct { |
| 196 | + servers.Server |
| 197 | + availabilityzones.ServerAvailabilityZoneExt |
| 198 | + } |
| 199 | + |
| 200 | + // Do another Get so the above work is not disturbed. |
| 201 | + err = servers.Get(computeClient, d.Id()).ExtractInto(&serverWithAZ) |
| 202 | + if err != nil { |
| 203 | + return CheckDeleted(d, err, "server") |
| 204 | + } |
| 205 | + // Set the availability zone |
| 206 | + d.Set("availability_zone", serverWithAZ.AvailabilityZone) |
| 207 | + |
| 208 | + // Set the region |
| 209 | + d.Set("region", GetRegion(d, config)) |
| 210 | + |
| 211 | + // Set the current power_state |
| 212 | + currentStatus := strings.ToLower(server.Status) |
| 213 | + switch currentStatus { |
| 214 | + case "active", "shutoff", "error", "migrating", "shelved_offloaded", "shelved": |
| 215 | + d.Set("power_state", currentStatus) |
| 216 | + default: |
| 217 | + return fmt.Errorf("Invalid power_state for instance %s: %s", d.Id(), server.Status) |
| 218 | + } |
| 219 | + |
| 220 | + // Populate tags. |
| 221 | + computeClient.Microversion = computeV2TagsExtensionMicroversion |
| 222 | + instanceTags, err := tags.List(computeClient, server.ID).Extract() |
| 223 | + if err != nil { |
| 224 | + log.Printf("[DEBUG] Unable to get tags for openstack_compute_instance_v2: %s", err) |
| 225 | + } else { |
| 226 | + d.Set("tags", instanceTags) |
| 227 | + } |
| 228 | + |
| 229 | + return nil |
| 230 | +} |
0 commit comments