github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_eventhub_consumer_group_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 TestAccAzureRMEventHubConsumerGroup_basic(t *testing.T) {
    14  
    15  	ri := acctest.RandInt()
    16  	config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_basic, ri, ri, ri, ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: config,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMEventHubConsumerGroupExists("azurerm_eventhub_consumer_group.test"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAzureRMEventHubConsumerGroup_complete(t *testing.T) {
    34  
    35  	ri := acctest.RandInt()
    36  	config := fmt.Sprintf(testAccAzureRMEventHubConsumerGroup_complete, ri, ri, ri, ri)
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testCheckAzureRMEventHubConsumerGroupDestroy,
    42  		Steps: []resource.TestStep{
    43  			{
    44  				Config: config,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testCheckAzureRMEventHubConsumerGroupExists("azurerm_eventhub_consumer_group.test"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testCheckAzureRMEventHubConsumerGroupDestroy(s *terraform.State) error {
    54  	conn := testAccProvider.Meta().(*ArmClient).eventHubConsumerGroupClient
    55  
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "azurerm_eventhub_consumer_group" {
    58  			continue
    59  		}
    60  
    61  		name := rs.Primary.Attributes["name"]
    62  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    63  		namespaceName := rs.Primary.Attributes["namespace_name"]
    64  		eventHubName := rs.Primary.Attributes["eventhub_name"]
    65  
    66  		resp, err := conn.Get(resourceGroup, namespaceName, eventHubName, name)
    67  
    68  		if err != nil {
    69  			return nil
    70  		}
    71  
    72  		if resp.StatusCode != http.StatusNotFound {
    73  			return fmt.Errorf("EventHub Consumer Group still exists:\n%#v", resp.ConsumerGroupProperties)
    74  		}
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func testCheckAzureRMEventHubConsumerGroupExists(name string) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		// Ensure we have enough information in state to look up in API
    83  		rs, ok := s.RootModule().Resources[name]
    84  		if !ok {
    85  			return fmt.Errorf("Not found: %s", name)
    86  		}
    87  
    88  		name := rs.Primary.Attributes["name"]
    89  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    90  		if !hasResourceGroup {
    91  			return fmt.Errorf("Bad: no resource group found in state for Event Hub Consumer Group: %s", name)
    92  		}
    93  
    94  		conn := testAccProvider.Meta().(*ArmClient).eventHubConsumerGroupClient
    95  
    96  		namespaceName := rs.Primary.Attributes["namespace_name"]
    97  		eventHubName := rs.Primary.Attributes["eventhub_name"]
    98  
    99  		resp, err := conn.Get(resourceGroup, namespaceName, eventHubName, name)
   100  		if err != nil {
   101  			return fmt.Errorf("Bad: Get on eventHubConsumerGroupClient: %s", err)
   102  		}
   103  
   104  		if resp.StatusCode == http.StatusNotFound {
   105  			return fmt.Errorf("Bad: Event Hub Consumer Group %q (resource group: %q) does not exist", name, resourceGroup)
   106  		}
   107  
   108  		return nil
   109  	}
   110  }
   111  
   112  var testAccAzureRMEventHubConsumerGroup_basic = `
   113  resource "azurerm_resource_group" "test" {
   114      name = "acctestRG-%d"
   115      location = "West US"
   116  }
   117  resource "azurerm_eventhub_namespace" "test" {
   118      name = "acctesteventhubnamespace-%d"
   119      location = "${azurerm_resource_group.test.location}"
   120      resource_group_name = "${azurerm_resource_group.test.name}"
   121      sku = "Standard"
   122  }
   123  
   124  resource "azurerm_eventhub" "test" {
   125    name                = "acctesteventhub-%d"
   126    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   127    location            = "${azurerm_resource_group.test.location}"
   128    resource_group_name = "${azurerm_resource_group.test.name}"
   129    partition_count     = 2
   130    message_retention   = 7
   131  }
   132  
   133  resource "azurerm_eventhub_consumer_group" "test" {
   134      name = "acctesteventhubcg-%d"
   135      namespace_name = "${azurerm_eventhub_namespace.test.name}"
   136      eventhub_name = "${azurerm_eventhub.test.name}"
   137      resource_group_name = "${azurerm_resource_group.test.name}"
   138      location = "${azurerm_resource_group.test.location}"
   139  }
   140  `
   141  
   142  var testAccAzureRMEventHubConsumerGroup_complete = `
   143  resource "azurerm_resource_group" "test" {
   144      name = "acctestRG-%d"
   145      location = "West US"
   146  }
   147  resource "azurerm_eventhub_namespace" "test" {
   148      name = "acctesteventhubnamespace-%d"
   149      location = "${azurerm_resource_group.test.location}"
   150      resource_group_name = "${azurerm_resource_group.test.name}"
   151      sku = "Standard"
   152  }
   153  
   154  resource "azurerm_eventhub" "test" {
   155    name                = "acctesteventhub-%d"
   156    namespace_name      = "${azurerm_eventhub_namespace.test.name}"
   157    location            = "${azurerm_resource_group.test.location}"
   158    resource_group_name = "${azurerm_resource_group.test.name}"
   159    partition_count     = 2
   160    message_retention   = 7
   161  }
   162  
   163  resource "azurerm_eventhub_consumer_group" "test" {
   164      name = "acctesteventhubcg-%d"
   165      namespace_name = "${azurerm_eventhub_namespace.test.name}"
   166      eventhub_name = "${azurerm_eventhub.test.name}"
   167      resource_group_name = "${azurerm_resource_group.test.name}"
   168      location = "${azurerm_resource_group.test.location}"
   169      user_metadata = "some-meta-data"
   170  }
   171  `