| Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 1 | package ports | 
 | 2 |  | 
 | 3 | import ( | 
 | 4 | 	"github.com/mitchellh/mapstructure" | 
 | 5 | 	"github.com/rackspace/gophercloud/pagination" | 
 | 6 | ) | 
 | 7 |  | 
 | 8 | type IP struct { | 
 | 9 | 	SubnetID  string `mapstructure:"subnet_id" json:"subnet_id"` | 
| Jamie Hannaford | 2a0492a | 2014-09-22 12:02:11 +0200 | [diff] [blame] | 10 | 	IPAddress string `mapstructure:"ip_address" json:"ip_address,omitempty"` | 
| Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 11 | } | 
 | 12 |  | 
 | 13 | type Port struct { | 
| Jamie Hannaford | 965ae70 | 2014-09-22 14:58:19 +0200 | [diff] [blame^] | 14 | 	// UUID for the port. | 
 | 15 | 	ID string `mapstructure:"id" json:"id"` | 
 | 16 | 	// Network that this port is associated with. | 
 | 17 | 	NetworkID string `mapstructure:"network_id" json:"network_id"` | 
 | 18 | 	// Human-readable name for the port. Might not be unique. | 
 | 19 | 	Name string `mapstructure:"name" json:"name"` | 
 | 20 | 	// Administrative state of port. If false (down), port does not forward packets. | 
 | 21 | 	AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"` | 
 | 22 | 	// Indicates whether network is currently operational. Possible values include | 
 | 23 | 	// `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values. | 
 | 24 | 	Status string `mapstructure:"status" json:"status"` | 
 | 25 | 	// Mac address to use on this port. | 
 | 26 | 	MACAddress string `mapstructure:"mac_address" json:"mac_address"` | 
 | 27 | 	// Specifies IP addresses for the port thus associating the port itself with | 
 | 28 | 	// the subnets where the IP addresses are picked from | 
 | 29 | 	FixedIPs []IP `mapstructure:"fixed_ips" json:"fixed_ips"` | 
 | 30 | 	// Owner of network. Only admin users can specify a tenant_id other than its own. | 
 | 31 | 	TenantID string `mapstructure:"tenant_id" json:"tenant_id"` | 
 | 32 | 	// Identifies the entity (e.g.: dhcp agent) using this port. | 
 | 33 | 	DeviceOwner string `mapstructure:"device_owner" json:"device_owner"` | 
 | 34 | 	// Specifies the IDs of any security groups associated with a port. | 
 | 35 | 	SecurityGroups []string `mapstructure:"security_groups" json:"security_groups"` | 
 | 36 | 	// Identifies the device (e.g., virtual server) using this port. | 
 | 37 | 	DeviceID string `mapstructure:"device_id" json:"device_id"` | 
 | 38 |  | 
| Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 39 | 	AllowedAddressPairs []interface{} `mapstructure:"allowed" json:"allowed"` | 
| Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 40 | 	ExtraDHCPOpts       interface{}   `mapstructure:"extra_dhcp_opts" json:"extra_dhcp_opts"` | 
| Jamie Hannaford | 548d340 | 2014-09-18 15:50:08 +0200 | [diff] [blame] | 41 | 	BindingHostID       string        `mapstructure:"binding:host_id" json:"binding:host_id"` | 
 | 42 | 	BindingVIFDetails   interface{}   `mapstructure:"binding:vif_details" json:"binding:vif_details"` | 
 | 43 | 	BindingVIFType      string        `mapstructure:"binding:vif_type" json:"binding:vif_type"` | 
 | 44 | 	BindingProfile      interface{}   `mapstructure:"binding:profile" json:"binding:profile"` | 
 | 45 | 	BindingVNICType     string        `mapstructure:"binding:vnic_type" json:"binding:vnic_type"` | 
 | 46 | } | 
 | 47 |  | 
 | 48 | type PortPage struct { | 
 | 49 | 	pagination.LinkedPageBase | 
 | 50 | } | 
 | 51 |  | 
 | 52 | func (current PortPage) NextPageURL() (string, error) { | 
 | 53 | 	type resp struct { | 
 | 54 | 		Links []struct { | 
 | 55 | 			Href string `mapstructure:"href"` | 
 | 56 | 			Rel  string `mapstructure:"rel"` | 
 | 57 | 		} `mapstructure:"ports_links"` | 
 | 58 | 	} | 
 | 59 |  | 
 | 60 | 	var r resp | 
 | 61 | 	err := mapstructure.Decode(current.Body, &r) | 
 | 62 | 	if err != nil { | 
 | 63 | 		return "", err | 
 | 64 | 	} | 
 | 65 |  | 
 | 66 | 	var url string | 
 | 67 | 	for _, l := range r.Links { | 
 | 68 | 		if l.Rel == "next" { | 
 | 69 | 			url = l.Href | 
 | 70 | 		} | 
 | 71 | 	} | 
 | 72 | 	if url == "" { | 
 | 73 | 		return "", nil | 
 | 74 | 	} | 
 | 75 |  | 
 | 76 | 	return url, nil | 
 | 77 | } | 
 | 78 |  | 
 | 79 | func (r PortPage) IsEmpty() (bool, error) { | 
 | 80 | 	is, err := ExtractPorts(r) | 
 | 81 | 	if err != nil { | 
 | 82 | 		return true, nil | 
 | 83 | 	} | 
 | 84 | 	return len(is) == 0, nil | 
 | 85 | } | 
 | 86 |  | 
 | 87 | func ExtractPorts(page pagination.Page) ([]Port, error) { | 
 | 88 | 	var resp struct { | 
 | 89 | 		Ports []Port `mapstructure:"ports" json:"ports"` | 
 | 90 | 	} | 
 | 91 |  | 
 | 92 | 	err := mapstructure.Decode(page.(PortPage).Body, &resp) | 
 | 93 | 	if err != nil { | 
 | 94 | 		return nil, err | 
 | 95 | 	} | 
 | 96 |  | 
 | 97 | 	return resp.Ports, nil | 
 | 98 | } |