github.com/mponton/terratest@v0.44.0/modules/azure/servicebus.go (about)

     1  package azure
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func serviceBusNamespaceClientE(subscriptionID string) (*servicebus.NamespacesClient, error) {
    12  	authorizer, err := NewAuthorizer()
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  
    17  	nsClient := servicebus.NewNamespacesClient(subscriptionID)
    18  	nsClient.Authorizer = *authorizer
    19  	return &nsClient, nil
    20  }
    21  
    22  func serviceBusTopicClientE(subscriptionID string) (*servicebus.TopicsClient, error) {
    23  	authorizer, err := NewAuthorizer()
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	tClient := servicebus.NewTopicsClient(subscriptionID)
    29  	tClient.Authorizer = *authorizer
    30  	return &tClient, nil
    31  }
    32  
    33  func serviceBusSubscriptionsClientE(subscriptionID string) (*servicebus.SubscriptionsClient, error) {
    34  	authorizer, err := NewAuthorizer()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	sClient := servicebus.NewSubscriptionsClient(subscriptionID)
    40  	sClient.Authorizer = *authorizer
    41  	return &sClient, nil
    42  }
    43  
    44  // ListServiceBusNamespaceE list all SB namespaces in all resource groups in the given subscription ID.
    45  func ListServiceBusNamespaceE(subscriptionID string) ([]servicebus.SBNamespace, error) {
    46  	nsClient, err := serviceBusNamespaceClientE(subscriptionID)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	iteratorSBNamespace, err := nsClient.ListComplete(context.Background())
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	results := make([]servicebus.SBNamespace, 0)
    57  	for iteratorSBNamespace.NotDone() {
    58  		results = append(results, iteratorSBNamespace.Value())
    59  		if err := iteratorSBNamespace.Next(); err != nil {
    60  			return nil, err
    61  		}
    62  	}
    63  
    64  	return results, nil
    65  }
    66  
    67  // ListServiceBusNamespace - list all SB namespaces in all resource groups in the given subscription ID. This function would fail the test if there is an error.
    68  func ListServiceBusNamespace(t *testing.T, subscriptionID string) []servicebus.SBNamespace {
    69  	results, err := ListServiceBusNamespaceE(subscriptionID)
    70  
    71  	require.NoError(t, err)
    72  
    73  	return results
    74  }
    75  
    76  // ListServiceBusNamespaceNamesE list names of all SB namespaces in all resource groups in the given subscription ID.
    77  func ListServiceBusNamespaceNamesE(subscriptionID string) ([]string, error) {
    78  	sbNamespace, err := ListServiceBusNamespaceE(subscriptionID)
    79  
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	results := BuildNamespaceNamesList(sbNamespace)
    85  	return results, nil
    86  }
    87  
    88  // BuildNamespaceNamesList helper method to build namespace name list
    89  func BuildNamespaceNamesList(sbNamespace []servicebus.SBNamespace) []string {
    90  	results := []string{}
    91  	for _, namespace := range sbNamespace {
    92  		results = append(results, *namespace.Name)
    93  
    94  	}
    95  
    96  	return results
    97  }
    98  
    99  // BuildNamespaceIdsList helper method to build namespace id list
   100  func BuildNamespaceIdsList(sbNamespace []servicebus.SBNamespace) []string {
   101  	results := []string{}
   102  	for _, namespace := range sbNamespace {
   103  		results = append(results, *namespace.ID)
   104  
   105  	}
   106  
   107  	return results
   108  }
   109  
   110  // ListServiceBusNamespaceNames 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.
   111  func ListServiceBusNamespaceNames(t *testing.T, subscriptionID string) []string {
   112  	results, err := ListServiceBusNamespaceNamesE(subscriptionID)
   113  
   114  	require.NoError(t, err)
   115  
   116  	return results
   117  }
   118  
   119  // ListServiceBusNamespaceIDsE list IDs of all SB namespaces in all resource groups in the given subscription ID.
   120  func ListServiceBusNamespaceIDsE(subscriptionID string) ([]string, error) {
   121  	sbNamespace, err := ListServiceBusNamespaceE(subscriptionID)
   122  
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	results := BuildNamespaceIdsList(sbNamespace)
   128  	return results, nil
   129  }
   130  
   131  // ListServiceBusNamespaceIDs 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.
   132  func ListServiceBusNamespaceIDs(t *testing.T, subscriptionID string) []string {
   133  	results, err := ListServiceBusNamespaceIDsE(subscriptionID)
   134  	require.NoError(t, err)
   135  
   136  	return results
   137  }
   138  
   139  // ListServiceBusNamespaceByResourceGroupE list all SB namespaces in the given resource group.
   140  func ListServiceBusNamespaceByResourceGroupE(subscriptionID string, resourceGroup string) ([]servicebus.SBNamespace, error) {
   141  	nsClient, err := serviceBusNamespaceClientE(subscriptionID)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	iteratorSBNamespace, err := nsClient.ListByResourceGroupComplete(context.Background(), resourceGroup)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	results := make([]servicebus.SBNamespace, 0)
   152  
   153  	for iteratorSBNamespace.NotDone() {
   154  		results = append(results, iteratorSBNamespace.Value())
   155  		if err := iteratorSBNamespace.Next(); err != nil {
   156  			return nil, err
   157  		}
   158  	}
   159  
   160  	return results, nil
   161  }
   162  
   163  // ListServiceBusNamespaceByResourceGroup list all SB namespaces in the given resource group. This function would fail the test if there is an error.
   164  func ListServiceBusNamespaceByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) []servicebus.SBNamespace {
   165  	results, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)
   166  	require.NoError(t, err)
   167  
   168  	return results
   169  }
   170  
   171  // ListServiceBusNamespaceNamesByResourceGroupE list names of all SB namespaces in the given resource group. This function would fail the test if there is an error.
   172  func ListServiceBusNamespaceNamesByResourceGroupE(subscriptionID string, resourceGroup string) ([]string, error) {
   173  	sbNamespace, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)
   174  
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	results := BuildNamespaceNamesList(sbNamespace)
   180  	return results, nil
   181  }
   182  
   183  // ListServiceBusNamespaceNamesByResourceGroup list names of all SB namespaces in the given resource group.
   184  func ListServiceBusNamespaceNamesByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) []string {
   185  	results, err := ListServiceBusNamespaceNamesByResourceGroupE(subscriptionID, resourceGroup)
   186  	require.NoError(t, err)
   187  
   188  	return results
   189  }
   190  
   191  // ListServiceBusNamespaceIDsByResourceGroupE list IDs of all SB namespaces in the given resource group.
   192  func ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID string, resourceGroup string) ([]string, error) {
   193  	sbNamespace, err := ListServiceBusNamespaceByResourceGroupE(subscriptionID, resourceGroup)
   194  
   195  	if err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	results := BuildNamespaceIdsList(sbNamespace)
   200  	return results, nil
   201  }
   202  
   203  // ListServiceBusNamespaceIDsByResourceGroup list IDs of all SB namespaces in the given resource group. This function would fail the test if there is an error.
   204  func ListServiceBusNamespaceIDsByResourceGroup(t *testing.T, subscriptionID string, resourceGroup string) []string {
   205  	results, err := ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID, resourceGroup)
   206  	require.NoError(t, err)
   207  
   208  	return results
   209  }
   210  
   211  // ListNamespaceAuthRulesE - authenticate namespace client and enumerates all values to get list of authorization rules for the given namespace name,
   212  // automatically crossing page boundaries as required.
   213  func ListNamespaceAuthRulesE(subscriptionID string, namespace string, resourceGroup string) ([]string, error) {
   214  	nsClient, err := serviceBusNamespaceClientE(subscriptionID)
   215  	if err != nil {
   216  		return nil, err
   217  	}
   218  	iteratorNamespaceRules, err := nsClient.ListAuthorizationRulesComplete(
   219  		context.Background(), resourceGroup, namespace)
   220  
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	results := []string{}
   226  	for iteratorNamespaceRules.NotDone() {
   227  		results = append(results, *(iteratorNamespaceRules.Value()).Name)
   228  		if err := iteratorNamespaceRules.Next(); err != nil {
   229  			return nil, err
   230  		}
   231  	}
   232  	return results, nil
   233  }
   234  
   235  // ListNamespaceAuthRules - authenticate namespace client and enumerates all values to get list of authorization rules for the given namespace name,
   236  // automatically crossing page boundaries as required. This function would fail the test if there is an error.
   237  func ListNamespaceAuthRules(t *testing.T, subscriptionID string, namespace string, resourceGroup string) []string {
   238  	results, err := ListNamespaceAuthRulesE(subscriptionID, namespace, resourceGroup)
   239  	require.NoError(t, err)
   240  
   241  	return results
   242  }
   243  
   244  // ListNamespaceTopicsE - authenticate topic client and enumerates all values, automatically crossing page boundaries as required.
   245  func ListNamespaceTopicsE(subscriptionID string, namespace string, resourceGroup string) ([]servicebus.SBTopic, error) {
   246  	tClient, err := serviceBusTopicClientE(subscriptionID)
   247  	if err != nil {
   248  		return nil, err
   249  	}
   250  
   251  	iteratorTopics, err := tClient.ListByNamespaceComplete(context.Background(), resourceGroup, namespace, nil, nil)
   252  	if err != nil {
   253  		return nil, err
   254  	}
   255  
   256  	results := make([]servicebus.SBTopic, 0)
   257  
   258  	for iteratorTopics.NotDone() {
   259  		results = append(results, iteratorTopics.Value())
   260  		if err := iteratorTopics.Next(); err != nil {
   261  			return nil, err
   262  		}
   263  	}
   264  
   265  	return results, nil
   266  }
   267  
   268  // ListNamespaceTopics - authenticate topic client and enumerates all values, automatically crossing page boundaries as required. This function would fail the test if there is an error.
   269  func ListNamespaceTopics(t *testing.T, subscriptionID string, namespace string, resourceGroup string) []servicebus.SBTopic {
   270  	results, err := ListNamespaceTopicsE(subscriptionID, namespace, resourceGroup)
   271  	require.NoError(t, err)
   272  
   273  	return results
   274  }
   275  
   276  // ListTopicSubscriptionsE - authenticate subscriptions client and enumerates all values, automatically crossing page boundaries as required.
   277  func ListTopicSubscriptionsE(subscriptionID string, namespace string, resourceGroup string, topicName string) ([]servicebus.SBSubscription, error) {
   278  	sClient, err := serviceBusSubscriptionsClientE(subscriptionID)
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  	iteratorSubscription, err := sClient.ListByTopicComplete(context.Background(), resourceGroup, namespace, topicName, nil, nil)
   283  
   284  	if err != nil {
   285  		return nil, err
   286  	}
   287  
   288  	results := make([]servicebus.SBSubscription, 0)
   289  
   290  	for iteratorSubscription.NotDone() {
   291  		results = append(results, iteratorSubscription.Value())
   292  		if err := iteratorSubscription.Next(); err != nil {
   293  			return nil, err
   294  		}
   295  	}
   296  	return results, nil
   297  }
   298  
   299  // ListTopicSubscriptions - authenticate subscriptions client and enumerates all values, automatically crossing page boundaries as required. This function would fail the test if there is an error.
   300  func ListTopicSubscriptions(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) []servicebus.SBSubscription {
   301  	results, err := ListTopicSubscriptionsE(subscriptionID, namespace, resourceGroup, topicName)
   302  	require.NoError(t, err)
   303  
   304  	return results
   305  }
   306  
   307  // ListTopicSubscriptionsNameE - authenticate subscriptions client and enumerates all values to get list of subscriptions for the given topic name,
   308  // automatically crossing page boundaries as required.
   309  func ListTopicSubscriptionsNameE(subscriptionID string, namespace string, resourceGroup string, topicName string) ([]string, error) {
   310  	sClient, err := serviceBusSubscriptionsClientE(subscriptionID)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  	iteratorSubscription, err := sClient.ListByTopicComplete(context.Background(), resourceGroup, namespace, topicName, nil, nil)
   315  
   316  	if err != nil {
   317  		return nil, err
   318  	}
   319  
   320  	results := []string{}
   321  	for iteratorSubscription.NotDone() {
   322  		results = append(results, *(iteratorSubscription.Value()).Name)
   323  		if err := iteratorSubscription.Next(); err != nil {
   324  			return nil, err
   325  		}
   326  	}
   327  	return results, nil
   328  }
   329  
   330  // ListTopicSubscriptionsName -  authenticate subscriptions client and enumerates all values to get list of subscriptions for the given topic name,
   331  // automatically crossing page boundaries as required. This function would fail the test if there is an error.
   332  func ListTopicSubscriptionsName(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) []string {
   333  	results, err := ListTopicSubscriptionsNameE(subscriptionID, namespace, resourceGroup, topicName)
   334  	require.NoError(t, err)
   335  
   336  	return results
   337  }
   338  
   339  // ListTopicAuthRulesE - authenticate topic client and enumerates all values to get list of authorization rules for the given topic name,
   340  // automatically crossing page boundaries as required.
   341  func ListTopicAuthRulesE(subscriptionID string, namespace string, resourceGroup string, topicName string) ([]string, error) {
   342  	tClient, err := serviceBusTopicClientE(subscriptionID)
   343  	if err != nil {
   344  		return nil, err
   345  	}
   346  	iteratorTopicsRules, err := tClient.ListAuthorizationRulesComplete(
   347  		context.Background(), resourceGroup, namespace, topicName)
   348  
   349  	if err != nil {
   350  		return nil, err
   351  	}
   352  
   353  	results := []string{}
   354  	for iteratorTopicsRules.NotDone() {
   355  		results = append(results, *(iteratorTopicsRules.Value()).Name)
   356  		if err := iteratorTopicsRules.Next(); err != nil {
   357  			return nil, err
   358  		}
   359  	}
   360  	return results, nil
   361  }
   362  
   363  // ListTopicAuthRules - authenticate topic client and enumerates all values to get list of authorization rules for the given topic name,
   364  // automatically crossing page boundaries as required.  This function would fail the test if there is an error.
   365  func ListTopicAuthRules(t *testing.T, subscriptionID string, namespace string, resourceGroup string, topicName string) []string {
   366  	results, err := ListTopicAuthRulesE(subscriptionID, namespace, resourceGroup, topicName)
   367  	require.NoError(t, err)
   368  
   369  	return results
   370  }