package goctk import ( "encoding/json" "errors" "strconv" ) type ( networkSwitch struct { Name string `json:"name"` ID string `json:"id"` Address string `json:"address"` DataCenter string `json:"datacenter"` Ports NetSwitchAttribQuery `json:"ports"` } NetSwitchAttribQuery struct { Attribute string `json:"attribute"` SubAttributes NetSwitchPortQuery `json:"subattributes"` } NetSwitchPortQuery struct { Number string `json:"computer"` Name string `json:"computer_name"` StatusCode string `json:"computer_status"` PlatformType string `json:"platform_type"` PlatformName string `json:"platform_name"` AccountNumber string `json:"account_number"` AccountName string `json:"account_name"` Port string `json:"port"` } NetworkSwitchQuery struct { Count int `json:"count"` Limit int `json:"limit"` Result []*NetworkSwitch `json:"result"` Offset int `json:"offset"` Class string `json:"class"` } NetworkSwitch struct { Name string `json:"name"` ID int `json:"id"` DataCenter string `json:"datacenter"` Address string `json:"address"` PortData *NetworkSwitchAttribute `json:"ports"` } NetworkSwitchAttribute struct { Attribute string `json:"attribute"` Subattributes []*NetworkSwitchPort `json:"subattributes"` } NetworkSwitchPort struct { Number *int `json:"computer"` Name string `json:"computer_name"` StatusCode int `json:"computer_status"` PlatformType string `json:"platform_type"` PlatformName string `json:"platform_name"` AccountNumber string `json:"account_number"` AccountName string `json:"account_name"` Port int `json:"port"` } ) var ( NETWORKSWITCH = networkSwitch{ Name: "number", ID: "id", Address: "address.address", DataCenter: "zone.datacenter.symbol", Ports: NetSwitchAttribQuery{ Attribute: "ports", SubAttributes: NetSwitchPortQuery{ Number: "computer.number", Name: "computer.name", StatusCode: "computer.status.number", PlatformType: "computer.platform_type", PlatformName: "computer.platform_name", AccountNumber: "computer.account.number", AccountName: "computer.account.name", Port: "number", }, }, } ) func (c *Client) NetworkSwitchQuery(switchID int) (*NetworkSwitch, error) { js, err := json.Marshal(ctkQuery{ Class: "Network.Switch", LoadArg: switchID, Attributes: NETWORKSWITCH, }) if err != nil { return nil, err } res, err := c.Query(js) if err != nil { return nil, err } var r []*NetworkSwitchQuery if err = json.Unmarshal(res, &r); err != nil { return nil, err } for _, i := range r { if len(i.Result) == 1 { return i.Result[0], nil } } return nil, errors.New("unable to retrieve switch data") } func (n *NetworkSwitch) DeviceByAccount(act int) (p []*NetworkSwitchPort) { if n.PortData == nil { return } account := strconv.Itoa(act) for _, port := range n.PortData.Subattributes { if port.AccountNumber == account { p = append(p, port) } } return }