Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit f824a86

Browse files
author
Kirill Shirinkin
committed
Fixes as in review
1 parent defa84a commit f824a86

File tree

1 file changed

+34
-28
lines changed
  • openstack/networking/v2/extensions/layer3/routers

1 file changed

+34
-28
lines changed

openstack/networking/v2/extensions/layer3/routers/requests.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,62 +42,68 @@ func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
4242
})
4343
}
4444

45+
// CreateOptsBuilder is the interface options structs have to satisfy in order
46+
// to be used in the main Create operation in this package. Since many
47+
// extensions decorate or modify the common logic, it is useful for them to
48+
// satisfy a basic interface in order for them to be used.
49+
type CreateOptsBuilder interface {
50+
ToRouterCreateMap() (map[string]interface{}, error)
51+
}
52+
4553
// CreateOpts contains all the values needed to create a new router. There are
4654
// no required values.
4755
type CreateOpts struct {
4856
Name string
4957
AdminStateUp *bool
5058
Distributed *bool
5159
TenantID string
52-
DriverOpts map[string]string
5360
GatewayInfo *GatewayInfo
5461
}
5562

56-
// Create accepts a CreateOpts struct and uses the values to create a new
57-
// logical router. When it is created, the router does not have an internal
58-
// interface - it is not associated to any subnet.
59-
//
60-
// You can optionally specify an external gateway for a router using the
61-
// GatewayInfo struct. The external gateway for the router must be plugged into
62-
// an external network (it is external if its `router:external' field is set to
63-
// true).
64-
func Create(c *gophercloud.ServiceClient, opts CreateOpts) CreateResult {
65-
66-
type request struct {
67-
Router map[string]interface{} `json:"router"`
68-
}
69-
70-
routerMap := make(map[string]interface{})
63+
// ToRouterCreateMap casts a CreateOpts struct to a map.
64+
func (opts CreateOpts) ToRouterCreateMap() (map[string]interface{}, error) {
65+
r := make(map[string]interface{})
7166

7267
if gophercloud.MaybeString(opts.Name) != nil {
73-
routerMap["name"] = opts.Name
68+
r["name"] = opts.Name
7469
}
7570

7671
if opts.AdminStateUp != nil {
77-
routerMap["admin_state_up"] = opts.AdminStateUp
72+
r["admin_state_up"] = opts.AdminStateUp
7873
}
7974

8075
if opts.Distributed != nil {
81-
routerMap["distributed"] = opts.Distributed
76+
r["distributed"] = opts.Distributed
8277
}
8378

8479
if gophercloud.MaybeString(opts.TenantID) != nil {
85-
routerMap["tenant_id"] = opts.TenantID
80+
r["tenant_id"] = opts.TenantID
8681
}
8782

8883
if opts.GatewayInfo != nil {
89-
routerMap["external_gateway_info"] = opts.GatewayInfo
90-
}
91-
92-
if opts.DriverOpts != nil {
93-
for k, v := range opts.DriverOpts {
94-
routerMap[k] = v
95-
}
84+
r["external_gateway_info"] = opts.GatewayInfo
9685
}
9786

98-
reqBody := request{Router: routerMap}
87+
return map[string]interface{}{"router": r}, nil
88+
}
9989

90+
// Create accepts a CreateOpts struct and uses the values to create a new
91+
// logical router. When it is created, the router does not have an internal
92+
// interface - it is not associated to any subnet.
93+
//
94+
// You can optionally specify an external gateway for a router using the
95+
// GatewayInfo struct. The external gateway for the router must be plugged into
96+
// an external network (it is external if its `router:external' field is set to
97+
// true).
98+
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
10099
var res CreateResult
100+
101+
reqBody, err := opts.ToRouterCreateMap()
102+
if err != nil {
103+
res.Err = err
104+
return res
105+
}
106+
101107
_, res.Err = c.Post(rootURL(c), reqBody, &res.Body, nil)
102108
return res
103109
}

0 commit comments

Comments
 (0)