blob: c5d5f14417add6421bcf4bd824cf10922a3e9f58 [file] [log] [blame]
Jamie Hannaford9823bb62014-09-26 17:06:36 +02001package external
2
3import (
Jamie Hannaford9823bb62014-09-26 17:06:36 +02004 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06005 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
6 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaford9823bb62014-09-26 17:06:36 +02007)
8
9// NetworkExternal represents a decorated form of a Network with based on the
10// "external-net" extension.
11type NetworkExternal struct {
12 // UUID for the network
13 ID string `mapstructure:"id" json:"id"`
14
15 // Human-readable name for the network. Might not be unique.
16 Name string `mapstructure:"name" json:"name"`
17
18 // The administrative state of network. If false (down), the network does not forward packets.
19 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
20
21 // Indicates whether network is currently operational. Possible values include
22 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
23 Status string `mapstructure:"status" json:"status"`
24
25 // Subnets associated with this network.
26 Subnets []string `mapstructure:"subnets" json:"subnets"`
27
28 // Owner of network. Only admin users can specify a tenant_id other than its own.
29 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
30
31 // Specifies whether the network resource can be accessed by any tenant or not.
32 Shared bool `mapstructure:"shared" json:"shared"`
33
34 // Specifies whether the network is an external network or not.
35 External bool `mapstructure:"router:external" json:"router:external"`
36}
37
Ash Wilsond3dc2542014-10-20 10:10:48 -040038func commonExtract(e error, response interface{}) (*NetworkExternal, error) {
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020039 if e != nil {
40 return nil, e
Jamie Hannaford9823bb62014-09-26 17:06:36 +020041 }
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020042
Jamie Hannaford9823bb62014-09-26 17:06:36 +020043 var res struct {
44 Network *NetworkExternal `json:"network"`
45 }
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020046
47 err := mapstructure.Decode(response, &res)
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020048
Jamie Hannafordc1f36492014-10-08 15:48:48 +020049 return res.Network, err
Jamie Hannaford9823bb62014-09-26 17:06:36 +020050}
51
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020052// ExtractGet decorates a GetResult struct returned from a networks.Get()
53// function with extended attributes.
54func ExtractGet(r networks.GetResult) (*NetworkExternal, error) {
Ash Wilsond3dc2542014-10-20 10:10:48 -040055 return commonExtract(r.Err, r.Body)
Jamie Hannaford91cd64e2014-10-08 13:08:52 +020056}
57
Jamie Hannaford9823bb62014-09-26 17:06:36 +020058// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
59// function with extended attributes.
60func ExtractCreate(r networks.CreateResult) (*NetworkExternal, error) {
Ash Wilsond3dc2542014-10-20 10:10:48 -040061 return commonExtract(r.Err, r.Body)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020062}
63
64// ExtractUpdate decorates a UpdateResult struct returned from a
65// networks.Update() function with extended attributes.
66func ExtractUpdate(r networks.UpdateResult) (*NetworkExternal, error) {
Ash Wilsond3dc2542014-10-20 10:10:48 -040067 return commonExtract(r.Err, r.Body)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020068}
69
70// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
Jon Perrittb0b9c0d2015-03-10 17:24:08 -060071// extracts the elements into a slice of NetworkExternal structs. In other
Jamie Hannaford9823bb62014-09-26 17:06:36 +020072// words, a generic collection is mapped into a relevant slice.
73func ExtractList(page pagination.Page) ([]NetworkExternal, error) {
74 var resp struct {
75 Networks []NetworkExternal `mapstructure:"networks" json:"networks"`
76 }
77
78 err := mapstructure.Decode(page.(networks.NetworkPage).Body, &resp)
Jamie Hannaford9823bb62014-09-26 17:06:36 +020079
Jamie Hannafordc1f36492014-10-08 15:48:48 +020080 return resp.Networks, err
Jamie Hannaford9823bb62014-09-26 17:06:36 +020081}