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

     1  // Copyright 2013 Joyent Inc.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package joyent
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/schema"
    10  
    11  	"github.com/juju/juju/environs/config"
    12  )
    13  
    14  var (
    15  	configFields          = schema.Fields{}
    16  	configDefaults        = schema.Defaults{}
    17  	requiredFields        = []string{}
    18  	configImmutableFields = []string{}
    19  )
    20  
    21  func validateConfig(cfg, old *config.Config) (*environConfig, error) {
    22  	// Check for valid changes for the base config values.
    23  	if err := config.Validate(cfg, old); err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	newAttrs, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	envConfig := &environConfig{cfg, newAttrs}
    32  	// If an old config was supplied, check any immutable fields have not changed.
    33  	if old != nil {
    34  		oldEnvConfig, err := validateConfig(old, nil)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  		for _, field := range configImmutableFields {
    39  			if oldEnvConfig.attrs[field] != envConfig.attrs[field] {
    40  				return nil, fmt.Errorf(
    41  					"%s: cannot change from %v to %v",
    42  					field, oldEnvConfig.attrs[field], envConfig.attrs[field],
    43  				)
    44  			}
    45  		}
    46  	}
    47  
    48  	// Check for missing fields.
    49  	for _, field := range requiredFields {
    50  		if nilOrEmptyString(envConfig.attrs[field]) {
    51  			return nil, fmt.Errorf("%s: must not be empty", field)
    52  		}
    53  	}
    54  	return envConfig, nil
    55  }
    56  
    57  type environConfig struct {
    58  	*config.Config
    59  	attrs map[string]interface{}
    60  }
    61  
    62  func (ecfg *environConfig) GetAttrs() map[string]interface{} {
    63  	return ecfg.attrs
    64  }
    65  
    66  func nilOrEmptyString(i interface{}) bool {
    67  	return i == nil || i == ""
    68  }