github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/cloudspec.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package environs
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	names "gopkg.in/juju/names.v2"
     9  
    10  	jujucloud "github.com/juju/juju/cloud"
    11  )
    12  
    13  // CloudSpec describes a specific cloud configuration, for the purpose
    14  // of opening an Environ to manage the cloud resources.
    15  type CloudSpec struct {
    16  	// Type is the type of cloud, eg aws, openstack etc.
    17  	Type string
    18  
    19  	// Name is the name of the cloud.
    20  	Name string
    21  
    22  	// Region is the name of the cloud region, if the cloud supports
    23  	// regions.
    24  	Region string
    25  
    26  	// Endpoint is the endpoint for the cloud (region).
    27  	Endpoint string
    28  
    29  	// IdentityEndpoint is the identity endpoint for the cloud (region).
    30  	IdentityEndpoint string
    31  
    32  	// StorageEndpoint is the storage endpoint for the cloud (region).
    33  	StorageEndpoint string
    34  
    35  	// Credential is the cloud credential to use to authenticate
    36  	// with the cloud, or nil if the cloud does not require any
    37  	// credentials.
    38  	Credential *jujucloud.Credential
    39  }
    40  
    41  // Validate validates that the CloudSpec is well-formed. It does
    42  // not ensure that the cloud type and credentials are valid.
    43  func (cs CloudSpec) Validate() error {
    44  	if cs.Type == "" {
    45  		return errors.NotValidf("empty Type")
    46  	}
    47  	if !names.IsValidCloud(cs.Name) {
    48  		return errors.NotValidf("cloud name %q", cs.Name)
    49  	}
    50  	return nil
    51  }
    52  
    53  // MakeCloudSpec returns a CloudSpec from the given
    54  // Cloud, cloud and region names, and credential.
    55  func MakeCloudSpec(cloud jujucloud.Cloud, cloudName, cloudRegionName string, credential *jujucloud.Credential) (CloudSpec, error) {
    56  	cloudSpec := CloudSpec{
    57  		Type:             cloud.Type,
    58  		Name:             cloudName,
    59  		Region:           cloudRegionName,
    60  		Endpoint:         cloud.Endpoint,
    61  		IdentityEndpoint: cloud.IdentityEndpoint,
    62  		StorageEndpoint:  cloud.StorageEndpoint,
    63  		Credential:       credential,
    64  	}
    65  	if cloudRegionName != "" {
    66  		cloudRegion, err := jujucloud.RegionByName(cloud.Regions, cloudRegionName)
    67  		if err != nil {
    68  			return CloudSpec{}, errors.Annotate(err, "getting cloud region definition")
    69  		}
    70  		cloudSpec.Endpoint = cloudRegion.Endpoint
    71  		cloudSpec.IdentityEndpoint = cloudRegion.IdentityEndpoint
    72  		cloudSpec.StorageEndpoint = cloudRegion.StorageEndpoint
    73  	}
    74  	return cloudSpec, nil
    75  }
    76  
    77  // RegionSpec contains the information needed to lookup specific region
    78  // configuration. This is for use in calling
    79  // state/modelconfig.(ComposeNewModelConfig) so there is no need to serialize
    80  // it.
    81  type RegionSpec struct {
    82  	// Cloud is the name of the cloud.
    83  	Cloud string
    84  
    85  	// Region is the name of the cloud region.
    86  	Region string
    87  }
    88  
    89  // NewRegionSpec returns a RegionSpec ensuring neither arg is empty.
    90  func NewRegionSpec(cloud, region string) (*RegionSpec, error) {
    91  	if cloud == "" || region == "" {
    92  		return nil, errors.New("cloud and region are required to be non empty strings")
    93  	}
    94  	return &RegionSpec{Cloud: cloud, Region: region}, nil
    95  }