//nolint:golint package dcxAutohost import ( "encoding/json" "fmt" "net/url" ) type ( // EnvironmentMigrationVlan type is the decoded value for the Migrations VLAN calls EnvironmentMigrationVlan struct { ConfiguredVlan struct { VlanNumber int `json:"vlan_number" bson:"vlan_number"` ZoneName string `json:"zone_name" bson:"zone_name"` NetworkSegments []string `json:"network_segments" bson:"network_segments"` } `json:"configured_vlan" bson:"configured_vlan"` MigrationVlan struct { VlanNumber int `json:"vlan_number" bson:"vlan_number"` ZoneName string `json:"zone_name" bson:"zone_name"` NetworkSegments []string `json:"network_segments" bson:"network_segments"` } `json:"migration_vlan" bson:"migration_vlan"` } // IPBlockCIDR type is the decoded type from the ip_block_cidrs DCX // API call. IPBlockCIDR struct { Public []string `json:"public" bson:"public"` Private []string `json:"private" bson:"private"` } // ExcludedVlan type will destruct the API payload for vlans to // exclude during a migration_vlans call. ExcludedVlan struct { ZoneName string `json:"zone_name" bson:"zone_name"` VlanNumber int `json:"vlan_number" bson:"vlan_number"` } // MigratedVlans type will represent the return values for the migrate_vlans // DCX API call MigratedVlans struct { VlansMigrated []*migratedVlansItem `json:"vlans_migrated" bson:"vlans_migrated"` VlansNotMigrated []*migratedVlansItem `json:"vlans_not_migrated" bson:"vlans_not_migrated"` } // EnvironmentAutoDiscover is the response payload for the EnvironmentAutoDiscoverExnet method EnvironmentAutoDiscover struct { Devices []AutoDiscoverEntry `json:"devices" bson:"devices"` } // AutoDiscoverEntry is an item in the EnvironmentAutoDiscover.Devices array AutoDiscoverEntry struct { Actions []string `json:"actions" bson:"actions"` DeviceNumber string `json:"device_number" bson:"device_number"` Errors []string `json:"errors" bson:"errors"` } // EnvironmentAssignments is the type decoded from the assignments environment // DCX API call. EnvironmentAssignments struct { AssignedDevices []int `json:"assigned_devices" bson:"assigned_devices"` UnassignedDevices []int `json:"unassigned_devices" bson:"unassigned_devices"` } // EnvironmentIpAddress is the type decoded from the ip_addresses environment // DCX API call. EnvironmentIpAddress struct { Devices []*EnvironmentDevices `json:"devices" bson:"devices"` } // EnvironmentDevices type are the detailed addresses and NAT info // for a given device. EnvironmentDevices struct { DeviceNumber int `json:"device_number" bson:"device_number"` IpAddress *EnvironmentIPs `json:"ip_addresses" bson:"ip_addresses"` Nats []*EnvironmentNat `json:"nats" bson:"nats"` } // EnvironmentIPs type is the decoded public/private assignments EnvironmentIPs struct { Public []string `json:"public" bson:"public"` Private []string `json:"private" bson:"private"` } // EnvironmentNat represents the public/private type for a NAT statement EnvironmentNat struct { Public string `json:"public" bson:"public"` Private string `json:"private" bson:"private"` } // MigrateVlansPayload is a typed payload to send to the migration vlans // call. MigrateVlansPayload struct { ExcludedVlans []*ExcludedVlan `json:"excluded_vlans" bson:"excluded_vlans"` } // VlanRequest is the request payload to the DCX API to create a new vlan in // an environment VlanRequest struct { Shared bool `json:"shared" bson:"shared"` Segment NetworkSegment `json:"network_segment" bson:"network_segment"` } // VlanResponse is the expected response from a vlan create from the DCX API VlanResponse struct { ID int `json:"id"` SegmentName string `json:"segment_name"` VlanNumber int `json:"vlan_number"` VlanName string `json:"vlan_name"` VlanType string `json:"vlan_type"` VlanZone string `json:"vlan_zone"` PrivateBlock string `json:"private_block"` AvailableIPAddressCount int `json:"available_ip_address_count"` GatewayIP string `json:"gateway_ip"` GatewayDevices []string `json:"gateway_devices"` Ipv6Block string `json:"ipv6_block"` DeviceNumbers []string `json:"device_numbers"` } // NetworkSegment type is the network-segment that is created with the VlanNew type // in a vlan add request. NetworkSegment struct { NSS string `json:"network_segment_service_name" bson:"network_segment_service_name"` CIDR string `json:"cidr,omitempty" bson:"cidr,omitempty"` Name string `json:"name" bson:"name"` } environmentDevices struct { DeviceNumber int `json:"device_number" bson:"device_number"` IPAddress EnvironmentIPs `json:"ip_addresses" bson:"ip_addresses"` Nats map[string]string `json:"nats" bson:"nats"` } environmentIPAddress struct { Devices []environmentDevices `json:"devices" bson:"devices"` } environmentDeviceList struct { Devices []int `json:"devices" bson:"devices"` } migratedVlansItem struct { SourceVlan string `json:"source_vlan" bson:"source_vlan"` SourceZone string `json:"source_zone" bson:"source_zone"` DestinationVlan string `json:"destination_vlan" bson:"destination_vlan"` DestinationZone string `json:"destination_zone" bson:"destination_zone"` } ) // EnvironmentConfig method will return the Environment information for a given // configuration number. // // See: https://pages.github.rackspace.com/dcx/gateway/#/configurations/environment func (a *Client) EnvironmentConfig(cfg string) (*DeviceEnvInfo, error) { body, err := a.get("/configurations/"+cfg+"/environment.json", nil) if err != nil { return nil, err } var d DeviceEnvironment if err = json.Unmarshal(body, &d); err != nil { return nil, err } return d.Environment, nil } // IPBlockCIDR method will return the public and private IP blocks associated with a given // environment. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getEnvironmentIpBlockCidrs func (a *Client) IPBlockCIDR(env string) (*IPBlockCIDR, error) { path := "/environments/" + env + "/ip_block_cidrs.json" body, err := a.get(path, nil) if err != nil { return nil, err } var ibc IPBlockCIDR if err = json.Unmarshal(body, &ibc); err != nil { return nil, err } return &ibc, nil } // EnvironmentAssignments method will return two list of core device numbers assigned to environment // and devices that are eligible to assign to an environment. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getAssignments func (a *Client) EnvironmentAssignments(env string) (*EnvironmentAssignments, error) { body, err := a.get(fmt.Sprintf("/environments/%s/devices/assignments.json", env), nil) if err != nil { return nil, err } var ea EnvironmentAssignments if err = json.Unmarshal(body, &ea); err != nil { return nil, err } return &ea, nil } // EnvironmentIpAddress method will list devices, their public and private IP addresses, // and NAT associations for an environment // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getDeviceIpAddresses func (a *Client) EnvironmentIpAddress(env string) (*EnvironmentIpAddress, error) { body, err := a.get(fmt.Sprintf("/environments/%s/devices/ip_addresses.json", env), nil) if err != nil { return nil, err } var tmp environmentIPAddress if err = json.Unmarshal(body, &tmp); err != nil { return nil, err } var r EnvironmentIpAddress for _, v := range tmp.Devices { v := v d := EnvironmentDevices{ DeviceNumber: v.DeviceNumber, IpAddress: &v.IPAddress, } var n []*EnvironmentNat for k := range v.Nats { n = append(n, &EnvironmentNat{ Public: k, Private: v.Nats[k], }) } d.Nats = n r.Devices = append(r.Devices, &d) } return &r, nil } // EnvironmentData method will take an environment as an argument, and do a lookup for the // gateway / edge devices for an environment // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getGatewayDevices func (a *Client) EnvironmentData(env string) (*DeviceEnvInfo, error) { body, err := a.get(fmt.Sprintf("/environments/%s/gateway_devices", env), nil) if err != nil { return nil, err } var d DeviceEnvInfo if err = json.Unmarshal(body, &d); err != nil { return nil, err } return &d, nil } // EnvironmentVlans method will return the VLANS for the specified environment given the // environment number as an input. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getEnvironmentVlans func (a *Client) EnvironmentVlans(env string) ([]*EnvironmentVlan, error) { body, err := a.get(fmt.Sprintf("/environments/%s/vlans.json", env), nil) if err != nil { return nil, err } var vlans []*EnvironmentVlan if err = json.Unmarshal(body, &vlans); err != nil { return nil, err } return vlans, nil } // EnvironmentVlansWithSecondary method will return the VLANS for the specified environment given the // environment number as an input. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getEnvironmentVlansWithSecondaryVlans func (a *Client) EnvironmentVlansWithSecondary(env string) ([]EnvironmentVlan, error) { body, err := a.get(fmt.Sprintf("/environments/%s/vlans_with_secondary_vlans", env), nil) if err != nil { return nil, err } var vlans []EnvironmentVlan if err = json.Unmarshal(body, &vlans); err != nil { return nil, err } vlanMap := make(map[int]EnvironmentVlan) for _, vlan := range vlans { vlanMap[vlan.ID] = vlan } for i, vlan := range vlans { for _, secVlan := range vlan.SecondaryVlanIDs { vlans[i].SecondaryVlans = append(vlans[i].SecondaryVlans, vlanMap[secVlan]) } } return vlans, nil } // EnvironmentAddVlan method will return the VLANS for the specified environment given the // environment number as an input. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getEnvironmentVlans func (a *Client) EnvironmentAddVlan(env string, req VlanRequest, opts map[string]string) (VlanResponse, error) { var r VlanResponse if req.Segment.NSS == "" { req.Segment.NSS = "AggExNet" } payload, err := json.Marshal(req) if err != nil { return r, err } reqURL := fmt.Sprintf("/environments/%s/vlans", env) // I hate that I'm doing this, but this is the way the post method was setup. if opts != nil { params := url.Values{} for k, v := range opts { params.Set(k, v) } reqURL += "?" + params.Encode() } body, err := a.post(reqURL, payload) if err != nil { return r, err } if err = json.Unmarshal(body, &r); err != nil { return r, err } return r, nil } // EnvironmentAutoDiscoverExnet method will update environment manager to match the vlans currently configured // on the exnet ports. DryRun is true by default, processMigrationPorts is false by default // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/autoDiscoverExnet func (a *Client) EnvironmentAutoDiscoverExnet(env string, devicesToIgnore []string, dryRun, processMigrationPorts *bool) (*EnvironmentAutoDiscover, error) { var data = map[string]interface{}{"dry_run": true, "process_migration_ports": false} if len(devicesToIgnore) != 0 { data["device_numbers_to_ignore"] = devicesToIgnore } if dryRun != nil { data["dry_run"] = dryRun } if processMigrationPorts != nil { data["process_migration_ports"] = processMigrationPorts } payload, err := json.Marshal(data) if err != nil { return nil, err } body, err := a.post(fmt.Sprintf("/environments/%s/auto_discover_exnet", env), payload) if err != nil { return nil, err } resp := new(EnvironmentAutoDiscover) err = json.Unmarshal(body, resp) return resp, err } // EnvironmentDevices returns a slice of device id's belonging to the given environment // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getEnvironmentDeviceNumbers func (a *Client) EnvironmentDevices(env string) ([]int, error) { body, err := a.get(fmt.Sprintf("/environments/%s/devices", env), nil) if err != nil { return nil, err } devices := new(environmentDeviceList) if err = json.Unmarshal(body, devices); err != nil { return nil, err } return devices.Devices, nil } // EnvironmentMigrationVlans method will return VLANs in the environment and they're // currently assigned migration VLANs. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/getMigrationVlans func (a *Client) EnvironmentMigrationVlans(env string) ([]EnvironmentMigrationVlan, error) { body, err := a.get(fmt.Sprintf("/environments/%s/migration_vlans", env), nil) if err != nil { return nil, err } var vlans []EnvironmentMigrationVlan err = json.Unmarshal(body, &vlans) return vlans, err } // EnvironmentAssignMigVlans method will create migration VLANs in the environment and return their // currently assigned and migration VLANs. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/assignMigrationVlans func (a *Client) EnvironmentAssignMigVlans(env string, devicesToIgnore []string, legacy bool, skipSharedServices bool) ([]EnvironmentMigrationVlan, error) { var data = map[string]interface{}{"legacy_port_translations": legacy, "skip_shared_services": skipSharedServices} if len(devicesToIgnore) != 0 { data["device_numbers_to_ignore"] = devicesToIgnore } payload, err := json.Marshal(data) if err != nil { return nil, err } body, err := a.post(fmt.Sprintf("/environments/%s/migration_vlans", env), payload) if err != nil { return nil, err } var vlans []EnvironmentMigrationVlan err = json.Unmarshal(body, &vlans) return vlans, err } // EnvironmentAddDevice method will add a device number to a given environment number. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/addDeviceToEnvironment func (a *Client) EnvironmentAddDevice(device int, env, dType string) error { type payloadType struct { ID int `json:"device_number" bson:"device_number"` Company string `json:"company_name" bson:"company_name"` Type string `json:"device_type" bson:"device_type"` } if dType != "virtual" && dType != "physical" { return fmt.Errorf("device type must be one of [physical,virtual]. got %s", dType) } payload, err := json.Marshal(payloadType{ID: device, Company: "Rackspace", Type: dType}) if err != nil { return err } _, err = a.post(fmt.Sprintf("/environments/%s/devices", env), payload) return err } // MigrateVlans method will migrate all VLANs assigned to the environment and VLANs // associated to devices within the environment. // // See: https://pages.github.rackspace.com/dcx/gateway/#/environments/migrateVlans func (a *Client) MigrateVlans(environment string, excludedVlans MigrateVlansPayload) (data MigratedVlans, err error) { payload, err := json.Marshal(excludedVlans) if err != nil { return } body, err := a.post(fmt.Sprintf("/environments/%s/migrate_vlans", environment), payload) if err != nil { return } err = json.Unmarshal(body, &data) return } // AddSecondaryVlan method will Lease a new ServiceNet VLAN and associate it with // the specified AggEx VLAN in the NetworkEnvironment. // // See: https://pages.github.rackspace.com/dcx/gateway/?urls.primaryName=Staging#/environments/createSecondaryVlan func (a *Client) AddSecondaryVlan(environment string, exNetVlan int) (data EnvironmentVlan, err error) { body, err := a.post(fmt.Sprintf("/environments/%s/vlans/%d/add_secondary_vlan", environment, exNetVlan), nil) if err != nil { return } err = json.Unmarshal(body, &data) return } // FindPrivateIP method is a helper method to return true if a given IP address // exist in a list of EnvironmentIPs func (ei *EnvironmentIPs) FindPrivateIP(ip string) bool { for _, i := range ei.Private { if i == ip { return true } } return false }