//nolint:golint package dcxAutohost import ( "encoding/json" "errors" "fmt" "net" "net/url" "regexp" "strconv" "strings" "github.rackspace.com/gorax/tools/util" ) type ( // DeallocateOptions are the possible parameters for the DeviceDeallocateIPAddress() method DeallocateOptions struct { Private *bool Public *bool IPv6 *bool DRACNet *bool ServiceNet *bool IPAddress *net.IP } // DeviceAutoAssignedIP is the response struct for DeviceAutoAssignDracIP method DeviceAutoAssignedIP struct { DracIPAddress string `json:"drac_ip_address"` } // DeviceDeallocatedIPs is the response struct for the DeviceDeallocateMigratedIPs method DeviceDeallocatedIPs struct { DeallocatedIPs []string `json:"deallocated_ips" bson:"deallocated_ips"` } // DeviceConfiguration is the parent type to the DeviceInfo decoder DeviceConfiguration struct { Configuration deviceInfo `json:"configuration" bson:"configuration"` } // IPCount type is a decoder type to return a count of // IP addresses IPCount struct { Count int `json:"count" bson:"count"` } deviceInfo struct { ContainerName string `json:"container_name" bson:"container_name"` Name string `json:"name" bson:"name"` CompanyName string `json:"company_name" bson:"company_name"` DataCenter deviceDc `json:"data_center" bson:"data_center"` DeviceNumber string `json:"device_number" bson:"device_number"` ConfigNumber string `json:"configuration_number" bson:"configuration_number"` } deviceDc struct { Name string `json:"name" bson:"name"` Symbol string `json:"symbol" bson:"symbol"` } // DeviceCopyMigSwitchPorts is the response struct from DCX for the copy_migration_switch_ports method DeviceCopyMigSwitchPorts struct { PortsCopied []CopiedPortSettings `json:"ports_copied" bson:"ports_copied"` } // CopiedPortSettings defines the struct for the copy_migration_switch_ports payload items CopiedPortSettings struct { TargetSwitch string `json:"target_switch" bson:"target_switch"` TargetPortNumber int `json:"target_port_number" bson:"target_port_number"` PortSettings struct { Speed int `json:"speed" bson:"speed"` Duplex string `json:"duplex" bson:"duplex"` Admin string `json:"admin" bson:"admin"` Trunking struct { Enabled bool `json:"enabled" bson:"enabled"` VlansEnabled []int `json:"vlans_enabled" bson:"vlans_enabled"` } `json:"trunking" bson:"trunking"` } `json:"port_settings" bson:"port_settings"` VlanID int `json:"vlan_id,omitempty" bson:"vlan_id,omitempty"` } // DeviceEnvironment is the parent type to decode device environment // info from DCX. DeviceEnvironment struct { Environment *DeviceEnvInfo `json:"environment" bson:"environment"` } // DeviceEnvInfo type represents the DeviceEnvironment data returned from the // DCX API DeviceEnvInfo struct { Name string `json:"name" bson:"name"` Environment string `json:"number" bson:"number"` ID int `json:"id" bson:"id"` Gateways []int `json:"gateway_device_numbers" bson:"gateway_device_numbers"` } // DeviceEnvironmentData is the parent type for the EnvironmentData decoder DeviceEnvironmentData struct { EnvironmentData environmentData `json:"environment_data" bson:"environment_data"` } environmentData struct { Devices []EnvironmentDevice `json:"devices" bson:"devices"` Vlans []EnvironmentVlan `json:"vlans" bson:"vlans"` RemoveVlans []EnvironmentVlan `json:"remote_vlans" bson:"remote_vlans"` } // EnvironmentDevice type is the detailed IP/Device information for the DeviceEnvironmentData // method call EnvironmentDevice struct { Ipv6Ips []string `json:"ipv6_ips" bson:"ipv6_ips"` CoreNumber string `json:"core_device_number" bson:"core_device_number"` PublicIps []string `json:"public_ips" bson:"public_ips"` PrivateIps []string `json:"private_ips" bson:"private_ips"` IsGateway bool `json:"is_gateway" bson:"is_gateway"` ReservedGatewayIps []string `json:"reserved_gateway_ips" bson:"reserved_gateway_ips"` ManagedColoIps []string `json:"managed_colo_ips" bson:"managed_colo_ips"` Nats []struct { Inside string `json:"inside" bson:"inside"` Outside string `json:"outside" bson:"outside"` } `json:"nats" bson:"nats"` GatewayIps []string `json:"gateway_ips" bson:"gateway_ips"` PublicVlans []deviceVlan `json:"public_vlans" bson:"public_vlans"` Vlans []deviceVlan `json:"vlans" bson:"vlans"` } deviceVlan struct { NetworkSegmentService string `json:"network_segment_service" bson:"network_segment_service"` SegmentName string `json:"segment_name" bson:"segment_name"` Ipv4Blocks []string `json:"ipv4_ip_blocks" bson:"ipv4_ip_blocks"` VlanNumber int `json:"vlan_number" bson:"vlan_number"` SwitchPorts []struct { Name string `json:"name" bson:"name"` SwitchName string `json:"switch_name" bson:"switch_name"` SwitchPortInterfaceTypeName string `json:"switch_port_interface_type_name" bson:"switch_port_interface_type_name"` } `json:"switch_ports" bson:"switch_ports"` Ipv6Blocks []string `json:"ipv6_ip_blocks" bson:"ipv6_ip_blocks"` } // EnvironmentVlan type is the detailed VLAN information for the DeviceEnvironmentData // method call EnvironmentVlan struct { ID int `json:"id" bson:"id"` SegmentName string `json:"segment_name" bson:"segment_name"` VlanNumber int `json:"vlan_number" bson:"vlan_number"` VlanName string `json:"vlan_name" bson:"vlan_name"` VlanType string `json:"vlan_type" bson:"vlan_type"` VlanZone string `json:"vlan_zone" bson:"vlan_zone"` PrivateBlock string `json:"private_block" bson:"private_block"` AvailableCount int `json:"available_ip_address_count" bson:"available_ip_address_count"` GatewayIP string `json:"gateway_ip" bson:"gateway_ip"` GatewayDevices []string `json:"gateway_devices" bson:"gateway_devices"` Ipv6Block string `json:"ipv6_block" bson:"ipv6_block"` DeviceNumbers []string `json:"device_numbers" bson:"device_numbers"` SecondaryVlanIDs []int `json:"secondary_vlan_ids" bson:"secondary_vlan_ids"` SecondaryVlans []EnvironmentVlan `json:"secondary_vlans" bson:"secondary_vlans"` } // DeviceVMNetData type is the details from the decoded values // in the /devices/DEVICE/vm_net_data DCX API call. DeviceVMNetData struct { VlanName string `json:"vlan_name" bson:"vlan_name"` ManagementIP string `json:"management_ip" bson:"management_ip"` VMotionIP string `json:"vmotion_ip" bson:"vmotion_ip"` Gateway string `json:"gateway" bson:"gateway"` Netmask string `json:"netmask" bson:"netmask"` PrimaryDNS string `json:"primary_dns" bson:"primary_dns"` SecondaryDNS string `json:"secondary_dns" bson:"secondary_dns"` } // DeviceConnectedGateways type is the details from the decoded values // in the /devices/DEVICE/directly_connected_gateways DCX API call. DeviceConnectedGateways struct { ConnectedGateways []int `json:"connected_gateways" bson:"connected_gateways"` } // PrivateBlockCIDRs type is the decoded value from the // /devices/%d/environment/private_block_cidrs DCX API call PrivateBlockCIDRs struct { PrivateBlocks []string `json:"private_block_cidrs" bson:"private_block_cidrs"` } // EnvironmentDeviceAssignment type is the details from the // from the /environments/ENV/devices/assignments DCX API Call EnvironmentDeviceAssignment struct { Assigned []int `json:"assigned_devices" bson:"assigned_devices"` UnAssigned []int `json:"unassigned_devices" bson:"unassigned_devices"` } exNetAggrZoneTop struct { Info *ExNetAggrZoneInfo `json:"exnet_aggr_and_zone_info" bson:"exnet_aggr_and_zone_info"` } // ExNetAggrZoneInfo type is the detailed information from the exnet_aggr_and_zone_info // DCX call ExNetAggrZoneInfo struct { AggrNames []string `json:"aggr_names" bson:"aggr_names"` Device string `json:"device_number" bson:"device_number"` ZoneName string `json:"zone_name" bson:"zone_name"` } // DeviceIPs is the parent type for the decoded response from the device IPs calls DeviceIPs struct { Items []struct { DeviceNumber string `json:"device_number" bson:"device_number"` IPv6IPs []IPv6IP `json:"ipv6_ips" bson:"ipv6_ips"` PublicIPs []PublicIP `json:"public_ips" bson:"public_ips"` PrivateIPs []PrivateIP `json:"private_ips" bson:"private_ips"` } `json:"items" bson:"items"` } // PublicIP type is details about an IP address classified as a Public address // in DCX's systems PublicIP struct { Reserved bool `json:"reserved" bson:"reserved"` IPBlockType string `json:"ip_block_type" bson:"ip_block_type"` Label string `json:"label" bson:"label"` NetworkType string `json:"network_type" bson:"network_type"` VLANNumber *int `json:"vlan_number" bson:"vlan_number"` NetworkNumber string `json:"network_number" bson:"network_number"` NetworkName string `json:"network_name" bson:"network_name"` Netmask string `json:"netmask" bson:"netmask"` IPAddress string `json:"ip_address" bson:"ip_address"` IPBlockCIDR string `json:"ip_block_cidr" bson:"ip_block_cidr"` GatewayIP string `json:"gateway_ip" bson:"gateway_ip"` Zone string `json:"zone" bson:"zone"` StaticRoutes []ipStaticRoutes `json:"static_routes" bson:"static_routes"` NattedTo string `json:"natted_to" bson:"natted_to"` } // PrivateIP type is details about an IP address classified as a Private IP address // in DCX's systems PrivateIP struct { Reserved bool `json:"reserved" bson:"reserved"` IPBlockType string `json:"ip_block_type" bson:"ip_block_type"` Label string `json:"label" bson:"label"` NetworkType string `json:"network_type" bson:"network_type"` VLANNumber *int `json:"vlan_number" bson:"vlan_number"` NetworkNumber string `json:"network_number" bson:"network_number"` NetworkName string `json:"network_name" bson:"network_name"` Netmask string `json:"netmask" bson:"netmask"` IPAddress string `json:"ip_address" bson:"ip_address"` IPBlockCIDR string `json:"ip_block_cidr" bson:"ip_block_cidr"` GatewayIP string `json:"gateway_ip" bson:"gateway_ip"` Zone string `json:"zone" bson:"zone"` StaticRoutes []ipStaticRoutes `json:"static_routes" bson:"static_routes"` } // IPv6IP type is details about an IP address classified as a IPv6 IP address // in DCX's systems IPv6IP struct { Address string `json:"ip_address" bson:"ip_address"` Netmask string `json:"netmask" bson:"netmask"` Label string `json:"label" bson:"label"` ServerAllocBlock string `json:"server_allocation_block" bson:"server_allocation_block"` IPBlockCIDR string `json:"ip_block_cidr" bson:"ip_block_cidr"` NetworkType string `json:"network_type" bson:"network_type"` NetworkName string `json:"network_name" bson:"network_name"` VLANNumber int `json:"vlan_number" bson:"vlan_number"` GatewayIP string `json:"gateway_ip" bson:"gateway_ip"` IPBlockType string `json:"ip_block_type" bson:"ip_block_type"` Zone string `json:"zone" bson:"zone"` StaticRoutes []string `json:"static_routes" bson:"static_routes"` } ipStaticRoutes struct { NextHopIndex int `json:"next_hop_index" bson:"next_hop_index"` CIDR string `json:"cidr" bson:"cidr"` NextHop string `json:"next_hop" bson:"next_hop"` } ) // CheckForKIRSetupErrors Checks if the given device is ready for KIR automation. // Returns a slice of errors found. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/kirSetupVerify func (a *Client) CheckForKIRSetupErrors(device int) ([]string, error) { body, err := a.get(fmt.Sprintf("/devices/%v/check_for_kir_setup_errors", device), nil) if err != nil { return []string{}, err } var r DCXErr if err := json.Unmarshal(body, &r); err != nil { return []string{}, err } return r.Errors, nil } // DeviceInfo method will return the configuration for a device number // // See: https://pages.github.rackspace.com/dcx/gateway/#/companies/deviceConfiguration func (a *Client) DeviceInfo(device int) (*DeviceConfiguration, error) { body, err := a.get(fmt.Sprintf("/companies/rackspace/devices/%d.json", device), nil) if err != nil { return nil, err } var dc DeviceConfiguration if err := json.Unmarshal(body, &dc); err != nil { return nil, err } return &dc, nil } // DeviceIPs method will return a DeviceIPs type that contains the IP address assignment information for // requested devices, account number, or network environment number // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/ipAddresses func (a *Client) DeviceIPs(device int) (*DeviceIPs, error) { body, err := a.get(fmt.Sprintf("/devices/ip_addresses?device_numbers=%v", device), nil) if err != nil { return nil, err } var ips DeviceIPs if err := json.Unmarshal(body, &ips); err != nil { return nil, err } return &ips, nil } // AvailableIPCount method will return an IpCount type that describes the available public IP // count in the given device's environment // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/availableIpCount func (a *Client) AvailableIPCount(device int) (*IPCount, error) { body, err := a.get(fmt.Sprintf("/devices/%d/available_ip_count.json", device), nil) if err != nil { return nil, err } var ic IPCount if err = json.Unmarshal(body, &ic); err != nil { return nil, err } return &ic, nil } // DeviceEnvironment method will return a DeviceEnvInfo type describing the given // devices environment data // // See: https://pages.github.rackspace.com/dcx/gateway/#/companies/deviceEnvironment func (a *Client) DeviceEnvironment(device int) (*DeviceEnvInfo, error) { body, err := a.get(fmt.Sprintf("/companies/rackspace/devices/%d/environment.json", device), nil) if err != nil { return nil, fmt.Errorf("dcxAutohost.Client.DeviceEnvironment(): %s", err.Error()) } var d DeviceEnvironment if err = json.Unmarshal(body, &d); err != nil { return nil, fmt.Errorf("dcxAutohost.Client.DeviceEnvironment(): %s", err.Error()) } return d.Environment, nil } // DeviceEnvironmentData method returns a DeviceEnvironmentData type describing devices and VLANs of the // environment that the device with the given core device number belongs to. // NOTE: This endpoint is dedicated to firewall KIR and is subject to change without notice. // // See: https://pages.github.rackspace.com/dcx/gateway/#/companies/deviceEnvironment func (a *Client) DeviceEnvironmentData(device int) (*DeviceEnvironmentData, error) { body, err := a.get(fmt.Sprintf("/devices/%d/environment_data.json", device), nil) if err != nil { return nil, err } var d DeviceEnvironmentData if err = json.Unmarshal(body, &d); err != nil { return nil, err } return &d, nil } // DeviceJoinExNetVlan method will associate the device with the vlan specified. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/joinExnetVlan func (a *Client) DeviceJoinExNetVlan(device, vlanID int) error { if _, err := a.put(fmt.Sprintf("/devices/%d/join_exnet_vlan?vlan_number=%d", device, vlanID), nil); err != nil { return err } return nil } // DeviceVMNetData method returns a DeviceVmNetData type describing VMNet information for a given device // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/vmNetDataXml func (a *Client) DeviceVMNetData(device int) ([]DeviceVMNetData, error) { body, err := a.get(fmt.Sprintf("/devices/%d/vm_net_data.json", device), nil) if err != nil { return nil, err } var d []DeviceVMNetData if err = json.Unmarshal(body, &d); err != nil { return nil, err } if len(d) == 0 { return nil, errors.New("unable to retrieve data") } return d, nil } // DeviceConnectedGateways method returns a DeviceConnectedGateways type describing the device numbers of // gateways that share a VLAN with the device. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/directlyConnectedGateways func (a *Client) DeviceConnectedGateways(device int) (*DeviceConnectedGateways, error) { body, err := a.get(fmt.Sprintf("/devices/%d/directly_connected_gateways", device), nil) if err != nil { return nil, err } type deviceConnectedGateways struct { ConnectedGateways string `json:"connected_gateways" bson:"connected_gateways"` } var td deviceConnectedGateways if err = json.Unmarshal(body, &td); err != nil { return nil, err } var d DeviceConnectedGateways gw := regexp.MustCompile(`([0-9]+)`).FindAllString(td.ConnectedGateways, -1) if len(gw) > 1 { for _, v := range gw { n, err := strconv.Atoi(v) if err == nil { d.ConnectedGateways = append(d.ConnectedGateways, n) } } } return &d, nil } // PrivateBlockCIDRs method returns a PrivateBlockCIDRs type describing the CIDRs for the environment's // private IP blocks, given a gateway_device_number within that environment // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/privateBlockCidrsXml func (a *Client) PrivateBlockCIDRs(device int) (*PrivateBlockCIDRs, error) { body, err := a.get(fmt.Sprintf("/devices/%d/environment/private_block_cidrs.json", device), nil) if err != nil { return nil, err } var p PrivateBlockCIDRs if err = json.Unmarshal(body, &p.PrivateBlocks); err != nil { return nil, err } return &p, nil } // DeviceDeallocateIPAddress will deallocate the IPs for a device based on the options // provided in the DeallocateOptions struct. If opts is nil, the default action of deallocating // all IPs will take effect. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/deallocateIp func (a *Client) DeviceDeallocateIPAddress(device int, opts *DeallocateOptions) error { if opts == nil { opts = new(DeallocateOptions) } uri := fmt.Sprintf("/devices/%d/ip_addresses/deallocate", device) query := opts.process().Encode() if query != "" { uri += fmt.Sprintf("?%s", query) } _, err := a.post(uri, []byte("")) return err } // DeviceDeallocateMigratedIPs will deallocate the source DRACNet, SNet, VMNet IPs for a // device based on the source DC value provided. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/deallocatedMigrationIps func (a *Client) DeviceDeallocateMigratedIPs(device int, dc string, dryRun bool) (*DeviceDeallocatedIPs, error) { j := struct { DC string `json:"dc" bson:"dc"` DryRun bool `json:"dry_run" bson:"dry_run"` }{DC: strings.ToUpper(dc), DryRun: dryRun} payload, err := json.Marshal(&j) if err != nil { return nil, err } body, err := a.post(fmt.Sprintf("/devices/%d/deallocate_migrated_ips", device), payload) if err != nil { return nil, err } resp := new(DeviceDeallocatedIPs) err = json.Unmarshal(body, resp) return resp, err } // DeviceAutoAssignDracIP will auto-assign a drac IP, will optionally set reserve flag as true // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/autoAssignDrac func (a *Client) DeviceAutoAssignDracIP(device int, reservation bool) (*DeviceAutoAssignedIP, error) { j := struct { Reservation bool `json:"reservation"` }{Reservation: reservation} payload, err := json.Marshal(&j) if err != nil { return nil, err } body, err := a.post(fmt.Sprintf("/devices/%d/ip_addresses/auto_assign_drac_ip", device), payload) if err != nil { return nil, err } resp := new(DeviceAutoAssignedIP) err = json.Unmarshal(body, resp) return resp, err } // DeviceManualAssignIP will assign a specified IP, will optionally set reserve flag as true // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/manualAssignIps func (a *Client) DeviceManualAssignIP(device int, privateIP, publicIP string, isPrimary, isReserved bool) error { j := struct { PrivateIP string `json:"private_ip_address,omitempty"` PublicIP string `json:"public_ip_address,omitempty"` IsPrimary bool `json:"is_primary"` IsReserved bool `json:"is_reserved"` }{ PrivateIP: privateIP, PublicIP: publicIP, IsPrimary: isPrimary, IsReserved: isReserved, } payload, err := json.Marshal(&j) if err != nil { return err } _, err = a.post(fmt.Sprintf("/devices/%d/ip_addresses/manual_assign", device), payload) return err } // DeviceAssignments method returns an EnvironmentDeviceAssignment type describing core device numbers // assigned to environment and those that could be // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getAssignments func (a *Client) DeviceAssignments(env string) (*EnvironmentDeviceAssignment, error) { body, err := a.get("/environments/"+env+"/devices/assignments.json", nil) if err != nil { return nil, err } var eda EnvironmentDeviceAssignment if err = json.Unmarshal(body, &eda); err != nil { return nil, err } return &eda, nil } // DeviceListEnvironment method will range through a list of devices and return a common environment // or return an error if devices are in multiple environments. func (a *Client) DeviceListEnvironment(devices []int) (*DeviceEnvInfo, error) { var envList []*DeviceEnvInfo for _, d := range devices { // NOTE: external call to /companies/rackspace/devices/%d/environment if envInfo, err := a.DeviceEnvironment(d); err == nil { envList = append(envList, envInfo) } } if len(envList) == 0 { return nil, errors.New("no valid environments where found") } m := envList[0] for _, env := range envList[0:] { if m.Environment != env.Environment { return nil, fmt.Errorf("found multiple environment in the device list, expected %s found %s", m.Environment, env.Environment) } } return m, nil } // ExNetAggrZoneInfo method will return an ExNetAggrZoneInfo type describing aggr and zone information for // a given device. When the given device is not a physical device, then its // underlying physical device is used. // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/exnetAggrAndZoneInfoXml func (a *Client) ExNetAggrZoneInfo(device int) (*ExNetAggrZoneInfo, error) { body, err := a.get(fmt.Sprintf("/devices/%d/zones/exnet_aggr_and_zone_info.json", device), nil) if err != nil { return nil, err } ez := new(exNetAggrZoneTop) if err = json.Unmarshal(body, ez); err != nil { return nil, err } if ez.Info != nil { return ez.Info, nil } return nil, fmt.Errorf("unable to find info for %d", device) } // GatewayPublicIPs returns a string list of IPs assigned to the gateway devices, // that are not part of the routed blocks func (a *Client) GatewayPublicIPs(env *DeviceEnvInfo, blocks []string) ([]string, error) { if len(env.Gateways) == 0 { return nil, errors.New("gateway list cannot be empty") } // NOTE: external call to /environments/%s/devices/ip_addresses ips, err := a.EnvironmentIpAddress(env.Environment) if err != nil { return nil, err } var networks []*net.IPNet for _, block := range blocks { _, network, err := net.ParseCIDR(block) if err != nil { return nil, err } networks = append(networks, network) } var gwIPs []string for _, gw := range env.Gateways { d := findDevice(gw, ips) if d == nil { continue } for _, ip := range d.IpAddress.Public { IP := net.ParseIP(ip) if IP != nil { if !ipInNetworks(networks, IP) { if !util.IsPrivate(ip) { gwIPs = append(gwIPs, fmt.Sprintf("%s/32", ip)) } } } } } return gwIPs, nil } // CopyMigrationPortsSegments method will copy the Environment Manager segment associations from the devices // source ports to their mapped migration ports // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/copyMigrationPortsSegments func (a *Client) CopyMigrationPortsSegments(device int) error { if _, err := a.post(fmt.Sprintf("/devices/%d/copy_migration_port_segments", device), nil); err != nil { return err } return nil } // CopyMigrationSwitchPorts method will copy the switch ports settings from source ports // to reservation ports assigned in Location Manager // // See: https://pages.github.rackspace.com/dcx/gateway/#/devices/copyMigrationSwitchPorts func (a *Client) CopyMigrationSwitchPorts(device int, makeChanges bool) (*DeviceCopyMigSwitchPorts, error) { body, err := a.post( fmt.Sprintf("/devices/%d/copy_migration_switch_ports?make_changes=%s", device, strconv.FormatBool(makeChanges)), nil, ) if err != nil { return nil, err } resp := new(DeviceCopyMigSwitchPorts) err = json.Unmarshal(body, resp) return resp, err } // findDevice function will range through a list of environment IP data and look // for a specific device func findDevice(device int, eip *EnvironmentIpAddress) *EnvironmentDevices { for _, d := range eip.Devices { if d.DeviceNumber == device { return d } } return nil } // ipInNetworks is an internal function to return a bool if the IP address is within // the passed in IP list. func ipInNetworks(networks []*net.IPNet, ip net.IP) bool { for _, n := range networks { if n.Contains(ip) { return true } } return false } // process() will check the option values in the struct and generate a // url.Values map based on the set values func (o *DeallocateOptions) process() url.Values { ret := url.Values{} if o.Private != nil { ret.Set("private", strconv.FormatBool(*o.Private)) } if o.Public != nil { ret.Set("public", strconv.FormatBool(*o.Public)) } if o.IPv6 != nil { ret.Set("ipv6", strconv.FormatBool(*o.IPv6)) } if o.DRACNet != nil { ret.Set("drac_net", strconv.FormatBool(*o.DRACNet)) } if o.ServiceNet != nil { ret.Set("services_net", strconv.FormatBool(*o.ServiceNet)) } if o.IPAddress != nil { ret.Set("address", o.IPAddress.String()) } return ret } // SetPrivate method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetPrivate(private bool) *DeallocateOptions { o.Private = &private return o } // SetPublic method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetPublic(public bool) *DeallocateOptions { o.Public = &public return o } // SetIPv6 method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetIPv6(ipv6 bool) *DeallocateOptions { o.IPv6 = &ipv6 return o } // SetDRACNet method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetDRACNet(dracnet bool) *DeallocateOptions { o.DRACNet = &dracnet return o } // SetServiceNet method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetServiceNet(servicenet bool) *DeallocateOptions { o.ServiceNet = &servicenet return o } // SetIPAddress method will assign the boolean operator to the DeallocateOptions type func (o *DeallocateOptions) SetIPAddress(ip net.IP) *DeallocateOptions { o.IPAddress = &ip return o }