Skip to content

Commit

Permalink
Refactoring: splitting DTL VM into DTL Linux VM and DTL Windows VM
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Oct 12, 2018
1 parent c307dd5 commit 5ea28a7
Show file tree
Hide file tree
Showing 8 changed files with 821 additions and 158 deletions.
3 changes: 2 additions & 1 deletion azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func Provider() terraform.ResourceProvider {
"azurerm_data_lake_store_file": resourceArmDataLakeStoreFile(),
"azurerm_data_lake_store_firewall_rule": resourceArmDataLakeStoreFirewallRule(),
"azurerm_dev_test_lab": resourceArmDevTestLab(),
"azurerm_dev_test_virtual_machine": resourceArmDevTestVirtualMachine(),
"azurerm_dev_test_linux_virtual_machine": resourceArmDevTestLinuxVirtualMachine(),
"azurerm_dev_test_windows_virtual_machine": resourceArmDevTestWindowsVirtualMachine(),
"azurerm_dev_test_virtual_network": resourceArmDevTestVirtualNetwork(),
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmDevTestVirtualMachine() *schema.Resource {
func resourceArmDevTestLinuxVirtualMachine() *schema.Resource {
return &schema.Resource{
Create: resourceArmDevTestVirtualMachineCreateUpdate,
Read: resourceArmDevTestVirtualMachineRead,
Update: resourceArmDevTestVirtualMachineCreateUpdate,
Delete: resourceArmDevTestVirtualMachineDelete,
Create: resourceArmDevTestLinuxVirtualMachineCreateUpdate,
Read: resourceArmDevTestLinuxVirtualMachineRead,
Update: resourceArmDevTestLinuxVirtualMachineCreateUpdate,
Delete: resourceArmDevTestLinuxVirtualMachineDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand All @@ -28,7 +28,6 @@ func resourceArmDevTestVirtualMachine() *schema.Resource {
Required: true,
ForceNew: true,
// The virtual machine name must be between 1 and 62 characters and cannot contain any spaces or special characters. The name may contain letters, numbers, or '-'. However, it must begin and end with a letter or number, and cannot be all numbers.
// The name must be between 1 and 15 characters, cannot be entirely numeric, and cannot contain most special characters
},

"lab_name": {
Expand All @@ -44,16 +43,6 @@ func resourceArmDevTestVirtualMachine() *schema.Resource {

"location": locationSchema(),

"os_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"Linux",
"Windows",
}, false),
},

"size": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -141,11 +130,11 @@ func resourceArmDevTestVirtualMachine() *schema.Resource {
}
}

func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceArmDevTestLinuxVirtualMachineCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).devTestVirtualMachinesClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for DevTest Lab Virtual Machine creation")
log.Printf("[INFO] preparing arguments for DevTest Linux Virtual Machine creation")

name := d.Get("name").(string)
labName := d.Get("lab_name").(string)
Expand All @@ -157,7 +146,6 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i
labSubnetName := d.Get("lab_subnet_name").(string)
labVirtualNetworkId := d.Get("lab_virtual_network_id").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
osType := d.Get("os_type").(string)
notes := d.Get("notes").(string)
password := d.Get("password").(string)
sshKey := d.Get("ssh_key").(string)
Expand All @@ -166,12 +154,12 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i
username := d.Get("username").(string)

galleryImageReferenceRaw := d.Get("gallery_image_reference").([]interface{})
galleryImageReference := azure.ExpandDevTestLabVirtualMachineGalleryImageReference(galleryImageReferenceRaw, osType)
galleryImageReference := azure.ExpandDevTestLabVirtualMachineGalleryImageReference(galleryImageReferenceRaw, "Linux")

natRulesRaw := d.Get("inbound_nat_rule").(*schema.Set)
natRules := azure.ExpandDevTestLabVirtualMachineNatRules(natRulesRaw)

if len(natRules) > 0 && disallowPublicIPAddress {
if len(natRules) > 0 && !disallowPublicIPAddress {
return fmt.Errorf("If `inbound_nat_rule` is specified then `disallow_public_ip_address` must be set to true.")
}

Expand All @@ -193,7 +181,7 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i
LabSubnetName: utils.String(labSubnetName),
LabVirtualNetworkID: utils.String(labVirtualNetworkId),
NetworkInterface: &nic,
OsType: utils.String(osType),
OsType: utils.String("Linux"),
Notes: utils.String(notes),
Password: utils.String(password),
Size: utils.String(size),
Expand All @@ -206,29 +194,29 @@ func resourceArmDevTestVirtualMachineCreateUpdate(d *schema.ResourceData, meta i

future, err := client.CreateOrUpdate(ctx, resourceGroup, labName, name, parameters)
if err != nil {
return fmt.Errorf("Error creating/updating DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error creating/updating DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for creation/update of DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error waiting for creation/update of DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, labName, name, "")
if err != nil {
return fmt.Errorf("Error retrieving DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error retrieving DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read DevTest Virtual Machine %q (Lab %q / Resource Group %q) ID", name, labName, resourceGroup)
return fmt.Errorf("Cannot read DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q) ID", name, labName, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmDevTestVirtualMachineRead(d, meta)
return resourceArmDevTestLinuxVirtualMachineRead(d, meta)
}

func resourceArmDevTestVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
func resourceArmDevTestLinuxVirtualMachineRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).devTestVirtualMachinesClient
ctx := meta.(*ArmClient).StopContext

Expand All @@ -243,12 +231,12 @@ func resourceArmDevTestVirtualMachineRead(d *schema.ResourceData, meta interface
read, err := client.Get(ctx, resourceGroup, labName, name, "")
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
log.Printf("[DEBUG] DevTest Virtual Machine %q was not found in Lab %q / Resource Group %q - removing from state!", name, labName, resourceGroup)
log.Printf("[DEBUG] DevTest Linux Virtual Machine %q was not found in Lab %q / Resource Group %q - removing from state!", name, labName, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error making Read request on DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

d.Set("name", read.Name)
Expand All @@ -261,7 +249,6 @@ func resourceArmDevTestVirtualMachineRead(d *schema.ResourceData, meta interface
if props := read.LabVirtualMachineProperties; props != nil {
d.Set("allow_claim", props.AllowClaim)
d.Set("disallow_public_ip_address", props.DisallowPublicIPAddress)
d.Set("os_type", props.OsType)
d.Set("notes", props.Notes)
d.Set("size", props.Size)
d.Set("storage_type", props.StorageType)
Expand All @@ -282,7 +269,7 @@ func resourceArmDevTestVirtualMachineRead(d *schema.ResourceData, meta interface
return nil
}

func resourceArmDevTestVirtualMachineDelete(d *schema.ResourceData, meta interface{}) error {
func resourceArmDevTestLinuxVirtualMachineDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).devTestVirtualMachinesClient
ctx := meta.(*ArmClient).StopContext

Expand All @@ -298,21 +285,21 @@ func resourceArmDevTestVirtualMachineDelete(d *schema.ResourceData, meta interfa
if err != nil {
if utils.ResponseWasNotFound(read.Response) {
// deleted outside of TF
log.Printf("[DEBUG] DevTest Virtual Machine %q was not found in Lab %q / Resource Group %q - assuming removed!", name, labName, resourceGroup)
log.Printf("[DEBUG] DevTest Linux Virtual Machine %q was not found in Lab %q / Resource Group %q - assuming removed!", name, labName, resourceGroup)
return nil
}

return fmt.Errorf("Error retrieving DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error retrieving DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

future, err := client.Delete(ctx, resourceGroup, labName, name)
if err != nil {
return fmt.Errorf("Error deleting DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error deleting DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for the deletion of DevTest Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
return fmt.Errorf("Error waiting for the deletion of DevTest Linux Virtual Machine %q (Lab %q / Resource Group %q): %+v", name, labName, resourceGroup, err)
}

return err
Expand Down
Loading

0 comments on commit 5ea28a7

Please sign in to comment.