provider/openstack: gophercloud migration: fwaas

This commit is contained in:
Joe Topjian 2016-09-04 02:53:44 +00:00
parent 8b6c3a1bbc
commit a1d76c8d07
6 changed files with 72 additions and 37 deletions

View File

@ -5,10 +5,10 @@ import (
"log" "log"
"time" "time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
) )
func resourceFWFirewallV1() *schema.Resource { func resourceFWFirewallV1() *schema.Resource {
@ -231,7 +231,7 @@ func waitForFirewallDeletion(networkingClient *gophercloud.ServiceClient, id str
log.Printf("[DEBUG] Get firewall %s => %#v", id, fw) log.Printf("[DEBUG] Get firewall %s => %#v", id, fw)
if err != nil { if err != nil {
httpStatus := err.(*gophercloud.UnexpectedResponseCodeError) httpStatus := err.(*gophercloud.ErrUnexpectedResponseCode)
log.Printf("[DEBUG] Get firewall %s status is %d", id, httpStatus.Actual) log.Printf("[DEBUG] Get firewall %s status is %d", id, httpStatus.Actual)
if httpStatus.Actual == 404 { if httpStatus.Actual == 404 {

View File

@ -5,10 +5,10 @@ import (
"testing" "testing"
"time" "time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/firewalls"
) )
func TestAccFWFirewallV1_basic(t *testing.T) { func TestAccFWFirewallV1_basic(t *testing.T) {
@ -51,7 +51,7 @@ func testAccCheckFWFirewallV1Destroy(s *terraform.State) error {
if err == nil { if err == nil {
return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID) return fmt.Errorf("Firewall (%s) still exists.", rs.Primary.ID)
} }
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
return httpError return httpError
} }
@ -84,7 +84,7 @@ func testAccCheckFWFirewallV1Exists(n, expectedName, expectedDescription string,
// if we get a 404 error. Fail on any other error. // if we get a 404 error. Fail on any other error.
found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract() found, err = firewalls.Get(networkingClient, rs.Primary.ID).Extract()
if err != nil { if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue

View File

@ -5,9 +5,9 @@ import (
"log" "log"
"time" "time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
) )
func resourceFWPolicyV1() *schema.Resource { func resourceFWPolicyV1() *schema.Resource {
@ -186,7 +186,7 @@ func resourceFWPolicyV1Delete(d *schema.ResourceData, meta interface{}) error {
break break
} }
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 409 { if !ok || httpError.Actual != 409 {
return err return err
} }

View File

@ -5,10 +5,10 @@ import (
"testing" "testing"
"time" "time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
) )
func TestAccFWPolicyV1_basic(t *testing.T) { func TestAccFWPolicyV1_basic(t *testing.T) {
@ -80,7 +80,7 @@ func testAccCheckFWPolicyV1Destroy(s *terraform.State) error {
if err == nil { if err == nil {
return fmt.Errorf("Firewall policy (%s) still exists.", rs.Primary.ID) return fmt.Errorf("Firewall policy (%s) still exists.", rs.Primary.ID)
} }
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
return httpError return httpError
} }
@ -113,7 +113,7 @@ func testAccCheckFWPolicyV1Exists(n, name, description string, ruleCount int) re
// if we get a 404 error. Fail on any other error. // if we get a 404 error. Fail on any other error.
found, err = policies.Get(networkingClient, rs.Primary.ID).Extract() found, err = policies.Get(networkingClient, rs.Primary.ID).Extract()
if err != nil { if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue

View File

@ -4,9 +4,10 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/policies"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
) )
func resourceFWRuleV1() *schema.Resource { func resourceFWRuleV1() *schema.Resource {
@ -86,13 +87,14 @@ func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
} }
enabled := d.Get("enabled").(bool) enabled := d.Get("enabled").(bool)
ipVersion := resourceFWRuleV1DetermineIPVersion(d.Get("ip_version").(int))
ruleConfiguration := rules.CreateOpts{ ruleConfiguration := rules.CreateOpts{
Name: d.Get("name").(string), Name: d.Get("name").(string),
Description: d.Get("description").(string), Description: d.Get("description").(string),
Protocol: d.Get("protocol").(string), Protocol: d.Get("protocol").(string),
Action: d.Get("action").(string), Action: d.Get("action").(string),
IPVersion: d.Get("ip_version").(int), IPVersion: ipVersion,
SourceIPAddress: d.Get("source_ip_address").(string), SourceIPAddress: d.Get("source_ip_address").(string),
DestinationIPAddress: d.Get("destination_ip_address").(string), DestinationIPAddress: d.Get("destination_ip_address").(string),
SourcePort: d.Get("source_port").(string), SourcePort: d.Get("source_port").(string),
@ -101,6 +103,11 @@ func resourceFWRuleV1Create(d *schema.ResourceData, meta interface{}) error {
TenantID: d.Get("tenant_id").(string), TenantID: d.Get("tenant_id").(string),
} }
if v, ok := d.GetOk("ip_version"); ok {
ipVersion := resourceFWRuleV1DetermineIPVersion(v.(int))
ruleConfiguration.IPVersion = ipVersion
}
log.Printf("[DEBUG] Create firewall rule: %#v", ruleConfiguration) log.Printf("[DEBUG] Create firewall rule: %#v", ruleConfiguration)
rule, err := rules.Create(networkingClient, ruleConfiguration).Extract() rule, err := rules.Create(networkingClient, ruleConfiguration).Extract()
@ -156,39 +163,54 @@ func resourceFWRuleV1Update(d *schema.ResourceData, meta interface{}) error {
opts := rules.UpdateOpts{} opts := rules.UpdateOpts{}
if d.HasChange("name") { if d.HasChange("name") {
opts.Name = d.Get("name").(string) v := d.Get("name").(string)
opts.Name = &v
} }
if d.HasChange("description") { if d.HasChange("description") {
opts.Description = d.Get("description").(string) v := d.Get("description").(string)
opts.Description = &v
} }
if d.HasChange("protocol") { if d.HasChange("protocol") {
opts.Protocol = d.Get("protocol").(string) v := d.Get("protocol").(string)
opts.Protocol = &v
} }
if d.HasChange("action") { if d.HasChange("action") {
opts.Action = d.Get("action").(string) v := d.Get("action").(string)
opts.Action = &v
} }
if d.HasChange("ip_version") { if d.HasChange("ip_version") {
opts.IPVersion = d.Get("ip_version").(int) v := d.Get("ip_version").(int)
ipVersion := resourceFWRuleV1DetermineIPVersion(v)
opts.IPVersion = &ipVersion
} }
if d.HasChange("source_ip_address") { if d.HasChange("source_ip_address") {
sourceIPAddress := d.Get("source_ip_address").(string) v := d.Get("source_ip_address").(string)
opts.SourceIPAddress = &sourceIPAddress opts.SourceIPAddress = &v
} }
if d.HasChange("destination_ip_address") { if d.HasChange("destination_ip_address") {
destinationIPAddress := d.Get("destination_ip_address").(string) v := d.Get("destination_ip_address").(string)
opts.DestinationIPAddress = &destinationIPAddress opts.DestinationIPAddress = &v
} }
if d.HasChange("source_port") { if d.HasChange("source_port") {
sourcePort := d.Get("source_port").(string) v := d.Get("source_port").(string)
opts.SourcePort = &sourcePort opts.SourcePort = &v
} }
if d.HasChange("destination_port") { if d.HasChange("destination_port") {
destinationPort := d.Get("destination_port").(string) v := d.Get("destination_port").(string)
opts.DestinationPort = &destinationPort opts.DestinationPort = &v
} }
if d.HasChange("enabled") { if d.HasChange("enabled") {
enabled := d.Get("enabled").(bool) v := d.Get("enabled").(bool)
opts.Enabled = &enabled opts.Enabled = &v
} }
log.Printf("[DEBUG] Updating firewall rules: %#v", opts) log.Printf("[DEBUG] Updating firewall rules: %#v", opts)
@ -216,7 +238,7 @@ func resourceFWRuleV1Delete(d *schema.ResourceData, meta interface{}) error {
} }
if rule.PolicyID != "" { if rule.PolicyID != "" {
err := policies.RemoveRule(networkingClient, rule.PolicyID, rule.ID) _, err := policies.RemoveRule(networkingClient, rule.PolicyID, rule.ID).Extract()
if err != nil { if err != nil {
return err return err
} }
@ -224,3 +246,16 @@ func resourceFWRuleV1Delete(d *schema.ResourceData, meta interface{}) error {
return rules.Delete(networkingClient, d.Id()).Err return rules.Delete(networkingClient, d.Id()).Err
} }
func resourceFWRuleV1DetermineIPVersion(ipv int) gophercloud.IPVersion {
// Determine the IP Version
var ipVersion gophercloud.IPVersion
switch ipv {
case 4:
ipVersion = gophercloud.IPv4
case 6:
ipVersion = gophercloud.IPv6
}
return ipVersion
}

View File

@ -6,10 +6,10 @@ import (
"testing" "testing"
"time" "time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/fwaas/rules"
) )
func TestAccFWRuleV1_basic(t *testing.T) { func TestAccFWRuleV1_basic(t *testing.T) {
@ -88,7 +88,7 @@ func testAccCheckFWRuleV1Destroy(s *terraform.State) error {
if err == nil { if err == nil {
return fmt.Errorf("Firewall rule (%s) still exists.", rs.Primary.ID) return fmt.Errorf("Firewall rule (%s) still exists.", rs.Primary.ID)
} }
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
return httpError return httpError
} }
@ -121,7 +121,7 @@ func testAccCheckFWRuleV1Exists(n string, expected *rules.Rule) resource.TestChe
// if we get a 404 error. Fail on any other error. // if we get a 404 error. Fail on any other error.
found, err = rules.Get(networkingClient, rs.Primary.ID).Extract() found, err = rules.Get(networkingClient, rs.Primary.ID).Extract()
if err != nil { if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError) httpError, ok := err.(*gophercloud.ErrUnexpectedResponseCode)
if !ok || httpError.Actual != 404 { if !ok || httpError.Actual != 404 {
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue