//nolint:golint package dcxAutohost import ( "encoding/json" "net" "time" ) type ( // IPBlock is the type that represents the output from the top_level_public_ip_blocks // DCX API call. IPBlock struct { IPBlockInfo struct { ASNumber int `json:"autonomous_system_number,omitempty" bson:"autonomous_system_number,omitempty"` ASName string `json:"autonomous_system_name,omitempty" bson:"autonomous_system_name,omitempty"` CIDR string `json:"cidr" bson:"cidr"` ipNet *net.IPNet } `json:"block" bson:"block"` } // BlockSpec is the request type that is sent to the DCX API for the PublicIPBlocksCarve method BlockSpec struct { AccountNumber string `json:"account_number" bson:"account_number"` Description string `json:"description" bson:"description"` CIDR string `json:"cidr" bson:"cidr"` Environment string `json:"environment_number" bson:"environment_number"` PolicyName string `json:"policy_name" bson:"policy_name"` Override bool `json:"override" bson:"override"` } // BlockCarveResp is the response type returned from the PublicIPBlocksCarve method BlockCarveResp struct { PrefixLength int `json:"prefix_length"` PolicyID int `json:"policy_id"` OwnerNumber string `json:"owner_number"` CreatedAt time.Time `json:"created_at"` AutomaticallySubnettable bool `json:"automatically_subnettable"` AutoDelegable bool `json:"auto_delegable"` UpdatedAt time.Time `json:"updated_at"` Layer3BlockID string `json:"layer3_block_id"` RootID int `json:"root_id"` ID int `json:"id"` DisableSwip bool `json:"disable_swip"` NoNat bool `json:"no_nat"` LastFoundAt time.Time `json:"last_found_at"` AnycastChildrenCount int `json:"anycast_children_count"` ShortCidr string `json:"short_cidr"` NetworkEnvironmentID int `json:"network_environment_id"` AutonomousSystemID int `json:"autonomous_system_id"` AutoSubDelegable bool `json:"auto_sub_delegable"` Address string `json:"address"` ParentID int `json:"parent_id"` Anycastable bool `json:"anycastable"` Undelegable bool `json:"undelegable"` Description string `json:"description"` IPBlockType string `json:"ip_block_type"` Locked bool `json:"locked"` Cidr string `json:"cidr"` Routable bool `json:"routable"` NetworkEnvironmentNumber string `json:"network_environment_number"` } ) // PublicIPBlocks method will return a PublicIPBlocks type that will comprise of a // list of all top level public ip blocks // // See: https://pages.github.rackspace.com/dcx/gateway/#/top_level_public_ip_blocks/getTopLevelPublicIpBlocksXml func (a *Client) PublicIPBlocks() ([]IPBlock, error) { var res []IPBlock body, err := a.get("/top_level_public_ip_blocks.json", nil) if err != nil { return res, err } if err = json.Unmarshal(body, &res); err != nil { return res, err } return res, nil } // PublicIPBlocks method will return a PublicIPBlocks type that will comprise of a // list of all top level public ip blocks // // See: https://pages.github.rackspace.com/dcx/gateway/#/public_ip_blocks/carve func (a *Client) PublicIPBlocksCarve(spec BlockSpec) (BlockCarveResp, error) { r := make([]BlockCarveResp, 0) req := struct { Spec []BlockSpec `json:"block_specifications" bson:"block_specifications"` }{Spec: []BlockSpec{spec}} payload, err := json.Marshal(&req) if err != nil { return BlockCarveResp{}, err } body, err := a.post("/public_ip_blocks/carve", payload) if err != nil { return BlockCarveResp{}, err } if err = json.Unmarshal(body, &r); err != nil { return BlockCarveResp{}, err } return r[0], nil }