Skip to content

Commit

Permalink
Add azurerm_servicebus_namespace_network_rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Apr 7, 2020
1 parent 10269d7 commit f5e48d6
Show file tree
Hide file tree
Showing 13 changed files with 999 additions and 15 deletions.
33 changes: 33 additions & 0 deletions azurerm/internal/services/servicebus/parse/namespace.go
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ServiceBusNamespaceId struct {
Name string
ResourceGroup string
}

func ServiceBusNamespaceID(input string) (*ServiceBusNamespaceId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("unable to parse Service Bus Namespace ID %q: %+v", input, err)
}

namespace := ServiceBusNamespaceId{
ResourceGroup: id.ResourceGroup,
}

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

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

return &namespace, nil
}
@@ -0,0 +1,38 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ServiceBusNamespaceNetworkRuleId struct {
Name string
NamespaceName string
ResourceGroup string
}

func ServiceBusNamespaceNetworkRuleID(input string) (*ServiceBusNamespaceNetworkRuleId, 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)
}

rule := ServiceBusNamespaceNetworkRuleId{
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)
}

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)
}

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 &rule, nil
}
@@ -0,0 +1,87 @@
package parse

import "testing"

func TestServiceBusNamespaceNetworkRuleID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expected *ServiceBusNamespaceNetworkRuleId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing Service Bus Namespace Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/",
Error: true,
},
{
Name: "Service Bus Namespace ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/namespace1",
Error: true,
},
{
Name: "Missing Network Rule Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/namespace1/networkrulesets/",
Error: true,
},
{
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{
Name: "default",
NamespaceName: "namespace1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/Namespaces/namespace1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ServiceBusNamespaceNetworkRuleID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.NamespaceName != v.Expected.NamespaceName {
t.Fatalf("Expected %q but got %q for Name", v.Expected.NamespaceName, actual.NamespaceName)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
74 changes: 74 additions & 0 deletions azurerm/internal/services/servicebus/parse/namespace_test.go
@@ -0,0 +1,74 @@
package parse

import (
"testing"
)

func TestServiceBusNamespaceID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expected *ServiceBusNamespaceId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing Service Bus Namespace Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/",
Error: true,
},
{
Name: "Service Bus Namespace ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/namespaces/namespace1",
Expected: &ServiceBusNamespaceId{
ResourceGroup: "resGroup1",
Name: "namespace1",
},
},
{
Name: "Wrong casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ServiceBus/Namespaces/namespace1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ServiceBusNamespaceID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
7 changes: 4 additions & 3 deletions azurerm/internal/services/servicebus/registration.go
Expand Up @@ -31,12 +31,13 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_servicebus_namespace_authorization_rule": resourceArmServiceBusNamespaceAuthorizationRule(),
"azurerm_servicebus_namespace": resourceArmServiceBusNamespace(),
"azurerm_servicebus_queue_authorization_rule": resourceArmServiceBusQueueAuthorizationRule(),
"azurerm_servicebus_namespace_authorization_rule": resourceArmServiceBusNamespaceAuthorizationRule(),
"azurerm_servicebus_namespace_network_rule": resourceServiceBusNamespaceNetworkRule(),
"azurerm_servicebus_queue": resourceArmServiceBusQueue(),
"azurerm_servicebus_subscription_rule": resourceArmServiceBusSubscriptionRule(),
"azurerm_servicebus_queue_authorization_rule": resourceArmServiceBusQueueAuthorizationRule(),
"azurerm_servicebus_subscription": resourceArmServiceBusSubscription(),
"azurerm_servicebus_subscription_rule": resourceArmServiceBusSubscriptionRule(),
"azurerm_servicebus_topic_authorization_rule": resourceArmServiceBusTopicAuthorizationRule(),
"azurerm_servicebus_topic": resourceArmServiceBusTopic(),
}
Expand Down
Expand Up @@ -3,7 +3,6 @@ package servicebus
import (
"fmt"
"log"
"regexp"
"strings"
"time"

Expand All @@ -16,7 +15,10 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/servicebus/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -32,9 +34,10 @@ func resourceArmServiceBusNamespace() *schema.Resource {
Update: resourceArmServiceBusNamespaceCreateUpdate,
Delete: resourceArmServiceBusNamespaceDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ServiceBusNamespaceID(id)
return err
}),

MigrateState: ResourceAzureRMServiceBusNamespaceMigrateState,
SchemaVersion: 1,
Expand All @@ -48,13 +51,10 @@ func resourceArmServiceBusNamespace() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-a-zA-Z0-9]{0,100}[a-zA-Z0-9]$"),
"The namespace can contain only letters, numbers, and hyphens. The namespace must start with a letter, and it must end with a letter or number.",
),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ServiceBusNamespaceName,
},

"location": azure.SchemaLocation(),
Expand Down

0 comments on commit f5e48d6

Please sign in to comment.