package goctk import ( "encoding/json" "fmt" ) type ( // IPAssignmentResp defines a response to a direct query of an attribute IPAssignmentResp struct { PlatformName string `json:"computer.platform_name"` PlatformType string `json:"computer.platform_type"` PlatformModel string `json:"computer.platform_model"` DeviceID int `json:"computer.number"` Status string `json:"computer.status.name"` DC string `json:"computer.datacenter.symbol"` Name string `json:"computer.name"` } ) // IPAssignment takes an ip address and returns CORE data about the address func (c *Client) IPAssignment(ipAddr string) (IPAssignmentResp, error) { var ( // Make the return response data ipAssignments IPAssignmentResp // Struct to unmarshal the CTK response into. Will format the // "result" field into json.RawMessage res coreResponse logBase = "goctk:IPAssignment:%s" ) t := map[string]interface{}{ "class": "IPSpace.IPAssignment", "load_arg": map[string]string{"ip_address": ipAddr}, "load_method": "loadList", "attributes": []string{ "computer.number", "computer.platform_name", "computer.platform_type", "computer.platform_model", "computer.status.name", "computer.datacenter.symbol", "computer.name", }, } // Marshal the query data to send to CTK js, err := json.Marshal(t) if err != nil { return ipAssignments, fmt.Errorf(logBase, err) } // Perform a CTK query method ctkResponse, err := c.Query(js) if err != nil { return ipAssignments, fmt.Errorf(logBase, err) } // Unmarshal the response into the res struct json.Unmarshal(ctkResponse, &res) if len(res) != 1 { return ipAssignments, fmt.Errorf(logBase, "invalid ctk response") } if len(res[0].Result) == 0 { return ipAssignments, fmt.Errorf(logBase, "no data found for "+ipAddr) } // Unmarshal the result into the response for IPAssignments. The parent coreResponse brings the // Result in as a RawMessage, so we have to unmarshal it here. if err = json.Unmarshal(res[0].Result[0], &ipAssignments); err != nil { return ipAssignments, fmt.Errorf(logBase, err) } return ipAssignments, nil }