2016-11-09 16:13:48 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-27 16:35:40 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/structure"
|
|
|
|
"github.com/hashicorp/terraform/helper/validation"
|
2016-11-09 16:13:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmVirtualMachineExtensions() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmVirtualMachineExtensionsCreate,
|
|
|
|
Read: resourceArmVirtualMachineExtensionsRead,
|
|
|
|
Update: resourceArmVirtualMachineExtensionsCreate,
|
|
|
|
Delete: resourceArmVirtualMachineExtensionsDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-27 16:35:40 +02:00
|
|
|
"name": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-29 16:54:00 +01:00
|
|
|
"location": locationSchema(),
|
2016-11-09 16:13:48 +01:00
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"resource_group_name": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"virtual_machine_name": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"publisher": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"type": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"type_handler_version": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"auto_upgrade_minor_version": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
"settings": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-03-27 16:35:40 +02:00
|
|
|
ValidateFunc: validation.ValidateJsonString,
|
|
|
|
DiffSuppressFunc: structure.SuppressJsonDiff,
|
2016-11-09 16:13:48 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// due to the sensitive nature, these are not returned by the API
|
2017-03-27 16:35:40 +02:00
|
|
|
"protected_settings": {
|
2016-11-09 16:13:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Sensitive: true,
|
2017-03-27 16:35:40 +02:00
|
|
|
ValidateFunc: validation.ValidateJsonString,
|
|
|
|
DiffSuppressFunc: structure.SuppressJsonDiff,
|
2016-11-09 16:13:48 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualMachineExtensionsCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vmExtensionClient
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
|
|
|
vmName := d.Get("virtual_machine_name").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
publisher := d.Get("publisher").(string)
|
|
|
|
extensionType := d.Get("type").(string)
|
|
|
|
typeHandlerVersion := d.Get("type_handler_version").(string)
|
|
|
|
autoUpgradeMinor := d.Get("auto_upgrade_minor_version").(bool)
|
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
|
|
|
|
|
|
|
extension := compute.VirtualMachineExtension{
|
|
|
|
Location: &location,
|
2016-12-06 09:39:47 +01:00
|
|
|
VirtualMachineExtensionProperties: &compute.VirtualMachineExtensionProperties{
|
2016-11-09 16:13:48 +01:00
|
|
|
Publisher: &publisher,
|
|
|
|
Type: &extensionType,
|
|
|
|
TypeHandlerVersion: &typeHandlerVersion,
|
|
|
|
AutoUpgradeMinorVersion: &autoUpgradeMinor,
|
|
|
|
},
|
|
|
|
Tags: expandTags(tags),
|
|
|
|
}
|
|
|
|
|
|
|
|
if settingsString := d.Get("settings").(string); settingsString != "" {
|
2017-03-27 16:35:40 +02:00
|
|
|
settings, err := structure.ExpandJsonFromString(settingsString)
|
2016-11-09 16:13:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse settings: %s", err)
|
|
|
|
}
|
2016-12-06 09:39:47 +01:00
|
|
|
extension.VirtualMachineExtensionProperties.Settings = &settings
|
2016-11-09 16:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if protectedSettingsString := d.Get("protected_settings").(string); protectedSettingsString != "" {
|
2017-03-27 16:35:40 +02:00
|
|
|
protectedSettings, err := structure.ExpandJsonFromString(protectedSettingsString)
|
2016-11-09 16:13:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse protected_settings: %s", err)
|
|
|
|
}
|
2016-12-06 09:39:47 +01:00
|
|
|
extension.VirtualMachineExtensionProperties.ProtectedSettings = &protectedSettings
|
2016-11-09 16:13:48 +01:00
|
|
|
}
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
_, error := client.CreateOrUpdate(resGroup, vmName, name, extension, make(chan struct{}))
|
|
|
|
err := <-error
|
2016-11-09 16:13:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := client.Get(resGroup, vmName, name, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read Virtual Machine Extension %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmVirtualMachineExtensionsRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualMachineExtensionsRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vmExtensionClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
vmName := id.Path["virtualMachines"]
|
|
|
|
name := id.Path["extensions"]
|
|
|
|
|
|
|
|
resp, err := client.Get(resGroup, vmName, name, "")
|
2017-02-12 20:54:28 +01:00
|
|
|
|
2016-11-09 16:13:48 +01:00
|
|
|
if err != nil {
|
2017-02-12 20:54:28 +01:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-09 16:13:48 +01:00
|
|
|
return fmt.Errorf("Error making Read request on Virtual Machine Extension %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("location", azureRMNormalizeLocation(*resp.Location))
|
|
|
|
d.Set("virtual_machine_name", vmName)
|
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-12-06 09:39:47 +01:00
|
|
|
d.Set("publisher", resp.VirtualMachineExtensionProperties.Publisher)
|
|
|
|
d.Set("type", resp.VirtualMachineExtensionProperties.Type)
|
|
|
|
d.Set("type_handler_version", resp.VirtualMachineExtensionProperties.TypeHandlerVersion)
|
|
|
|
d.Set("auto_upgrade_minor_version", resp.VirtualMachineExtensionProperties.AutoUpgradeMinorVersion)
|
2016-11-09 16:13:48 +01:00
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
if resp.VirtualMachineExtensionProperties.Settings != nil {
|
2017-03-27 16:35:40 +02:00
|
|
|
settings, err := structure.FlattenJsonToString(*resp.VirtualMachineExtensionProperties.Settings)
|
2016-11-09 16:13:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse settings from response: %s", err)
|
|
|
|
}
|
|
|
|
d.Set("settings", settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmVirtualMachineExtensionsDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).vmExtensionClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["extensions"]
|
|
|
|
vmName := id.Path["virtualMachines"]
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
_, error := client.Delete(resGroup, vmName, name, make(chan struct{}))
|
|
|
|
err = <-error
|
2016-11-09 16:13:48 +01:00
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
return err
|
2016-11-09 16:13:48 +01:00
|
|
|
}
|