//nolint:golint package dcxAutohost import ( "encoding/json" "fmt" "strings" ) type ( // DeviceSwitchVlans type is the main structure for the SwitchVlans function // call. DeviceSwitchVlans struct { DataCenter string `json:"data_center" bson:"data_center"` Device string `json:"device_number" bson:"device_number"` SwitchVlans []*switchVlan `json:"switch_vlans" bson:"switch_vlans"` } // SwitchVlanReq is the request type used to on the PutSwitchVlans method // which will associate the port+segment for a device SwitchVlanReq struct { Switch string `json:"switch" bson:"switch"` SwitchPort int `json:"switch_port" bson:"switch_port"` VlanNumber int `json:"vlan_number" bson:"vlan_number"` } // PortVlanData type is the structured type for the PortVlanData helper // function PortVlanData struct { Port int `json:"port_number" bson:"port_number"` PortType string `json:"port_type" bson:"port_type"` PortVlanData []*VlanData `json:"vlan_device_data" bson:"vlan_device_data"` } switchVlan struct { SwitchName string `json:"switch_name" bson:"switch_name"` SwitchPortData []*PortVlanData `json:"switch_port_data" bson:"switch_port_data"` } // VlanData type is a complex type containing a Vlans segment name along with the // actual configured Vlan number VlanData struct { Segment string `json:"segment_name" bson:"segment_name"` Vlan int `json:"vlan_number" bson:"vlan_number"` } ) // SwitchVlans method will take a device as an input and return a DeviceSwitchVlans type that will contain // switch vlan information of a given device. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/getSwitchVlans func (a *Client) SwitchVlans(device int) (*DeviceSwitchVlans, error) { body, err := a.get(fmt.Sprintf("/devices/%d/switch_vlans", device), nil) if err != nil { return nil, err } dsv := new(DeviceSwitchVlans) if err = json.Unmarshal(body, &dsv); err != nil { return nil, err } return &DeviceSwitchVlans{ DataCenter: dsv.DataCenter, Device: dsv.Device, SwitchVlans: dsv.SwitchVlans, }, nil } // PutSwitchVlans method will take a SwitchVlan request type and attempt to associate the // port+segment association on a device. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/putSwitchVlans func (a *Client) PutSwitchVlans(device int, req SwitchVlanReq) error { payload, err := json.Marshal(&req) if err != nil { return err } if _, err = a.put(fmt.Sprintf("/devices/%d/switch_vlans", device), payload); err != nil { return err } return nil } // PortsInUse is a helper method on the DeviceSwitchVlans receiver that will return a list of ports that // are in use based on the SwitchPortData type. func (d *DeviceSwitchVlans) PortsInUse() map[string][]*PortVlanData { inUse := make(map[string][]*PortVlanData) for _, switchVLANs := range d.SwitchVlans { for _, p := range switchVLANs.SwitchPortData { if len(p.PortVlanData) == 0 { continue } inUse[switchName(switchVLANs.SwitchName, d.DataCenter)] = append(inUse[switchName(switchVLANs.SwitchName, d.DataCenter)], p) } } return inUse } // Vlans is a helper method on the PortVlanData receiver that will return a list of // vlans for a given PortVlanData type. func (p *PortVlanData) Vlans() (vl []int) { for _, v := range p.PortVlanData { vl = append(vl, v.Vlan) } return } // switchName function is a small internal function to format a switch and DC into // the common Rackspace format. func switchName(sw, dc string) string { return strings.ToLower(fmt.Sprintf("%s.%s", sw, dc)) }