github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/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 "gopkg.in/juju/names.v2" 8 9 "github.com/juju/juju/apiserver/common" 10 "github.com/juju/juju/apiserver/params" 11 "github.com/juju/juju/environs" 12 ) 13 14 // CloudSpecAPI implements common methods for use by various 15 // facades for querying the cloud spec of models. 16 type CloudSpecAPI interface { 17 // CloudSpec returns the model's cloud spec. 18 CloudSpec(args params.Entities) (params.CloudSpecResults, error) 19 20 // GetCloudSpec constructs the CloudSpec for a validated and authorized model. 21 GetCloudSpec(tag names.ModelTag) params.CloudSpecResult 22 } 23 24 type cloudSpecAPI struct { 25 getCloudSpec func(names.ModelTag) (environs.CloudSpec, error) 26 getAuthFunc common.GetAuthFunc 27 } 28 29 // NewCloudSpec returns a new CloudSpecAPI. 30 func NewCloudSpec( 31 getCloudSpec func(names.ModelTag) (environs.CloudSpec, error), 32 getAuthFunc common.GetAuthFunc, 33 ) CloudSpecAPI { 34 return cloudSpecAPI{getCloudSpec, getAuthFunc} 35 } 36 37 // CloudSpec returns the model's cloud spec. 38 func (s cloudSpecAPI) CloudSpec(args params.Entities) (params.CloudSpecResults, error) { 39 authFunc, err := s.getAuthFunc() 40 if err != nil { 41 return params.CloudSpecResults{}, err 42 } 43 results := params.CloudSpecResults{ 44 Results: make([]params.CloudSpecResult, len(args.Entities)), 45 } 46 for i, arg := range args.Entities { 47 tag, err := names.ParseModelTag(arg.Tag) 48 if err != nil { 49 results.Results[i].Error = common.ServerError(err) 50 continue 51 } 52 if !authFunc(tag) { 53 results.Results[i].Error = common.ServerError(common.ErrPerm) 54 continue 55 } 56 results.Results[i] = s.GetCloudSpec(tag) 57 } 58 return results, nil 59 } 60 61 // GetCloudSpec constructs the CloudSpec for a validated and authorized model. 62 func (s cloudSpecAPI) GetCloudSpec(tag names.ModelTag) params.CloudSpecResult { 63 var result params.CloudSpecResult 64 spec, err := s.getCloudSpec(tag) 65 if err != nil { 66 result.Error = common.ServerError(err) 67 return result 68 } 69 var paramsCloudCredential *params.CloudCredential 70 if spec.Credential != nil && spec.Credential.AuthType() != "" { 71 paramsCloudCredential = ¶ms.CloudCredential{ 72 AuthType: string(spec.Credential.AuthType()), 73 Attributes: spec.Credential.Attributes(), 74 } 75 } 76 result.Result = ¶ms.CloudSpec{ 77 Type: spec.Type, 78 Name: spec.Name, 79 Region: spec.Region, 80 Endpoint: spec.Endpoint, 81 IdentityEndpoint: spec.IdentityEndpoint, 82 StorageEndpoint: spec.StorageEndpoint, 83 Credential: paramsCloudCredential, 84 CACertificates: spec.CACertificates, 85 } 86 return result 87 }