// 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 dp import ( "fmt" "strconv" "strings" ) const ( networkSwitchType = "switch" firewallType = "firewall" serverType = "server" loadBalancerType = "load balancer" netscalerType = "netscaler" hubType = "hub" storageArrayType = "storage array" routerType = "router" f5Type = "f5" serverIronType = "serveriron" ) // specificDevice will determine the correct SpecificDevice type, to subsequently call the // correct API endpoint for SpecificDevice info. func (c *Client) specificDevice(d *Device) error { switch d.Device.Type { case hubType: return c.getHub(d) case f5Type: return c.getF5(d) case firewallType: return c.getFirewall(d) case routerType: return c.getRouter(d) case serverType: return c.getServer(d) case serverIronType: return c.getServerIron(d) case netscalerType: return c.getNetscaler(d) case storageArrayType: return c.getStorage(d) case networkSwitchType: return c.getSwitch(d) default: return fmt.Errorf("SpecificDeviceInfo: %s unknown platform", d.Device.Type) } } // getHub method will populate the location and SpecificDevice information for a hub SpecificDevice func (c *Client) getHub(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } hub, err := c.dp.GetHub(id) if err != nil { return err } d.Device.OSName = hub.HubType d.Device.HostName = hub.DeviceName d.Device.Type = strings.ToTitle(hubType) d.Device.ComponentConfigType = hub.HubType d.Device.MakeModel = hub.HubType return nil } // getRouter method will populate the location and SpecificDevice information for a router SpecificDevice func (c *Client) getRouter(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } r, err := c.dp.GetRouter(id) if err != nil { return err } d.Device.Type = strings.ToTitle(routerType) d.Device.HostName = r.DeviceName return nil } // getF5 method will populate the location and SpecificDevice information for a F5 SpecificDevice func (c *Client) getF5(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } f, err := c.dp.GetF5(id) if err != nil { return err } d.Device.OSName = f.OSVersion d.Device.OS = f.OSVersion d.Device.FailoverID = f.FailoverPartnerID d.Device.HostName = f.DeviceName d.Device.Type = strings.ToTitle(f5Type) d.Device.ComponentConfigType = f.Model d.Device.MakeModel = f.Model return nil } // getServerIron method will populate the location and SpecificDevice information for a ServerIron SpecificDevice func (c *Client) getServerIron(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } f, err := c.dp.GetServerIron(id) if err != nil { return err } d.Device.OSName = f.OSVersion d.Device.OS = f.OSVersion d.Device.FailoverID = f.FailoverPartnerID d.Device.HostName = f.DeviceName d.Device.Type = strings.ToTitle(serverIronType) d.Device.ComponentConfigType = f.Model d.Device.MakeModel = f.Model return nil } // getFirewall method will populate the location and SpecificDevice information for a firewall SpecificDevice func (c *Client) getFirewall(d *Device) error { fw, err := c.dp.GetFirewallJSON(d.ID) if err != nil { return err } d.Device.OSName = fmt.Sprintf("%s %s", mustString(fw.Manufacturer), mustString(fw.Model)) d.Device.Type = strings.ToTitle(firewallType) d.Device.HostName = mustString(fw.DeviceName) d.Device.PrimaryIP = mustString(fw.DevicePrimaryIP) d.Device.ComponentConfigType = fmt.Sprintf("%s %s", mustString(fw.Manufacturer), mustString(fw.Model)) d.Device.MakeModel = fmt.Sprintf("%s %s", mustString(fw.Manufacturer), mustString(fw.Model)) d.Device.OS = mustString(fw.Version) d.Device.FailoverID = mustString(fw.FailoverPartner) d.Environment.Current.StartingSpace = getRackU(mustString(fw.RackU)) return nil } // getServer method will populate the location and SpecificDevice information for a server/web mux SpecificDevice func (c *Client) getServer(d *Device) error { // use JSON method which provides RackU data s, err := c.dp.GetServerJSON(d.ID) if err != nil { return err } d.Device.OSName = mustString(s.OperatingSystem) d.Device.Type = strings.ToTitle(serverType) d.Device.ComponentConfigType = mustString(s.Hardware) d.Device.MakeModel = mustString(s.Hardware) d.Device.SLA = mustString(s.Managed) d.Device.OS = mustString(s.OperatingSystem) d.Environment.Current.StartingSpace = getRackU(mustString(s.RackU)) if strings.Contains(strings.ToLower(mustString(s.Hardware)), "webmux") { d.Type = strings.ToTitle(loadBalancerType) } return nil } // getNetscaler method will populate the location and SpecificDevice information for a Citrix NetScaler SpecificDevice func (c *Client) getNetscaler(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } ns, err := c.dp.GetNetscaler(id) if err != nil { return err } d.Device.OSName = ns.HardwareType d.Device.Type = strings.ToTitle(loadBalancerType) d.Device.ComponentConfigType = ns.HardwareType d.Device.OS = ns.OSVersion d.Device.MakeModel = ns.HardwareType d.Device.FailoverID = ns.FailoverPartnerID d.Environment.Current.StartingSpace = getRackU(ns.RackU) return nil } // getStorage method will populate the location and SpecificDevice information for a storage SpecificDevice func (c *Client) getStorage(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } sa, err := c.dp.GetStorageArray(id) if err != nil { return err } d.Device.OSName = sa.Firmware d.Device.Type = strings.ToTitle(storageArrayType) d.Device.ComponentConfigType = fmt.Sprintf("%s %s", sa.Manufacturer, sa.Model) d.Device.OS = sa.Model d.Device.MakeModel = fmt.Sprintf("%s %s", sa.Manufacturer, sa.Model) d.Environment.Current.StartingSpace = getRackU(sa.RackU) return nil } // getSwitch method will populate the location and SpecificDevice information for a switch SpecificDevice func (c *Client) getSwitch(d *Device) error { id, err := strconv.Atoi(d.Device.ID) if err != nil { return err } sa, err := c.dp.GetSwitch(id) if err != nil { return err } d.Device.OSName = mustString(sa.SwitchType) d.Device.Type = strings.ToTitle(networkSwitchType) d.Device.ComponentConfigType = mustString(sa.SwitchType) d.Device.OS = mustString(sa.SwitchType) d.Device.MakeModel = mustString(sa.SwitchType) d.Environment.Current.StartingSpace = getRackU(mustString(sa.RackU)) return nil }