// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package mydp import ( "encoding/json" "fmt" "net/url" "strconv" "github.com/kolo/xmlrpc" ) type ( // SwitchType struct contains the details returned from the SwitchList // call to return multiple switches along with their type. SwitchType struct { ID string `xmlrpc:"id"` SwitchType string `xmlrpc:"s_type"` } // Switch type is a specific device identifier used to provide more specific // information about a network switch type in admin.mydatapipe. // Using pointers to handle nullable strings in json response. Switch struct { AdaptSysid *string `xmlrpc:"AdaptSysid" json:"AdaptSysid"` AXDeviceID *string `xmlrpc:"AXDeviceID" json:"AXDeviceID"` Colo *string `xmlrpc:"-" json:"colo"` ContractNotes *string `xmlrpc:"ContractNotes" json:"ContractNotes"` CustomerID *string `xmlrpc:"CustomerID" json:"CustomerID"` DateCreated *string `xmlrpc:"-" json:"DateCreated"` DateLastModified *string `xmlrpc:"-" json:"DateLastModified"` DeviceID *string `xmlrpc:"DeviceID" json:"DeviceID"` DeviceName *string `xmlrpc:"Device_Name" json:"Device_Name"` DevicePrimaryIP *string `xmlrpc:"Device_PrimaryIP" json:"Device_PrimaryIP"` Disabled *string `xmlrpc:"-" json:"disabled"` EndSupportContract *string `xmlrpc:"EndSupportContract" json:"EndSupportContract"` ERPDeviceID *string `xmlrpc:"ERPDeviceID" json:"ERPDeviceID"` LocationTitle *string `xmlrpc:"Location_Title" json:"Location_Title"` NetworkZoneID *string `xmlrpc:"NetworkZoneID" json:"NetworkZoneID"` NetworkZoneTitle *string `xmlrpc:"NetworkZoneTitle" json:"NetworkZoneTitle"` RackID *string `xmlrpc:"RackID" json:"RackID"` RackTitle *string `xmlrpc:"RackTitle" json:"RackTitle"` RackU *string `xmlrpc:"RackU" json:"RackU"` RSCoreDeviceID *string `xmlrpc:"-" json:"RSCoreDeviceID"` RSPlatform *string `xmlrpc:"-" json:"rs_platform"` RSPlatformSku *string `xmlrpc:"-" json:"RSPlatformSku"` StartSupportContract *string `xmlrpc:"StartSupportContract" json:"StartSupportContract"` SwitchType *string `xmlrpc:"s_type" json:"s_type"` VLANGroup *string `xmlrpc:"vlan_group" json:"vlan_group"` } ) // GetSwitch method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.switch.Get func (c *Client) GetSwitch(switchID int) (*Switch, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/switch.php"), nil) if err != nil { return nil, err } var result Switch err = client.Call("mydatapipe.switch.Get", switchID, &result) return &result, err } // GetSwitchJSON uses the *mydp.Client to invoke a JSON call to the /json/Switch.php endpoint // requires *device ID* (not specific ID) // doc: https://one.rackspace.com/pages/viewpage.action?pageId=586744870#MyDatapipeAPI-GET/json/Firewall.php?DeviceID=DeviceID func (c *Client) GetSwitchJSON(deviceID int) (*Switch, error) { response := new(Switch) res, err := c.get("json/Switch.php", url.Values{"DeviceID": []string{strconv.Itoa(deviceID)}}) if err != nil { return response, err } if res != nil { defer func() { if defErr := res.Body.Close(); defErr != nil { err = fmt.Errorf("%s::%s", err, defErr) } }() } err = json.NewDecoder(res.Body).Decode(response) return response, err } // SwitchListDetails method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.switch.List func (c *Client) SwitchListDetails(offset int) ([]Switch, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/switch.php"), nil) if err != nil { return nil, err } var result []Switch err = client.Call("mydatapipe.switch.List", offset, &result) return result, err } // SwitchList method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.switch.SwitchList func (c *Client) SwitchList() ([]SwitchType, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/switch.php"), nil) if err != nil { return nil, err } var result []SwitchType err = client.Call("mydatapipe.switch.SwitchList", nil, &result) return result, err }