From f2ba380794e266fffc584fa99730c4163004079c Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Sun, 4 Sep 2016 02:54:53 +0000 Subject: [PATCH] provider/openstack: gophercloud migration: networking secgroup --- ...e_openstack_networking_secgroup_rule_v2.go | 64 +++++++++++++++++-- ...nstack_networking_secgroup_rule_v2_test.go | 4 +- ...source_openstack_networking_secgroup_v2.go | 8 +-- ...e_openstack_networking_secgroup_v2_test.go | 2 +- 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2.go b/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2.go index 396d836b8..df2802fea 100644 --- a/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2.go @@ -9,8 +9,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" - "github.com/rackspace/gophercloud" - "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/rules" + "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" ) func resourceNetworkingSecGroupRuleV2() *schema.Resource { @@ -106,17 +106,29 @@ func resourceNetworkingSecGroupRuleV2Create(d *schema.ResourceData, meta interfa } opts := rules.CreateOpts{ - Direction: d.Get("direction").(string), - EtherType: d.Get("ethertype").(string), SecGroupID: d.Get("security_group_id").(string), PortRangeMin: d.Get("port_range_min").(int), PortRangeMax: d.Get("port_range_max").(int), - Protocol: d.Get("protocol").(string), RemoteGroupID: d.Get("remote_group_id").(string), RemoteIPPrefix: d.Get("remote_ip_prefix").(string), TenantID: d.Get("tenant_id").(string), } + if v, ok := d.GetOk("direction"); ok { + direction := resourceNetworkingSecGroupRuleV2DetermineDirection(v.(string)) + opts.Direction = direction + } + + if v, ok := d.GetOk("ethertype"); ok { + ethertype := resourceNetworkingSecGroupRuleV2DetermineEtherType(v.(string)) + opts.EtherType = ethertype + } + + if v, ok := d.GetOk("protocol"); ok { + protocol := resourceNetworkingSecGroupRuleV2DetermineProtocol(v.(string)) + opts.Protocol = protocol + } + log.Printf("[DEBUG] Create OpenStack Neutron security group: %#v", opts) security_group_rule, err := rules.Create(networkingClient, opts).Extract() @@ -185,13 +197,51 @@ func resourceNetworkingSecGroupRuleV2Delete(d *schema.ResourceData, meta interfa return err } +func resourceNetworkingSecGroupRuleV2DetermineDirection(v string) rules.RuleDirection { + var direction rules.RuleDirection + switch v { + case "ingress": + direction = rules.DirIngress + case "egress": + direction = rules.DirEgress + } + + return direction +} + +func resourceNetworkingSecGroupRuleV2DetermineEtherType(v string) rules.RuleEtherType { + var etherType rules.RuleEtherType + switch v { + case "IPv4": + etherType = rules.EtherType4 + case "IPv6": + etherType = rules.EtherType6 + } + + return etherType +} + +func resourceNetworkingSecGroupRuleV2DetermineProtocol(v string) rules.RuleProtocol { + var protocol rules.RuleProtocol + switch v { + case "tcp": + protocol = rules.ProtocolTCP + case "udp": + protocol = rules.ProtocolUDP + case "icmp": + protocol = rules.ProtocolICMP + } + + return protocol +} + func waitForSecGroupRuleDelete(networkingClient *gophercloud.ServiceClient, secGroupRuleId string) resource.StateRefreshFunc { return func() (interface{}, string, error) { log.Printf("[DEBUG] Attempting to delete OpenStack Security Group Rule %s.\n", secGroupRuleId) r, err := rules.Get(networkingClient, secGroupRuleId).Extract() if err != nil { - errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) + errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode) if !ok { return r, "ACTIVE", err } @@ -203,7 +253,7 @@ func waitForSecGroupRuleDelete(networkingClient *gophercloud.ServiceClient, secG err = rules.Delete(networkingClient, secGroupRuleId).ExtractErr() if err != nil { - errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) + errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode) if !ok { return r, "ACTIVE", err } diff --git a/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2_test.go b/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2_test.go index c92b45a10..5d3d7e972 100644 --- a/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_networking_secgroup_rule_v2_test.go @@ -7,8 +7,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/groups" - "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/rules" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" ) func TestAccNetworkingV2SecGroupRule_basic(t *testing.T) { diff --git a/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go b/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go index 26ddca40d..902cc1968 100644 --- a/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go +++ b/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go @@ -8,8 +8,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" - "github.com/rackspace/gophercloud" - "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/groups" + "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" ) func resourceNetworkingSecGroupV2() *schema.Resource { @@ -131,7 +131,7 @@ func waitForSecGroupDelete(networkingClient *gophercloud.ServiceClient, secGroup r, err := groups.Get(networkingClient, secGroupId).Extract() if err != nil { - errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) + errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode) if !ok { return r, "ACTIVE", err } @@ -143,7 +143,7 @@ func waitForSecGroupDelete(networkingClient *gophercloud.ServiceClient, secGroup err = groups.Delete(networkingClient, secGroupId).ExtractErr() if err != nil { - errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) + errCode, ok := err.(*gophercloud.ErrUnexpectedResponseCode) if !ok { return r, "ACTIVE", err } diff --git a/builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go b/builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go index 198d74a5c..1662f1e62 100644 --- a/builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go +++ b/builtin/providers/openstack/resource_openstack_networking_secgroup_v2_test.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/groups" + "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups" ) func TestAccNetworkingV2SecGroup_basic(t *testing.T) {