//nolint:golint package dcxAutohost import ( "encoding/json" "fmt" "net/url" "strings" ) type ( // IPAddressInfo is the struct response for the /ip_addresses/info endpoint and will return // IP block, block policy, block description, block account number, IP usage, environment, // account, device number, and autonomous system number for an ip address. IPAddressInfo struct { Account int `json:"account" bson:"account"` Address string `json:"address" bson:"address"` Asn int `json:"asn" bson:"asn"` Block string `json:"block" bson:"block"` BlockDescription string `json:"block_description" bson:"block_description"` BlockAccount int `json:"block_account_number" bson:"block_account_number"` BlockPolicy string `json:"block_policy" bson:"block_policy"` Device int `json:"device" bson:"device"` Environment string `json:"environment" bson:"environment"` IPAddressUsage string `json:"ip_address_usage" bson:"ip_address_usage"` } // IPAssign struct defines the request body for the DCX API to assign IP addresses // to a given device. IPAssign struct { Addresses []Address `json:"addresses" bson:"addresses"` DeviceID int `json:"device_number" bson:"device_number"` } // IPAssignResp struct defines the request body for the DCX API to assign IP addresses // to a given device. IPAssignResp struct { Addresses []Address `json:"assignments" bson:"assignments"` Errors []string `json:"errors" bson:"errors"` DeviceID int `json:"device_number" bson:"device_number"` } // Address type defines a single address record in the IPAssign type Address struct { Type string `json:"type" bson:"type"` Address string `json:"address,omitempty" bson:"address,omitempty"` CIDR string `json:"cidr,omitempty" bson:"cidr,omitempty"` Vlan int `json:"vlan,omitempty" bson:"vlan,omitempty"` Role string `json:"role,omitempty" bson:"role,omitempty"` SVI bool `json:"svi" bson:"svi"` NAT StaticNAT `json:"static_nat,omitempty" bson:"static_nat,omitempty"` ClearPTR bool `json:"clear_ptr" bson:"clear_ptr"` } // StaticNAT struct is the request type to create a static NAT for a given address StaticNAT struct { Address string `json:"address,omitempty" bson:"address,omitempty"` CIDR string `json:"cidr,omitempty" bson:"cidr,omitempty"` Vlan int `json:"vlan,omitempty" bson:"vlan,omitempty"` Role string `json:"role,omitempty" bson:"role,omitempty"` } ) // IPAddressInfo method will return a IPAddressInfo type that will give the IP block, block policy, // IP usage, environment, account, device number, and autonomous system number for an ip address // // See: https://pages.github.rackspace.com/dcx/gateway/#/ip_addresses/ipInfo func (a *Client) IPAddressInfo(ip string) (*IPAddressInfo, error) { body, err := a.get("/ip_addresses/info.json", url.Values{"address": []string{ip}}) if err != nil { return nil, err } var ipi IPAddressInfo if err = json.Unmarshal(body, &ipi); err != nil { return nil, err } return &ipi, nil } // IPAddressAssign method will assign IP addresses to one or more devices. // // See: https://pages.github.rackspace.com/dcx/gateway/#/ip_addresses/assignIpAddresses func (a *Client) IPAddressAssign(req []IPAssign) ([]IPAssignResp, error) { type ( reqType struct { Devices []IPAssign `json:"devices" bson:"devices"` } respType struct { Devices []IPAssignResp `json:"devices" bson:"devices"` } ) var resp respType payload, err := json.Marshal(reqType{Devices: req}) if err != nil { return resp.Devices, err } body, err := a.post("/ip_addresses/assign", payload) if err != nil { return resp.Devices, err } if err = json.Unmarshal(body, &resp); err != nil { return resp.Devices, err } for _, d := range resp.Devices { if len(d.Errors) != 0 { return resp.Devices, fmt.Errorf("%d:%s", d.DeviceID, strings.Join(d.Errors, ";")) } } return resp.Devices, nil }