2015-04-24 18:18:24 +02:00
|
|
|
package azure
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-08-03 22:12:30 +02:00
|
|
|
"math/rand"
|
2015-05-28 00:50:45 +02:00
|
|
|
"testing"
|
2015-08-03 22:12:30 +02:00
|
|
|
"time"
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
"github.com/Azure/azure-sdk-for-go/management"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/management/virtualmachine"
|
2016-02-03 19:11:47 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-05-28 00:50:45 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-08-03 22:12:30 +02:00
|
|
|
var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
|
|
|
var instanceName = fmt.Sprintf("terraform-test-%d", randInt)
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
func TestAccAzureInstance_basic(t *testing.T) {
|
|
|
|
var dpmt virtualmachine.DeploymentResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-06-26 14:48:55 +02:00
|
|
|
CheckDestroy: testAccCheckAzureInstanceDestroyed(""),
|
2015-05-28 00:50:45 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureInstance_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureInstanceExists(
|
2015-06-26 14:48:55 +02:00
|
|
|
"azure_instance.foo", "", &dpmt),
|
2015-05-28 00:50:45 +02:00
|
|
|
testAccCheckAzureInstanceBasicAttributes(&dpmt),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-08-03 22:12:30 +02:00
|
|
|
"azure_instance.foo", "name", instanceName),
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-08-03 22:12:30 +02:00
|
|
|
"azure_instance.foo", "hosted_service_name", instanceName),
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "location", "West US"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.2462817782.public_port", "22"),
|
2016-08-14 12:02:49 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "custom_data", "0ea0f28b0c42d6bef7d0c7ab4886324feaa8b5e1"),
|
2015-06-26 14:48:55 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureInstance_separateHostedService(t *testing.T) {
|
|
|
|
var dpmt virtualmachine.DeploymentResponse
|
|
|
|
|
2016-02-03 19:11:47 +01:00
|
|
|
hostedServiceName := fmt.Sprintf("terraform-testing-service%d", acctest.RandInt())
|
|
|
|
|
|
|
|
config := fmt.Sprintf(testAccAzureInstance_separateHostedService, hostedServiceName, instanceName, testAccStorageServiceName)
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2016-02-03 23:11:36 +01:00
|
|
|
CheckDestroy: testAccCheckAzureInstanceDestroyed(hostedServiceName),
|
2015-06-26 14:48:55 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-02-03 19:11:47 +01:00
|
|
|
Config: config,
|
2015-06-26 14:48:55 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureInstanceExists(
|
2016-02-03 19:11:47 +01:00
|
|
|
"azure_instance.foo", hostedServiceName, &dpmt),
|
2015-06-26 14:48:55 +02:00
|
|
|
testAccCheckAzureInstanceBasicAttributes(&dpmt),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-11-19 23:28:24 +01:00
|
|
|
"azure_instance.foo", "name", instanceName),
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-02-03 19:11:47 +01:00
|
|
|
"azure_instance.foo", "hosted_service_name", hostedServiceName),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "location", "West US"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.2462817782.public_port", "22"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureInstance_advanced(t *testing.T) {
|
|
|
|
var dpmt virtualmachine.DeploymentResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-06-26 14:48:55 +02:00
|
|
|
CheckDestroy: testAccCheckAzureInstanceDestroyed(""),
|
2015-05-28 00:50:45 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureInstance_advanced,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureInstanceExists(
|
2015-06-26 14:48:55 +02:00
|
|
|
"azure_instance.foo", "", &dpmt),
|
2015-05-28 00:50:45 +02:00
|
|
|
testAccCheckAzureInstanceAdvancedAttributes(&dpmt),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "name", "terraform-test1"),
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "hosted_service_name", "terraform-test1"),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "size", "Basic_A1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "subnet", "subnet1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-08 02:38:34 +01:00
|
|
|
"azure_instance.foo", "virtual_network", "terraform-vnet-advanced-test"),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "security_group", "terraform-security-group1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.1814039778.public_port", "3389"),
|
2016-08-14 12:02:49 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "custom_data", "04c589e0edaa5ffe185d1e5532e77d1b2ac4b948"),
|
2015-05-28 00:50:45 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAzureInstance_update(t *testing.T) {
|
|
|
|
var dpmt virtualmachine.DeploymentResponse
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2015-06-26 14:48:55 +02:00
|
|
|
CheckDestroy: testAccCheckAzureInstanceDestroyed(""),
|
2015-05-28 00:50:45 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureInstance_advanced,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureInstanceExists(
|
2015-06-26 14:48:55 +02:00
|
|
|
"azure_instance.foo", "", &dpmt),
|
2015-05-28 00:50:45 +02:00
|
|
|
testAccCheckAzureInstanceAdvancedAttributes(&dpmt),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "name", "terraform-test1"),
|
2015-06-26 14:48:55 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "hosted_service_name", "terraform-test1"),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "size", "Basic_A1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "subnet", "subnet1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-08 02:38:34 +01:00
|
|
|
"azure_instance.foo", "virtual_network", "terraform-vnet-advanced-test"),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "security_group", "terraform-security-group1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.1814039778.public_port", "3389"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAzureInstance_update,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAzureInstanceExists(
|
2015-06-26 14:48:55 +02:00
|
|
|
"azure_instance.foo", "", &dpmt),
|
2015-05-28 00:50:45 +02:00
|
|
|
testAccCheckAzureInstanceUpdatedAttributes(&dpmt),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "size", "Basic_A2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-01-08 18:20:49 +01:00
|
|
|
"azure_instance.foo", "security_group", "terraform-security-update-group2"),
|
2015-05-28 00:50:45 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.1814039778.public_port", "3389"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"azure_instance.foo", "endpoint.3713350066.public_port", "5985"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureInstanceExists(
|
|
|
|
n string,
|
2015-06-26 14:48:55 +02:00
|
|
|
hostedServiceName string,
|
2015-05-28 00:50:45 +02:00
|
|
|
dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No instance ID is set")
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
// if not hosted service was provided; it means that we expect it
|
|
|
|
// to be identical with the name of the instance; which is in the ID.
|
|
|
|
var serviceName string
|
|
|
|
if hostedServiceName == "" {
|
|
|
|
serviceName = rs.Primary.ID
|
|
|
|
} else {
|
|
|
|
serviceName = hostedServiceName
|
|
|
|
}
|
|
|
|
|
2015-06-16 19:54:52 +02:00
|
|
|
vmClient := testAccProvider.Meta().(*Client).vmClient
|
2015-06-26 14:48:55 +02:00
|
|
|
vm, err := vmClient.GetDeployment(serviceName, rs.Primary.ID)
|
2015-05-28 00:50:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vm.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Instance not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*dpmt = vm
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureInstanceBasicAttributes(
|
|
|
|
dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
2015-08-03 22:12:30 +02:00
|
|
|
if dpmt.Name != instanceName {
|
2015-05-28 00:50:45 +02:00
|
|
|
return fmt.Errorf("Bad name: %s", dpmt.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dpmt.RoleList) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList))
|
|
|
|
}
|
|
|
|
|
|
|
|
if dpmt.RoleList[0].RoleSize != "Basic_A1" {
|
|
|
|
return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureInstanceAdvancedAttributes(
|
|
|
|
dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if dpmt.Name != "terraform-test1" {
|
|
|
|
return fmt.Errorf("Bad name: %s", dpmt.Name)
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:15:59 +01:00
|
|
|
if dpmt.VirtualNetworkName != "terraform-vnet-advanced-test" {
|
2015-05-28 00:50:45 +02:00
|
|
|
return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName)
|
|
|
|
}
|
|
|
|
|
2016-01-08 17:08:03 +01:00
|
|
|
if len(dpmt.RoleList) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList))
|
|
|
|
}
|
|
|
|
|
|
|
|
if dpmt.RoleList[0].RoleSize != "Basic_A1" {
|
|
|
|
return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range dpmt.RoleList[0].ConfigurationSets {
|
|
|
|
if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork {
|
|
|
|
if len(c.InputEndpoints) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of endpoints %d",
|
|
|
|
dpmt.Name, len(c.InputEndpoints))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[0].Name != "RDP" {
|
|
|
|
return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[0].Port != 3389 {
|
|
|
|
return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[0].Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.SubnetNames) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of associated subnets %d",
|
|
|
|
dpmt.Name, len(c.SubnetNames))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SubnetNames[0] != "subnet1" {
|
|
|
|
return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.NetworkSecurityGroup != "terraform-security-group1" {
|
|
|
|
return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureInstanceAdvancedUpdatedAttributes(
|
|
|
|
dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if dpmt.Name != "terraform-test1" {
|
|
|
|
return fmt.Errorf("Bad name: %s", dpmt.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dpmt.VirtualNetworkName != "terraform-vnet-update-test" {
|
|
|
|
return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName)
|
|
|
|
}
|
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
if len(dpmt.RoleList) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList))
|
|
|
|
}
|
|
|
|
|
|
|
|
if dpmt.RoleList[0].RoleSize != "Basic_A1" {
|
|
|
|
return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range dpmt.RoleList[0].ConfigurationSets {
|
|
|
|
if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork {
|
|
|
|
if len(c.InputEndpoints) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of endpoints %d",
|
|
|
|
dpmt.Name, len(c.InputEndpoints))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[0].Name != "RDP" {
|
|
|
|
return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[0].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[0].Port != 3389 {
|
|
|
|
return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[0].Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.SubnetNames) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of associated subnets %d",
|
|
|
|
dpmt.Name, len(c.SubnetNames))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SubnetNames[0] != "subnet1" {
|
|
|
|
return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.NetworkSecurityGroup != "terraform-security-group1" {
|
|
|
|
return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAzureInstanceUpdatedAttributes(
|
|
|
|
dpmt *virtualmachine.DeploymentResponse) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if dpmt.Name != "terraform-test1" {
|
|
|
|
return fmt.Errorf("Bad name: %s", dpmt.Name)
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:20:49 +01:00
|
|
|
if dpmt.VirtualNetworkName != "terraform-vnet-update-test" {
|
2015-05-28 00:50:45 +02:00
|
|
|
return fmt.Errorf("Bad virtual network: %s", dpmt.VirtualNetworkName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dpmt.RoleList) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of roles: %d", dpmt.Name, len(dpmt.RoleList))
|
|
|
|
}
|
|
|
|
|
|
|
|
if dpmt.RoleList[0].RoleSize != "Basic_A2" {
|
|
|
|
return fmt.Errorf("Bad size: %s", dpmt.RoleList[0].RoleSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range dpmt.RoleList[0].ConfigurationSets {
|
|
|
|
if c.ConfigurationSetType == virtualmachine.ConfigurationSetTypeNetwork {
|
|
|
|
if len(c.InputEndpoints) != 2 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of endpoints %d",
|
|
|
|
dpmt.Name, len(c.InputEndpoints))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[1].Name != "WINRM" {
|
|
|
|
return fmt.Errorf("Bad endpoint name: %s", c.InputEndpoints[1].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InputEndpoints[1].Port != 5985 {
|
|
|
|
return fmt.Errorf("Bad endpoint port: %d", c.InputEndpoints[1].Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.SubnetNames) != 1 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Instance %s has an unexpected number of associated subnets %d",
|
|
|
|
dpmt.Name, len(c.SubnetNames))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SubnetNames[0] != "subnet1" {
|
|
|
|
return fmt.Errorf("Bad subnet: %s", c.SubnetNames[0])
|
|
|
|
}
|
|
|
|
|
2016-01-08 18:20:49 +01:00
|
|
|
if c.NetworkSecurityGroup != "terraform-security-update-group2" {
|
2015-05-28 00:50:45 +02:00
|
|
|
return fmt.Errorf("Bad security group: %s", c.NetworkSecurityGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
func testAccCheckAzureInstanceDestroyed(hostedServiceName string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
hostedServiceClient := testAccProvider.Meta().(*Client).hostedServiceClient
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "azure_instance" {
|
|
|
|
continue
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No instance ID is set")
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
// if not hosted service was provided; it means that we expect it
|
|
|
|
// to be identical with the name of the instance; which is in the ID.
|
|
|
|
var serviceName string
|
|
|
|
if hostedServiceName == "" {
|
|
|
|
serviceName = rs.Primary.ID
|
|
|
|
} else {
|
|
|
|
serviceName = hostedServiceName
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
_, err := hostedServiceClient.GetHostedService(serviceName)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Instance %s still exists", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !management.IsResourceNotFoundError(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 14:48:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var testAccAzureInstance_basic = fmt.Sprintf(`
|
|
|
|
resource "azure_instance" "foo" {
|
2015-08-03 22:12:30 +02:00
|
|
|
name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
2016-08-14 12:02:49 +02:00
|
|
|
custom_data = "# Hello world"
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
endpoint {
|
|
|
|
name = "SSH"
|
|
|
|
protocol = "tcp"
|
|
|
|
public_port = 22
|
|
|
|
private_port = 22
|
|
|
|
}
|
2015-08-03 22:12:30 +02:00
|
|
|
}`, instanceName, testAccStorageServiceName)
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2016-02-03 19:11:47 +01:00
|
|
|
var testAccAzureInstance_separateHostedService = `
|
2015-06-26 14:48:55 +02:00
|
|
|
resource "azure_hosted_service" "foo" {
|
2016-08-14 12:02:49 +02:00
|
|
|
name = "%s"
|
|
|
|
location = "West US"
|
|
|
|
ephemeral_contents = true
|
2015-06-26 14:48:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_instance" "foo" {
|
2015-11-19 23:28:24 +01:00
|
|
|
name = "%s"
|
|
|
|
hosted_service_name = "${azure_hosted_service.foo.name}"
|
2015-06-26 14:48:55 +02:00
|
|
|
image = "Ubuntu Server 14.04 LTS"
|
|
|
|
size = "Basic_A1"
|
|
|
|
storage_service_name = "%s"
|
|
|
|
location = "West US"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
|
|
|
|
endpoint {
|
|
|
|
name = "SSH"
|
|
|
|
protocol = "tcp"
|
|
|
|
public_port = 22
|
|
|
|
private_port = 22
|
|
|
|
}
|
2016-02-03 19:11:47 +01:00
|
|
|
}`
|
2015-06-26 14:48:55 +02:00
|
|
|
|
2015-05-28 00:50:45 +02:00
|
|
|
var testAccAzureInstance_advanced = fmt.Sprintf(`
|
|
|
|
resource "azure_virtual_network" "foo" {
|
2016-01-07 20:09:40 +01:00
|
|
|
name = "terraform-vnet-advanced-test"
|
2015-05-28 00:50:45 +02:00
|
|
|
address_space = ["10.1.2.0/24"]
|
2016-08-14 12:02:49 +02:00
|
|
|
location = "West US"
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2016-08-14 12:02:49 +02:00
|
|
|
subnet {
|
2015-05-28 00:50:45 +02:00
|
|
|
name = "subnet1"
|
2016-08-14 12:02:49 +02:00
|
|
|
address_prefix = "10.1.2.0/25"
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2016-08-14 12:02:49 +02:00
|
|
|
subnet {
|
2015-05-28 00:50:45 +02:00
|
|
|
name = "subnet2"
|
2016-08-14 12:02:49 +02:00
|
|
|
address_prefix = "10.1.2.128/25"
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_security_group" "foo" {
|
|
|
|
name = "terraform-security-group1"
|
|
|
|
location = "West US"
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
resource "azure_security_group_rule" "foo" {
|
|
|
|
name = "rdp"
|
2015-06-25 20:56:46 +02:00
|
|
|
security_group_names = ["${azure_security_group.foo.name}"]
|
2015-06-05 16:12:21 +02:00
|
|
|
priority = 101
|
|
|
|
source_address_prefix = "*"
|
|
|
|
source_port_range = "*"
|
|
|
|
destination_address_prefix = "*"
|
|
|
|
destination_port_range = "3389"
|
2016-08-14 12:02:49 +02:00
|
|
|
action = "Deny"
|
|
|
|
type = "Inbound"
|
2015-06-05 16:12:21 +02:00
|
|
|
protocol = "TCP"
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_instance" "foo" {
|
|
|
|
name = "terraform-test1"
|
2016-03-02 20:02:48 +01:00
|
|
|
image = "Windows Server 2012 R2 Datacenter, January 2016"
|
2015-05-28 00:50:45 +02:00
|
|
|
size = "Basic_A1"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
time_zone = "America/Los_Angeles"
|
|
|
|
subnet = "subnet1"
|
|
|
|
virtual_network = "${azure_virtual_network.foo.name}"
|
|
|
|
security_group = "${azure_security_group.foo.name}"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
2016-08-14 12:02:49 +02:00
|
|
|
custom_data = "IyBIZWxsbyB3b3JsZA=="
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
endpoint {
|
|
|
|
name = "RDP"
|
|
|
|
protocol = "tcp"
|
|
|
|
public_port = 3389
|
|
|
|
private_port = 3389
|
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
}`, testAccStorageServiceName)
|
2015-05-28 00:50:45 +02:00
|
|
|
|
|
|
|
var testAccAzureInstance_update = fmt.Sprintf(`
|
|
|
|
resource "azure_virtual_network" "foo" {
|
2016-01-07 20:09:40 +01:00
|
|
|
name = "terraform-vnet-update-test"
|
2015-05-28 00:50:45 +02:00
|
|
|
address_space = ["10.1.2.0/24"]
|
2016-08-14 12:02:49 +02:00
|
|
|
location = "West US"
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
subnet {
|
2015-05-28 00:50:45 +02:00
|
|
|
name = "subnet1"
|
2016-08-14 12:02:49 +02:00
|
|
|
address_prefix = "10.1.2.0/25"
|
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
subnet {
|
2015-05-28 00:50:45 +02:00
|
|
|
name = "subnet2"
|
2016-08-14 12:02:49 +02:00
|
|
|
address_prefix = "10.1.2.128/25"
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_security_group" "foo" {
|
|
|
|
name = "terraform-security-group1"
|
|
|
|
location = "West US"
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
resource "azure_security_group_rule" "foo" {
|
|
|
|
name = "rdp"
|
2015-06-25 20:56:46 +02:00
|
|
|
security_group_names = ["${azure_security_group.foo.name}"]
|
2015-06-05 16:12:21 +02:00
|
|
|
priority = 101
|
|
|
|
source_address_prefix = "*"
|
|
|
|
source_port_range = "*"
|
|
|
|
destination_address_prefix = "*"
|
|
|
|
destination_port_range = "3389"
|
2016-08-14 12:02:49 +02:00
|
|
|
type = "Inbound"
|
|
|
|
action = "Deny"
|
2015-06-05 16:12:21 +02:00
|
|
|
protocol = "TCP"
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_security_group" "bar" {
|
2016-01-07 20:09:40 +01:00
|
|
|
name = "terraform-security-update-group2"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
2015-06-05 16:12:21 +02:00
|
|
|
}
|
2015-05-28 00:50:45 +02:00
|
|
|
|
2015-06-05 16:12:21 +02:00
|
|
|
resource "azure_security_group_rule" "bar" {
|
|
|
|
name = "rdp"
|
2015-06-25 20:56:46 +02:00
|
|
|
security_group_names = ["${azure_security_group.bar.name}"]
|
2015-06-05 16:12:21 +02:00
|
|
|
priority = 101
|
|
|
|
source_address_prefix = "192.168.0.0/24"
|
|
|
|
source_port_range = "*"
|
|
|
|
destination_address_prefix = "*"
|
|
|
|
destination_port_range = "3389"
|
2016-08-14 12:02:49 +02:00
|
|
|
type = "Inbound"
|
|
|
|
action = "Deny"
|
2015-06-05 16:12:21 +02:00
|
|
|
protocol = "TCP"
|
2015-05-28 00:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "azure_instance" "foo" {
|
|
|
|
name = "terraform-test1"
|
2016-03-02 20:02:48 +01:00
|
|
|
image = "Windows Server 2012 R2 Datacenter, January 2016"
|
2015-05-28 00:50:45 +02:00
|
|
|
size = "Basic_A2"
|
2015-06-05 16:12:21 +02:00
|
|
|
storage_service_name = "%s"
|
2015-05-28 00:50:45 +02:00
|
|
|
location = "West US"
|
|
|
|
time_zone = "America/Los_Angeles"
|
|
|
|
subnet = "subnet1"
|
|
|
|
virtual_network = "${azure_virtual_network.foo.name}"
|
|
|
|
security_group = "${azure_security_group.bar.name}"
|
|
|
|
username = "terraform"
|
|
|
|
password = "Pass!admin123"
|
|
|
|
|
|
|
|
endpoint {
|
|
|
|
name = "RDP"
|
|
|
|
protocol = "tcp"
|
|
|
|
public_port = 3389
|
|
|
|
private_port = 3389
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint {
|
|
|
|
name = "WINRM"
|
|
|
|
protocol = "tcp"
|
|
|
|
public_port = 5985
|
|
|
|
private_port = 5985
|
|
|
|
}
|
2015-06-05 16:12:21 +02:00
|
|
|
}`, testAccStorageServiceName)
|