Skip to content

Commit

Permalink
Merge pull request hashicorp#2 from fatmcgav/gophercloud-migration
Browse files Browse the repository at this point in the history
Add `BuildRequest` and `MapValueSpecs` functions, and refactor types
  • Loading branch information
jtopjian authored Oct 6, 2016
2 parents 6de87c2 + 8f5a32b commit e375ee3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,41 +62,6 @@ func resourceNetworkingNetworkV2() *schema.Resource {
}
}

// NetworkCreateOpts contains all teh values needed to create a new network.
type NetworkCreateOpts struct {
AdminStateUp *bool
Name string
Shared *bool
TenantID string
ValueSpecs map[string]string
}

// ToNetworkCreateMpa casts a networkCreateOpts struct to a map.
func (opts NetworkCreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
n := make(map[string]interface{})

if opts.AdminStateUp != nil {
n["admin_state_up"] = &opts.AdminStateUp
}
if opts.Name != "" {
n["name"] = opts.Name
}
if opts.Shared != nil {
n["shared"] = &opts.Shared
}
if opts.TenantID != "" {
n["tenant_id"] = opts.TenantID
}

if opts.ValueSpecs != nil {
for k, v := range opts.ValueSpecs {
n[k] = v
}
}

return map[string]interface{}{"network": n}, nil
}

func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
Expand All @@ -105,9 +70,11 @@ func resourceNetworkingNetworkV2Create(d *schema.ResourceData, meta interface{})
}

createOpts := NetworkCreateOpts{
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
ValueSpecs: networkValueSpecs(d),
networks.CreateOpts{
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
},
MapValueSpecs(d),
}

asuRaw := d.Get("admin_state_up").(string)
Expand Down Expand Up @@ -284,11 +251,3 @@ func waitForNetworkDelete(networkingClient *gophercloud.ServiceClient, networkId
return n, "ACTIVE", nil
}
}

func networkValueSpecs(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("value_specs").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,6 @@ func resourceNetworkingRouterV2() *schema.Resource {
}
}

// routerCreateOpts contains all the values needed to create a new router. There are
// no required values.
type RouterCreateOpts struct {
Name string
AdminStateUp *bool
Distributed *bool
TenantID string
GatewayInfo *routers.GatewayInfo
ValueSpecs map[string]string
}

// ToRouterCreateMap casts a routerCreateOpts struct to a map.
func (opts RouterCreateOpts) ToRouterCreateMap() (map[string]interface{}, error) {
r := make(map[string]interface{})

if gophercloud.MaybeString(opts.Name) != nil {
r["name"] = opts.Name
}

if opts.AdminStateUp != nil {
r["admin_state_up"] = opts.AdminStateUp
}

if opts.Distributed != nil {
r["distributed"] = opts.Distributed
}

if gophercloud.MaybeString(opts.TenantID) != nil {
r["tenant_id"] = opts.TenantID
}

if opts.GatewayInfo != nil {
r["external_gateway_info"] = opts.GatewayInfo
}

if opts.ValueSpecs != nil {
for k, v := range opts.ValueSpecs {
r[k] = v
}
}

return map[string]interface{}{"router": r}, nil
}

func resourceNetworkingRouterV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
Expand All @@ -115,9 +71,11 @@ func resourceNetworkingRouterV2Create(d *schema.ResourceData, meta interface{})
}

createOpts := RouterCreateOpts{
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
ValueSpecs: routerValueSpecs(d),
routers.CreateOpts{
Name: d.Get("name").(string),
TenantID: d.Get("tenant_id").(string),
},
MapValueSpecs(d),
}

if asuRaw, ok := d.GetOk("admin_state_up"); ok {
Expand Down Expand Up @@ -292,11 +250,3 @@ func waitForRouterDelete(networkingClient *gophercloud.ServiceClient, routerId s
return r, "ACTIVE", nil
}
}

func routerValueSpecs(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("value_specs").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func resourceNetworkingSubnetV2Create(d *schema.ResourceData, meta interface{})
HostRoutes: resourceSubnetHostRoutesV2(d),
EnableDHCP: nil,
},
subnetValueSpecs(d),
MapValueSpecs(d),
}

noGateway := d.Get("no_gateway").(bool)
Expand Down Expand Up @@ -377,11 +377,3 @@ func waitForSubnetDelete(networkingClient *gophercloud.ServiceClient, subnetId s
return s, "ACTIVE", nil
}
}

func subnetValueSpecs(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("value_specs").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}
40 changes: 27 additions & 13 deletions builtin/providers/openstack/types.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package openstack

import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)

// NetworkCreateOpts represents the attributes used when creating a new network.
type NetworkCreateOpts struct {
networks.CreateOpts
ValueSpecs map[string]string `json:"value_specs,omitempty"`
}

// ToNetworkCreateMap casts a CreateOpts struct to a map.
// It overrides networks.ToNetworkCreateMap to add the ValueSpecs field.
func (opts NetworkCreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
return BuildRequest(opts, "network")
}

// RouterCreateOpts represents the attributes used when creating a new router.
type RouterCreateOpts struct {
routers.CreateOpts
ValueSpecs map[string]string `json:"value_specs,omitempty"`
}

// ToRouterCreateMap casts a CreateOpts struct to a map.
// It overrides routers.ToRouterCreateMap to add the ValueSpecs field.
func (opts RouterCreateOpts) ToRouterCreateMap() (map[string]interface{}, error) {
return BuildRequest(opts, "router")
}

// SubnetCreateOpts represents the attributes used when creating a new subnet.
type SubnetCreateOpts struct {
subnets.CreateOpts
Expand All @@ -14,16 +39,5 @@ type SubnetCreateOpts struct {
// ToSubnetCreateMap casts a CreateOpts struct to a map.
// It overrides subnets.ToSubnetCreateMap to add the ValueSpecs field.
func (opts SubnetCreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}

if opts.ValueSpecs != nil {
for k, v := range opts.ValueSpecs {
b[k] = v
}
}

return map[string]interface{}{"subnet": b}, nil
return BuildRequest(opts, "subnet")
}
27 changes: 27 additions & 0 deletions builtin/providers/openstack/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@ func CheckDeleted(d *schema.ResourceData, err error, msg string) error {

return fmt.Errorf("%s: %s", msg, err)
}

// MapValueSpecs converts ResourceData into a map
func MapValueSpecs(d *schema.ResourceData) map[string]string {
m := make(map[string]string)
for key, val := range d.Get("value_specs").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}

// BuildRequest takes an opts struct and builds a request body for
// Gophercloud to execute
func BuildRequest(opts interface{}, parent string) (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}

if b["value_specs"] != nil {
for k, v := range b["value_specs"].(map[string]interface{}) {
b[k] = v
}
delete(b, "value_specs")
}

return map[string]interface{}{parent: b}, nil
}

0 comments on commit e375ee3

Please sign in to comment.