Skip to content

Commit

Permalink
servicebus related resources: reference parent resource ID (#14784)
Browse files Browse the repository at this point in the history
Deprecating the original ways to reference parent resources via its
scopes (e.g. resource group name, parent resource name, grand parent
resource name, etc) - instead use the parent resource ID.

The benefit is to allow the force new on parent resource can be
propagate to its child resources.

Fixes #14748
  • Loading branch information
magodo authored Jan 7, 2022
1 parent 4d43e01 commit f25724a
Show file tree
Hide file tree
Showing 24 changed files with 523 additions and 376 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,37 @@ func resourceServiceBusNamespaceAuthorizationRule() *pluginsdk.Resource {
ValidateFunc: validate.AuthorizationRuleName(),
},

// TODO 3.0 - Make it required
"namespace_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NamespaceID,
ConflictsWith: []string{"namespace_name", "resource_group_name"},
},

// TODO 3.0 - Remove in favor of namespace_id
"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
Deprecated: `Deprecated in favor of "namespace_id"`,
ConflictsWith: []string{"namespace_id"},
},

"resource_group_name": azure.SchemaResourceGroupName(),
// TODO 3.0 - Remove in favor of namespace_id
"resource_group_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceGroupName,
Deprecated: `Deprecated in favor of "namespace_id"`,
ConflictsWith: []string{"namespace_id"},
},
}),

CustomizeDiff: pluginsdk.CustomizeDiffShim(authorizationRuleCustomizeDiff),
Expand All @@ -65,7 +88,15 @@ func resourceServiceBusNamespaceAuthorizationRuleCreateUpdate(d *pluginsdk.Resou
defer cancel()

log.Printf("[INFO] preparing arguments for ServiceBus Namespace Authorization Rule create/update.")
resourceId := parse.NewNamespaceAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("name").(string))

var resourceId parse.NamespaceAuthorizationRuleId
if namespaceIdLit := d.Get("namespace_id").(string); namespaceIdLit != "" {
namespaceId, _ := parse.NamespaceID(namespaceIdLit)
resourceId = parse.NewNamespaceAuthorizationRuleID(namespaceId.SubscriptionId, namespaceId.ResourceGroup, namespaceId.Name, d.Get("name").(string))
} else {
resourceId = parse.NewNamespaceAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("name").(string))
}

if d.IsNewResource() {
existing, err := client.GetAuthorizationRule(ctx, resourceId.ResourceGroup, resourceId.NamespaceName, resourceId.AuthorizationRuleName)
if err != nil {
Expand Down Expand Up @@ -126,6 +157,7 @@ func resourceServiceBusNamespaceAuthorizationRuleRead(d *pluginsdk.ResourceData,
d.Set("name", id.AuthorizationRuleName)
d.Set("namespace_name", id.NamespaceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("namespace_id", parse.NewNamespaceID(id.SubscriptionId, id.ResourceGroup, id.NamespaceName).ID())

if properties := resp.SBAuthorizationRuleProperties; properties != nil {
listen, send, manage := flattenAuthorizationRuleRights(properties.Rights)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAccServiceBusNamespaceAuthorizationRule_rightsUpdate(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("namespace_name").Exists(),
check.That(data.ResourceName).Key("namespace_id").Exists(),
check.That(data.ResourceName).Key("primary_key").Exists(),
check.That(data.ResourceName).Key("secondary_key").Exists(),
check.That(data.ResourceName).Key("primary_connection_string").Exists(),
Expand Down Expand Up @@ -136,9 +136,8 @@ resource "azurerm_servicebus_namespace" "test" {
}
resource "azurerm_servicebus_namespace_authorization_rule" "test" {
name = "acctest-%[1]d"
namespace_name = azurerm_servicebus_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
name = "acctest-%[1]d"
namespace_id = azurerm_servicebus_namespace.test.id
listen = %[3]t
send = %[4]t
Expand All @@ -152,9 +151,8 @@ func (r ServiceBusNamespaceAuthorizationRuleResource) requiresImport(data accept
%s
resource "azurerm_servicebus_namespace_authorization_rule" "import" {
name = azurerm_servicebus_namespace_authorization_rule.test.name
namespace_name = azurerm_servicebus_namespace_authorization_rule.test.namespace_name
resource_group_name = azurerm_servicebus_namespace_authorization_rule.test.resource_group_name
name = azurerm_servicebus_namespace_authorization_rule.test.name
namespace_id = azurerm_servicebus_namespace_authorization_rule.test.namespace_id
listen = azurerm_servicebus_namespace_authorization_rule.test.listen
send = azurerm_servicebus_namespace_authorization_rule.test.send
Expand Down Expand Up @@ -202,12 +200,11 @@ resource "azurerm_servicebus_namespace_disaster_recovery_config" "pairing_test"
}
resource "azurerm_servicebus_namespace_authorization_rule" "test" {
name = "namespace_rule_test"
namespace_name = azurerm_servicebus_namespace.primary_namespace_test.name
resource_group_name = azurerm_resource_group.primary.name
listen = true
send = true
manage = true
name = "namespace_rule_test"
namespace_id = azurerm_servicebus_namespace.primary_namespace_test.id
listen = true
send = true
manage = true
depends_on = [
azurerm_servicebus_namespace_disaster_recovery_config.pairing_test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,36 @@ func resourceServiceBusNamespaceNetworkRuleSet() *pluginsdk.Resource {
},

Schema: map[string]*pluginsdk.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),
// TODO 3.0 - Make it required
"namespace_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NamespaceID,
ConflictsWith: []string{"namespace_name", "resource_group_name"},
},

// TODO 3.0 - Remove in favor of namespace_id
"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
Deprecated: `Deprecated in favor of "namespace_id"`,
ConflictsWith: []string{"namespace_id"},
},

// TODO 3.0 - Remove in favor of namespace_id
"resource_group_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceGroupName,
Deprecated: `Deprecated in favor of "namespace_id"`,
ConflictsWith: []string{"namespace_id"},
},

"default_action": {
Expand Down Expand Up @@ -107,7 +130,14 @@ func resourceServiceBusNamespaceNetworkRuleSetCreateUpdate(d *pluginsdk.Resource
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

resourceId := parse.NewNamespaceNetworkRuleSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), namespaceNetworkRuleSetName)
var resourceId parse.NamespaceNetworkRuleSetId
if namespaceIdLit := d.Get("namespace_id").(string); namespaceIdLit != "" {
namespaceId, _ := parse.NamespaceID(namespaceIdLit)
resourceId = parse.NewNamespaceNetworkRuleSetID(namespaceId.SubscriptionId, namespaceId.ResourceGroup, namespaceId.Name, namespaceNetworkRuleSetName)
} else {
resourceId = parse.NewNamespaceNetworkRuleSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), namespaceNetworkRuleSetName)
}

if d.IsNewResource() {
existing, err := client.GetNetworkRuleSet(ctx, resourceId.ResourceGroup, resourceId.NamespaceName)
if err != nil {
Expand Down Expand Up @@ -172,6 +202,7 @@ func resourceServiceBusNamespaceNetworkRuleSetRead(d *pluginsdk.ResourceData, me

d.Set("namespace_name", id.NamespaceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("namespace_id", parse.NewNamespaceID(id.SubscriptionId, id.ResourceGroup, id.NamespaceName).ID())

if props := resp.NetworkRuleSetProperties; props != nil {
d.Set("default_action", string(props.DefaultAction))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func (r ServiceBusNamespaceNetworkRuleSetResource) basic(data acceptance.TestDat
%s
resource "azurerm_servicebus_namespace_network_rule_set" "test" {
namespace_name = azurerm_servicebus_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
namespace_id = azurerm_servicebus_namespace.test.id
default_action = "Deny"
Expand All @@ -128,8 +127,7 @@ func (r ServiceBusNamespaceNetworkRuleSetResource) complete(data acceptance.Test
%s
resource "azurerm_servicebus_namespace_network_rule_set" "test" {
namespace_name = azurerm_servicebus_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
namespace_id = azurerm_servicebus_namespace.test.id
default_action = "Deny"
trusted_services_allowed = true
Expand Down Expand Up @@ -188,8 +186,7 @@ func (r ServiceBusNamespaceNetworkRuleSetResource) requiresImport(data acceptanc
%s
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
namespace_id = azurerm_servicebus_namespace_network_rule_set.test.namespace_id
}
`, r.basic(data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,48 @@ func resourceServiceBusQueueAuthorizationRule() *pluginsdk.Resource {
ValidateFunc: validate.AuthorizationRuleName(),
},

"namespace_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
// TODO 3.0 - Make it required
"queue_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.QueueID,
ConflictsWith: []string{"queue_name", "namespace_name", "resource_group_name"},
},

// TODO 3.0 - Remove in favor of queue_id
"queue_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.QueueName(),
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.QueueName(),
Deprecated: `Deprecated in favor of "queue_id"`,
ConflictsWith: []string{"queue_id"},
},

"resource_group_name": azure.SchemaResourceGroupName(),
// TODO 3.0 - Remove in favor of queue_id
"namespace_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.NamespaceName,
Deprecated: `Deprecated in favor of "queue_id"`,
ConflictsWith: []string{"queue_id"},
},

// TODO 3.0 - Remove in favor of queue_id
"resource_group_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceGroupName,
Deprecated: `Deprecated in favor of "queue_id"`,
ConflictsWith: []string{"queue_id"},
},
}),

CustomizeDiff: pluginsdk.CustomizeDiffShim(authorizationRuleCustomizeDiff),
Expand All @@ -72,7 +99,14 @@ func resourceServiceBusQueueAuthorizationRuleCreateUpdate(d *pluginsdk.ResourceD

log.Printf("[INFO] preparing arguments for ServiceBus Queue Authorization Rule creation.")

resourceId := parse.NewQueueAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("queue_name").(string), d.Get("name").(string))
var resourceId parse.QueueAuthorizationRuleId
if queueIdLit := d.Get("queue_id").(string); queueIdLit != "" {
queueId, _ := parse.QueueID(queueIdLit)
resourceId = parse.NewQueueAuthorizationRuleID(queueId.SubscriptionId, queueId.ResourceGroup, queueId.NamespaceName, queueId.Name, d.Get("name").(string))
} else {
resourceId = parse.NewQueueAuthorizationRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("namespace_name").(string), d.Get("queue_name").(string), d.Get("name").(string))
}

if d.IsNewResource() {
existing, err := client.GetAuthorizationRule(ctx, resourceId.ResourceGroup, resourceId.NamespaceName, resourceId.QueueName, resourceId.AuthorizationRuleName)
if err != nil {
Expand Down Expand Up @@ -135,6 +169,7 @@ func resourceServiceBusQueueAuthorizationRuleRead(d *pluginsdk.ResourceData, met
d.Set("queue_name", id.QueueName)
d.Set("namespace_name", id.NamespaceName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("queue_id", parse.NewQueueID(id.SubscriptionId, id.ResourceGroup, id.NamespaceName, id.QueueName).ID())

if properties := resp.SBAuthorizationRuleProperties; properties != nil {
listen, send, manage := flattenAuthorizationRuleRights(properties.Rights)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func testAccServiceBusQueueAuthorizationRule(t *testing.T, listen, send, manage
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("namespace_name").Exists(),
check.That(data.ResourceName).Key("queue_name").Exists(),
check.That(data.ResourceName).Key("queue_id").Exists(),
check.That(data.ResourceName).Key("primary_key").Exists(),
check.That(data.ResourceName).Key("secondary_key").Exists(),
check.That(data.ResourceName).Key("primary_connection_string").Exists(),
Expand Down Expand Up @@ -169,18 +168,15 @@ resource "azurerm_servicebus_namespace" "test" {
}
resource "azurerm_servicebus_queue" "test" {
name = "acctest-%[1]d"
resource_group_name = azurerm_resource_group.test.name
namespace_name = azurerm_servicebus_namespace.test.name
name = "acctest-%[1]d"
namespace_id = azurerm_servicebus_namespace.test.id
enable_partitioning = true
}
resource "azurerm_servicebus_queue_authorization_rule" "test" {
name = "acctest-%[1]d"
namespace_name = azurerm_servicebus_namespace.test.name
queue_name = azurerm_servicebus_queue.test.name
resource_group_name = azurerm_resource_group.test.name
name = "acctest-%[1]d"
queue_id = azurerm_servicebus_queue.test.id
listen = %[3]t
send = %[4]t
Expand All @@ -194,10 +190,8 @@ func (r ServiceBusQueueAuthorizationRuleResource) requiresImport(data acceptance
%s
resource "azurerm_servicebus_queue_authorization_rule" "import" {
name = azurerm_servicebus_queue_authorization_rule.test.name
namespace_name = azurerm_servicebus_queue_authorization_rule.test.namespace_name
queue_name = azurerm_servicebus_queue_authorization_rule.test.queue_name
resource_group_name = azurerm_servicebus_queue_authorization_rule.test.resource_group_name
name = azurerm_servicebus_queue_authorization_rule.test.name
queue_id = azurerm_servicebus_queue_authorization_rule.test.queue_id
listen = azurerm_servicebus_queue_authorization_rule.test.listen
send = azurerm_servicebus_queue_authorization_rule.test.send
Expand Down Expand Up @@ -231,9 +225,8 @@ resource "azurerm_servicebus_namespace" "primary_namespace_test" {
}
resource "azurerm_servicebus_queue" "example" {
name = "queue-test"
resource_group_name = azurerm_resource_group.primary.name
namespace_name = azurerm_servicebus_namespace.primary_namespace_test.name
name = "queue-test"
namespace_id = azurerm_servicebus_namespace.primary_namespace_test.id
}
resource "azurerm_servicebus_namespace" "secondary_namespace_test" {
Expand All @@ -251,13 +244,11 @@ resource "azurerm_servicebus_namespace_disaster_recovery_config" "pairing_test"
}
resource "azurerm_servicebus_queue_authorization_rule" "test" {
name = "example_queue_rule"
namespace_name = azurerm_servicebus_namespace.primary_namespace_test.name
queue_name = azurerm_servicebus_queue.example.name
resource_group_name = azurerm_resource_group.primary.name
manage = true
listen = true
send = true
name = "example_queue_rule"
queue_id = azurerm_servicebus_queue.example.id
manage = true
listen = true
send = true
depends_on = [
azurerm_servicebus_namespace_disaster_recovery_config.pairing_test
Expand Down
Loading

0 comments on commit f25724a

Please sign in to comment.