Skip to content

Commit

Permalink
azurerm_orchestrated_virtual_machine_scale_set - add support for `s…
Browse files Browse the repository at this point in the history
…ku_profile` block (#27599)

* add `sku_profile` to support mix instance

* resolve comments
  • Loading branch information
ms-zhenhua authored Oct 29, 2024
1 parent 0ae6436 commit 0af5a37
Show file tree
Hide file tree
Showing 4 changed files with 639 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

const (
SkuNameMix = "Mix"
)

func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceOrchestratedVirtualMachineScaleSetCreate,
Expand Down Expand Up @@ -93,6 +97,35 @@ func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {
ValidateFunc: computeValidate.OrchestratedVirtualMachineScaleSetSku,
},

"sku_profile": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"allocation_strategy": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(
virtualmachinescalesets.PossibleValuesForAllocationStrategy(),
false,
),
},

"vm_sizes": {
Type: pluginsdk.TypeSet,
Required: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},
},

"os_profile": OrchestratedVirtualMachineScaleSetOSProfileSchema(),

// Optional
Expand Down Expand Up @@ -293,6 +326,23 @@ func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {

return false
}),

pluginsdk.CustomizeDiffShim(func(ctx context.Context, diff *pluginsdk.ResourceDiff, v interface{}) error {
skuName, hasSkuName := diff.GetOk("sku_name")
_, hasSkuProfile := diff.GetOk("sku_profile")

if hasSkuProfile {
if !hasSkuName || skuName != SkuNameMix {
return fmt.Errorf("`sku_profile` can only be set when `sku_name` is set to `Mix`")
}
} else {
if hasSkuName && skuName == SkuNameMix {
return fmt.Errorf("`sku_profile` must be set when `sku_name` is set to `Mix`")
}
}

return nil
}),
),
}
}
Expand Down Expand Up @@ -376,6 +426,10 @@ func resourceOrchestratedVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData,
props.Sku = sku
}

if v, ok := d.GetOk("sku_profile"); ok {
props.Properties.SkuProfile = expandOrchestratedVirtualMachineScaleSetSkuProfile(v.([]interface{}))
}

if v, ok := d.GetOk("capacity_reservation_group_id"); ok {
if d.Get("single_placement_group").(bool) {
return fmt.Errorf("`single_placement_group` must be set to `false` when `capacity_reservation_group_id` is specified")
Expand Down Expand Up @@ -757,6 +811,11 @@ func resourceOrchestratedVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData,
}
}

if d.HasChange("sku_profile") {
updateInstances = true
updateProps.SkuProfile = expandOrchestratedVirtualMachineScaleSetSkuProfile(d.Get("sku_profile").([]interface{}))
}

if d.HasChange("max_bid_price") {
if priority != virtualmachinescalesets.VirtualMachinePriorityTypesSpot {
return fmt.Errorf("`max_bid_price` can only be configured when `priority` is set to `Spot`")
Expand Down Expand Up @@ -1223,6 +1282,11 @@ func resourceOrchestratedVirtualMachineScaleSetRead(d *pluginsdk.ResourceData, m
if props.SinglePlacementGroup != nil {
d.Set("single_placement_group", props.SinglePlacementGroup)
}

if err := d.Set("sku_profile", flattenOrchestratedVirtualMachineScaleSetSkuProfile(props.SkuProfile)); err != nil {
return fmt.Errorf("setting `sku_profile`: %+v", err)
}

d.Set("unique_id", props.UniqueId)
d.Set("zone_balance", props.ZoneBalance)

Expand Down Expand Up @@ -1390,17 +1454,60 @@ func resourceOrchestratedVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData,
return nil
}

func expandOrchestratedVirtualMachineScaleSetSkuProfile(input []interface{}) *virtualmachinescalesets.SkuProfile {
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})
vmSizesRaw := v["vm_sizes"].(*pluginsdk.Set).List()
vmSizes := make([]virtualmachinescalesets.SkuProfileVMSize, 0)
for _, vmSize := range vmSizesRaw {
vmSizes = append(vmSizes, virtualmachinescalesets.SkuProfileVMSize{
Name: pointer.To(vmSize.(string)),
})
}

return &virtualmachinescalesets.SkuProfile{
AllocationStrategy: pointer.To((virtualmachinescalesets.AllocationStrategy)(v["allocation_strategy"].(string))),
VMSizes: pointer.To(vmSizes),
}
}

func flattenOrchestratedVirtualMachineScaleSetSkuProfile(input *virtualmachinescalesets.SkuProfile) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

vmSizes := make([]string, 0)
if input.VMSizes != nil {
for _, vmSize := range *input.VMSizes {
vmSizes = append(vmSizes, *vmSize.Name)
}
}

return []interface{}{
map[string]interface{}{
"allocation_strategy": string(pointer.From(input.AllocationStrategy)),
"vm_sizes": vmSizes,
},
}
}

func expandOrchestratedVirtualMachineScaleSetSku(input string, capacity int) (*virtualmachinescalesets.Sku, error) {
skuParts := strings.Split(input, "_")

if len(skuParts) < 2 || strings.Contains(input, "__") || strings.Contains(input, " ") {
if (input != SkuNameMix && len(skuParts) < 2) || strings.Contains(input, "__") || strings.Contains(input, " ") {
return nil, fmt.Errorf("'sku_name'(%q) is not formatted properly", input)
}

sku := &virtualmachinescalesets.Sku{
Name: pointer.To(input),
Capacity: utils.Int64(int64(capacity)),
Tier: pointer.To("Standard"),
}

if input != SkuNameMix {
sku.Tier = pointer.To("Standard")
}

return sku, nil
Expand All @@ -1409,7 +1516,7 @@ func expandOrchestratedVirtualMachineScaleSetSku(input string, capacity int) (*v
func flattenOrchestratedVirtualMachineScaleSetSku(input *virtualmachinescalesets.Sku) (*string, error) {
var skuName string
if input != nil && input.Name != nil {
if strings.HasPrefix(strings.ToLower(*input.Name), "standard") {
if strings.HasPrefix(strings.ToLower(*input.Name), "standard") || *input.Name == SkuNameMix {
skuName = *input.Name
} else {
skuName = fmt.Sprintf("Standard_%s", *input.Name)
Expand Down
Loading

0 comments on commit 0af5a37

Please sign in to comment.