github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/caas/config.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caas
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/schema"
    11  	"gopkg.in/juju/environschema.v1"
    12  )
    13  
    14  const (
    15  	// OperatorStoragePoolName is the storage pool used to define
    16  	// storage for application operators.
    17  	OperatorStoragePoolName = "operator-storage"
    18  
    19  	// JujuExternalHostNameKey specifies the hostname of a CAAS application.
    20  	JujuExternalHostNameKey = "juju-external-hostname"
    21  
    22  	// JujuApplicationPath specifies the relative http path used to access a CAAS application.
    23  	JujuApplicationPath = "juju-application-path"
    24  
    25  	// JujuDefaultApplicationPath is the default value for juju-application-path.
    26  	JujuDefaultApplicationPath = "/"
    27  )
    28  
    29  var configFields = environschema.Fields{
    30  	JujuExternalHostNameKey: {
    31  		Description: "the external hostname of an exposed application",
    32  		Type:        environschema.Tstring,
    33  		Group:       environschema.EnvironGroup,
    34  	},
    35  	JujuApplicationPath: {
    36  		Description: "the relative http path used to access an application",
    37  		Type:        environschema.Tstring,
    38  		Group:       environschema.EnvironGroup,
    39  	},
    40  }
    41  
    42  // ConfigSchema returns the valid fields for a CAAS application config.
    43  func ConfigSchema(providerFields environschema.Fields) (environschema.Fields, error) {
    44  	fields, err := configSchema(providerFields)
    45  	if err != nil {
    46  		return nil, errors.Trace(err)
    47  	}
    48  	return fields, nil
    49  }
    50  
    51  func configSchema(extra environschema.Fields) (environschema.Fields, error) {
    52  	fields := make(environschema.Fields)
    53  	for name, field := range configFields {
    54  		fields[name] = field
    55  	}
    56  	for name, field := range extra {
    57  		if _, ok := configFields[name]; ok {
    58  			return nil, errors.Errorf("config field %q clashes with common config", name)
    59  		}
    60  		fields[name] = field
    61  	}
    62  	return fields, nil
    63  }
    64  
    65  // ConfigDefaults returns the default values for a CAAS application config.
    66  func ConfigDefaults(providerDefaults schema.Defaults) schema.Defaults {
    67  	defaults := schema.Defaults{
    68  		JujuApplicationPath: JujuDefaultApplicationPath,
    69  	}
    70  	for key, value := range providerDefaults {
    71  		if value == schema.Omit {
    72  			continue
    73  		}
    74  		defaults[key] = value
    75  	}
    76  	return defaults
    77  }
    78  
    79  // OperatorStorageClassLabels returns possible labels used to search for a
    80  // storage class used to provision operator storage.
    81  func OperatorStorageClassLabels(appName, model string) []string {
    82  	volStorageLabel := fmt.Sprintf("%s-operator-storage", appName)
    83  	return []string{volStorageLabel, model, "default"}
    84  }
    85  
    86  // UnitStorageClassLabels returns possible labels used to search for a
    87  // storage class used to provision unit storage.
    88  func UnitStorageClassLabels(appName, model string) []string {
    89  	volStorageLabel := fmt.Sprintf("%s-unit-storage", appName)
    90  	return []string{volStorageLabel, model, "default"}
    91  }