github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/cloud/personalclouds.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // Package cloud provides functionality to parse information
     5  // describing clouds, including regions, supported auth types etc.
     6  
     7  package cloud
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  
    13  	"github.com/juju/errors"
    14  
    15  	"github.com/juju/juju/juju/osenv"
    16  )
    17  
    18  // JujuPersonalCloudsPath is the location where personal cloud information is
    19  // expected to be found. Requires JUJU_HOME to be set.
    20  func JujuPersonalCloudsPath() string {
    21  	return osenv.JujuXDGDataHomePath("clouds.yaml")
    22  }
    23  
    24  // PersonalCloudMetadata loads any personal cloud metadata defined
    25  // in the Juju Home directory. If not cloud metadata is found,
    26  // that is not an error; nil is returned.
    27  func PersonalCloudMetadata() (map[string]Cloud, error) {
    28  	clouds, err := ParseCloudMetadataFile(JujuPersonalCloudsPath())
    29  	if err != nil && os.IsNotExist(err) {
    30  		return nil, nil
    31  	}
    32  	return clouds, err
    33  }
    34  
    35  // ParseCloudMetadataFile loads any cloud metadata defined
    36  // in the specified file.
    37  func ParseCloudMetadataFile(file string) (map[string]Cloud, error) {
    38  	data, err := ioutil.ReadFile(file)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	clouds, err := ParseCloudMetadata(data)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return clouds, err
    47  }
    48  
    49  // WritePersonalCloudMetadata marshals to YAMl and writes the cloud metadata
    50  // to the personal cloud file.
    51  func WritePersonalCloudMetadata(cloudsMap map[string]Cloud) error {
    52  	data, err := marshalCloudMetadata(cloudsMap)
    53  	if err != nil {
    54  		return errors.Trace(err)
    55  	}
    56  	return ioutil.WriteFile(JujuPersonalCloudsPath(), data, os.FileMode(0600))
    57  }