Skip to content

Commit

Permalink
provider/azurerm: add virtual_machine_extension resource
Browse files Browse the repository at this point in the history
Picked up from where hashicorp#6548 left off

settings and protected_settings take JSON objects as strings to make extension
generic

TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMVirtualMachineExtension" -timeout 120m
=== RUN   TestAccAzureRMVirtualMachineExtension_importBasic
--- PASS: TestAccAzureRMVirtualMachineExtension_importBasic (697.55s)
=== RUN   TestAccAzureRMVirtualMachineExtension_basic
--- PASS: TestAccAzureRMVirtualMachineExtension_basic (824.17s)
=== RUN   TestAccAzureRMVirtualMachineExtension_concurrent
--- PASS: TestAccAzureRMVirtualMachineExtension_concurrent (929.74s)
=== RUN   TestAccAzureRMVirtualMachineExtension_linuxDiagnostics
--- PASS: TestAccAzureRMVirtualMachineExtension_linuxDiagnostics (803.19s)
PASS
ok 	github.com/hashicorp/terraform/builtin/providers/azurerm	3254.663s
  • Loading branch information
Peter McAtominey committed Nov 9, 2016
1 parent 6c801d0 commit cdbedcb
Show file tree
Hide file tree
Showing 6 changed files with 1,004 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMVirtualMachineExtension_importBasic(t *testing.T) {
resourceName := "azurerm_virtual_machine_extension.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMVirtualMachineExtension_basic, ri, ri, ri, ri, ri, ri, ri, ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineExtensionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
},

resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"protected_settings"},
},
},
})
}
1 change: 1 addition & 0 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_template_deployment": resourceArmTemplateDeployment(),
"azurerm_traffic_manager_endpoint": resourceArmTrafficManagerEndpoint(),
"azurerm_traffic_manager_profile": resourceArmTrafficManagerProfile(),
"azurerm_virtual_machine_extension": resourceArmVirtualMachineExtensions(),
"azurerm_virtual_machine": resourceArmVirtualMachine(),
"azurerm_virtual_machine_scale_set": resourceArmVirtualMachineScaleSet(),
"azurerm_virtual_network": resourceArmVirtualNetwork(),
Expand Down
236 changes: 236 additions & 0 deletions builtin/providers/azurerm/resource_arm_virtual_machine_extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package azurerm

import (
"encoding/json"
"fmt"
"net/http"
"reflect"

"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/hashicorp/terraform/helper/schema"
)

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{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},

"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"virtual_machine_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

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

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

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

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

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

// due to the sensitive nature, these are not returned by the API
"protected_settings": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressDiffVirtualMachineExtensionSettings,
},

"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,
Properties: &compute.VirtualMachineExtensionProperties{
Publisher: &publisher,
Type: &extensionType,
TypeHandlerVersion: &typeHandlerVersion,
AutoUpgradeMinorVersion: &autoUpgradeMinor,
},
Tags: expandTags(tags),
}

if settingsString := d.Get("settings").(string); settingsString != "" {
settings, err := expandArmVirtualMachineExtensionSettings(settingsString)
if err != nil {
return fmt.Errorf("unable to parse settings: %s", err)
}
extension.Properties.Settings = &settings
}

if protectedSettingsString := d.Get("protected_settings").(string); protectedSettingsString != "" {
protectedSettings, err := expandArmVirtualMachineExtensionSettings(protectedSettingsString)
if err != nil {
return fmt.Errorf("unable to parse protected_settings: %s", err)
}
extension.Properties.ProtectedSettings = &protectedSettings
}

_, err := client.CreateOrUpdate(resGroup, vmName, name, extension, make(chan struct{}))
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, "")
if err != nil {
return fmt.Errorf("Error making Read request on Virtual Machine Extension %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("virtual_machine_name", vmName)
d.Set("resource_group_name", resGroup)
d.Set("publisher", resp.Properties.Publisher)
d.Set("type", resp.Properties.Type)
d.Set("type_handler_version", resp.Properties.TypeHandlerVersion)
d.Set("auto_upgrade_minor_version", resp.Properties.AutoUpgradeMinorVersion)

if resp.Properties.Settings != nil {
settings, err := flattenArmVirtualMachineExtensionSettings(*resp.Properties.Settings)
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"]

_, err = client.Delete(resGroup, vmName, name, make(chan struct{}))

return nil
}

func expandArmVirtualMachineExtensionSettings(jsonString string) (map[string]interface{}, error) {
var result map[string]interface{}

err := json.Unmarshal([]byte(jsonString), &result)

return result, err
}

func flattenArmVirtualMachineExtensionSettings(settingsMap map[string]interface{}) (string, error) {
result, err := json.Marshal(settingsMap)
if err != nil {
return "", err
}

return string(result), nil
}

func suppressDiffVirtualMachineExtensionSettings(k, old, new string, d *schema.ResourceData) bool {
oldMap, err := expandArmVirtualMachineExtensionSettings(old)
if err != nil {
return false
}

newMap, err := expandArmVirtualMachineExtensionSettings(new)
if err != nil {
return false
}

return reflect.DeepEqual(oldMap, newMap)
}
Loading

0 comments on commit cdbedcb

Please sign in to comment.