Skip to content

Commit

Permalink
Merge branch 'vmss_extensions' into vnext
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Rudduck committed Mar 3, 2017
2 parents 27c88e9 + 0ca483f commit b8bfbed
Show file tree
Hide file tree
Showing 3 changed files with 437 additions and 3 deletions.
154 changes: 151 additions & 3 deletions builtin/providers/azurerm/resource_arm_virtual_machine_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,55 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
Set: resourceArmVirtualMachineScaleSetStorageProfileImageReferenceHash,
},

"extension": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"publisher": {
Type: schema.TypeString,
Required: true,
},

"type": {
Type: schema.TypeString,
Required: true,
},

"type_handler_version": {
Type: schema.TypeString,
Required: true,
},

"auto_upgrade_minor_version": {
Type: schema.TypeBool,
Optional: true,
},

"settings": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressDiffVirtualMachineExtensionSettings,
},

"protected_settings": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressDiffVirtualMachineExtensionSettings,
},
},
},
Set: resourceArmVirtualMachineScaleSetExtensionHash,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -381,16 +430,22 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
return err
}

extensions, err := expandAzureRMVirtualMachineScaleSetExtensions(d)
if err != nil {
return err
}

updatePolicy := d.Get("upgrade_policy_mode").(string)
overprovision := d.Get("overprovision").(bool)
scaleSetProps := compute.VirtualMachineScaleSetProperties{
UpgradePolicy: &compute.UpgradePolicy{
Mode: compute.UpgradeMode(updatePolicy),
},
VirtualMachineProfile: &compute.VirtualMachineScaleSetVMProfile{
NetworkProfile: expandAzureRmVirtualMachineScaleSetNetworkProfile(d),
StorageProfile: &storageProfile,
OsProfile: osProfile,
NetworkProfile: expandAzureRmVirtualMachineScaleSetNetworkProfile(d),
StorageProfile: &storageProfile,
OsProfile: osProfile,
ExtensionProfile: extensions,
},
Overprovision: &overprovision,
}
Expand Down Expand Up @@ -488,6 +543,12 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Storage Profile OS Disk error: %#v", err)
}

if properties.VirtualMachineProfile.ExtensionProfile != nil {
if err := d.Set("extension", flattenAzureRmVirtualMachineScaleSetExtensionProfile(properties.VirtualMachineProfile.ExtensionProfile)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Extension Profile error: %#v", err)
}
}

flattenAndSetTags(d, resp.Tags)

return nil
Expand Down Expand Up @@ -709,6 +770,38 @@ func flattenAzureRmVirtualMachineScaleSetSku(sku *compute.Sku) []interface{} {
return []interface{}{result}
}

func flattenAzureRmVirtualMachineScaleSetExtensionProfile(profile *compute.VirtualMachineScaleSetExtensionProfile) []map[string]interface{} {
if profile.Extensions == nil {
return nil
}

result := make([]map[string]interface{}, 0, len(*profile.Extensions))
for _, extension := range *profile.Extensions {
e := make(map[string]interface{})
e["name"] = *extension.Name
properties := extension.VirtualMachineScaleSetExtensionProperties
if properties != nil {
e["publisher"] = *properties.Publisher
e["type"] = *properties.Type
e["type_handler_version"] = *properties.TypeHandlerVersion
if properties.AutoUpgradeMinorVersion != nil {
e["auto_upgrade_minor_version"] = *properties.AutoUpgradeMinorVersion
}

if properties.Settings != nil {
settings, err := flattenArmVirtualMachineExtensionSettings(*properties.Settings)
if err == nil {
e["settings"] = settings
}
}
}

result = append(result, e)
}

return result
}

func resourceArmVirtualMachineScaleSetStorageProfileImageReferenceHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
Expand Down Expand Up @@ -783,6 +876,13 @@ func resourceArmVirtualMachineScaleSetOsProfileLWindowsConfigHash(v interface{})
return hashcode.String(buf.String())
}

func resourceArmVirtualMachineScaleSetExtensionHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s", m["name"].(string)))
return hashcode.String(buf.String())
}

func expandVirtualMachineScaleSetSku(d *schema.ResourceData) (*compute.Sku, error) {
skuConfig := d.Get("sku").(*schema.Set).List()

Expand Down Expand Up @@ -1111,3 +1211,51 @@ func expandAzureRmVirtualMachineScaleSetOsProfileSecrets(d *schema.ResourceData)

return &secrets
}

func expandAzureRMVirtualMachineScaleSetExtensions(d *schema.ResourceData) (*compute.VirtualMachineScaleSetExtensionProfile, error) {
extensions := d.Get("extension").(*schema.Set).List()
resources := make([]compute.VirtualMachineScaleSetExtension, 0, len(extensions))
for _, e := range extensions {
config := e.(map[string]interface{})
name := config["name"].(string)
publisher := config["publisher"].(string)
t := config["type"].(string)
version := config["type_handler_version"].(string)

extension := compute.VirtualMachineScaleSetExtension{
Name: &name,
VirtualMachineScaleSetExtensionProperties: &compute.VirtualMachineScaleSetExtensionProperties{
Publisher: &publisher,
Type: &t,
TypeHandlerVersion: &version,
},
}

if u := config["auto_upgrade_minor_version"]; u != nil {
upgrade := u.(bool)
extension.VirtualMachineScaleSetExtensionProperties.AutoUpgradeMinorVersion = &upgrade
}

if s := config["settings"].(string); s != "" {
settings, err := expandArmVirtualMachineExtensionSettings(s)
if err != nil {
return nil, fmt.Errorf("unable to parse settings: %s", err)
}
extension.VirtualMachineScaleSetExtensionProperties.Settings = &settings
}

if s := config["protected_settings"].(string); s != "" {
protectedSettings, err := expandArmVirtualMachineExtensionSettings(s)
if err != nil {
return nil, fmt.Errorf("unable to parse protected_settings: %s", err)
}
extension.VirtualMachineScaleSetExtensionProperties.ProtectedSettings = &protectedSettings
}

resources = append(resources, extension)
}

return &compute.VirtualMachineScaleSetExtensionProfile{
Extensions: &resources,
}, nil
}
Loading

0 comments on commit b8bfbed

Please sign in to comment.