github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	// CACertificates contains an optional list of Certificate
    41  	// Authority certificates to be used to validate certificates
    42  	// of cloud infrastructure components
    43  	// The contents are Base64 encoded x.509 certs.
    44  	CACertificates []string
    45  }
    46  
    47  // Validate validates that the CloudSpec is well-formed. It does
    48  // not ensure that the cloud type and credentials are valid.
    49  func (cs CloudSpec) Validate() error {
    50  	if cs.Type == "" {
    51  		return errors.NotValidf("empty Type")
    52  	}
    53  	if !names.IsValidCloud(cs.Name) {
    54  		return errors.NotValidf("cloud name %q", cs.Name)
    55  	}
    56  	return nil
    57  }
    58  
    59  // MakeCloudSpec returns a CloudSpec from the given
    60  // Cloud, cloud and region names, and credential.
    61  func MakeCloudSpec(cloud jujucloud.Cloud, cloudRegionName string, credential *jujucloud.Credential) (CloudSpec, error) {
    62  	cloudSpec := CloudSpec{
    63  		Type:             cloud.Type,
    64  		Name:             cloud.Name,
    65  		Region:           cloudRegionName,
    66  		Endpoint:         cloud.Endpoint,
    67  		IdentityEndpoint: cloud.IdentityEndpoint,
    68  		StorageEndpoint:  cloud.StorageEndpoint,
    69  		CACertificates:   cloud.CACertificates,
    70  		Credential:       credential,
    71  	}
    72  	if cloudRegionName != "" {
    73  		cloudRegion, err := jujucloud.RegionByName(cloud.Regions, cloudRegionName)
    74  		if err != nil {
    75  			return CloudSpec{}, errors.Annotate(err, "getting cloud region definition")
    76  		}
    77  		cloudSpec.Endpoint = cloudRegion.Endpoint
    78  		cloudSpec.IdentityEndpoint = cloudRegion.IdentityEndpoint
    79  		cloudSpec.StorageEndpoint = cloudRegion.StorageEndpoint
    80  	}
    81  	return cloudSpec, nil
    82  }
    83  
    84  // RegionSpec contains the information needed to lookup specific region
    85  // configuration. This is for use in calling
    86  // state/modelconfig.(ComposeNewModelConfig) so there is no need to serialize
    87  // it.
    88  type RegionSpec struct {
    89  	// Cloud is the name of the cloud.
    90  	Cloud string
    91  
    92  	// Region is the name of the cloud region.
    93  	Region string
    94  }
    95  
    96  // NewRegionSpec returns a RegionSpec ensuring neither arg is empty.
    97  func NewRegionSpec(cloud, region string) (*RegionSpec, error) {
    98  	if cloud == "" || region == "" {
    99  		return nil, errors.New("cloud and region are required to be non empty strings")
   100  	}
   101  	return &RegionSpec{Cloud: cloud, Region: region}, nil
   102  }