blob: 578254311f14f5727fd32b876b55faaeafa174b2 [file] [log] [blame]
Jamie Hannaforda7f671a2014-09-11 10:25:08 +02001package networks
2
Jamie Hannaford01e14922014-09-11 15:23:49 +02003import (
Jamie Hannaford4721abc2014-09-16 16:29:04 +02004 "strconv"
5
Jamie Hannaford01e14922014-09-11 15:23:49 +02006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
Jamie Hannaford4721abc2014-09-16 16:29:04 +02008 "github.com/rackspace/gophercloud/openstack/utils"
Jamie Hannafordf0c615b2014-09-17 10:56:52 +02009 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford01e14922014-09-11 15:23:49 +020010)
11
Jamie Hannaford4721abc2014-09-16 16:29:04 +020012type ListOpts struct {
13 Status string
14 Name string
15 AdminStateUp *bool
16 TenantID string
17 Shared *bool
18 ID string
19 Page int
20 PerPage int
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020021 Limit int
Jamie Hannaford4721abc2014-09-16 16:29:04 +020022}
23
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020024type NetworkOpts struct {
25 AdminStateUp bool
26 Name string
27 Shared *bool
28 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020029}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020030
Jamie Hannaford4721abc2014-09-16 16:29:04 +020031func ptrToStr(val *bool) string {
32 if *val == true {
33 return "true"
34 } else if *val == false {
35 return "false"
36 } else {
37 return ""
38 }
39}
40
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020041func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020042 // Build query parameters
43 q := make(map[string]string)
44 if opts.Status != "" {
45 q["status"] = opts.Status
46 }
47 if opts.Name != "" {
48 q["name"] = opts.Name
49 }
50 if opts.AdminStateUp != nil {
51 q["admin_state_up"] = ptrToStr(opts.AdminStateUp)
52 }
53 if opts.TenantID != "" {
54 q["tenant_id"] = opts.TenantID
55 }
56 if opts.Shared != nil {
57 q["shared"] = ptrToStr(opts.Shared)
58 }
59 if opts.ID != "" {
60 q["id"] = opts.ID
61 }
62 if opts.Page != 0 {
63 q["page"] = strconv.Itoa(opts.Page)
64 }
65 if opts.PerPage != 0 {
66 q["per_page"] = strconv.Itoa(opts.PerPage)
67 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020068 if opts.Limit != 0 {
69 q["limit"] = strconv.Itoa(opts.Limit)
70 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020071
72 u := ListURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020073 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
74 return NetworkPage{pagination.LinkedPageBase(r)}
75 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020076}
77
78func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
79 var n Network
80 _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020081 MoreHeaders: c.Provider.AuthenticatedHeaders(),
82 Results: &struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020083 Network *Network `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020084 }{&n},
85 OkCodes: []int{200},
86 })
87 if err != nil {
88 return nil, err
89 }
90 return &n, nil
91}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020092
Jamie Hannaford79475052014-09-15 17:08:06 +020093func Create(c *gophercloud.ServiceClient, opts NetworkOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +020094 // Define structures
95 type network struct {
96 AdminStateUp bool `json:"admin_state_up"`
97 Name string `json:"name"`
98 Shared *bool `json:"shared,omitempty"`
99 TenantID *string `json:"tenant_id,omitempty"`
100 }
101 type request struct {
102 Network network `json:"network"`
103 }
104 type response struct {
Jamie Hannaford79475052014-09-15 17:08:06 +0200105 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200106 }
107
108 // Validate
109 if opts.Name == "" {
110 return nil, ErrNameRequired
111 }
112
113 // Populate request body
114 reqBody := request{Network: network{
115 AdminStateUp: opts.AdminStateUp,
116 Name: opts.Name,
117 Shared: opts.Shared,
118 }}
119
120 if opts.TenantID != "" {
121 reqBody.Network.TenantID = &opts.TenantID
122 }
123
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200124 // Send request to API
125 var res response
126 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
127 MoreHeaders: c.Provider.AuthenticatedHeaders(),
128 ReqBody: &reqBody,
129 Results: &res,
130 OkCodes: []int{201},
131 })
132 if err != nil {
133 return nil, err
134 }
135
136 return res.Network, nil
137}
Jamie Hannaford79475052014-09-15 17:08:06 +0200138
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200139func Update(c *gophercloud.ServiceClient, networkID string, opts NetworkOpts) (*Network, error) {
Jamie Hannaford79475052014-09-15 17:08:06 +0200140 // Define structures
141 type network struct {
142 AdminStateUp bool `json:"admin_state_up"`
143 Name string `json:"name"`
144 Shared *bool `json:"shared,omitempty"`
145 TenantID *string `json:"tenant_id,omitempty"`
146 }
147
148 type request struct {
149 Network network `json:"network"`
150 }
151 type response struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200152 Network *Network `json:"network"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200153 }
154
155 // Populate request body
156 reqBody := request{Network: network{
157 AdminStateUp: opts.AdminStateUp,
158 Name: opts.Name,
159 Shared: opts.Shared,
160 }}
161
162 if opts.TenantID != "" {
163 reqBody.Network.TenantID = &opts.TenantID
164 }
165
166 // Send request to API
167 var res response
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200168 _, err := perigee.Request("PUT", GetURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200169 MoreHeaders: c.Provider.AuthenticatedHeaders(),
170 ReqBody: &reqBody,
171 Results: &res,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200172 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200173 })
174 if err != nil {
175 return nil, err
176 }
177
178 return res.Network, nil
179}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200180
181func Delete(c *gophercloud.ServiceClient, networkID string) error {
182 _, err := perigee.Request("DELETE", DeleteURL(c, networkID), perigee.Options{
183 MoreHeaders: c.Provider.AuthenticatedHeaders(),
184 OkCodes: []int{204},
185 })
186 return err
187}