// 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 ( // Device is a representation of the generic device details in admin.mydatapipe with // an ID that is unique across all specific device types. Device struct { AdaptSysid string `xmlrpc:"AdaptSysid" json:"-"` AXDeviceID string `xmlrpc:"AXDeviceID" json:"-"` CustomerID string `xmlrpc:"CustomerID" json:"CustomerID"` CustomerName string `xmlrpc:"CustomerName" json:"-"` DatacenterID string `xmlrpc:"DatacenterID" json:"-"` DatacenterTitle string `xmlrpc:"DatacenterTitle" json:"-"` DateCreated string `xmlrpc:"DateCreated" json:"DateCreated"` DateLastModified string `xmlrpc:"DateLastModified" json:"DateLastModified"` DeviceID string `xmlrpc:"DeviceID" json:"DeviceID"` Disabled string `xmlrpc:"Disabled" json:"Disabled"` Groups []DeviceGroup `xmlrpc:"Groups" json:"-"` LocationID string `xmlrpc:"LocationID" json:"LocationID"` LocationTitle string `xmlrpc:"LocationTitle" json:"-"` Name string `xmlrpc:"Name" json:"Device_Name"` PrimaryIP string `xmlrpc:"PrimaryIP" json:"Device_PrimaryIP"` RackID string `xmlrpc:"RackID" json:"RackID"` RackTitle string `xmlrpc:"RackTitle" json:"RackTitle"` RackU string `xmlrpc:"-" json:"RackU"` RSCoreCustomerID string `xmlrpc:"-" json:"RSCoreCustomerID"` RSCoreDeviceID string `xmlrpc:"RSCoreDeviceID" json:"RSCoreDeviceID"` RSPlatform string `xmlrpc:"-" json:"rs_platform"` SalesEngineerName string `xmlrpc:"SalesEngineerName" json:"-"` SpecificDeviceID string `xmlrpc:"SpecificDeviceID" json:"SpecificDeviceID"` SpecificDeviceType string `xmlrpc:"SpecificDeviceType" json:"SpecificDeviceType"` WhoCreated string `xmlrpc:"WhoCreated" json:"-"` WhoModified string `xmlrpc:"WhoModified" json:"-"` } // DeviceGroup type defines the groups this device is a member of DeviceGroup struct { DeviceID string `xmlrpc:"DeviceID"` Name string `xmlrpc:"Name"` Description string `xmlrpc:"Description"` GroupID string `xmlrpc:"GroupID"` } // DeviceList is the response type return from the DevicesByCustomer call that can return multiple // Devices in its response. DeviceList struct { RowCount string `xmlrpc:"RowCount"` Devices []Device `xmlrpc:"Devices"` } // OS type defines the details about a specific OS as they are defined in admin.mydatapipe. OS struct { ID string `xmlrpc:"os_id"` Name string `xmlrpc:"os_os"` Platform string `xmlrpc:"platform"` PlatformID string `xmlrpc:"os_platform"` } ) // DeviceByDeviceID method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.device.DeviceByDeviceID func (c *Client) DeviceByDeviceID(deviceID int) (*Device, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/device.php"), nil) if err != nil { return nil, err } var result Device err = client.Call("mydatapipe.device.DeviceByDeviceID", deviceID, &result) return &result, err } // DevicesByCustomer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.device.DevicesByCustomer func (c *Client) DevicesByCustomer(customerID, resultsPer, resultsOffset int, orderBy, filter string) (*DeviceList, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/device.php"), nil) if err != nil { return nil, err } var result DeviceList args := []interface{}{ customerID, resultsPer, resultsOffset, orderBy, filter, } err = client.Call("mydatapipe.device.DevicesByCustomer", args, &result) return &result, err } // DeviceIDByDevice method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.device.DeviceIDByDevice func (c *Client) DeviceIDByDevice(deviceType string, deviceID int) (int, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/device.php"), nil) if err != nil { return 0, err } var result int err = client.Call("mydatapipe.device.DeviceIDByDevice", []interface{}{deviceType, deviceID}, &result) return result, err } // GetDeviceJSON uses the *mydp.Client to invoke a JSON call to the /json/Device.php endpoint // requires *device ID* (not specific ID/firewall ID) // doc: https://one.rackspace.com/pages/viewpage.action?pageId=586744870#MyDatapipeAPI-GET/json/Device.php?DeviceID=DeviceID func (c *Client) GetDeviceJSON(deviceID int) (*Device, error) { response := new(Device) res, err := c.get("json/Device.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 } // GetDeviceTypes method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.device.GetDeviceTypes func (c *Client) GetDeviceTypes() ([]string, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/device.php"), nil) if err != nil { return []string{}, err } var result []string err = client.Call("mydatapipe.device.GetDeviceTypes", nil, &result) return result, err } // OSList method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.os.OSList func (c *Client) OSList() ([]OS, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/os.php"), nil) if err != nil { return nil, err } var result []OS err = client.Call("mydatapipe.os.OSList", nil, &result) return result, err }