blob: 14c7a47adba85df99aefeaf07619ff55ced5d835 [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.
Jamie Hannafordb65675f2014-10-27 14:03:28 +010034type DeleteResult struct {
35 gophercloud.ExtractErrResult
36}
Jon Perritt44b1ea22014-10-22 00:13:23 -050037
38// IPAddress represents a vitual address attached to a VirtualInterface.
39type IPAddress struct {
40 Address string `mapstructure:"address" json:"address"`
41 NetworkID string `mapstructure:"network_id" json:"network_id"`
42 NetworkLabel string `mapstructure:"network_label" json:"network_label"`
43}
44
45// VirtualInterface represents a virtual interface.
46type VirtualInterface struct {
47 // UUID for the virtual interface
48 ID string `mapstructure:"id" json:"id"`
49
50 MACAddress string `mapstructure:"mac_address" json:"mac_address"`
51
52 IPAddresses []IPAddress `mapstructure:"ip_addresses" json:"ip_addresses"`
53}
54
55// VirtualInterfacePage is the page returned by a pager when traversing over a
56// collection of virtual interfaces.
57type VirtualInterfacePage struct {
58 pagination.SinglePageBase
59}
60
61// IsEmpty returns true if the NetworkPage contains no Networks.
62func (r VirtualInterfacePage) IsEmpty() (bool, error) {
63 networks, err := ExtractVirtualInterfaces(r)
64 if err != nil {
65 return true, err
66 }
67 return len(networks) == 0, nil
68}
69
70// ExtractVirtualInterfaces accepts a Page struct, specifically a VirtualInterfacePage struct,
71// and extracts the elements into a slice of VirtualInterface structs. In other words,
72// a generic collection is mapped into a relevant slice.
73func ExtractVirtualInterfaces(page pagination.Page) ([]VirtualInterface, error) {
74 var resp struct {
75 VirtualInterfaces []VirtualInterface `mapstructure:"virtual_interfaces" json:"virtual_interfaces"`
76 }
77
78 err := mapstructure.Decode(page.(VirtualInterfacePage).Body, &resp)
79
80 return resp.VirtualInterfaces, err
81}