-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource:
azurerm_orchestrated_virtual_machine_scale_set
(#6626)
This PR implements the new orchestration mode VM of VMSS by introducing the new resource azurerm_orchestrated_virtual_machine_scale_set. For more information about the orchestration mode of VMSS, please refer this doc Fixes #6085
- Loading branch information
1 parent
57c513c
commit 9982911
Showing
13 changed files
with
1,099 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
azurerm/internal/services/compute/orchestrated_virtual_machine_scale_set_resource.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package compute | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/compute/parse" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmOrchestratedVirtualMachineScaleSet() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmOrchestratedVirtualMachineScaleSetCreateUpdate, | ||
Read: resourceArmOrchestratedVirtualMachineScaleSetRead, | ||
Update: resourceArmOrchestratedVirtualMachineScaleSetCreateUpdate, | ||
Delete: resourceArmOrchestratedVirtualMachineScaleSetDelete, | ||
|
||
Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error { | ||
_, err := parse.VirtualMachineScaleSetID(id) | ||
return err | ||
}, importOrchestratedVirtualMachineScaleSet), | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(30 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(30 * time.Minute), | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: ValidateOrchestratedVMSSName, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"platform_fault_domain_count": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ForceNew: true, | ||
// The range of this value varies in different locations | ||
ValidateFunc: validation.IntBetween(0, 5), | ||
}, | ||
|
||
"single_placement_group": { | ||
Type: schema.TypeBool, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
// the VMO mode can only be deployed into one zone for now, and its zone will also be assigned to all its VM instances | ||
"zones": azure.SchemaSingleZone(), | ||
|
||
"unique_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tags.Schema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmOrchestratedVirtualMachineScaleSetCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Compute.VMScaleSetClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
resourceGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
|
||
if d.IsNewResource() { | ||
existing, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("checking for existing Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
if existing.ID != nil && *existing.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_orchestrated_virtual_machine_scale_set", *existing.ID) | ||
} | ||
} | ||
|
||
props := compute.VirtualMachineScaleSet{ | ||
Location: utils.String(location.Normalize(d.Get("location").(string))), | ||
Tags: tags.Expand(d.Get("tags").(map[string]interface{})), | ||
VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{ | ||
PlatformFaultDomainCount: utils.Int32(int32(d.Get("platform_fault_domain_count").(int))), | ||
SinglePlacementGroup: utils.Bool(d.Get("single_placement_group").(bool)), | ||
}, | ||
Zones: azure.ExpandZones(d.Get("zones").([]interface{})), | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, props) | ||
if err != nil { | ||
return fmt.Errorf("creating Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for creation of Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("retrieving Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("retrieving Orchestrated Virtual Machine Scale Set %q (Resource Group %q): ID was empty", name, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmOrchestratedVirtualMachineScaleSetRead(d, meta) | ||
} | ||
|
||
func resourceArmOrchestratedVirtualMachineScaleSetRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Compute.VMScaleSetClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.VirtualMachineScaleSetID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, id.ResourceGroup, id.Name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Orchestrated Virtual Machine Scale Set %q was not found in Resource Group %q - removing from state!", id.Name, id.ResourceGroup) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("retrieving Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
d.Set("name", id.Name) | ||
d.Set("resource_group_name", id.ResourceGroup) | ||
d.Set("location", location.NormalizeNilable(resp.Location)) | ||
|
||
if props := resp.VirtualMachineScaleSetProperties; props != nil { | ||
d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount) | ||
d.Set("single_placement_group", props.SinglePlacementGroup) | ||
d.Set("unique_id", props.UniqueID) | ||
} | ||
|
||
if err := d.Set("zones", resp.Zones); err != nil { | ||
return fmt.Errorf("setting `zones`: %+v", err) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
func resourceArmOrchestratedVirtualMachineScaleSetDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Compute.VMScaleSetClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := parse.VirtualMachineScaleSetID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
future, err := client.Delete(ctx, id.ResourceGroup, id.Name) | ||
if err != nil { | ||
return fmt.Errorf("deleting Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("waiting for deletion of Orchestrated Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.