package dcxSwitchapi import ( "encoding/json" "errors" "fmt" ) type ( // InterfaceSettings is the exported struct that allows indirect access via Set() // methods; the Set methods are intended to help enforce legitimate input values InterfaceSettings struct { settings interfaceSettings } // interfaceSettings is the actual payload used to modify interface settings; // the struct is unexported so that values are (ideally) enforced via Set() methods; // fields are exported so json marshaling is supported // // Docs: https://github.rackspace.com/dcx/fire_engine/wiki/SwitchResource interfaceSettings struct { Admin *string `json:"admin,omitempty"` CDP *string `json:"cdp,omitempty"` Duplex *string `json:"duplex,omitempty"` LLDP *string `json:"lldp,omitempty"` Trunking struct { Enabled *bool `json:"enabled,omitempty"` NativeVlan *int `json:"native_vlan,omitempty"` // VlansAllowed will allow only the specified vlans VlansAllowed []int `json:"vlans_allowed,omitempty"` // VlansEnabled is additive with the VLANs already allowed on the interface VlansEnabled []int `json:"vlans_enabled,omitempty"` } `json:"trunking,omitempty"` Speed *interface{} `json:"speed,omitempty"` VlanID *int `json:"vlan_id,omitempty"` } ) var ( settingDisabled = "disabled" settingDuplexAuto = "auto" settingDuplexFull = "full" settingEnabled = "enabled" settingSpeed100 interface{} = 100 settingSpeed1000 interface{} = 1000 settingSpeedAuto interface{} = "auto" ) // SetAccessVlan will set the access vlan if a valid vlan value is given func (i *InterfaceSettings) SetAccessVlan(vlan int) { if vlan < 1 || vlan > 4094 { return } i.settings.VlanID = &vlan } // SetAdmin sets Admin to "enabled" for true, "disabled" for false func (i *InterfaceSettings) SetAdmin(enabled bool) { i.settings.Admin = &settingDisabled if enabled { i.settings.Admin = &settingEnabled } } // SetCDP sets CDP to "enabled" for true, "disabled" for false func (i *InterfaceSettings) SetCDP(enabled bool) { i.settings.CDP = &settingDisabled if enabled { i.settings.CDP = &settingEnabled } } // SetDuplexAuto sets Duplex to "auto" func (i *InterfaceSettings) SetDuplexAuto() { i.settings.Duplex = &settingDuplexAuto } // SetDuplexFull sets Duplex to "full" func (i *InterfaceSettings) SetDuplexFull() { i.settings.Duplex = &settingDuplexFull } // SetLLDP sets LLDP to "enabled" for true, "disabled" for false func (i *InterfaceSettings) SetLLDP(enabled bool) { i.settings.LLDP = &settingDisabled if enabled { i.settings.LLDP = &settingEnabled } } // SetNativeVLAN will set the native vlan if a valid vlan value is given func (i *InterfaceSettings) SetNativeVlan(vlan int) { if vlan < 1 || vlan > 4094 { return } i.settings.Trunking.NativeVlan = &vlan } // SetSpeed100 sets Speed to 100 func (i *InterfaceSettings) SetSpeed100() { i.settings.Speed = &settingSpeed100 } // SetSpeed1000 sets Speed to 1000 func (i *InterfaceSettings) SetSpeed1000() { i.settings.Speed = &settingSpeed1000 } // SetSpeedAuto sets Speed to "auto" func (i *InterfaceSettings) SetSpeedAuto() { i.settings.Speed = &settingSpeedAuto } // SetTrunkingEnabled will set the trunking.enabled boolean value func (i *InterfaceSettings) SetTrunkingEnabled(enabled bool) { i.settings.Trunking.Enabled = &enabled } func (i *InterfaceSettings) SetTrunkAllowedVlans(vlansAllowed []int) { i.settings.Trunking.VlansAllowed = vlansAllowed } func (i *InterfaceSettings) SetTrunkEnabledVlans(vlansEnabled []int) { i.settings.Trunking.VlansEnabled = vlansEnabled } // isNil will return true if all fields are nil // TODO: find a pretty way to do this, maybe with 'reflect' pkg func (i *InterfaceSettings) isNil() bool { if i.settings.Admin != nil { return false } if i.settings.CDP != nil { return false } if i.settings.Duplex != nil { return false } if i.settings.LLDP != nil { return false } if i.settings.Trunking.Enabled != nil { return false } if i.settings.Trunking.NativeVlan != nil { return false } if len(i.settings.Trunking.VlansAllowed) > 0 { return false } if len(i.settings.Trunking.VlansEnabled) > 0 { return false } if i.settings.Speed != nil { return false } if i.settings.VlanID != nil { return false } return true } func (c *Client) ModifyInterface(switchName string, interfaceID int, settings *InterfaceSettings) error { if settings.isNil() { return errors.New("cannot send empty settings. use Set..() methods to alter settings") } payload, err := json.Marshal(settings.settings) if err != nil { return err } _, err = c.put(fmt.Sprintf("%s/interfaces/%d", switchName, interfaceID), payload) return err }