blob: aafc703b4efebf69600625e3653464e97eaff0cb [file] [log] [blame]
Jamie Hannaford9823bb62014-09-26 17:06:36 +02001package external
2
3import (
4 "fmt"
5
6 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11// NetworkExternal represents a decorated form of a Network with based on the
12// "external-net" extension.
13type NetworkExternal struct {
14 // UUID for the network
15 ID string `mapstructure:"id" json:"id"`
16
17 // Human-readable name for the network. Might not be unique.
18 Name string `mapstructure:"name" json:"name"`
19
20 // The administrative state of network. If false (down), the network does not forward packets.
21 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
22
23 // Indicates whether network is currently operational. Possible values include
24 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
25 Status string `mapstructure:"status" json:"status"`
26
27 // Subnets associated with this network.
28 Subnets []string `mapstructure:"subnets" json:"subnets"`
29
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
33 // Specifies whether the network resource can be accessed by any tenant or not.
34 Shared bool `mapstructure:"shared" json:"shared"`
35
36 // Specifies whether the network is an external network or not.
37 External bool `mapstructure:"router:external" json:"router:external"`
38}
39
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020040func commonExtract(e error, response map[string]interface{}) (*NetworkExternal, error) {
41 if e != nil {
42 return nil, e
Jamie Hannaford9823bb62014-09-26 17:06:36 +020043 }
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020044
Jamie Hannaford9823bb62014-09-26 17:06:36 +020045 var res struct {
46 Network *NetworkExternal `json:"network"`
47 }
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020048
49 err := mapstructure.Decode(response, &res)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020050 if err != nil {
51 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
52 }
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020053
Jamie Hannaford9823bb62014-09-26 17:06:36 +020054 return res.Network, nil
55}
56
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020057// ExtractGet decorates a GetResult struct returned from a networks.Get()
58// function with extended attributes.
59func ExtractGet(r networks.GetResult) (*NetworkExternal, error) {
60 return commonExtract(r.Err, r.Resp)
61}
62
Jamie Hannaford9823bb62014-09-26 17:06:36 +020063// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
64// function with extended attributes.
65func ExtractCreate(r networks.CreateResult) (*NetworkExternal, error) {
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020066 return commonExtract(r.Err, r.Resp)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020067}
68
69// ExtractUpdate decorates a UpdateResult struct returned from a
70// networks.Update() function with extended attributes.
71func ExtractUpdate(r networks.UpdateResult) (*NetworkExternal, error) {
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020072 return commonExtract(r.Err, r.Resp)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020073}
74
75// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
76// extracts the elements into a slice of NetworkExtAttrs structs. In other
77// words, a generic collection is mapped into a relevant slice.
78func ExtractList(page pagination.Page) ([]NetworkExternal, error) {
79 var resp struct {
80 Networks []NetworkExternal `mapstructure:"networks" json:"networks"`
81 }
82
83 err := mapstructure.Decode(page.(networks.NetworkPage).Body, &resp)
84 if err != nil {
85 return nil, err
86 }
87
88 return resp.Networks, nil
89}