@@ -42,62 +42,68 @@ 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 {
48
56
Name string
49
57
AdminStateUp * bool
50
58
Distributed * bool
51
59
TenantID string
52
- DriverOpts map [string ]string
53
60
GatewayInfo * GatewayInfo
54
61
}
55
62
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 {})
71
66
72
67
if gophercloud .MaybeString (opts .Name ) != nil {
73
- routerMap ["name" ] = opts .Name
68
+ r ["name" ] = opts .Name
74
69
}
75
70
76
71
if opts .AdminStateUp != nil {
77
- routerMap ["admin_state_up" ] = opts .AdminStateUp
72
+ r ["admin_state_up" ] = opts .AdminStateUp
78
73
}
79
74
80
75
if opts .Distributed != nil {
81
- routerMap ["distributed" ] = opts .Distributed
76
+ r ["distributed" ] = opts .Distributed
82
77
}
83
78
84
79
if gophercloud .MaybeString (opts .TenantID ) != nil {
85
- routerMap ["tenant_id" ] = opts .TenantID
80
+ r ["tenant_id" ] = opts .TenantID
86
81
}
87
82
88
83
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
96
85
}
97
86
98
- reqBody := request {Router : routerMap }
87
+ return map [string ]interface {}{"router" : r }, nil
88
+ }
99
89
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 {
100
99
var res CreateResult
100
+
101
+ reqBody , err := opts .ToRouterCreateMap ()
102
+ if err != nil {
103
+ res .Err = err
104
+ return res
105
+ }
106
+
101
107
_ , res .Err = c .Post (rootURL (c ), reqBody , & res .Body , nil )
102
108
return res
103
109
}
0 commit comments