package dcx import ( "net" "strings" "github.rackspace.com/DCM/tools" "github.rackspace.com/gorax/clients/dcxAutohost" ) type ( // DeallocatedIPList is the return struct for DeallocateIPs() DeallocatedIPList struct { DeallocatedIPs []string `json:"deallocated_ips"` Errors []DeallocatedIPError } // DeallocatedIPError lists the IP and error message for a failed deallocate IP attempt DeallocatedIPError struct { IPAddress string `json:"ip_address"` Error string `json:"error"` } ) // DeallocateIPs will deallocate the source IPs of a device func (c *Client) DeallocateIPs(device tools.Device, currentDC, migrationDC string) (*DeallocatedIPList, error) { ret := new(DeallocatedIPList) ret.DeallocatedIPs = make([]string, 0) ret.Errors = make([]DeallocatedIPError, 0) if currentDC == migrationDC { ips := make([]string, 0) // compile list of source DRACNet, ServiceNet, VMNet IPs to deallocate for _, ip := range device.Network.Address.DracIPs.Source { ips = append(ips, ip.IPAddress) } for _, ip := range device.Network.Address.ServiceNetIPs.Source { ips = append(ips, ip.IPAddress) } for _, ip := range device.Network.Address.VMNetMgmt.Source { ips = append(ips, ip.IPAddress) } for _, ip := range device.Network.Address.VMNetVMotion.Source { ips = append(ips, ip.IPAddress) } for _, ip := range ips { opts := new(dcxAutohost.DeallocateOptions) err := c.dcx.DeviceDeallocateIPAddress(device.ID, opts.SetIPAddress(net.ParseIP(ip))) // don't stop iterative process if a deallocate attempt fails; add it to list and continue if err == nil { ret.DeallocatedIPs = append(ret.DeallocatedIPs, ip) } else { ret.Errors = append(ret.Errors, DeallocatedIPError{IPAddress: ip, Error: err.Error()}) } } } else { resp, err := c.dcx.DeviceDeallocateMigratedIPs(device.ID, strings.ToUpper(currentDC), false) if err != nil { return ret, err } ret.DeallocatedIPs = resp.DeallocatedIPs } return ret, nil }