blob: 7b647fe27622bbe19f45bb09bdab43e2d359000a [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannafordd2d9f562014-09-15 15:35:07 +02004 "encoding/json"
5 "fmt"
6
Jamie Hannaford01e14922014-09-11 15:23:49 +02007 "github.com/racker/perigee"
8 "github.com/rackspace/gophercloud"
9)
10
Jamie Hannaford01e14922014-09-11 15:23:49 +020011func APIVersions(c *gophercloud.ServiceClient) (*APIVersionsList, error) {
Jamie Hannaford01e14922014-09-11 15:23:49 +020012 var resp APIVersionsList
Jamie Hannafordf14d4562014-09-11 17:46:18 +020013 _, err := perigee.Request("GET", APIVersionsURL(c), perigee.Options{
14 MoreHeaders: c.Provider.AuthenticatedHeaders(),
15 Results: &resp,
16 OkCodes: []int{200},
17 })
18 if err != nil {
19 return nil, err
20 }
21
22 return &resp, nil
23}
24
25func APIInfo(c *gophercloud.ServiceClient, v string) (*APIInfoList, error) {
26 var resp APIInfoList
27 _, err := perigee.Request("GET", APIInfoURL(c, v), perigee.Options{
Jamie Hannaford01e14922014-09-11 15:23:49 +020028 MoreHeaders: c.Provider.AuthenticatedHeaders(),
29 Results: &resp,
30 OkCodes: []int{200},
31 })
32 if err != nil {
33 return nil, err
34 }
35
36 return &resp, nil
37}
Jamie Hannaford12bc2472014-09-15 12:14:31 +020038
39func GetExtension(c *gophercloud.ServiceClient, name string) (*Extension, error) {
40 var ext Extension
41 _, err := perigee.Request("GET", ExtensionURL(c, name), perigee.Options{
42 MoreHeaders: c.Provider.AuthenticatedHeaders(),
43 Results: &struct {
44 Extension *Extension `json:"extension"`
45 }{&ext},
46 OkCodes: []int{200},
47 })
48
49 if err != nil {
50 return nil, err
51 }
52 return &ext, nil
53}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020054
55func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
56 var n Network
57 _, err := perigee.Request("GET", NetworkURL(c, id), perigee.Options{
58 MoreHeaders: c.Provider.AuthenticatedHeaders(),
59 Results: &struct {
60 Network *Network `json:"network"`
61 }{&n},
62 OkCodes: []int{200},
63 })
64 if err != nil {
65 return nil, err
66 }
67 return &n, nil
68}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020069
70type NetworkOpts struct {
71 AdminStateUp bool
72 Name string
73 Shared *bool
74 TenantID string
75}
76
77type NetworkProvider struct {
78 ProviderSegmentationID int `json:"provider:segmentation_id"`
79 ProviderPhysicalNetwork string `json:"provider:physical_network"`
80 ProviderNetworkType string `json:"provider:network_type"`
81}
82
83type NetworkResult struct {
84 Status string `json:"status"`
85 Subnets []interface{} `json:"subnets"`
86 Name string `json:"name"`
87 AdminStateUp bool `json:"admin_state_up"`
88 TenantID string `json:"tenant_id"`
89 Segments []NetworkProvider `json:"segments"`
90 Shared bool `json:"shared"`
91 PortSecurityEnabled bool `json:"port_security_enabled"`
92 ID string `json:"id"`
93}
94
95func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkResult, error) {
96 // Define structures
97 type network struct {
98 AdminStateUp bool `json:"admin_state_up"`
99 Name string `json:"name"`
100 Shared *bool `json:"shared,omitempty"`
101 TenantID *string `json:"tenant_id,omitempty"`
102 }
103 type request struct {
104 Network network `json:"network"`
105 }
106 type response struct {
107 Network *NetworkResult `json:"network"`
108 }
109
110 // Validate
111 if opts.Name == "" {
112 return nil, ErrNameRequired
113 }
114
115 // Populate request body
116 reqBody := request{Network: network{
117 AdminStateUp: opts.AdminStateUp,
118 Name: opts.Name,
119 Shared: opts.Shared,
120 }}
121
122 if opts.TenantID != "" {
123 reqBody.Network.TenantID = &opts.TenantID
124 }
125
126 j, _ := json.Marshal(reqBody)
127 fmt.Println(string(j))
128
129 // Send request to API
130 var res response
131 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
132 MoreHeaders: c.Provider.AuthenticatedHeaders(),
133 ReqBody: &reqBody,
134 Results: &res,
135 OkCodes: []int{201},
136 })
137 if err != nil {
138 return nil, err
139 }
140
141 return res.Network, nil
142}