~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/ec2/config.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2011, 2012, 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package ec2
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        "github.com/juju/schema"
 
10
        "gopkg.in/juju/environschema.v1"
 
11
 
 
12
        "github.com/juju/juju/environs/config"
 
13
)
 
14
 
 
15
var configSchema = environschema.Fields{
 
16
        "vpc-id": {
 
17
                Description: "Use a specific AWS VPC ID (optional). When not specified, Juju requires a default VPC or EC2-Classic features to be available for the account/region.",
 
18
                Example:     "vpc-a1b2c3d4",
 
19
                Type:        environschema.Tstring,
 
20
                Group:       environschema.AccountGroup,
 
21
                Immutable:   true,
 
22
        },
 
23
        "vpc-id-force": {
 
24
                Description: "Force Juju to use the AWS VPC ID specified with vpc-id, when it fails the minimum validation criteria. Not accepted without vpc-id",
 
25
                Type:        environschema.Tbool,
 
26
                Group:       environschema.AccountGroup,
 
27
                Immutable:   true,
 
28
        },
 
29
}
 
30
 
 
31
var configFields = func() schema.Fields {
 
32
        fs, _, err := configSchema.ValidationSchema()
 
33
        if err != nil {
 
34
                panic(err)
 
35
        }
 
36
        return fs
 
37
}()
 
38
 
 
39
var configDefaults = schema.Defaults{
 
40
        "vpc-id":       "",
 
41
        "vpc-id-force": false,
 
42
}
 
43
 
 
44
type environConfig struct {
 
45
        *config.Config
 
46
        attrs map[string]interface{}
 
47
}
 
48
 
 
49
func (c *environConfig) vpcID() string {
 
50
        return c.attrs["vpc-id"].(string)
 
51
}
 
52
 
 
53
func (c *environConfig) forceVPCID() bool {
 
54
        return c.attrs["vpc-id-force"].(bool)
 
55
}
 
56
 
 
57
func (p environProvider) newConfig(cfg *config.Config) (*environConfig, error) {
 
58
        valid, err := p.Validate(cfg, nil)
 
59
        if err != nil {
 
60
                return nil, err
 
61
        }
 
62
        return &environConfig{valid, valid.UnknownAttrs()}, nil
 
63
}
 
64
 
 
65
// Schema returns the configuration schema for an environment.
 
66
func (environProvider) Schema() environschema.Fields {
 
67
        fields, err := config.Schema(configSchema)
 
68
        if err != nil {
 
69
                panic(err)
 
70
        }
 
71
        return fields
 
72
}
 
73
 
 
74
func validateConfig(cfg, old *config.Config) (*environConfig, error) {
 
75
        // Check for valid changes for the base config values.
 
76
        if err := config.Validate(cfg, old); err != nil {
 
77
                return nil, err
 
78
        }
 
79
        validated, err := cfg.ValidateUnknownAttrs(configFields, configDefaults)
 
80
        if err != nil {
 
81
                return nil, err
 
82
        }
 
83
        ecfg := &environConfig{cfg, validated}
 
84
 
 
85
        if vpcID := ecfg.vpcID(); isVPCIDSetButInvalid(vpcID) {
 
86
                return nil, fmt.Errorf("vpc-id: %q is not a valid AWS VPC ID", vpcID)
 
87
        } else if !isVPCIDSet(vpcID) && ecfg.forceVPCID() {
 
88
                return nil, fmt.Errorf("cannot use vpc-id-force without specifying vpc-id as well")
 
89
        }
 
90
 
 
91
        if old != nil {
 
92
                attrs := old.UnknownAttrs()
 
93
 
 
94
                if vpcID, _ := attrs["vpc-id"].(string); vpcID != ecfg.vpcID() {
 
95
                        return nil, fmt.Errorf("cannot change vpc-id from %q to %q", vpcID, ecfg.vpcID())
 
96
                }
 
97
 
 
98
                if forceVPCID, _ := attrs["vpc-id-force"].(bool); forceVPCID != ecfg.forceVPCID() {
 
99
                        return nil, fmt.Errorf("cannot change vpc-id-force from %v to %v", forceVPCID, ecfg.forceVPCID())
 
100
                }
 
101
        }
 
102
 
 
103
        // ssl-hostname-verification cannot be disabled
 
104
        if !ecfg.SSLHostnameVerification() {
 
105
                return nil, fmt.Errorf("disabling ssh-hostname-verification is not supported")
 
106
        }
 
107
        return ecfg, nil
 
108
}