blob: f4374618d49ad5dc0f94f41342f59b333e7041dc [file] [log] [blame]
Jamie Hannafordb0d99122014-09-25 10:49:14 +02001package provider
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
5 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannafordb0d99122014-09-25 10:49:14 +02006)
7
Jamie Hannafordc7c49a72014-10-06 15:49:53 +02008// AdminState gives users a solid type to work with for create and update
9// operations. It is recommended that users use the `Up` and `Down` enums.
Jamie Hannaforda241e312014-10-01 16:54:33 +020010type AdminState *bool
11
12// Convenience vars for AdminStateUp values.
13var (
14 iTrue = true
15 iFalse = false
16
Jamie Hannafordc7c49a72014-10-06 15:49:53 +020017 Up AdminState = &iTrue
18 Down AdminState = &iFalse
Jamie Hannaforda241e312014-10-01 16:54:33 +020019)
20
Jamie Hannafordb0d99122014-09-25 10:49:14 +020021// NetworkExtAttrs represents an extended form of a Network with additional fields.
22type NetworkExtAttrs struct {
23 // UUID for the network
Jon Perritt3c166472016-02-25 03:07:41 -060024 ID string `json:"id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020025
26 // Human-readable name for the network. Might not be unique.
Jon Perritt3c166472016-02-25 03:07:41 -060027 Name string `json:"name"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020028
29 // The administrative state of network. If false (down), the network does not forward packets.
Jon Perritt3c166472016-02-25 03:07:41 -060030 AdminStateUp bool `json:"admin_state_up"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020031
32 // Indicates whether network is currently operational. Possible values include
33 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
Jon Perritt3c166472016-02-25 03:07:41 -060034 Status string `json:"status"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020035
36 // Subnets associated with this network.
Jon Perritt3c166472016-02-25 03:07:41 -060037 Subnets []string `json:"subnets"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020038
39 // Owner of network. Only admin users can specify a tenant_id other than its own.
Jon Perritt3c166472016-02-25 03:07:41 -060040 TenantID string `json:"tenant_id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020041
42 // Specifies whether the network resource can be accessed by any tenant or not.
Jon Perritt3c166472016-02-25 03:07:41 -060043 Shared bool `json:"shared"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020044
45 // Specifies the nature of the physical network mapped to this network
46 // resource. Examples are flat, vlan, or gre.
Jon Perritt3c166472016-02-25 03:07:41 -060047 NetworkType string `json:"provider:network_type"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020048
49 // Identifies the physical network on top of which this network object is
50 // being implemented. The OpenStack Networking API does not expose any facility
51 // for retrieving the list of available physical networks. As an example, in
52 // the Open vSwitch plug-in this is a symbolic name which is then mapped to
53 // specific bridges on each compute host through the Open vSwitch plug-in
54 // configuration file.
Jon Perritt3c166472016-02-25 03:07:41 -060055 PhysicalNetwork string `json:"provider:physical_network"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020056
57 // Identifies an isolated segment on the physical network; the nature of the
58 // segment depends on the segmentation model defined by network_type. For
59 // instance, if network_type is vlan, then this is a vlan identifier;
60 // otherwise, if network_type is gre, then this will be a gre key.
Jon Perritt3c166472016-02-25 03:07:41 -060061 SegmentationID string `json:"provider:segmentation_id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020062}
63
Jamie Hannaford5a531902014-09-26 15:03:31 +020064// ExtractGet decorates a GetResult struct returned from a networks.Get()
65// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020066func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060067 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +020068 Network *NetworkExtAttrs `json:"network"`
69 }
Jon Perritt3c166472016-02-25 03:07:41 -060070 err := r.ExtractInto(&s)
71 return s.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020072}
73
Jamie Hannafordc7c49a72014-10-06 15:49:53 +020074// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
Jamie Hannaford5a531902014-09-26 15:03:31 +020075// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020076func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060077 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +020078 Network *NetworkExtAttrs `json:"network"`
79 }
Jon Perritt3c166472016-02-25 03:07:41 -060080 err := r.ExtractInto(&s)
81 return s.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020082}
83
Jamie Hannaford5a531902014-09-26 15:03:31 +020084// ExtractUpdate decorates a UpdateResult struct returned from a
85// networks.Update() function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020086func ExtractUpdate(r networks.UpdateResult) (*NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060087 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +020088 Network *NetworkExtAttrs `json:"network"`
89 }
Jon Perritt3c166472016-02-25 03:07:41 -060090 err := r.ExtractInto(&s)
91 return s.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020092}
93
94// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
95// extracts the elements into a slice of NetworkExtAttrs structs. In other
96// words, a generic collection is mapped into a relevant slice.
97func ExtractList(page pagination.Page) ([]NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060098 r := page.(networks.NetworkPage)
99 var s struct {
100 Networks []NetworkExtAttrs `json:"networks" json:"networks"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200101 }
Jon Perritt3c166472016-02-25 03:07:41 -0600102 err := r.ExtractInto(&s)
103 return s.Networks, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200104}