Skip to content

Commit

Permalink
Refactored to use a local Firewall type, removes use of routerinserti…
Browse files Browse the repository at this point in the history
…on package. Read, Create, Update all appear to function correctly
  • Loading branch information
fatmcgav committed Mar 10, 2017
1 parent 140b60c commit 865a2d8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 30 deletions.
111 changes: 82 additions & 29 deletions builtin/providers/openstack/resource_openstack_fw_firewall_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/routerinsertion"
// "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/routerinsertion"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -53,10 +53,11 @@ func resourceFWFirewallV1() *schema.Resource {
Computed: true,
},
"associated_routers": &schema.Schema{
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
// Set: schema.HashString,
Set: schema.HashString,
},
"value_specs": &schema.Schema{
Type: schema.TypeMap,
Expand All @@ -67,6 +68,18 @@ func resourceFWFirewallV1() *schema.Resource {
}
}

// Firewall is an OpenStack firewall.
type Firewall struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
AdminStateUp bool `json:"admin_state_up"`
Status string `json:"status"`
PolicyID string `json:"firewall_policy_id"`
TenantID string `json:"tenant_id"`
RouterIDs []string `json:"router_ids"`
}

func resourceFWFirewallV1Create(d *schema.ResourceData, meta interface{}) error {

config := meta.(*Config)
Expand All @@ -86,7 +99,9 @@ func resourceFWFirewallV1Create(d *schema.ResourceData, meta interface{}) error
}

var firewallConfiguration firewalls.CreateOptsBuilder
associatedRoutersRaw := d.Get("associated_routers").([]interface{})
var associatedRouters []string

associatedRoutersRaw := d.Get("associated_routers").(*schema.Set).List()
log.Printf("[DEBUG] associated_routers: %#v", associatedRoutersRaw)
log.Printf("[DEBUG] associated_routers count: %d", len(associatedRoutersRaw))
if len(associatedRoutersRaw) > 0 {
Expand All @@ -97,19 +112,26 @@ func resourceFWFirewallV1Create(d *schema.ResourceData, meta interface{}) error
associatedRouters[i] = raw.(string)
}

log.Printf("Initial firewallCreateOpts: %#v", firewallCreateOpts)
firewallConfiguration = FirewallCreateOptsExt{
routerinsertion.CreateOptsExt{
firewallCreateOpts,
associatedRouters,
},
MapValueSpecs(d),
}
} else {
firewallConfiguration = FirewallCreateOpts{
firewallCreateOpts,
MapValueSpecs(d),
}
// log.Printf("Initial firewallCreateOpts: %#v", firewallCreateOpts)
// firewallConfiguration = FirewallCreateOptsExt{
// routerinsertion.CreateOptsExt{
// firewallCreateOpts,
// associatedRouters,
// },
// MapValueSpecs(d),
// }
}
// else {
// firewallConfiguration = FirewallCreateOpts{
// firewallCreateOpts,
// []string{},
// MapValueSpecs(d),
// }
// }
firewallConfiguration = FirewallCreateOpts{
firewallCreateOpts,
associatedRouters,
MapValueSpecs(d),
}

log.Printf("[DEBUG] Create firewall: %#v", firewallConfiguration)
Expand Down Expand Up @@ -146,10 +168,17 @@ func resourceFWFirewallV1Read(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

firewall, err := firewalls.Get(networkingClient, d.Id()).Extract()
result := firewalls.Get(networkingClient, d.Id())

// Test overloading the Firewall struct to support RouterIDs
var s struct {
Firewall *Firewall `json:"firewall"`
}
err = result.ExtractInto(&s)
if err != nil {
return CheckDeleted(d, err, "firewall")
}
firewall := s.Firewall

log.Printf("[DEBUG] Read OpenStack Firewall %s: %#v", d.Id(), firewall)

Expand All @@ -159,6 +188,7 @@ func resourceFWFirewallV1Read(d *schema.ResourceData, meta interface{}) error {
d.Set("admin_state_up", firewall.AdminStateUp)
d.Set("tenant_id", firewall.TenantID)
d.Set("region", GetRegion(d))
d.Set("associated_routers", firewall.RouterIDs)

return nil
}
Expand All @@ -171,7 +201,9 @@ func resourceFWFirewallV1Update(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}

opts := firewalls.UpdateOpts{}
var opts FirewallUpdateOpts
// PolicyID is required
opts.PolicyID = d.Get("policy_id").(string)

if d.HasChange("name") {
opts.Name = d.Get("name").(string)
Expand All @@ -181,17 +213,36 @@ func resourceFWFirewallV1Update(d *schema.ResourceData, meta interface{}) error
opts.Description = d.Get("description").(string)
}

if d.HasChange("policy_id") {
opts.PolicyID = d.Get("policy_id").(string)
}

if d.HasChange("admin_state_up") {
adminStateUp := d.Get("admin_state_up").(bool)
opts.AdminStateUp = &adminStateUp
}

log.Printf("[DEBUG] opts looks like: %#v", opts)

// var updateOpts firewalls.UpdateOptsBuilder
// updateOpts = opts
if d.HasChange("associated_routers") {
log.Print("[DEBUG] 'associated_routers' has changed")
associatedRoutersRaw := d.Get("associated_routers").(*schema.Set).List()
associatedRouters := make([]string, len(associatedRoutersRaw))
for i, raw := range associatedRoutersRaw {
associatedRouters[i] = raw.(string)
}
opts.RouterIDs = associatedRouters
// updateOpts = routerinsertion.UpdateOptsExt{
// opts,
// associatedRouters,
// }
}

log.Printf("[DEBUG] Updating firewall with id %s: %#v", d.Id(), opts)

err = firewalls.Update(networkingClient, d.Id(), opts).Err
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"},
Target: []string{"ACTIVE"},
Expand All @@ -203,11 +254,6 @@ func resourceFWFirewallV1Update(d *schema.ResourceData, meta interface{}) error

_, err = stateConf.WaitForState()

err = firewalls.Update(networkingClient, d.Id(), opts).Err
if err != nil {
return err
}

return resourceFWFirewallV1Read(d, meta)
}

Expand Down Expand Up @@ -254,7 +300,14 @@ func resourceFWFirewallV1Delete(d *schema.ResourceData, meta interface{}) error
func waitForFirewallActive(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {

return func() (interface{}, string, error) {
fw, err := firewalls.Get(networkingClient, id).Extract()
// fw, err := firewalls.Get(networkingClient, id).Extract()
result := firewalls.Get(networkingClient, id)
// Test overloading the Firewall struct to support RouterIDs
var s struct {
Firewall *Firewall `json:"firewall"`
}
err := result.ExtractInto(&s)
fw := s.Firewall
log.Printf("[DEBUG] Get firewall %s => %#v", id, fw)

if err != nil {
Expand Down
47 changes: 46 additions & 1 deletion builtin/providers/openstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,34 @@ func (lrt *LogRoundTripper) formatJSON(raw []byte) string {
// FirewallCreateOpts represents the attributes used when creating a new firewall.
type FirewallCreateOpts struct {
firewalls.CreateOpts
RouterIDs []string `json:"router_ids,omitempty"`
ValueSpecs map[string]string `json:"value_specs,omitempty"`
}

// ToFirewallCreateMap casts a CreateOpts struct to a map.
// It overrides firewalls.ToFirewallCreateMap to add the ValueSpecs field.
// func (opts FirewallCreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) {
// return BuildRequest(opts, "firewall")
// }
// ToFirewallCreateMap casts a CreateOptsExt struct to a map.
// It overrides firewalls.ToFirewallCreateMap to add the ValueSpecs field.
func (opts FirewallCreateOpts) ToFirewallCreateMap() (map[string]interface{}, error) {
return BuildRequest(opts, "firewall")
// b, err := opts.CreateOptsBuilder.ToFirewallCreateMap()
b, err := BuildRequest(opts, "firewall")
log.Printf("[DEBUG] Body looks like: %#v", b)
if err != nil {
return nil, err
}

if len(opts.RouterIDs) == 0 {
log.Print("[DEBUG] No RouterIDs provided")
return b, nil
}

firewallMap := b["firewall"].(map[string]interface{})
firewallMap["router_ids"] = opts.RouterIDs

return b, nil
}

// FirewallCreateOptsExt represents the attributes used when creating a new firewall.
Expand Down Expand Up @@ -176,6 +197,30 @@ func (opts FirewallCreateOptsExt) ToFirewallCreateMap() (map[string]interface{},
return b, nil
}

//FirewallUpdateOpts
type FirewallUpdateOpts struct {
firewalls.UpdateOpts
RouterIDs []string `json:"router_ids,omitempty"`
// routerinsertion.UpdateOptsExt
}

func (opts FirewallUpdateOpts) ToFirewallUpdateMap() (map[string]interface{}, error) {
// b, err := opts.UpdateOptsBuilder.ToFirewallUpdateMap()
b, err := BuildRequest(opts, "firewall")
if err != nil {
return nil, err
}

if len(opts.RouterIDs) == 0 {
return b, nil
}

firewallMap := b["firewall"].(map[string]interface{})
firewallMap["router_ids"] = opts.RouterIDs

return b, nil
}

// FloatingIPCreateOpts represents the attributes used when creating a new floating ip.
type FloatingIPCreateOpts struct {
floatingips.CreateOpts
Expand Down

0 comments on commit 865a2d8

Please sign in to comment.