@@ -42,6 +42,14 @@ func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
42
42
})
43
43
}
44
44
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
+
45
53
// CreateOpts contains all the values needed to create a new router. There are
46
54
// no required values.
47
55
type CreateOpts struct {
@@ -52,6 +60,33 @@ type CreateOpts struct {
52
60
GatewayInfo * GatewayInfo
53
61
}
54
62
63
+ // ToRouterCreateMap casts a CreateOpts struct to a map.
64
+ func (opts CreateOpts ) ToRouterCreateMap () (map [string ]interface {}, error ) {
65
+ r := make (map [string ]interface {})
66
+
67
+ if gophercloud .MaybeString (opts .Name ) != nil {
68
+ r ["name" ] = opts .Name
69
+ }
70
+
71
+ if opts .AdminStateUp != nil {
72
+ r ["admin_state_up" ] = opts .AdminStateUp
73
+ }
74
+
75
+ if opts .Distributed != nil {
76
+ r ["distributed" ] = opts .Distributed
77
+ }
78
+
79
+ if gophercloud .MaybeString (opts .TenantID ) != nil {
80
+ r ["tenant_id" ] = opts .TenantID
81
+ }
82
+
83
+ if opts .GatewayInfo != nil {
84
+ r ["external_gateway_info" ] = opts .GatewayInfo
85
+ }
86
+
87
+ return map [string ]interface {}{"router" : r }, nil
88
+ }
89
+
55
90
// Create accepts a CreateOpts struct and uses the values to create a new
56
91
// logical router. When it is created, the router does not have an internal
57
92
// interface - it is not associated to any subnet.
@@ -60,31 +95,15 @@ type CreateOpts struct {
60
95
// GatewayInfo struct. The external gateway for the router must be plugged into
61
96
// an external network (it is external if its `router:external' field is set to
62
97
// true).
63
- func Create (c * gophercloud.ServiceClient , opts CreateOpts ) CreateResult {
64
- type router struct {
65
- Name * string `json:"name,omitempty"`
66
- AdminStateUp * bool `json:"admin_state_up,omitempty"`
67
- Distributed * bool `json:"distributed,omitempty"`
68
- TenantID * string `json:"tenant_id,omitempty"`
69
- GatewayInfo * GatewayInfo `json:"external_gateway_info,omitempty"`
70
- }
71
-
72
- type request struct {
73
- Router router `json:"router"`
74
- }
75
-
76
- reqBody := request {Router : router {
77
- Name : gophercloud .MaybeString (opts .Name ),
78
- AdminStateUp : opts .AdminStateUp ,
79
- Distributed : opts .Distributed ,
80
- TenantID : gophercloud .MaybeString (opts .TenantID ),
81
- }}
98
+ func Create (c * gophercloud.ServiceClient , opts CreateOptsBuilder ) CreateResult {
99
+ var res CreateResult
82
100
83
- if opts .GatewayInfo != nil {
84
- reqBody .Router .GatewayInfo = opts .GatewayInfo
101
+ reqBody , err := opts .ToRouterCreateMap ()
102
+ if err != nil {
103
+ res .Err = err
104
+ return res
85
105
}
86
106
87
- var res CreateResult
88
107
_ , res .Err = c .Post (rootURL (c ), reqBody , & res .Body , nil )
89
108
return res
90
109
}
0 commit comments