Skip to content

Commit

Permalink
cloud: unexport the Clouds and Credentials types
Browse files Browse the repository at this point in the history
These types are used only for un/marshalling, and do not deserve
to be part of the external API.
  • Loading branch information
axw committed Feb 9, 2016
1 parent 4f4786c commit a080b6b
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 210 deletions.
19 changes: 10 additions & 9 deletions cloud/clouds.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ const (
EmptyAuthType AuthType = "empty"
)

// Clouds is a struct containing cloud definitions.
type Clouds struct {
// clouds is a struct containing cloud definitions, used for marshalling
// and unmarshalling.
type clouds struct {
// Clouds is a map of cloud definitions, keyed on cloud name.
Clouds map[string]Cloud `yaml:"clouds"`
}
Expand Down Expand Up @@ -118,27 +119,27 @@ func PublicCloudMetadata(searchPath ...string) (result map[string]Cloud, fallbac
if err != nil {
return nil, false, errors.Trace(err)
}
return clouds.Clouds, false, err
return clouds, false, err
}
clouds, err := ParseCloudMetadata([]byte(fallbackPublicCloudInfo))
return clouds.Clouds, true, err
return clouds, true, err
}

// ParseCloudMetadata parses the given yaml bytes into Clouds metadata.
func ParseCloudMetadata(data []byte) (*Clouds, error) {
var metadata Clouds
func ParseCloudMetadata(data []byte) (map[string]Cloud, error) {
var metadata clouds
err := yaml.Unmarshal(data, &metadata)
if err != nil {
return nil, errors.Annotate(err, "cannot unmarshal yaml cloud metadata")
}
metadata.denormaliseMetadata()
return &metadata, nil
return metadata.Clouds, nil
}

// To keep the metadata concise, attributes on the metadata struct which have the same value for each
// item may be moved up to a higher level in the tree. denormaliseMetadata descends the tree
// and fills in any missing attributes with values from a higher level.
func (metadata *Clouds) denormaliseMetadata() {
func (metadata *clouds) denormaliseMetadata() {
for _, cloud := range metadata.Clouds {
for name, region := range cloud.Regions {
r := region
Expand All @@ -162,7 +163,7 @@ func RegisterStructTags(vals ...interface{}) {
}

func init() {
RegisterStructTags(Clouds{}, Cloud{}, Region{})
RegisterStructTags(clouds{}, Cloud{}, Region{})
}

func mkTags(vals ...interface{}) map[reflect.Type]map[string]int {
Expand Down
10 changes: 5 additions & 5 deletions cloud/clouds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ var publicCloudNames = []string{
"aws", "aws-china", "aws-gov", "google", "azure", "azure-china", "rackspace", "joyent", "cloudsigma",
}

func parsePublicClouds(c *gc.C) *cloud.Clouds {
func parsePublicClouds(c *gc.C) map[string]cloud.Cloud {
clouds, err := cloud.ParseCloudMetadata([]byte(cloud.FallbackPublicCloudInfo))
c.Assert(err, jc.ErrorIsNil)
c.Assert(clouds.Clouds, gc.HasLen, len(publicCloudNames))
c.Assert(clouds, gc.HasLen, len(publicCloudNames))
return clouds
}

func (s *cloudSuite) TestParseClouds(c *gc.C) {
clouds := parsePublicClouds(c)
var cloudNames []string
for name, _ := range clouds.Clouds {
for name, _ := range clouds {
cloudNames = append(cloudNames, name)
}
c.Assert(cloudNames, jc.SameContents, publicCloudNames)
}

func (s *cloudSuite) TestParseCloudsEndpointDenormalisation(c *gc.C) {
clouds := parsePublicClouds(c)
rackspace := clouds.Clouds["rackspace"]
rackspace := clouds["rackspace"]
c.Assert(rackspace.Type, gc.Equals, "openstack")
c.Assert(rackspace.Endpoint, gc.Equals, "https://identity.api.rackspacecloud.com/v2.0")
var regionNames []string
Expand All @@ -57,7 +57,7 @@ func (s *cloudSuite) TestParseCloudsEndpointDenormalisation(c *gc.C) {

func (s *cloudSuite) TestParseCloudsAuthTypes(c *gc.C) {
clouds := parsePublicClouds(c)
rackspace := clouds.Clouds["rackspace"]
rackspace := clouds["rackspace"]
c.Assert(rackspace.AuthTypes, jc.SameContents, []cloud.AuthType{"access-key", "userpass"})
}

Expand Down
24 changes: 17 additions & 7 deletions cloud/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
"github.com/juju/juju/juju/osenv"
)

// Credentials is a struct containing cloud credential information.
type Credentials struct {
// credentials is a struct containing cloud credential information,
// used marshalling and unmarshalling.
type credentials struct {
// Credentials is a map of cloud credentials, keyed on cloud name.
Credentials map[string]CloudCredential `yaml:"credentials"`
}
Expand Down Expand Up @@ -212,7 +213,7 @@ func CredentialByName(
if err != nil {
return nil, "", "", errors.Annotate(err, "parsing credentials")
}
cloudCredentials, ok := credentials.Credentials[cloudName]
cloudCredentials, ok := credentials[cloudName]
if !ok {
return nil, "", "", errors.NotFoundf("credentials for cloud %q", cloudName)
}
Expand All @@ -238,25 +239,34 @@ func JujuCredentials() string {

// ParseCredentials parses the given yaml bytes into Credentials, but does
// not validate the credential attributes.
func ParseCredentials(data []byte) (*Credentials, error) {
func ParseCredentials(data []byte) (map[string]CloudCredential, error) {
var credentialsYAML struct {
Credentials map[string]interface{} `yaml:"credentials"`
}
err := yaml.Unmarshal(data, &credentialsYAML)
if err != nil {
return nil, errors.Annotate(err, "cannot unmarshal yaml credentials")
}
credentials := Credentials{make(map[string]CloudCredential)}
credentials := make(map[string]CloudCredential)
for cloud, v := range credentialsYAML.Credentials {
v, err := cloudCredentialChecker{}.Coerce(
v, []string{"credentials." + cloud},
)
if err != nil {
return nil, errors.Trace(err)
}
credentials.Credentials[cloud] = v.(CloudCredential)
credentials[cloud] = v.(CloudCredential)
}
return &credentials, nil
return credentials, nil
}

// MarshalCredentials marshals the given credentials to YAML
func MarshalCredentials(credentialsMap map[string]CloudCredential) ([]byte, error) {
data, err := yaml.Marshal(credentials{credentialsMap})
if err != nil {
return nil, errors.Annotate(err, "cannot marshal credentials")
}
return data, nil
}

func copyStringMap(in map[string]string) map[string]string {
Expand Down
Loading

0 comments on commit a080b6b

Please sign in to comment.