github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/builtin/providers/azurerm/resource_arm_servicebus_topic_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 TestAccAzureRMServiceBusTopic_basic(t *testing.T) {
    14  	ri := acctest.RandInt()
    15  	config := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: config,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAzureRMServiceBusTopic_update(t *testing.T) {
    33  	ri := acctest.RandInt()
    34  	preConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
    35  	postConfig := fmt.Sprintf(testAccAzureRMServiceBusTopic_update, ri, ri, ri)
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: preConfig,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testCheckAzureRMServiceBusTopicExists("azurerm_servicebus_topic.test"),
    46  				),
    47  			},
    48  			resource.TestStep{
    49  				Config: postConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					resource.TestCheckResourceAttr(
    52  						"azurerm_servicebus_topic.test", "enable_batched_operations", "true"),
    53  					resource.TestCheckResourceAttr(
    54  						"azurerm_servicebus_topic.test", "enable_express", "true"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testCheckAzureRMServiceBusTopicDestroy(s *terraform.State) error {
    62  	client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient
    63  
    64  	for _, rs := range s.RootModule().Resources {
    65  		if rs.Type != "azurerm_servicebus_topic" {
    66  			continue
    67  		}
    68  
    69  		name := rs.Primary.Attributes["name"]
    70  		namespaceName := rs.Primary.Attributes["namespace_name"]
    71  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
    72  
    73  		resp, err := client.Get(resourceGroup, namespaceName, name)
    74  		if err != nil {
    75  			if resp.StatusCode == http.StatusNotFound {
    76  				return nil
    77  			}
    78  			return err
    79  		}
    80  
    81  		if resp.StatusCode != http.StatusNotFound {
    82  			return fmt.Errorf("ServiceBus Topic still exists:\n%#v", resp.Properties)
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func testCheckAzureRMServiceBusTopicExists(name string) resource.TestCheckFunc {
    90  	return func(s *terraform.State) error {
    91  		// Ensure we have enough information in state to look up in API
    92  		rs, ok := s.RootModule().Resources[name]
    93  		if !ok {
    94  			return fmt.Errorf("Not found: %s", name)
    95  		}
    96  
    97  		topicName := rs.Primary.Attributes["name"]
    98  		namespaceName := rs.Primary.Attributes["namespace_name"]
    99  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   100  		if !hasResourceGroup {
   101  			return fmt.Errorf("Bad: no resource group found in state for topic: %s", topicName)
   102  		}
   103  
   104  		client := testAccProvider.Meta().(*ArmClient).serviceBusTopicsClient
   105  
   106  		resp, err := client.Get(resourceGroup, namespaceName, topicName)
   107  		if err != nil {
   108  			return fmt.Errorf("Bad: Get on serviceBusTopicsClient: %s", err)
   109  		}
   110  
   111  		if resp.StatusCode == http.StatusNotFound {
   112  			return fmt.Errorf("Bad: Topic %q (resource group: %q) does not exist", namespaceName, resourceGroup)
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  var testAccAzureRMServiceBusTopic_basic = `
   120  resource "azurerm_resource_group" "test" {
   121      name = "acctestRG-%d"
   122      location = "West US"
   123  }
   124  
   125  resource "azurerm_servicebus_namespace" "test" {
   126      name = "acctestservicebusnamespace-%d"
   127      location = "West US"
   128      resource_group_name = "${azurerm_resource_group.test.name}"
   129      sku = "standard"
   130  }
   131  
   132  resource "azurerm_servicebus_topic" "test" {
   133      name = "acctestservicebustopic-%d"
   134      location = "West US"
   135      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   136      resource_group_name = "${azurerm_resource_group.test.name}"
   137  }
   138  `
   139  
   140  var testAccAzureRMServiceBusTopic_update = `
   141  resource "azurerm_resource_group" "test" {
   142      name = "acctestRG-%d"
   143      location = "West US"
   144  }
   145  
   146  resource "azurerm_servicebus_namespace" "test" {
   147      name = "acctestservicebusnamespace-%d"
   148      location = "West US"
   149      resource_group_name = "${azurerm_resource_group.test.name}"
   150      sku = "standard"
   151  }
   152  
   153  resource "azurerm_servicebus_topic" "test" {
   154      name = "acctestservicebustopic-%d"
   155      location = "West US"
   156      namespace_name = "${azurerm_servicebus_namespace.test.name}"
   157      resource_group_name = "${azurerm_resource_group.test.name}"
   158      enable_batched_operations = true
   159      enable_express = true
   160  }
   161  `