-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/azurerm: add virtual_machine_extension resource
Picked up from where #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 (810.44s) === RUN TestAccAzureRMVirtualMachineExtension_basic --- PASS: TestAccAzureRMVirtualMachineExtension_basic (839.55s) === RUN TestAccAzureRMVirtualMachineExtension_concurrent --- PASS: TestAccAzureRMVirtualMachineExtension_concurrent (689.69s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 2339.695s
- Loading branch information
Peter McAtominey
committed
Nov 9, 2016
1 parent
6c801d0
commit 668dae8
Showing
6 changed files
with
1,004 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
builtin/providers/azurerm/import_arm_virtual_machine_extension_test.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,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"}, | ||
}, | ||
}, | ||
}) | ||
} |
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
236 changes: 236 additions & 0 deletions
236
builtin/providers/azurerm/resource_arm_virtual_machine_extension.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,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) | ||
} |
Oops, something went wrong.