Skip to content

Commit

Permalink
Rename ip_masks -> ip_rules, azurerm_servicebus_namespace_network_rul…
Browse files Browse the repository at this point in the history
…e -> azurerm_servicebus_namespace_network_rule_set
  • Loading branch information
ArcturusZhang committed Apr 15, 2020
1 parent ce9d86b commit be98e95
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 104 deletions.
Expand Up @@ -6,32 +6,32 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ServiceBusNamespaceNetworkRuleId struct {
type ServiceBusNamespaceNetworkRuleSetId struct {
Name string
NamespaceName string
ResourceGroup string
}

func ServiceBusNamespaceNetworkRuleID(input string) (*ServiceBusNamespaceNetworkRuleId, error) {
func ServiceBusNamespaceNetworkRuleSetID(input string) (*ServiceBusNamespaceNetworkRuleSetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule Set ID %q: %+v", input, err)
}

rule := ServiceBusNamespaceNetworkRuleId{
rule := ServiceBusNamespaceNetworkRuleSetId{
ResourceGroup: id.ResourceGroup,
}

if rule.Name, err = id.PopSegment("networkrulesets"); err != nil {
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule Set ID %q: %+v", input, err)
}

if rule.NamespaceName, err = id.PopSegment("namespaces"); err != nil {
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule Set ID %q: %+v", input, err)
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule ID %q: %+v", input, err)
return nil, fmt.Errorf("unable to parse Service Bus Namespace Network Rule Set ID %q: %+v", input, err)
}

return &rule, nil
Expand Down
Expand Up @@ -7,7 +7,7 @@ func TestServiceBusNamespaceNetworkRuleID(t *testing.T) {
Name string
Input string
Error bool
Expected *ServiceBusNamespaceNetworkRuleId
Expected *ServiceBusNamespaceNetworkRuleSetId
}{
{
Name: "Empty",
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestServiceBusNamespaceNetworkRuleID(t *testing.T) {
{
Name: "Service Bus Namespace Network Rule ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/namespace1/networkrulesets/default",
Expected: &ServiceBusNamespaceNetworkRuleId{
Expected: &ServiceBusNamespaceNetworkRuleSetId{
Name: "default",
NamespaceName: "namespace1",
ResourceGroup: "resGroup1",
Expand All @@ -63,7 +63,7 @@ func TestServiceBusNamespaceNetworkRuleID(t *testing.T) {
for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ServiceBusNamespaceNetworkRuleID(v.Input)
actual, err := ServiceBusNamespaceNetworkRuleSetID(v.Input)
if err != nil {
if v.Error {
continue
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/servicebus/registration.go
Expand Up @@ -33,7 +33,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_servicebus_namespace": resourceArmServiceBusNamespace(),
"azurerm_servicebus_namespace_authorization_rule": resourceArmServiceBusNamespaceAuthorizationRule(),
"azurerm_servicebus_namespace_network_rule": resourceServiceBusNamespaceNetworkRule(),
"azurerm_servicebus_namespace_network_rule_set": resourceServiceBusNamespaceNetworkRuleSet(),
"azurerm_servicebus_queue": resourceArmServiceBusQueue(),
"azurerm_servicebus_queue_authorization_rule": resourceArmServiceBusQueueAuthorizationRule(),
"azurerm_servicebus_subscription": resourceArmServiceBusSubscription(),
Expand Down
Expand Up @@ -21,15 +21,15 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceServiceBusNamespaceNetworkRule() *schema.Resource {
func resourceServiceBusNamespaceNetworkRuleSet() *schema.Resource {
return &schema.Resource{
Create: resourceServiceBusNamespaceNetworkRuleCreateUpdate,
Read: resourceServiceBusNamespaceNetworkRuleRead,
Update: resourceServiceBusNamespaceNetworkRuleCreateUpdate,
Delete: resourceServiceBusNamespaceNetworkRuleDelete,
Create: resourceServiceBusNamespaceNetworkRuleSetCreateUpdate,
Read: resourceServiceBusNamespaceNetworkRuleSetRead,
Update: resourceServiceBusNamespaceNetworkRuleSetCreateUpdate,
Delete: resourceServiceBusNamespaceNetworkRuleSetDelete,

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ServiceBusNamespaceNetworkRuleID(id)
_, err := parse.ServiceBusNamespaceNetworkRuleSetID(id)
return err
}),

Expand Down Expand Up @@ -60,7 +60,7 @@ func resourceServiceBusNamespaceNetworkRule() *schema.Resource {
}, false),
},

"ip_masks": {
"ip_rules": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Expand Down Expand Up @@ -93,7 +93,7 @@ func resourceServiceBusNamespaceNetworkRule() *schema.Resource {
}
}

func resourceServiceBusNamespaceNetworkRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceServiceBusNamespaceNetworkRuleSetCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceBus.NamespacesClientPreview
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand All @@ -105,59 +105,59 @@ func resourceServiceBusNamespaceNetworkRuleCreateUpdate(d *schema.ResourceData,
existing, err := client.GetNetworkRuleSet(ctx, resourceGroup, namespaceName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("failed to check for presence of existing Service Bus Namespace Network Rule (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
return fmt.Errorf("failed to check for presence of existing Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
}
}

// This resource is unique to the corresponding service bus namespace.
// It will be created automatically along with the namespace, therefore we check whether this resource is identical to a "deleted" one
if !CheckNetworkRuleNullified(existing) {
return tf.ImportAsExistsError("azurerm_servicebus_namespace_network_rule", *existing.ID)
return tf.ImportAsExistsError("azurerm_servicebus_namespace_network_rule_set", *existing.ID)
}
}

parameters := servicebus.NetworkRuleSet{
NetworkRuleSetProperties: &servicebus.NetworkRuleSetProperties{
DefaultAction: servicebus.DefaultAction(d.Get("default_action").(string)),
VirtualNetworkRules: expandServiceBusNamespaceVirtualNetworkRules(d.Get("network_rules").(*schema.Set).List()),
IPRules: expandServiceBusNamespaceIPRules(d.Get("ip_masks").(*schema.Set).List()),
IPRules: expandServiceBusNamespaceIPRules(d.Get("ip_rules").(*schema.Set).List()),
},
}

if _, err := client.CreateOrUpdateNetworkRuleSet(ctx, resourceGroup, namespaceName, parameters); err != nil {
return fmt.Errorf("failed to create Service Bus Namespace Network Rule (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
return fmt.Errorf("failed to create Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
}

resp, err := client.GetNetworkRuleSet(ctx, resourceGroup, namespaceName)
if err != nil {
return fmt.Errorf("failed to retrieve Service Bus Namespace Network Rule (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
return fmt.Errorf("failed to retrieve Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q): %+v", namespaceName, resourceGroup, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("cannot read Service Bus Namespace Network Rule (Namespace %q / Resource Group %q) ID", namespaceName, resourceGroup)
return fmt.Errorf("cannot read Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q) ID", namespaceName, resourceGroup)
}
d.SetId(*resp.ID)

return resourceServiceBusNamespaceNetworkRuleRead(d, meta)
return resourceServiceBusNamespaceNetworkRuleSetRead(d, meta)
}

func resourceServiceBusNamespaceNetworkRuleRead(d *schema.ResourceData, meta interface{}) error {
func resourceServiceBusNamespaceNetworkRuleSetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceBus.NamespacesClientPreview
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ServiceBusNamespaceNetworkRuleID(d.Id())
id, err := parse.ServiceBusNamespaceNetworkRuleSetID(d.Id())
if err != nil {
return err
}

resp, err := client.GetNetworkRuleSet(ctx, id.ResourceGroup, id.NamespaceName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Service Bus Namespace Network Rule %q does not exist - removing from state", d.Id())
log.Printf("[INFO] Service Bus Namespace Network Rule Set %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("failed to read Service Bus Namespace Network Rule %q (Namespace %q / Resource Group %q): %+v", id.Name, id.NamespaceName, id.ResourceGroup, err)
return fmt.Errorf("failed to read Service Bus Namespace Network Rule Set %q (Namespace %q / Resource Group %q): %+v", id.Name, id.NamespaceName, id.ResourceGroup, err)
}

d.Set("namespace_name", id.NamespaceName)
Expand All @@ -170,20 +170,20 @@ func resourceServiceBusNamespaceNetworkRuleRead(d *schema.ResourceData, meta int
return fmt.Errorf("failed to set `network_rules`: %+v", err)
}

if err := d.Set("ip_masks", flattenServiceBusNamespaceIPRules(props.IPRules)); err != nil {
return fmt.Errorf("failed to set `ip_masks`: %+v", err)
if err := d.Set("ip_rules", flattenServiceBusNamespaceIPRules(props.IPRules)); err != nil {
return fmt.Errorf("failed to set `ip_rules`: %+v", err)
}
}

return nil
}

func resourceServiceBusNamespaceNetworkRuleDelete(d *schema.ResourceData, meta interface{}) error {
func resourceServiceBusNamespaceNetworkRuleSetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ServiceBus.NamespacesClientPreview
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ServiceBusNamespaceNetworkRuleID(d.Id())
id, err := parse.ServiceBusNamespaceNetworkRuleSetID(d.Id())
if err != nil {
return err
}
Expand All @@ -198,7 +198,7 @@ func resourceServiceBusNamespaceNetworkRuleDelete(d *schema.ResourceData, meta i
}

if _, err := client.CreateOrUpdateNetworkRuleSet(ctx, id.ResourceGroup, id.NamespaceName, parameters); err != nil {
return fmt.Errorf("failed to delete Service Bus Namespace Network Rule %q (Namespace %q / Resource Group %q): %+v", id.Name, id.NamespaceName, id.ResourceGroup, err)
return fmt.Errorf("failed to delete Service Bus Namespace Network Rule Set %q (Namespace %q / Resource Group %q): %+v", id.Name, id.NamespaceName, id.ResourceGroup, err)
}

return nil
Expand Down
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestAccAzureRMServiceBusNamespaceNetworkRule_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule", "test")
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand All @@ -33,7 +33,7 @@ func TestAccAzureRMServiceBusNamespaceNetworkRule_basic(t *testing.T) {
}

func TestAccAzureRMServiceBusNamespaceNetworkRule_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule", "test")
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand All @@ -52,7 +52,7 @@ func TestAccAzureRMServiceBusNamespaceNetworkRule_complete(t *testing.T) {
}

func TestAccAzureRMServiceBusNamespaceNetworkRule_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule", "test")
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestAccAzureRMServiceBusNamespaceNetworkRule_update(t *testing.T) {
}

func TestAccAzureRMServiceBusNamespaceNetworkRule_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule", "test")
data := acceptance.BuildTestData(t, "azurerm_servicebus_namespace_network_rule_set", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand All @@ -110,17 +110,17 @@ func testCheckAzureRMServiceBusNamespaceNetworkRuleExists(resourceName string) r

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Service Bus Namespace Network Rule not found: %s", resourceName)
return fmt.Errorf("Service Bus Namespace Network Rule Set not found: %s", resourceName)
}

id, err := parse.ServiceBusNamespaceNetworkRuleID(rs.Primary.ID)
id, err := parse.ServiceBusNamespaceNetworkRuleSetID(rs.Primary.ID)
if err != nil {
return err
}

if resp, err := client.GetNetworkRuleSet(ctx, id.ResourceGroup, id.NamespaceName); err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Service Bus Namespace Network Rule (Namespace %q / Resource Group %q) does not exist", id.NamespaceName, id.ResourceGroup)
return fmt.Errorf("Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q) does not exist", id.NamespaceName, id.ResourceGroup)
}
return fmt.Errorf("failed to GetNetworkRuleSet on ServiceBus.NamespacesClientPreview: %+v", err)
}
Expand All @@ -134,11 +134,11 @@ func testCheckAzureRMServiceBusNamespaceNetworkRuleDestroy(s *terraform.State) e
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_servicebus_namespace_network_rule" {
if rs.Type != "azurerm_servicebus_namespace_network_rule_set" {
continue
}

id, err := parse.ServiceBusNamespaceNetworkRuleID(rs.Primary.ID)
id, err := parse.ServiceBusNamespaceNetworkRuleSetID(rs.Primary.ID)
if err != nil {
return err
}
Expand All @@ -153,7 +153,7 @@ func testCheckAzureRMServiceBusNamespaceNetworkRuleDestroy(s *terraform.State) e
}

if !servicebus.CheckNetworkRuleNullified(resp) {
return fmt.Errorf("the Service Bus Namespace Network Rule (Namespace %q / Resource Group %q) still exists", id.NamespaceName, id.ResourceGroup)
return fmt.Errorf("the Service Bus Namespace Network Rule Set (Namespace %q / Resource Group %q) still exists", id.NamespaceName, id.ResourceGroup)
}
}

Expand All @@ -165,7 +165,7 @@ func testAccAzureRMServiceBusNamespaceNetworkRule_basic(data acceptance.TestData
return fmt.Sprintf(`
%s
resource "azurerm_servicebus_namespace_network_rule" "test" {
resource "azurerm_servicebus_namespace_network_rule_set" "test" {
namespace_name = azurerm_servicebus_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
Expand All @@ -184,7 +184,7 @@ func testAccAzureRMServiceBusNamespaceNetworkRule_complete(data acceptance.TestD
return fmt.Sprintf(`
%s
resource "azurerm_servicebus_namespace_network_rule" "test" {
resource "azurerm_servicebus_namespace_network_rule_set" "test" {
namespace_name = azurerm_servicebus_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
Expand All @@ -195,7 +195,7 @@ resource "azurerm_servicebus_namespace_network_rule" "test" {
ignore_missing_vnet_service_endpoint = false
}
ip_masks = ["1.1.1.1"]
ip_rules = ["1.1.1.1"]
}
`, template)
}
Expand Down Expand Up @@ -244,9 +244,9 @@ func testAccAzureRMServiceBusNamespaceNetworkRule_requiresImport(data acceptance
return fmt.Sprintf(`
%s
resource "azurerm_servicebus_namespace_network_rule" "import" {
namespace_name = azurerm_servicebus_namespace_network_rule.test.namespace_name
resource_group_name = azurerm_servicebus_namespace_network_rule.test.resource_group_name
resource "azurerm_servicebus_namespace_network_rule_set" "import" {
namespace_name = azurerm_servicebus_namespace_network_rule_set.test.namespace_name
resource_group_name = azurerm_servicebus_namespace_network_rule_set.test.resource_group_name
}
`, template)
}

This file was deleted.

@@ -0,0 +1,22 @@
package validate

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/parse"
)

func ServiceBusNamespaceNetworkRuleSetID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := parse.ServiceBusNamespaceNetworkRuleSetID(v); err != nil {
errors = append(errors, fmt.Errorf("cannot parse %q as a Service Bus Namespace Network Rule Set ID: %+v", k, err))
return
}

return warnings, errors
}

0 comments on commit be98e95

Please sign in to comment.