github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/utils/instance.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package utils
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/instance"
    10  	"github.com/juju/juju/provider/common"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  var getEnviron = GetEnviron
    15  
    16  // AvailabilityZone returns the availability zone associated with
    17  // an instance ID.
    18  func AvailabilityZone(st *state.State, instID instance.Id) (string, error) {
    19  	// Get the provider.
    20  	env, err := getEnviron(st)
    21  	if err != nil {
    22  		return "", errors.Trace(err)
    23  	}
    24  	zenv, ok := env.(common.ZonedEnviron)
    25  	if !ok {
    26  		return "", errors.NotSupportedf(`zones for provider "%T"`, env)
    27  	}
    28  
    29  	// Request the zone.
    30  	zones, err := zenv.InstanceAvailabilityZoneNames([]instance.Id{instID})
    31  	if err != nil {
    32  		return "", errors.Trace(err)
    33  	}
    34  	if len(zones) != 1 {
    35  		return "", errors.Errorf("received invalid zones: expected 1, got %d", len(zones))
    36  	}
    37  
    38  	return zones[0], nil
    39  }