// getNetworkMACs returns all MAC addresses connected to the given // network. func (environ *maasEnviron) getNetworkMACs(networkName string) ([]string, error) { client := environ.getMAASClient().GetSubObject("networks").GetSubObject(networkName) json, err := client.CallGet("list_connected_macs", nil) if err != nil { return nil, err } jsonMACs, err := json.GetArray() if err != nil { return nil, err } macs := make([]string, len(jsonMACs)) for i, jsonMAC := range jsonMACs { fields, err := jsonMAC.GetMap() if err != nil { return nil, err } macAddress, err := fields["mac_address"].GetString() if err != nil { return nil, fmt.Errorf("cannot get mac_address: %v", err) } macs[i] = macAddress } return macs, nil }
// listConnectedMacs calls the MAAS list_connected_macs API to fetch all the // the MAC addresses attached to a specific network. func (environ *maasEnviron) listConnectedMacs(network networkDetails) ([]string, error) { client := environ.getMAASClient().GetSubObject("networks").GetSubObject(network.Name) json, err := client.CallGet("list_connected_macs", nil) if err != nil { return nil, err } macs, err := json.GetArray() if err != nil { return nil, err } result := []string{} for _, macObj := range macs { macMap, err := macObj.GetMap() if err != nil { return nil, err } mac, err := macMap["mac_address"].GetString() if err != nil { return nil, err } result = append(result, mac) } return result, nil }
// getInstanceNetworks returns a list of all MAAS networks for a given node. func (environ *maasEnviron) getInstanceNetworks(inst instance.Instance) ([]networkDetails, error) { nodeId, err := environ.nodeIdFromInstance(inst) if err != nil { return nil, err } client := environ.getMAASClient().GetSubObject("networks") params := url.Values{"node": {nodeId}} json, err := client.CallGet("", params) if err != nil { return nil, err } jsonNets, err := json.GetArray() if err != nil { return nil, err } networks := make([]networkDetails, len(jsonNets)) for i, jsonNet := range jsonNets { fields, err := jsonNet.GetMap() if err != nil { return nil, errors.Annotatef(err, "parsing network details") } name, err := fields["name"].GetString() if err != nil { return nil, errors.Annotatef(err, "cannot get name") } ip, err := fields["ip"].GetString() if err != nil { return nil, errors.Annotatef(err, "cannot get ip") } defaultGateway := "" defaultGatewayField, ok := fields["default_gateway"] if ok && !defaultGatewayField.IsNil() { // default_gateway is optional, so ignore it when unset or // null. defaultGateway, err = defaultGatewayField.GetString() if err != nil { return nil, errors.Annotatef(err, "cannot get default_gateway") } } netmask, err := fields["netmask"].GetString() if err != nil { return nil, errors.Annotatef(err, "cannot get netmask") } vlanTag := 0 vlanTagField, ok := fields["vlan_tag"] if ok && !vlanTagField.IsNil() { // vlan_tag is optional, so assume it's 0 when missing or nil. vlanTagFloat, err := vlanTagField.GetFloat64() if err != nil { return nil, errors.Annotatef(err, "cannot get vlan_tag") } vlanTag = int(vlanTagFloat) } description, err := fields["description"].GetString() if err != nil { return nil, fmt.Errorf("cannot get description: %v", err) } networks[i] = networkDetails{ Name: name, IP: ip, Mask: netmask, DefaultGateway: defaultGateway, VLANTag: vlanTag, Description: description, } } return networks, nil }