github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/ipaddress.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package description 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/schema" 9 ) 10 11 type ipaddresses struct { 12 Version int `yaml:"version"` 13 IPAddresses_ []*ipaddress `yaml:"ip-addresses"` 14 } 15 16 type ipaddress struct { 17 ProviderID_ string `yaml:"provider-id,omitempty"` 18 DeviceName_ string `yaml:"device-name"` 19 MachineID_ string `yaml:"machine-id"` 20 SubnetCIDR_ string `yaml:"subnet-cidr"` 21 ConfigMethod_ string `yaml:"config-method"` 22 Value_ string `yaml:"value"` 23 DNSServers_ []string `yaml:"dns-servers"` 24 DNSSearchDomains_ []string `yaml:"dns-search-domains"` 25 GatewayAddress_ string `yaml:"gateway-address"` 26 } 27 28 // ProviderID implements IPAddress. 29 func (i *ipaddress) ProviderID() string { 30 return i.ProviderID_ 31 } 32 33 // DeviceName implements IPAddress. 34 func (i *ipaddress) DeviceName() string { 35 return i.DeviceName_ 36 } 37 38 // MachineID implements IPAddress. 39 func (i *ipaddress) MachineID() string { 40 return i.MachineID_ 41 } 42 43 // SubnetCIDR implements IPAddress. 44 func (i *ipaddress) SubnetCIDR() string { 45 return i.SubnetCIDR_ 46 } 47 48 // ConfigMethod implements IPAddress. 49 func (i *ipaddress) ConfigMethod() string { 50 return i.ConfigMethod_ 51 } 52 53 // Value implements IPAddress. 54 func (i *ipaddress) Value() string { 55 return i.Value_ 56 } 57 58 // DNSServers implements IPAddress. 59 func (i *ipaddress) DNSServers() []string { 60 return i.DNSServers_ 61 } 62 63 // DNSSearchDomains implements IPAddress. 64 func (i *ipaddress) DNSSearchDomains() []string { 65 return i.DNSSearchDomains_ 66 } 67 68 // GatewayAddress implements IPAddress. 69 func (i *ipaddress) GatewayAddress() string { 70 return i.GatewayAddress_ 71 } 72 73 // IPAddressArgs is an argument struct used to create a 74 // new internal ipaddress type that supports the IPAddress interface. 75 type IPAddressArgs struct { 76 ProviderID string 77 DeviceName string 78 MachineID string 79 SubnetCIDR string 80 ConfigMethod string 81 Value string 82 DNSServers []string 83 DNSSearchDomains []string 84 GatewayAddress string 85 } 86 87 func newIPAddress(args IPAddressArgs) *ipaddress { 88 return &ipaddress{ 89 ProviderID_: args.ProviderID, 90 DeviceName_: args.DeviceName, 91 MachineID_: args.MachineID, 92 SubnetCIDR_: args.SubnetCIDR, 93 ConfigMethod_: args.ConfigMethod, 94 Value_: args.Value, 95 DNSServers_: args.DNSServers, 96 DNSSearchDomains_: args.DNSSearchDomains, 97 GatewayAddress_: args.GatewayAddress, 98 } 99 } 100 101 func importIPAddresses(source map[string]interface{}) ([]*ipaddress, error) { 102 checker := versionedChecker("ip-addresses") 103 coerced, err := checker.Coerce(source, nil) 104 if err != nil { 105 return nil, errors.Annotatef(err, "ip-addresses version schema check failed") 106 } 107 valid := coerced.(map[string]interface{}) 108 109 version := int(valid["version"].(int64)) 110 importFunc, ok := ipaddressDeserializationFuncs[version] 111 if !ok { 112 return nil, errors.NotValidf("version %d", version) 113 } 114 sourceList := valid["ip-addresses"].([]interface{}) 115 return importIPAddressList(sourceList, importFunc) 116 } 117 118 func importIPAddressList(sourceList []interface{}, importFunc ipaddressDeserializationFunc) ([]*ipaddress, error) { 119 result := make([]*ipaddress, 0, len(sourceList)) 120 for i, value := range sourceList { 121 source, ok := value.(map[string]interface{}) 122 if !ok { 123 return nil, errors.Errorf("unexpected value for ip-address %d, %T", i, value) 124 } 125 ipaddress, err := importFunc(source) 126 if err != nil { 127 return nil, errors.Annotatef(err, "ip-address %d", i) 128 } 129 result = append(result, ipaddress) 130 } 131 return result, nil 132 } 133 134 type ipaddressDeserializationFunc func(map[string]interface{}) (*ipaddress, error) 135 136 var ipaddressDeserializationFuncs = map[int]ipaddressDeserializationFunc{ 137 1: importIPAddressV1, 138 } 139 140 func importIPAddressV1(source map[string]interface{}) (*ipaddress, error) { 141 fields := schema.Fields{ 142 "provider-id": schema.String(), 143 "device-name": schema.String(), 144 "machine-id": schema.String(), 145 "subnet-cidr": schema.String(), 146 "config-method": schema.String(), 147 "value": schema.String(), 148 "dns-servers": schema.List(schema.String()), 149 "dns-search-domains": schema.List(schema.String()), 150 "gateway-address": schema.String(), 151 } 152 // Some values don't have to be there. 153 defaults := schema.Defaults{ 154 "provider-id": "", 155 } 156 checker := schema.FieldMap(fields, defaults) 157 158 coerced, err := checker.Coerce(source, nil) 159 if err != nil { 160 return nil, errors.Annotatef(err, "ip address v1 schema check failed") 161 } 162 valid := coerced.(map[string]interface{}) 163 dnsserversInterface := valid["dns-servers"].([]interface{}) 164 dnsservers := make([]string, len(dnsserversInterface)) 165 for i, d := range dnsserversInterface { 166 dnsservers[i] = d.(string) 167 } 168 dnssearchInterface := valid["dns-search-domains"].([]interface{}) 169 dnssearch := make([]string, len(dnssearchInterface)) 170 for i, d := range dnssearchInterface { 171 dnssearch[i] = d.(string) 172 } 173 return &ipaddress{ 174 ProviderID_: valid["provider-id"].(string), 175 DeviceName_: valid["device-name"].(string), 176 MachineID_: valid["machine-id"].(string), 177 SubnetCIDR_: valid["subnet-cidr"].(string), 178 ConfigMethod_: valid["config-method"].(string), 179 Value_: valid["value"].(string), 180 DNSServers_: dnsservers, 181 DNSSearchDomains_: dnssearch, 182 GatewayAddress_: valid["gateway-address"].(string), 183 }, nil 184 }