github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_eventhub_authorization_rule_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMEventHubAuthorizationRule_listen(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_listen, ri, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMEventHubAuthorizationRuleExists("azurerm_eventhub_authorization_rule.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMEventHubAuthorizationRule_send(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_send, ri, ri, ri, ri)
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
    40  		Steps: []resource.TestStep{
    41  			{
    42  				Config: config,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testCheckAzureRMEventHubAuthorizationRuleExists("azurerm_eventhub_authorization_rule.test"),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func TestAccAzureRMEventHubAuthorizationRule_readwrite(t *testing.T) {
    52  	ri := acctest.RandInt()
    53  	config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_readwrite, ri, ri, ri, ri)
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
    59  		Steps: []resource.TestStep{
    60  			{
    61  				Config: config,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testCheckAzureRMEventHubAuthorizationRuleExists("azurerm_eventhub_authorization_rule.test"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAzureRMEventHubAuthorizationRule_manage(t *testing.T) {
    71  	ri := acctest.RandInt()
    72  	config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_manage, ri, ri, ri, ri)
    73  
    74  	resource.Test(t, resource.TestCase{
    75  		PreCheck:     func() { testAccPreCheck(t) },
    76  		Providers:    testAccProviders,
    77  		CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
    78  		Steps: []resource.TestStep{
    79  			{
    80  				Config: config,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testCheckAzureRMEventHubAuthorizationRuleExists("azurerm_eventhub_authorization_rule.test"),
    83  				),
    84  			},
    85  		},
    86  	})
    87  }
    88  
    89  func testCheckAzureRMEventHubAuthorizationRuleDestroy(s *terraform.State) error {
    90  	conn := testAccProvider.Meta().(*ArmClient).eventHubClient
    91  
    92  	for _, rs := range s.RootModule().Resources {
    93  		if rs.Type != "azurerm_eventhub_authorization_rule" {
    94  			continue
    95  		}
    96  
    97  		name := rs.Primary.Attributes["name"]
    98  		namespaceName := rs.Primary.Attributes["namespace_name"]
    99  		eventHubName := rs.Primary.Attributes["eventhub_name"]
   100  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   101  
   102  		resp, err := conn.GetAuthorizationRule(resourceGroup, namespaceName, eventHubName, name)
   103  		if err != nil {
   104  			return nil
   105  		}
   106  
   107  		if resp.StatusCode != http.StatusNotFound {
   108  			return fmt.Errorf("EventHub Authorization Rule still exists:\n%#v", resp)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func testCheckAzureRMEventHubAuthorizationRuleExists(name string) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		// Ensure we have enough information in state to look up in API
   118  		rs, ok := s.RootModule().Resources[name]
   119  		if !ok {
   120  			return fmt.Errorf("Not found: %s", name)
   121  		}
   122  
   123  		name := rs.Primary.Attributes["name"]
   124  		namespaceName := rs.Primary.Attributes["namespace_name"]
   125  		eventHubName := rs.Primary.Attributes["eventhub_name"]
   126  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   127  		if !hasResourceGroup {
   128  			return fmt.Errorf("Bad: no resource group found in state for Event Hub: %s", name)
   129  		}
   130  
   131  		conn := testAccProvider.Meta().(*ArmClient).eventHubClient
   132  		resp, err := conn.GetAuthorizationRule(resourceGroup, namespaceName, eventHubName, name)
   133  		if err != nil {
   134  			return fmt.Errorf("Bad: Get on eventHubClient: %s", err)
   135  		}
   136  
   137  		if resp.StatusCode == http.StatusNotFound {
   138  			return fmt.Errorf("Bad: Event Hub Authorization Rule %q (eventhub %s, namespace %s / resource group: %s) does not exist", name, eventHubName, namespaceName, resourceGroup)
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  var testAccAzureRMEventHubAuthorizationRule_listen = `
   146  resource "azurerm_resource_group" "test" {
   147      name = "acctestRG-%d"
   148      location = "West US"
   149  }
   150  resource "azurerm_eventhub_namespace" "test" {
   151      name = "acctesteventhubnamespace-%d"
   152      location = "${azurerm_resource_group.test.location}"
   153      resource_group_name = "${azurerm_resource_group.test.name}"
   154      sku = "Standard"
   155  }
   156  resource "azurerm_eventhub" "test" {
   157    name                = "acctesteventhub-%d"
   158    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   159    location            = "${azurerm_resource_group.test.location}"
   160    resource_group_name = "${azurerm_resource_group.test.name}"
   161    partition_count     = 2
   162    message_retention   = 7
   163  }
   164  resource "azurerm_eventhub_authorization_rule" "test" {
   165    name                = "acctesteventhubrule-%d"
   166    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   167    eventhub_name       = "${azurerm_eventhub.test.name}"
   168    resource_group_name = "${azurerm_resource_group.test.name}"
   169    location            = "${azurerm_resource_group.test.location}"
   170    listen              = true
   171    send                = false
   172    manage              = false
   173  }`
   174  
   175  var testAccAzureRMEventHubAuthorizationRule_send = `
   176  resource "azurerm_resource_group" "test" {
   177      name = "acctestRG-%d"
   178      location = "West US"
   179  }
   180  resource "azurerm_eventhub_namespace" "test" {
   181      name = "acctesteventhubnamespace-%d"
   182      location = "${azurerm_resource_group.test.location}"
   183      resource_group_name = "${azurerm_resource_group.test.name}"
   184      sku = "Standard"
   185  }
   186  resource "azurerm_eventhub" "test" {
   187    name                = "acctesteventhub-%d"
   188    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   189    location            = "${azurerm_resource_group.test.location}"
   190    resource_group_name = "${azurerm_resource_group.test.name}"
   191    partition_count     = 2
   192    message_retention   = 7
   193  }
   194  resource "azurerm_eventhub_authorization_rule" "test" {
   195    name                = "acctesteventhubrule-%d"
   196    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   197    eventhub_name       = "${azurerm_eventhub.test.name}"
   198    resource_group_name = "${azurerm_resource_group.test.name}"
   199    location            = "${azurerm_resource_group.test.location}"
   200    listen              = false
   201    send                = true
   202    manage              = false
   203  }`
   204  
   205  var testAccAzureRMEventHubAuthorizationRule_readwrite = `
   206  resource "azurerm_resource_group" "test" {
   207      name = "acctestRG-%d"
   208      location = "West US"
   209  }
   210  resource "azurerm_eventhub_namespace" "test" {
   211      name = "acctesteventhubnamespace-%d"
   212      location = "${azurerm_resource_group.test.location}"
   213      resource_group_name = "${azurerm_resource_group.test.name}"
   214      sku = "Standard"
   215  }
   216  resource "azurerm_eventhub" "test" {
   217    name                = "acctesteventhub-%d"
   218    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   219    location            = "${azurerm_resource_group.test.location}"
   220    resource_group_name = "${azurerm_resource_group.test.name}"
   221    partition_count     = 2
   222    message_retention   = 7
   223  }
   224  resource "azurerm_eventhub_authorization_rule" "test" {
   225    name                = "acctesteventhubrule-%d"
   226    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   227    eventhub_name       = "${azurerm_eventhub.test.name}"
   228    resource_group_name = "${azurerm_resource_group.test.name}"
   229    location            = "${azurerm_resource_group.test.location}"
   230    listen              = true
   231    send                = true
   232    manage              = false
   233  }`
   234  
   235  var testAccAzureRMEventHubAuthorizationRule_manage = `
   236  resource "azurerm_resource_group" "test" {
   237      name = "acctestRG-%d"
   238      location = "West US"
   239  }
   240  resource "azurerm_eventhub_namespace" "test" {
   241      name = "acctesteventhubnamespace-%d"
   242      location = "${azurerm_resource_group.test.location}"
   243      resource_group_name = "${azurerm_resource_group.test.name}"
   244      sku = "Standard"
   245  }
   246  resource "azurerm_eventhub" "test" {
   247    name                = "acctesteventhub-%d"
   248    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   249    location            = "${azurerm_resource_group.test.location}"
   250    resource_group_name = "${azurerm_resource_group.test.name}"
   251    partition_count     = 2
   252    message_retention   = 7
   253  }
   254  resource "azurerm_eventhub_authorization_rule" "test" {
   255    name                = "acctesteventhubrule-%d"
   256    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   257    eventhub_name       = "${azurerm_eventhub.test.name}"
   258    resource_group_name = "${azurerm_resource_group.test.name}"
   259    location            = "${azurerm_resource_group.test.location}"
   260    listen              = true
   261    send                = true
   262    manage              = true
   263  }`