github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/common/cloudspec/cloudspec.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cloudspec
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	names "gopkg.in/juju/names.v2"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/cloud"
    13  	"github.com/juju/juju/environs"
    14  )
    15  
    16  // CloudSpecAPI provides common client-side API functions
    17  // to call into apiserver/common/cloudspec.CloudSpec.
    18  type CloudSpecAPI struct {
    19  	facade   base.FacadeCaller
    20  	modelTag names.ModelTag
    21  }
    22  
    23  // NewCloudSpecAPI creates a CloudSpecAPI using the provided
    24  // FacadeCaller.
    25  func NewCloudSpecAPI(facade base.FacadeCaller, modelTag names.ModelTag) *CloudSpecAPI {
    26  	return &CloudSpecAPI{facade, modelTag}
    27  }
    28  
    29  // CloudSpec returns the cloud specification for the model associated
    30  // with the API facade.
    31  func (api *CloudSpecAPI) CloudSpec() (environs.CloudSpec, error) {
    32  	var results params.CloudSpecResults
    33  	args := params.Entities{Entities: []params.Entity{{api.modelTag.String()}}}
    34  	err := api.facade.FacadeCall("CloudSpec", args, &results)
    35  	if err != nil {
    36  		return environs.CloudSpec{}, err
    37  	}
    38  	if n := len(results.Results); n != 1 {
    39  		return environs.CloudSpec{}, errors.Errorf("expected 1 result, got %d", n)
    40  	}
    41  	result := results.Results[0]
    42  	if result.Error != nil {
    43  		return environs.CloudSpec{}, errors.Annotate(result.Error, "API request failed")
    44  	}
    45  	return api.MakeCloudSpec(result.Result)
    46  }
    47  
    48  // MakeCloudSpec creates an environs.CloudSpec from a params.CloudSpec
    49  // that has been returned from the apiserver.
    50  func (api *CloudSpecAPI) MakeCloudSpec(pSpec *params.CloudSpec) (environs.CloudSpec, error) {
    51  	if pSpec == nil {
    52  		return environs.CloudSpec{}, errors.NotValidf("nil value")
    53  	}
    54  	var credential *cloud.Credential
    55  	if pSpec.Credential != nil {
    56  		credentialValue := cloud.NewCredential(
    57  			cloud.AuthType(pSpec.Credential.AuthType),
    58  			pSpec.Credential.Attributes,
    59  		)
    60  		credential = &credentialValue
    61  	}
    62  	spec := environs.CloudSpec{
    63  		Type:             pSpec.Type,
    64  		Name:             pSpec.Name,
    65  		Region:           pSpec.Region,
    66  		Endpoint:         pSpec.Endpoint,
    67  		IdentityEndpoint: pSpec.IdentityEndpoint,
    68  		StorageEndpoint:  pSpec.StorageEndpoint,
    69  		CACertificates:   pSpec.CACertificates,
    70  		Credential:       credential,
    71  	}
    72  	if err := spec.Validate(); err != nil {
    73  		return environs.CloudSpec{}, errors.Annotate(err, "validating CloudSpec")
    74  	}
    75  	return spec, nil
    76  }