github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/linklayerdevice.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 linklayerdevices struct { 12 Version int `yaml:"version"` 13 LinkLayerDevices_ []*linklayerdevice `yaml:"link-layer-devices"` 14 } 15 16 type linklayerdevice struct { 17 Name_ string `yaml:"name"` 18 MTU_ uint `yaml:"mtu"` 19 ProviderID_ string `yaml:"provider-id,omitempty"` 20 MachineID_ string `yaml:"machine-id"` 21 Type_ string `yaml:"type"` 22 MACAddress_ string `yaml:"mac-address"` 23 IsAutoStart_ bool `yaml:"is-autostart"` 24 IsUp_ bool `yaml:"is-up"` 25 ParentName_ string `yaml:"parent-name"` 26 } 27 28 // ProviderID implements LinkLayerDevice. 29 func (i *linklayerdevice) ProviderID() string { 30 return i.ProviderID_ 31 } 32 33 // MachineID implements LinkLayerDevice. 34 func (i *linklayerdevice) MachineID() string { 35 return i.MachineID_ 36 } 37 38 // Name implements LinkLayerDevice. 39 func (i *linklayerdevice) Name() string { 40 return i.Name_ 41 } 42 43 // MTU implements LinkLayerDevice. 44 func (i *linklayerdevice) MTU() uint { 45 return i.MTU_ 46 } 47 48 // Type implements LinkLayerDevice. 49 func (i *linklayerdevice) Type() string { 50 return i.Type_ 51 } 52 53 // MACAddress implements LinkLayerDevice. 54 func (i *linklayerdevice) MACAddress() string { 55 return i.MACAddress_ 56 } 57 58 // IsAutoStart implements LinkLayerDevice. 59 func (i *linklayerdevice) IsAutoStart() bool { 60 return i.IsAutoStart_ 61 } 62 63 // IsUp implements LinkLayerDevice. 64 func (i *linklayerdevice) IsUp() bool { 65 return i.IsUp_ 66 } 67 68 // ParentName implements LinkLayerDevice. 69 func (i *linklayerdevice) ParentName() string { 70 return i.ParentName_ 71 } 72 73 // LinkLayerDeviceArgs is an argument struct used to create a 74 // new internal linklayerdevice type that supports the LinkLayerDevice interface. 75 type LinkLayerDeviceArgs struct { 76 Name string 77 MTU uint 78 ProviderID string 79 MachineID string 80 Type string 81 MACAddress string 82 IsAutoStart bool 83 IsUp bool 84 ParentName string 85 } 86 87 func newLinkLayerDevice(args LinkLayerDeviceArgs) *linklayerdevice { 88 return &linklayerdevice{ 89 ProviderID_: args.ProviderID, 90 MachineID_: args.MachineID, 91 Name_: args.Name, 92 MTU_: args.MTU, 93 Type_: args.Type, 94 MACAddress_: args.MACAddress, 95 IsAutoStart_: args.IsAutoStart, 96 IsUp_: args.IsUp, 97 ParentName_: args.ParentName, 98 } 99 } 100 101 func importLinkLayerDevices(source map[string]interface{}) ([]*linklayerdevice, error) { 102 checker := versionedChecker("link-layer-devices") 103 coerced, err := checker.Coerce(source, nil) 104 if err != nil { 105 return nil, errors.Annotatef(err, "link-layer-devices version schema check failed") 106 } 107 valid := coerced.(map[string]interface{}) 108 109 version := int(valid["version"].(int64)) 110 importFunc, ok := linklayerdeviceDeserializationFuncs[version] 111 if !ok { 112 return nil, errors.NotValidf("version %d", version) 113 } 114 sourceList := valid["link-layer-devices"].([]interface{}) 115 return importLinkLayerDeviceList(sourceList, importFunc) 116 } 117 118 func importLinkLayerDeviceList(sourceList []interface{}, importFunc linklayerdeviceDeserializationFunc) ([]*linklayerdevice, error) { 119 result := make([]*linklayerdevice, 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 link-layer-device %d, %T", i, value) 124 } 125 linklayerdevice, err := importFunc(source) 126 if err != nil { 127 return nil, errors.Annotatef(err, "link-layer-device %d", i) 128 } 129 result = append(result, linklayerdevice) 130 } 131 return result, nil 132 } 133 134 type linklayerdeviceDeserializationFunc func(map[string]interface{}) (*linklayerdevice, error) 135 136 var linklayerdeviceDeserializationFuncs = map[int]linklayerdeviceDeserializationFunc{ 137 1: importLinkLayerDeviceV1, 138 } 139 140 func importLinkLayerDeviceV1(source map[string]interface{}) (*linklayerdevice, error) { 141 fields := schema.Fields{ 142 "provider-id": schema.String(), 143 "machine-id": schema.String(), 144 "name": schema.String(), 145 "mtu": schema.Int(), 146 "type": schema.String(), 147 "mac-address": schema.String(), 148 "is-autostart": schema.Bool(), 149 "is-up": schema.Bool(), 150 "parent-name": 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, "linklayerdevice v1 schema check failed") 161 } 162 valid := coerced.(map[string]interface{}) 163 return &linklayerdevice{ 164 ProviderID_: valid["provider-id"].(string), 165 MachineID_: valid["machine-id"].(string), 166 Name_: valid["name"].(string), 167 MTU_: uint(valid["mtu"].(int64)), 168 Type_: valid["type"].(string), 169 MACAddress_: valid["mac-address"].(string), 170 IsAutoStart_: valid["is-autostart"].(bool), 171 IsUp_: valid["is-up"].(bool), 172 ParentName_: valid["parent-name"].(string), 173 }, nil 174 }