launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/provider/azure/config.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package azure
     5  
     6  import (
     7  	"fmt"
     8  	"io/ioutil"
     9  
    10  	"launchpad.net/juju-core/environs/config"
    11  	"launchpad.net/juju-core/schema"
    12  )
    13  
    14  var configFields = schema.Fields{
    15  	"location":                    schema.String(),
    16  	"management-subscription-id":  schema.String(),
    17  	"management-certificate-path": schema.String(),
    18  	"management-certificate":      schema.String(),
    19  	"storage-account-name":        schema.String(),
    20  	"force-image-name":            schema.String(),
    21  }
    22  var configDefaults = schema.Defaults{
    23  	"location":                    "",
    24  	"management-certificate":      "",
    25  	"management-certificate-path": "",
    26  	"force-image-name":            "",
    27  }
    28  
    29  type azureEnvironConfig struct {
    30  	*config.Config
    31  	attrs map[string]interface{}
    32  }
    33  
    34  func (cfg *azureEnvironConfig) location() string {
    35  	return cfg.attrs["location"].(string)
    36  }
    37  
    38  func (cfg *azureEnvironConfig) managementSubscriptionId() string {
    39  	return cfg.attrs["management-subscription-id"].(string)
    40  }
    41  
    42  func (cfg *azureEnvironConfig) managementCertificate() string {
    43  	return cfg.attrs["management-certificate"].(string)
    44  }
    45  
    46  func (cfg *azureEnvironConfig) storageAccountName() string {
    47  	return cfg.attrs["storage-account-name"].(string)
    48  }
    49  
    50  func (cfg *azureEnvironConfig) forceImageName() string {
    51  	return cfg.attrs["force-image-name"].(string)
    52  }
    53  
    54  func (prov azureEnvironProvider) newConfig(cfg *config.Config) (*azureEnvironConfig, error) {
    55  	validCfg, err := prov.Validate(cfg, nil)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	result := new(azureEnvironConfig)
    60  	result.Config = validCfg
    61  	result.attrs = validCfg.UnknownAttrs()
    62  	return result, nil
    63  }
    64  
    65  // Validate ensures that config is a valid configuration for this
    66  // provider like specified in the EnvironProvider interface.
    67  func (prov azureEnvironProvider) Validate(cfg, oldCfg *config.Config) (*config.Config, error) {
    68  	// Validate base configuration change before validating Azure specifics.
    69  	err := config.Validate(cfg, oldCfg)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	envCfg := new(azureEnvironConfig)
    79  	envCfg.Config = cfg
    80  	envCfg.attrs = validated
    81  
    82  	cert := envCfg.managementCertificate()
    83  	if cert == "" {
    84  		certPath := envCfg.attrs["management-certificate-path"].(string)
    85  		pemData, err := ioutil.ReadFile(certPath)
    86  		if err != nil {
    87  			return nil, fmt.Errorf("invalid management-certificate-path: %s", err)
    88  		}
    89  		envCfg.attrs["management-certificate"] = string(pemData)
    90  	}
    91  	delete(envCfg.attrs, "management-certificate-path")
    92  	if envCfg.location() == "" {
    93  		return nil, fmt.Errorf("environment has no location; you need to set one.  E.g. 'West US'")
    94  	}
    95  	return cfg.Apply(envCfg.attrs)
    96  }
    97  
    98  const boilerplateYAML = `
    99  # https://juju.ubuntu.com/docs/config-azure.html
   100  azure:
   101      type: azure
   102  
   103      # location specifies the place where instances will be started, for
   104      # example: West US, North Europe.
   105      location: West US
   106      
   107      # The following attributes specify Windows Azure Management information.
   108      # See  http://msdn.microsoft.com/en-us/library/windowsazure
   109      # for details.
   110      management-subscription-id: <00000000-0000-0000-0000-000000000000>
   111      management-certificate-path: /home/me/azure.pem
   112  
   113      # storage-account-name holds Windows Azure Storage info.
   114      storage-account-name: abcdefghijkl
   115  
   116      # force-image-name overrides the OS image selection to use
   117      # a fixed image for all deployments. Most useful for developers.
   118      # force-image-name: b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-13_10-amd64-server-DEVELOPMENT-20130713-Juju_ALPHA-en-us-30GB
   119  
   120      # image-stream chooses a simplestreams stream to select OS images from,
   121      # for example daily or released images (or any other stream available on simplestreams).
   122      # image-stream: "released"
   123  `
   124  
   125  func (prov azureEnvironProvider) BoilerplateConfig() string {
   126  	return boilerplateYAML
   127  }
   128  
   129  // SecretAttrs is specified in the EnvironProvider interface.
   130  func (prov azureEnvironProvider) SecretAttrs(cfg *config.Config) (map[string]string, error) {
   131  	secretAttrs := make(map[string]string)
   132  	azureCfg, err := prov.newConfig(cfg)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  	secretAttrs["management-certificate"] = azureCfg.managementCertificate()
   137  	return secretAttrs, nil
   138  }