blob: 6818fcbfe4052423d3e40ffcbb561b4d5262f533 [file] [log] [blame]
Jon Perrittf95e3e42014-10-21 21:11:25 -05001package virtualinterfaces
Jon Perritt44b1ea22014-10-22 00:13:23 -05002
3import (
4 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9type commonResult struct {
10 gophercloud.Result
11}
12
13// Extract is a function that accepts a result and extracts a network resource.
14func (r commonResult) Extract() (*VirtualInterface, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var res struct {
20 VirtualInterfaces []VirtualInterface `mapstructure:"virtual_interfaces" json:"virtual_interfaces"`
21 }
22
23 err := mapstructure.Decode(r.Body, &res)
24
25 return &res.VirtualInterfaces[0], err
26}
27
28// CreateResult represents the result of a create operation.
29type CreateResult struct {
30 commonResult
31}
32
33// DeleteResult represents the result of a delete operation.
34type DeleteResult commonResult
35
36// IPAddress represents a vitual address attached to a VirtualInterface.
37type IPAddress struct {
38 Address string `mapstructure:"address" json:"address"`
39 NetworkID string `mapstructure:"network_id" json:"network_id"`
40 NetworkLabel string `mapstructure:"network_label" json:"network_label"`
41}
42
43// VirtualInterface represents a virtual interface.
44type VirtualInterface struct {
45 // UUID for the virtual interface
46 ID string `mapstructure:"id" json:"id"`
47
48 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
49
50 IPAddresses []IPAddress `mapstructure:"ip_addresses" json:"ip_addresses"`
51}
52
53// VirtualInterfacePage is the page returned by a pager when traversing over a
54// collection of virtual interfaces.
55type VirtualInterfacePage struct {
56 pagination.SinglePageBase
57}
58
59// IsEmpty returns true if the NetworkPage contains no Networks.
60func (r VirtualInterfacePage) IsEmpty() (bool, error) {
61 networks, err := ExtractVirtualInterfaces(r)
62 if err != nil {
63 return true, err
64 }
65 return len(networks) == 0, nil
66}
67
68// ExtractVirtualInterfaces accepts a Page struct, specifically a VirtualInterfacePage struct,
69// and extracts the elements into a slice of VirtualInterface structs. In other words,
70// a generic collection is mapped into a relevant slice.
71func ExtractVirtualInterfaces(page pagination.Page) ([]VirtualInterface, error) {
72 var resp struct {
73 VirtualInterfaces []VirtualInterface `mapstructure:"virtual_interfaces" json:"virtual_interfaces"`
74 }
75
76 err := mapstructure.Decode(page.(VirtualInterfacePage).Body, &resp)
77
78 return resp.VirtualInterfaces, err
79}