// 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 failover import ( "fmt" "strconv" "github.rackspace.com/gorax/clients/mydp" ) const ( firewallType = "firewall" netscalerType = "netscaler" f5Type = "f5" serverIronType = "serveriron" ) // getSpecificDevice will determine the correct SpecificDevice type, to subsequently call the // correct API endpoint for SpecificDevice info. func (c *Client) getSpecificDevice(d mydp.Device) (string, error) { switch d.SpecificDeviceType { case f5Type: return c.getF5(d) case firewallType: return c.getFirewall(d) case serverIronType: return c.getServerIron(d) case netscalerType: return c.getNetscaler(d) default: return "", fmt.Errorf("SpecificDeviceType: %s has no failover", d.SpecificDeviceType) } } // getF5 method will populate the location and SpecificDevice information for a F5 SpecificDevice func (c *Client) getF5(d mydp.Device) (string, error) { id, err := strconv.Atoi(d.SpecificDeviceID) if err != nil { return "", err } f, err := c.dp.GetF5(id) if err != nil { return "", err } if f.FailoverPartnerID == "0" { return "", nil } id2, err := strconv.Atoi(f.FailoverPartnerID) if err != nil { return "", err } f2, err := c.dp.GetF5(id2) if err != nil { return "", err } return f2.DeviceID, nil } // getServerIron method will populate the location and SpecificDevice information for a ServerIron SpecificDevice func (c *Client) getServerIron(d mydp.Device) (string, error) { id, err := strconv.Atoi(d.SpecificDeviceID) if err != nil { return "", err } f, err := c.dp.GetServerIron(id) if err != nil { return "", err } if f.FailoverPartnerID == "0" { return "", nil } id2, err := strconv.Atoi(f.FailoverPartnerID) if err != nil { return "", err } f2, err := c.dp.GetServerIron(id2) if err != nil { return "", err } return f2.DeviceID, nil } // getFirewall method will populate the location and SpecificDevice information for a firewall SpecificDevice func (c *Client) getFirewall(d mydp.Device) (string, error) { id, err := strconv.Atoi(d.SpecificDeviceID) if err != nil { return "", err } fw, err := c.dp.GetFirewall(id) if err != nil { return "", err } if mustString(fw.FailoverPartner) == "0" { return "", nil } id2, err := strconv.Atoi(mustString(fw.FailoverPartner)) if err != nil { return "", err } f2, err := c.dp.GetFirewall(id2) if err != nil { return "", err } return mustString(f2.DeviceID), nil } // getNetscaler method will populate the location and SpecificDevice information for a Citrix NetScaler SpecificDevice func (c *Client) getNetscaler(d mydp.Device) (string, error) { id, err := strconv.Atoi(d.SpecificDeviceID) if err != nil { return "", err } ns, err := c.dp.GetNetscaler(id) if err != nil { return "", err } if ns.FailoverPartnerID == "0" { return "", nil } id2, err := strconv.Atoi(ns.FailoverPartnerID) if err != nil { return "", err } ns2, err := c.dp.GetNetscaler(id2) if err != nil { return "", err } return ns2.DeviceID, nil }