diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index 210627da7..c269310f1 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -10,6 +10,7 @@ import ( "time" "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs" @@ -103,6 +104,7 @@ func resourceComputeInstanceV2() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, + Computed: true, }, "network": &schema.Schema{ Type: schema.TypeList, @@ -571,6 +573,21 @@ func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) err return err } + // Build a custom struct for the availability zone extension + var serverWithAZ struct { + servers.Server + availabilityzones.ServerExt + } + + // Do another Get so the above work is not disturbed. + err = servers.Get(computeClient, d.Id()).ExtractInto(&serverWithAZ) + if err != nil { + return CheckDeleted(d, err, "server") + } + + // Set the availability zone + d.Set("availability_zone", serverWithAZ.AvailabilityZone) + return nil } diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go index 3665ff99d..d84c00ba8 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2_test.go @@ -29,6 +29,8 @@ func TestAccComputeV2Instance_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance), testAccCheckComputeV2InstanceMetadata(&instance, "foo", "bar"), + resource.TestCheckResourceAttr( + "openstack_compute_instance_v2.instance_1", "availability_zone", "nova"), ), }, }, diff --git a/vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones/results.go b/vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones/results.go new file mode 100644 index 000000000..96a6a50b3 --- /dev/null +++ b/vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones/results.go @@ -0,0 +1,12 @@ +package availabilityzones + +// ServerExt is an extension to the base Server object +type ServerExt struct { + // AvailabilityZone is the availabilty zone the server is in. + AvailabilityZone string `json:"OS-EXT-AZ:availability_zone"` +} + +// UnmarshalJSON to override default +func (r *ServerExt) UnmarshalJSON(b []byte) error { + return nil +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 7b4ee76c0..04590607e 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -1530,6 +1530,12 @@ "revision": "0f64da0e36de86a0ca1a8f2fc1b0570a0d3f7504", "revisionTime": "2017-03-10T01:59:53Z" }, + { + "checksumSHA1": "y49Ur726Juznj85+23ZgqMvehgg=", + "path": "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones", + "revision": "0f64da0e36de86a0ca1a8f2fc1b0570a0d3f7504", + "revisionTime": "2017-03-10T01:59:53Z" + }, { "checksumSHA1": "w2wHF5eEBE89ZYlkS9GAJsSIq9U=", "path": "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume",