Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add servicebus #525

Merged
merged 16 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions examples/azure/terraform-azure-servicebus-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ provider "azurerm" {
# DEPLOY A RESOURCE GROUP
# ---------------------------------------------------------------------------------------------------------------------

resource "azurerm_resource_group" "servicebus" {
name = "terratest-rg-${var.postfix}"
resource "azurerm_resource_group" "servicebus_rg" {
name = "terratest-sb-rg-${var.postfix}"
location = var.location
}

# ---------------------------------------------------------------------------------------------------------------------
# DDefine locals variables
# Define locals variables
# ---------------------------------------------------------------------------------------------------------------------
locals {
topic_authorization_rules = flatten([
Expand Down Expand Up @@ -66,8 +66,8 @@ locals {
# ---------------------------------------------------------------------------------------------------------------------
resource "azurerm_servicebus_namespace" "servicebus" {
name = "terratest-namespace-${var.namespace_name}"
location = azurerm_resource_group.servicebus.location
resource_group_name = azurerm_resource_group.servicebus.name
location = azurerm_resource_group.servicebus_rg.location
resource_group_name = azurerm_resource_group.servicebus_rg.name
sku = var.sku
tags = var.tags
}
Expand All @@ -80,7 +80,7 @@ resource "azurerm_servicebus_namespace_authorization_rule" "sbnamespaceauth" {

name = var.namespace_authorization_rules[count.index].policy_name
namespace_name = azurerm_servicebus_namespace.servicebus.name
resource_group_name = azurerm_resource_group.servicebus.name
resource_group_name = azurerm_resource_group.servicebus_rg.name

listen = var.namespace_authorization_rules[count.index].claims.listen
send = var.namespace_authorization_rules[count.index].claims.send
Expand All @@ -94,7 +94,7 @@ resource "azurerm_servicebus_topic" "sptopic" {
count = length(var.topics)

name = var.topics[count.index].name
resource_group_name = azurerm_resource_group.servicebus.name
resource_group_name = azurerm_resource_group.servicebus_rg.name
namespace_name = azurerm_servicebus_namespace.servicebus.name

requires_duplicate_detection = var.topics[count.index].requires_duplicate_detection
Expand All @@ -110,7 +110,7 @@ resource "azurerm_servicebus_topic_authorization_rule" "topicaauth" {
count = length(local.topic_authorization_rules)

name = local.topic_authorization_rules[count.index].policy_name
resource_group_name = azurerm_resource_group.servicebus.name
resource_group_name = azurerm_resource_group.servicebus_rg.name
namespace_name = azurerm_servicebus_namespace.servicebus.name
topic_name = local.topic_authorization_rules[count.index].topic_name

Expand All @@ -128,7 +128,7 @@ resource "azurerm_servicebus_subscription" "subscription" {
count = length(local.topic_subscriptions)

name = local.topic_subscriptions[count.index].name
resource_group_name = azurerm_resource_group.servicebus.name
resource_group_name = azurerm_resource_group.servicebus_rg.name
namespace_name = azurerm_servicebus_namespace.servicebus.name
topic_name = local.topic_subscriptions[count.index].topic_name

Expand All @@ -147,7 +147,7 @@ resource "azurerm_servicebus_subscription_rule" "subrules" {
count = length(local.topic_subscription_rules)

name = local.topic_subscription_rules[count.index].name
resource_group_name = azurerm_resource_group.servicebus.name
resource_group_name = azurerm_resource_group.servicebus_rg.name
namespace_name = azurerm_servicebus_namespace.servicebus.name
topic_name = local.topic_subscription_rules[count.index].topic_name
subscription_name = local.topic_subscription_rules[count.index].subscription_name
Expand All @@ -156,4 +156,4 @@ resource "azurerm_servicebus_subscription_rule" "subrules" {
action = local.topic_subscription_rules[count.index].action

depends_on = [azurerm_servicebus_subscription.subscription]
}
}
14 changes: 9 additions & 5 deletions examples/azure/terraform-azure-servicebus-example/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
output "resource_group" {
description = "The resource group name of the Service Bus namespace."
value = azurerm_resource_group.servicebus_rg.name
}

output "namespace_name" {
description = "The namespace name."
value = azurerm_servicebus_namespace.servicebus.name
}

output "resource_group" {
description = "The resource group name of the Service Bus namespace."
value = azurerm_resource_group.servicebus.name
}

output "namespace_id" {
description = "The namespace ID."
value = azurerm_servicebus_namespace.servicebus.id
sensitive = true
}

output "namespace_authorization_rules" {
Expand All @@ -23,16 +24,19 @@ output "namespace_authorization_rules" {
manage = auth.manage
}
}
sensitive = true
}

helayoty marked this conversation as resolved.
Show resolved Hide resolved
output "service_bus_namespace_default_primary_key" {
description = "The primary access key for the authorization rule RootManageSharedAccessKey which is created automatically by Azure."
value = azurerm_servicebus_namespace.servicebus.default_primary_key
sensitive = true
}

helayoty marked this conversation as resolved.
Show resolved Hide resolved
output "service_bus_namespace_default_connection_string" {
description = "The primary connection string for the authorization rule RootManageSharedAccessKey which is created automatically by Azure."
value = azurerm_servicebus_namespace.servicebus.default_primary_connection_string
sensitive = true
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# ---------------------------------------------------------------------------------------------------------------------

variable "postfix" {
helayoty marked this conversation as resolved.
Show resolved Hide resolved
description = "The name of the resource group for the provisioned service bus."
description = "string mitigate resource name collisions."
type = string
helayoty marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
94 changes: 39 additions & 55 deletions modules/azure/servicebus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/stretchr/testify/require"
)

func serviceBusNamespaceClientE(subscriptionID string) (*servicebus.NamespacesClient, error) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func serviceBusSubscriptionsClientE(subscriptionID string) (*servicebus.Subscrip
return &sClient, nil
}

// ListServiceBusNamespaceE list all SB namespaces in all resource groups in the given subscription ID
// ListServiceBusNamespaceE list all SB namespaces in all resource groups in the given subscription ID. This function would fail the test if there is an error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is incorrect with the function (E flavor returns errors, not fail the test). This applies to all functions in this file.

Suggested change
// ListServiceBusNamespaceE list all SB namespaces in all resource groups in the given subscription ID. This function would fail the test if there is an error.
// ListServiceBusNamespaceE list all SB namespaces in all resource groups in the given subscription ID.

func ListServiceBusNamespaceE(subscriptionID string) (*[]servicebus.SBNamespace, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch these slice pointer return values to simple slices (applies to all functions in this file)? Note that slices are already pointers so you can still return nil.

In general, we try to avoid usage of pointers, especially pointers to slices as it leads to increased complexity unless it is necessary. For example, in this case you will have to do nil checks whenever you access these slices as nil is no longer equivalent to [] and the dereference in the range calls can lead to panics. Additionally, slice pointers are most useful when you want to pass by reference, and the use case for that is usually limited to function args.

nsClient, err := serviceBusNamespaceClientE(subscriptionID)
if err != nil {
Expand All @@ -64,17 +65,16 @@ func ListServiceBusNamespaceE(subscriptionID string) (*[]servicebus.SBNamespace,
return &results, nil
}

// ListServiceBusNamespace - like ListServiceBusNamespaceE but fails in the case an error is returned
// ListServiceBusNamespace - list all SB namespaces in all resource groups in the given subscription ID.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Non-E flavored functions should include the text that this would fail the test. This applies to all functions in this file.

func ListServiceBusNamespace(t *testing.T, subscriptionID string) *[]servicebus.SBNamespace {
results, err := ListServiceBusNamespaceE(subscriptionID)
if err != nil {
t.Fatal(err)
}

require.NoError(t, err)

return results
}

// ListServiceBusNamespaceNamesE list names of all SB namespaces in all resource groups in the given subscription ID
// ListServiceBusNamespaceNamesE list names of all SB namespaces in all resource groups in the given subscription ID. This function would fail the test if there is an error.
func ListServiceBusNamespaceNamesE(subscriptionID string) (*[]string, error) {
sbNamespace, err := ListServiceBusNamespaceE(subscriptionID)

Expand All @@ -93,17 +93,16 @@ func ListServiceBusNamespaceNamesE(subscriptionID string) (*[]string, error) {
return &results, nil
}

// ListServiceBusNamespaceNames like ListServiceBusNamespaceNamesE but fails in the case an error is returned
// ListServiceBusNamespaceNames list names of all SB namespaces in all resource groups in the given subscription ID.
func ListServiceBusNamespaceNames(t *testing.T, subscriptionID string) *[]string {
results, err := ListServiceBusNamespaceNamesE(subscriptionID)
if err != nil {
t.Fatal(err)
}

require.NoError(t, err)

return results
}

// ListServiceBusNamespaceIDsE list IDs of all SB namespaces in all resource groups in the given subscription ID
// ListServiceBusNamespaceIDsE list IDs of all SB namespaces in all resource groups in the given subscription ID. This function would fail the test if there is an error.
func ListServiceBusNamespaceIDsE(subscriptionID string) (*[]string, error) {
sbNamespace, err := ListServiceBusNamespaceE(subscriptionID)

Expand All @@ -122,17 +121,15 @@ func ListServiceBusNamespaceIDsE(subscriptionID string) (*[]string, error) {
return &results, nil
}

// ListServiceBusNamespaceIDs like ListServiceBusNamespaceIDsE but fails in the case an error is returned
// ListServiceBusNamespaceIDs list IDs of all SB namespaces in all resource groups in the given subscription ID.
func ListServiceBusNamespaceIDs(t *testing.T, subscriptionID string) *[]string {
results, err := ListServiceBusNamespaceIDsE(subscriptionID)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListServiceBusNamespaceByResourceGroupE list all SB namespaces in the given resource group
// ListServiceBusNamespaceByResourceGroupE list all SB namespaces in the given resource group. This function would fail the test if there is an error.
func ListServiceBusNamespaceByResourceGroupE(subscriptionID string, resourceGroup string) (*[]servicebus.SBNamespace, error) {
nsClient, err := serviceBusNamespaceClientE(subscriptionID)
if err != nil {
Expand All @@ -157,17 +154,15 @@ func ListServiceBusNamespaceByResourceGroupE(subscriptionID string, resourceGrou
return &results, nil
}

// ListServiceBusNamespaceByResourceGroup like ListServiceBusNamespaceByResourceGroupE but fails in the case an error is returned
// ListServiceBusNamespaceByResourceGroup list all SB namespaces in the given resource group.
func ListServiceBusNamespaceByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) *[]servicebus.SBNamespace {
results, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListServiceBusNamespaceNamesByResourceGroupE list names of all SB namespaces in the given resource group
// ListServiceBusNamespaceNamesByResourceGroupE list names of all SB namespaces in the given resource group. This function would fail the test if there is an error.
func ListServiceBusNamespaceNamesByResourceGroupE(subscriptionID string, resourceGroup string) (*[]string, error) {
sbNamespace, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)

Expand All @@ -186,17 +181,15 @@ func ListServiceBusNamespaceNamesByResourceGroupE(subscriptionID string, resourc
return &results, nil
}

// ListServiceBusNamespaceNamesByResourceGroup like ListServiceBusNamespaceNamesByResourceGroupE but fails in the case an error is returned
// ListServiceBusNamespaceNamesByResourceGroup list names of all SB namespaces in the given resource group.
func ListServiceBusNamespaceNamesByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) *[]string {
results, err := ListServiceBusNamespaceNamesByResourceGroupE(subscriptionID, resourceGroup)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListServiceBusNamespaceIDsByResourceGroupE list IDs of all SB namespaces in the given resource group
// ListServiceBusNamespaceIDsByResourceGroupE list IDs of all SB namespaces in the given resource group. This function would fail the test if there is an error.
func ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID string, resourceGroup string) (*[]string, error) {
sbNamespace, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)

Expand All @@ -215,18 +208,16 @@ func ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID string, resourceG
return &results, nil
}

// ListServiceBusNamespaceIDsByResourceGroup like ListServiceBusNamespaceIDsByResourceGroupE but fails in the case an error is returned
// ListServiceBusNamespaceIDsByResourceGroup list IDs of all SB namespaces in the given resource group.
func ListServiceBusNamespaceIDsByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) *[]string {
results, err := ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID, resourceGroup)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListNamespaceAuthRulesE - authenticate namespace client and enumerates all values to get list of authorization rules for the given namespace name,
// automatically crossing page boundaries as required.
// automatically crossing page boundaries as required. This function would fail the test if there is an error.
func ListNamespaceAuthRulesE(subscriptionID string, namespace string, resourceGroup string) (*[]string, error) {
nsClient, err := serviceBusNamespaceClientE(subscriptionID)
if err != nil {
Expand All @@ -250,17 +241,16 @@ func ListNamespaceAuthRulesE(subscriptionID string, namespace string, resourceGr
return &results, nil
}

// ListNamespaceAuthRules - like ListNamespaceAuthRulesE but fails in the case an error is returned
// ListNamespaceAuthRules - authenticate namespace client and enumerates all values to get list of authorization rules for the given namespace name,
// automatically crossing page boundaries as required.
func ListNamespaceAuthRules(t *testing.T, subscriptionID string, namespace string, resourceGroup string) *[]string {
results, err := ListNamespaceAuthRulesE(subscriptionID, namespace, resourceGroup)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListNamespaceTopicsE - authenticate topic client and enumerates all values, automatically crossing page boundaries as required.
// ListNamespaceTopicsE - authenticate topic client and enumerates all values, automatically crossing page boundaries as required. This function would fail the test if there is an error.
func ListNamespaceTopicsE(subscriptionID string, namespace string, resourceGroup string) (*[]servicebus.SBTopic, error) {
tClient, err := serviceBusTopicClientE(subscriptionID)
if err != nil {
Expand All @@ -285,17 +275,15 @@ func ListNamespaceTopicsE(subscriptionID string, namespace string, resourceGroup
return &results, nil
}

// ListNamespaceTopics - like ListNamespaceTopicsE but fails in the case an error is returned
// ListNamespaceTopics - authenticate topic client and enumerates all values, automatically crossing page boundaries as required.
func ListNamespaceTopics(t *testing.T, subscriptionID string, namespace string, resourceGroup string) *[]servicebus.SBTopic {
results, err := ListNamespaceTopicsE(subscriptionID, namespace, resourceGroup)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListTopicSubscriptionsE - authenticate subscriptions client and enumerates all values, automatically crossing page boundaries as required.
// ListTopicSubscriptionsE - authenticate subscriptions client and enumerates all values, automatically crossing page boundaries as required. This function would fail the test if there is an error.
func ListTopicSubscriptionsE(subscriptionID string, namespace string, resourceGroup string, topicName string) ([]servicebus.SBSubscription, error) {
sClient, err := serviceBusSubscriptionsClientE(subscriptionID)
if err != nil {
Expand All @@ -319,18 +307,16 @@ func ListTopicSubscriptionsE(subscriptionID string, namespace string, resourceGr
return results, nil
}

// ListTopicSubscriptions - like ListTopicSubscriptionsE but fails in the case an error is returned
// ListTopicSubscriptions - authenticate subscriptions client and enumerates all values, automatically crossing page boundaries as required.
func ListTopicSubscriptions(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) *[]servicebus.SBSubscription {
results, err := ListTopicSubscriptionsE(subscriptionID, namespace, resourceGroup, topicName)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return &results
}

// ListTopicSubscriptionsNameE - authenticate subscriptions client and enumerates all values to get list of subscriptions for the given topic name,
// automatically crossing page boundaries as required.
// automatically crossing page boundaries as required. This function would fail the test if there is an error.
func ListTopicSubscriptionsNameE(subscriptionID string, namespace string, resourceGroup string, topicName string) (*[]string, error) {
sClient, err := serviceBusSubscriptionsClientE(subscriptionID)
if err != nil {
Expand All @@ -353,18 +339,17 @@ func ListTopicSubscriptionsNameE(subscriptionID string, namespace string, resour
return &results, nil
}

// ListTopicSubscriptionsName - like ListTopicSubscriptionsNameE but fails in the case an error is returned
// ListTopicSubscriptionsName - authenticate subscriptions client and enumerates all values to get list of subscriptions for the given topic name,
// automatically crossing page boundaries as required.
func ListTopicSubscriptionsName(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) *[]string {
results, err := ListTopicSubscriptionsNameE(subscriptionID, namespace, resourceGroup, topicName)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}

// ListTopicAuthRulesE - authenticate topic client and enumerates all values to get list of authorization rules for the given topic name,
// automatically crossing page boundaries as required.
// automatically crossing page boundaries as required. This function would fail the test if there is an error.
func ListTopicAuthRulesE(subscriptionID string, namespace string, resourceGroup string, topicName string) (*[]string, error) {
tClient, err := serviceBusTopicClientE(subscriptionID)
if err != nil {
Expand All @@ -388,12 +373,11 @@ func ListTopicAuthRulesE(subscriptionID string, namespace string, resourceGroup
return &results, nil
}

// ListTopicAuthRules - like ListTopicAuthRulesE but fails in the case an error is returned
// ListTopicAuthRules - authenticate topic client and enumerates all values to get list of authorization rules for the given topic name,
// automatically crossing page boundaries as required.
func ListTopicAuthRules(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) *[]string {
results, err := ListTopicAuthRulesE(subscriptionID, namespace, resourceGroup, topicName)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

return results
}
Loading