// 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" "strings" "github.com/kolo/xmlrpc" ) type ( // Customer type defines the elements returned from the admin.mydatapipe API // for a given customer. Customer struct { AdaptState string `xmlrpc:"AdaptState"` BandwidthType string `xmlrpc:"95p"` Bandwidth string `xmlrpc:"bandwidth"` CustomerID string `xmlrpc:"CustomerID"` CustomerName string `xmlrpc:"customer_name"` DomainType string `xmlrpc:"domain_type"` LastSNImport string `xmlrpc:"last_sn_import"` MigratedToSN string `xmlrpc:"migrated_to_sn"` MonthlyPowerLimit string `xmlrpc:"monthly_power_limit"` MonthlyBackupLimit string `xmlrpc:"monthly_backup_limit"` OffsiteBackups string `xmlrpc:"offsite_backups"` OldBackupBilling string `xmlrpc:"old_backup_billing"` PIN string `xmlrpc:"pin"` ProjectManagerID string `xmlrpc:"ProjectManagerID"` ProjectManager string `xmlrpc:"ProjectManager"` RSCoreCustomerID string `xmlrpc:"RSCoreCustomerID"` TicketSLATime string `xmlrpc:"ticket_sla_time"` SalesEngineerID string `xmlrpc:"SalesEngineerID"` SalesEngineer string `xmlrpc:"SalesEngineer"` SLAEmail string `xmlrpc:"sla_email"` SNMPCommunity string `xmlrpc:"SNMPCommunity"` TicketInstructionsLinkName string `xmlrpc:"ticket_instructions_link_name"` TicketInstructionsLinkURL string `xmlrpc:"ticket_instructions_link_url"` TicketSubject string `xmlrpc:"ticket_subject"` } // CustomerJSON is similar to the Customer structure, however, this is the response returned for a // customer from the JSON API implemented in mydatapipe. CustomerJSON struct { TicketSLATime interface{} `json:"ticket_sla_time"` SLAEmail interface{} `json:"sla_email"` CustomerName string `json:"customer_name"` CustomerID string `json:"CustomerID"` Bandwidth string `json:"bandwidth"` Nine5P string `json:"95p"` TicketSubject string `json:"ticket_subject"` TicketInstructionsLinkURL interface{} `json:"ticket_instructions_link_url"` TicketInstructionsLinkName interface{} `json:"ticket_instructions_link_name"` MonthlyPowerLimit string `json:"monthly_power_limit"` MonthlyBackupLimit interface{} `json:"monthly_backup_limit"` OldBackupBilling string `json:"old_backup_billing"` OffsiteBackups string `json:"offsite_backups"` MigratedToSn string `json:"migrated_to_sn"` DomainType string `json:"domain_type"` LastSnImport string `json:"last_sn_import"` AdaptState string `json:"AdaptState"` SalesEngineer interface{} `json:"SalesEngineer"` SalesEngineerID string `json:"SalesEngineerID"` ProjectManager interface{} `json:"ProjectManager"` ProjectManagerID string `json:"ProjectManagerID"` RSCoreCustomerID string `json:"RSCoreCustomerID"` } // NonCoreCustomers is a listing of all customers that do not have a correlated // CORE customer ID NonCoreCustomers struct { Customers []struct { CustomerID string `json:"CustomerID"` CustomerName string `json:"customer_name"` AccountID string `json:"AccountID"` RSCoreCustomerID string `json:"RSCoreCustomerID"` } `json:"rows"` Rowcount int `json:"rowcount"` } ) // GetCustomer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.customers.get func (c *Client) GetCustomer(customerID int) (*Customer, error) { client, err := xmlrpc.NewClient(c.url("xmlrpc/customers.php"), nil) if err != nil { return nil, err } var result Customer err = client.Call("mydatapipe.customers.get", customerID, &result) return &result, err } // GetCustomer method uses the *mydp.Client to invoke an XMLRPC call to the // Service: mydatapipe.customers.get func (c *Client) PutRSCustomerID(customerID, rsCustomerID int) (CustomerJSON, error) { response := new(CustomerJSON) res, err := c.post("json/Customer.php", nil, strings.NewReader(fmt.Sprintf(`{"CustomerID": %d,"RSCoreCustomerID": %d}`, customerID, rsCustomerID))) 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 } // ActiveNonCoreCustomers method uses the *mydp.Client to invoke an JSON API call to the // Service: /json/ActiveNonCoreCustomers.php func (c *Client) ActiveNonCoreCustomers(offset int) (NonCoreCustomers, error) { response := new(NonCoreCustomers) res, err := c.get("json/ActiveNonCoreCustomers.php", url.Values{"offset": []string{strconv.Itoa(offset)}}) 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 }