blob: 4f1037e014f8e3ee097151a7f562c4c140e4d892 [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 Hannafordd0f090c2014-09-22 13:44:34 +020022 SortKey string
23 SortDir string
Jamie Hannaford4721abc2014-09-16 16:29:04 +020024}
25
Jamie Hannaford965ae702014-09-22 14:58:19 +020026type networkOpts struct {
Jamie Hannaford1ce30f22014-09-16 11:23:34 +020027 AdminStateUp bool
28 Name string
29 Shared *bool
30 TenantID string
Jamie Hannaford12bc2472014-09-15 12:14:31 +020031}
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020032
Jamie Hannaford4721abc2014-09-16 16:29:04 +020033func ptrToStr(val *bool) string {
34 if *val == true {
35 return "true"
36 } else if *val == false {
37 return "false"
38 } else {
39 return ""
40 }
41}
42
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020043func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020044 // Build query parameters
45 q := make(map[string]string)
46 if opts.Status != "" {
47 q["status"] = opts.Status
48 }
49 if opts.Name != "" {
50 q["name"] = opts.Name
51 }
52 if opts.AdminStateUp != nil {
53 q["admin_state_up"] = ptrToStr(opts.AdminStateUp)
54 }
55 if opts.TenantID != "" {
56 q["tenant_id"] = opts.TenantID
57 }
58 if opts.Shared != nil {
59 q["shared"] = ptrToStr(opts.Shared)
60 }
61 if opts.ID != "" {
62 q["id"] = opts.ID
63 }
64 if opts.Page != 0 {
65 q["page"] = strconv.Itoa(opts.Page)
66 }
67 if opts.PerPage != 0 {
68 q["per_page"] = strconv.Itoa(opts.PerPage)
69 }
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020070 if opts.Limit != 0 {
71 q["limit"] = strconv.Itoa(opts.Limit)
72 }
Jamie Hannafordd0f090c2014-09-22 13:44:34 +020073 if opts.SortKey != "" {
74 q["sort_key"] = opts.SortKey
75 }
76 if opts.SortDir != "" {
77 q["sort_dir"] = opts.SortDir
78 }
Jamie Hannaford4721abc2014-09-16 16:29:04 +020079
80 u := ListURL(c) + utils.BuildQuery(q)
Jamie Hannafordf0c615b2014-09-17 10:56:52 +020081 return pagination.NewPager(c, u, func(r pagination.LastHTTPResponse) pagination.Page {
82 return NetworkPage{pagination.LinkedPageBase(r)}
83 })
Jamie Hannaford4721abc2014-09-16 16:29:04 +020084}
85
86func Get(c *gophercloud.ServiceClient, id string) (*Network, error) {
87 var n Network
88 _, err := perigee.Request("GET", GetURL(c, id), perigee.Options{
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020089 MoreHeaders: c.Provider.AuthenticatedHeaders(),
90 Results: &struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +020091 Network *Network `json:"network"`
Jamie Hannafordd01a3c72014-09-15 12:51:00 +020092 }{&n},
93 OkCodes: []int{200},
94 })
95 if err != nil {
96 return nil, err
97 }
98 return &n, nil
99}
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200100
Jamie Hannaford965ae702014-09-22 14:58:19 +0200101type CreateOpts networkOpts
102
103func Create(c *gophercloud.ServiceClient, opts CreateOpts) (*NetworkCreateResult, error) {
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200104 // Define structures
105 type network struct {
106 AdminStateUp bool `json:"admin_state_up"`
107 Name string `json:"name"`
108 Shared *bool `json:"shared,omitempty"`
109 TenantID *string `json:"tenant_id,omitempty"`
110 }
111 type request struct {
112 Network network `json:"network"`
113 }
114 type response struct {
Jamie Hannaford79475052014-09-15 17:08:06 +0200115 Network *NetworkCreateResult `json:"network"`
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200116 }
117
118 // Validate
119 if opts.Name == "" {
120 return nil, ErrNameRequired
121 }
122
123 // Populate request body
124 reqBody := request{Network: network{
125 AdminStateUp: opts.AdminStateUp,
126 Name: opts.Name,
127 Shared: opts.Shared,
128 }}
129
130 if opts.TenantID != "" {
131 reqBody.Network.TenantID = &opts.TenantID
132 }
133
Jamie Hannafordd2d9f562014-09-15 15:35:07 +0200134 // Send request to API
135 var res response
136 _, err := perigee.Request("POST", CreateURL(c), perigee.Options{
137 MoreHeaders: c.Provider.AuthenticatedHeaders(),
138 ReqBody: &reqBody,
139 Results: &res,
140 OkCodes: []int{201},
141 })
142 if err != nil {
143 return nil, err
144 }
145
146 return res.Network, nil
147}
Jamie Hannaford79475052014-09-15 17:08:06 +0200148
Jamie Hannaford965ae702014-09-22 14:58:19 +0200149type UpdateOpts networkOpts
150
151func Update(c *gophercloud.ServiceClient, networkID string, opts UpdateOpts) (*Network, error) {
Jamie Hannaford79475052014-09-15 17:08:06 +0200152 // Define structures
153 type network struct {
Jamie Hannaford965ae702014-09-22 14:58:19 +0200154 AdminStateUp bool `json:"admin_state_up"`
155 Name string `json:"name"`
156 Shared *bool `json:"shared,omitempty"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200157 }
158
159 type request struct {
160 Network network `json:"network"`
161 }
162 type response struct {
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200163 Network *Network `json:"network"`
Jamie Hannaford79475052014-09-15 17:08:06 +0200164 }
165
166 // Populate request body
167 reqBody := request{Network: network{
168 AdminStateUp: opts.AdminStateUp,
169 Name: opts.Name,
170 Shared: opts.Shared,
171 }}
172
Jamie Hannaford79475052014-09-15 17:08:06 +0200173 // Send request to API
174 var res response
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200175 _, err := perigee.Request("PUT", GetURL(c, networkID), perigee.Options{
Jamie Hannaford79475052014-09-15 17:08:06 +0200176 MoreHeaders: c.Provider.AuthenticatedHeaders(),
177 ReqBody: &reqBody,
178 Results: &res,
Jamie Hannafordf84171d2014-09-18 14:00:01 +0200179 OkCodes: []int{200, 201},
Jamie Hannaford79475052014-09-15 17:08:06 +0200180 })
181 if err != nil {
182 return nil, err
183 }
184
185 return res.Network, nil
186}
Jamie Hannaford4721abc2014-09-16 16:29:04 +0200187
188func Delete(c *gophercloud.ServiceClient, networkID string) error {
189 _, err := perigee.Request("DELETE", DeleteURL(c, networkID), perigee.Options{
190 MoreHeaders: c.Provider.AuthenticatedHeaders(),
191 OkCodes: []int{204},
192 })
193 return err
194}