// 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 ( "bytes" "encoding/json" "fmt" "net/url" "strconv" "github.com/kolo/xmlrpc" ) type ( // Server type is a specific device identifier used to provide more specific // information about the generic server type in admin.mydatapipe. // Using pointers to handle nullable strings in json response. Server struct { ServerID *string `xmlrpc:"ServerID" json:"id"` AdaptSysid *string `xmlrpc:"AdaptSysid" json:"-"` AssetID *string `xmlrpc:"AssetID" json:"asset_id"` Colo *string `xmlrpc:"-" json:"colo"` ContractNotes *string `xmlrpc:"-" json:"ContractNotes"` CustomerID *string `xmlrpc:"-" json:"CustomerID"` DataPipe *string `xmlrpc:"DataPipe" json:"datapipe"` DateCreated *string `xmlrpc:"-" json:"DateCreated"` DateLastModified *string `xmlrpc:"-" json:"DateLastModified"` DeviceID *string `xmlrpc:"-" json:"DeviceID"` DeviceName *string `xmlrpc:"-" json:"Device_Name"` DevicePrimaryIP *string `xmlrpc:"-" json:"Device_PrimaryIP"` Disabled *string `xmlrpc:"-" json:"disabled"` Email *string `xmlrpc:"Email" json:"Email"` EndSupportContract *string `xmlrpc:"-" json:"EndSupportContract"` FirewallID *string `xmlrpc:"FirewallID" json:"firewall"` FirewallIP *string `xmlrpc:"FirewallIP" json:"firewall_ip"` GrandSeap *string `xmlrpc:"GrandSeap" json:"grand_seap"` Hardware *string `xmlrpc:"Hardware" json:"hardwaretype"` HighProfile *string `xmlrpc:"HighProfile" json:"highprofile"` Location *string `xmlrpc:"-" json:"Location"` LocationID *string `xmlrpc:"-" json:"LocationID"` Managed *string `xmlrpc:"Managed" json:"managed"` ManagedID *string `xmlrpc:"ManagedID" json:"-"` OperatingSystem *string `xmlrpc:"OperatingSystem" json:"os_name"` OperatingSystemID *string `xmlrpc:"OperatingSystemID" json:"type"` ProjectManager *string `xmlrpc:"-" json:"ProjectManager"` RackID *string `xmlrpc:"-" json:"RackID"` RackTitle *string `xmlrpc:"-" json:"RackTitle"` RackU *string `xmlrpc:"-" json:"RackU"` RSCoreCustomerID *string `xmlrpc:"-" json:"RSCoreCustomerID"` RSCoreDeviceID *string `xmlrpc:"-" json:"RSCoreDeviceID"` RSPlatform *string `xmlrpc:"-" json:"rs_platform"` RSPlatformSku *string `xmlrpc:"-" json:"RSPlatformSku"` SalesRep *string `xmlrpc:"SalesRep" json:"sales_rep"` StartSupportContract *string `xmlrpc:"-" json:"StartSupportContract"` Temporary *string `xmlrpc:"Temporary" json:"temp_server"` } // ServerList is the response type received from the servers calls to // denote a list of Server types and a row count of items returned. ServerList struct { RowCount string `xmlrpc:"RowCount"` Servers []Server `xmlrpc:"Servers"` } // ServerEdit is the request payload sent to the EditServer function that will // update attributes on a myadmin device ServerEdit struct { DeviceID int `json:"DeviceID"` RSPlatform string `json:"rs_platform,omitempty"` RSCoreDeviceID string `json:"RSCoreDeviceID,omitempty"` RSPlatformSku string `json:"RSPlatformSku,omitempty"` RackID string `json:"RackID,omitempty"` RackU string `json:"RackU,omitempty"` } ) // GetServer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.server.Get func (c *Client) GetServer(serverID int) (*ServerList, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/server.php"), nil) if err != nil { return nil, err } var result ServerList err = client.Call("mydatapipe.server.Get", serverID, &result) return &result, err } // GetServerJSON uses the *mydp.Client to invoke a JSON call to the /json/Server.php endpoint // requires *device ID* (not specific ID) // doc: https://one.rackspace.com/pages/viewpage.action?pageId=586744870#MyDatapipeAPI-GET/json/Server.php?DeviceID=DeviceID func (c *Client) GetServerJSON(deviceID int) (*Server, error) { response := new(Server) res, err := c.get("json/Server.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 } // ServersGreaterThanServerID method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.server.ServersGreaterThanServerID func (c *Client) ServersGreaterThanServerID(serverID int) (*ServerList, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/server.php"), nil) if err != nil { return nil, err } var result ServerList err = client.Call("mydatapipe.server.ServersGreaterThanServerID", serverID, &result) return &result, err } // EditServer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.server.editServer func (c *Client) EditServer(serverID int, update ServerEdit) (interface{}, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/server.php"), nil) if err != nil { return nil, err } var result interface{} err = client.Call("mydatapipe.server.editServer", []interface{}{serverID, update}, &result) return &result, err } // ModifyServer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.server.editServer func (c *Client) ModifyServer(update ServerEdit) (Device, error) { response := new(Device) body, err := json.Marshal(update) if err != nil { return *response, err } res, err := c.post("json/Device.php", nil, bytes.NewReader(body)) 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 }