@@ -42,6 +42,14 @@ 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.
4755type CreateOpts struct {
@@ -52,6 +60,33 @@ type CreateOpts struct {
5260 GatewayInfo * GatewayInfo
5361}
5462
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+
5590// Create accepts a CreateOpts struct and uses the values to create a new
5691// logical router. When it is created, the router does not have an internal
5792// interface - it is not associated to any subnet.
@@ -60,31 +95,15 @@ type CreateOpts struct {
6095// GatewayInfo struct. The external gateway for the router must be plugged into
6196// an external network (it is external if its `router:external' field is set to
6297// 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
82100
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
85105 }
86106
87- var res CreateResult
88107 _ , res .Err = c .Post (rootURL (c ), reqBody , & res .Body , nil )
89108 return res
90109}
0 commit comments