github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/storage/config.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/schema"
     9  )
    10  
    11  const (
    12  	// ConfigStorageDir is the path to the directory which a
    13  	// machine-scoped storage source may use to contain storage
    14  	// artifacts. This should not be used for environment-wide
    15  	// storage sources, as the contents are bound to the
    16  	// lifetime of the machine.
    17  	//
    18  	// ConfigStorageDir is set by the storage provisioner, so
    19  	// should not be relied upon until a storage source is
    20  	// constructed.
    21  	ConfigStorageDir = "storage-dir"
    22  )
    23  
    24  // Config defines the configuration for a storage source.
    25  type Config struct {
    26  	name     string
    27  	provider ProviderType
    28  	attrs    map[string]interface{}
    29  }
    30  
    31  var fields = schema.Fields{}
    32  
    33  var configChecker = schema.FieldMap(
    34  	fields,
    35  	schema.Defaults{},
    36  )
    37  
    38  // NewConfig creates a new Config for instantiating a storage source.
    39  func NewConfig(name string, provider ProviderType, attrs map[string]interface{}) (*Config, error) {
    40  	_, err := configChecker.Coerce(attrs, nil)
    41  	if err != nil {
    42  		return nil, errors.Annotate(err, "validating common storage config")
    43  	}
    44  	return &Config{
    45  		name:     name,
    46  		provider: provider,
    47  		attrs:    attrs,
    48  	}, nil
    49  }
    50  
    51  // Name returns the name of a storage source. This is not necessarily unique,
    52  // and should only be used for informational purposes.
    53  func (c *Config) Name() string {
    54  	return c.name
    55  }
    56  
    57  // Provider returns the name of a storage provider.
    58  func (c *Config) Provider() ProviderType {
    59  	return c.provider
    60  }
    61  
    62  // Attrs returns the configuration attributes for a storage source.
    63  func (c *Config) Attrs() map[string]interface{} {
    64  	if c.attrs == nil {
    65  		return nil
    66  	}
    67  	attrs := make(map[string]interface{})
    68  	for k, v := range c.attrs {
    69  		attrs[k] = v
    70  	}
    71  	return attrs
    72  }
    73  
    74  // ValueString returns the named config attribute as a string.
    75  func (c *Config) ValueString(name string) (string, bool) {
    76  	v, ok := c.attrs[name].(string)
    77  	return v, ok
    78  }