github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/azure/resource_azure_hosted_service_test.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management/hostedservice"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccAzureHostedServiceBasic(t *testing.T) {
    13  	name := "azure_hosted_service.foo"
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAzureHostedServiceDestroyed,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAzureHostedServiceBasic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAzureHostedServiceExists(name),
    24  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-service"),
    25  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    26  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "false"),
    27  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    28  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccAzureHostedServiceUpdate(t *testing.T) {
    36  	name := "azure_hosted_service.foo"
    37  
    38  	resource.Test(t, resource.TestCase{
    39  		PreCheck:     func() { testAccPreCheck(t) },
    40  		Providers:    testAccProviders,
    41  		CheckDestroy: testAccCheckAzureHostedServiceDestroyed,
    42  		Steps: []resource.TestStep{
    43  			resource.TestStep{
    44  				Config: testAccAzureHostedServiceBasic,
    45  				Check: resource.ComposeTestCheckFunc(
    46  					testAccCheckAzureHostedServiceExists(name),
    47  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-service"),
    48  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    49  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "false"),
    50  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    51  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    52  				),
    53  			},
    54  
    55  			resource.TestStep{
    56  				Config: testAccAzureHostedServiceUpdate,
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckAzureHostedServiceExists(name),
    59  					resource.TestCheckResourceAttr(name, "name", "terraform-testing-service"),
    60  					resource.TestCheckResourceAttr(name, "location", "North Europe"),
    61  					resource.TestCheckResourceAttr(name, "ephemeral_contents", "true"),
    62  					resource.TestCheckResourceAttr(name, "description", "very discriptive"),
    63  					resource.TestCheckResourceAttr(name, "label", "very identifiable"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckAzureHostedServiceExists(name string) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		resource, ok := s.RootModule().Resources[name]
    73  		if !ok {
    74  			return fmt.Errorf("Hosted Service resource not found.")
    75  		}
    76  
    77  		if resource.Primary.ID == "" {
    78  			return fmt.Errorf("Resource's ID is not set.")
    79  		}
    80  
    81  		mgmtClient := testAccProvider.Meta().(*Client).mgmtClient
    82  		_, err := hostedservice.NewClient(mgmtClient).GetHostedService(resource.Primary.ID)
    83  		return err
    84  	}
    85  }
    86  
    87  func testAccCheckAzureHostedServiceDestroyed(s *terraform.State) error {
    88  	mgmtClient := testAccProvider.Meta().(*Client).mgmtClient
    89  
    90  	for _, resource := range s.RootModule().Resources {
    91  		if resource.Type != "azure_hosted_service" {
    92  			continue
    93  		}
    94  
    95  		if resource.Primary.ID == "" {
    96  			return fmt.Errorf("No Azure Hosted Service Resource found.")
    97  		}
    98  
    99  		_, err := hostedservice.NewClient(mgmtClient).GetHostedService(resource.Primary.ID)
   100  
   101  		return testAccResourceDestroyedErrorFilter("Hosted Service", err)
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  const testAccAzureHostedServiceBasic = `
   108  resource "azure_hosted_service" "foo" {
   109  	name = "terraform-testing-service"
   110  	location = "North Europe"
   111      ephemeral_contents = false
   112  	description = "very discriptive"
   113      label = "very identifiable"
   114  }
   115  `
   116  const testAccAzureHostedServiceUpdate = `
   117  resource "azure_hosted_service" "foo" {
   118  	name = "terraform-testing-service"
   119  	location = "North Europe"
   120      ephemeral_contents = true
   121  	description = "very discriptive"
   122      label = "very identifiable"
   123  }
   124  `