[WIP] arm/disk implementation
This commit is contained in:
parent
e3dbe4763c
commit
0a07c14377
|
@ -25,6 +25,7 @@ import (
|
||||||
"github.com/Azure/go-autorest/autorest/azure"
|
"github.com/Azure/go-autorest/autorest/azure"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
riviera "github.com/jen20/riviera/azure"
|
riviera "github.com/jen20/riviera/azure"
|
||||||
|
"github.com/Azure/azure-sdk-for-go/arm/disk"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ArmClient contains the handles to all the specific Azure Resource Manager
|
// ArmClient contains the handles to all the specific Azure Resource Manager
|
||||||
|
@ -47,6 +48,8 @@ type ArmClient struct {
|
||||||
vmImageClient compute.VirtualMachineImagesClient
|
vmImageClient compute.VirtualMachineImagesClient
|
||||||
vmClient compute.VirtualMachinesClient
|
vmClient compute.VirtualMachinesClient
|
||||||
|
|
||||||
|
diskClient disk.DisksClient
|
||||||
|
|
||||||
appGatewayClient network.ApplicationGatewaysClient
|
appGatewayClient network.ApplicationGatewaysClient
|
||||||
ifaceClient network.InterfacesClient
|
ifaceClient network.InterfacesClient
|
||||||
loadBalancerClient network.LoadBalancersClient
|
loadBalancerClient network.LoadBalancersClient
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package azurerm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/acctest"
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccAzureRMDisk_importEmpty(t *testing.T) {
|
||||||
|
runTestAzureRMDisk_import(t, "azurerm_disk.test", testAccAzureRMDisk_emptyDisk)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAccAzureRMDisk_importBlob(t *testing.T) {
|
||||||
|
runTestAzureRMDisk_import(t, "azurerm_disk.test", testAccAzureRMDisk_blob)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runTestAzureRMDisk_import(t *testing.T, resourceName string, configSource string) {
|
||||||
|
ri := acctest.RandInt()
|
||||||
|
config := fmt.Sprintf(configSource, ri, ri, ri)
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t)},
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testCheckAzureRMDiskDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: config,
|
||||||
|
},
|
||||||
|
|
||||||
|
resource.TestStep{
|
||||||
|
ResourceName: resourceName,
|
||||||
|
ImportState: true,
|
||||||
|
ImportStateVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,242 @@
|
||||||
|
package azurerm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Azure/azure-sdk-for-go/arm/disk"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
"github.com/hashicorp/terraform/helper/validation"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resourceArmDisk() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Create: resourceArmDiskCreate,
|
||||||
|
Read: resourceArmDiskRead,
|
||||||
|
Update: resourceArmDiskCreate,
|
||||||
|
Delete: resourceArmDiskDelete,
|
||||||
|
Importer: &schema.ResourceImporter{
|
||||||
|
State: schema.ImportStatePassthrough,
|
||||||
|
},
|
||||||
|
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"name": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
"location": locationSchema(),
|
||||||
|
|
||||||
|
"resource_group_name": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
"storage_account_type": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ValidateFunc: validation.StringInSlice([]string{
|
||||||
|
string(disk.PremiumLRS),
|
||||||
|
string(disk.StandardLRS),
|
||||||
|
}, true),
|
||||||
|
},
|
||||||
|
|
||||||
|
"create_option": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
ValidateFunc: validation.StringInSlice([]string{
|
||||||
|
string(disk.Import),
|
||||||
|
string(disk.Empty),
|
||||||
|
}, true),
|
||||||
|
},
|
||||||
|
|
||||||
|
"vhd_uri": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ForceNew: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
"os_type": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ValidateFunc: validation.StringInSlice([]string{
|
||||||
|
string(disk.Windows),
|
||||||
|
string(disk.Linux),
|
||||||
|
}, true),
|
||||||
|
},
|
||||||
|
|
||||||
|
"disk_size_gb": {
|
||||||
|
Type: schema.TypeInt,
|
||||||
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
|
ValidateFunc: validateDiskSizeGB,
|
||||||
|
},
|
||||||
|
|
||||||
|
"tags": tagsSchema(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateDiskSizeGB(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
value := v.(int)
|
||||||
|
if value < 1 || value > 1023 {
|
||||||
|
errors = append(errors, fmt.Errorf(
|
||||||
|
"The `disk_size_gb` can only be between 1 and 1023"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceArmDiskCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
client := meta.(*ArmClient)
|
||||||
|
diskClient := client.diskClient
|
||||||
|
|
||||||
|
log.Printf("[INFO] preparing arguments for Azure ARM Disk creation.")
|
||||||
|
|
||||||
|
name := d.Get("name").(string)
|
||||||
|
location := d.Get("location").(string)
|
||||||
|
resGroup := d.Get("resource_group_name").(string)
|
||||||
|
tags := d.Get("tags").(map[string]interface{})
|
||||||
|
expandedTags := expandTags(tags)
|
||||||
|
|
||||||
|
createDisk := &disk.Model {
|
||||||
|
Name: &name,
|
||||||
|
Location: &location,
|
||||||
|
Tags: expandedTags,
|
||||||
|
}
|
||||||
|
|
||||||
|
storageAccountType := d.Get("storage_account_type").(string)
|
||||||
|
osType := d.Get("os_type").(string)
|
||||||
|
diskSize := d.Get("disk_size_db").(int)
|
||||||
|
|
||||||
|
createDisk.Properties = &disk.Properties {
|
||||||
|
AccountType: &storageAccountType,
|
||||||
|
OsType: &osType,
|
||||||
|
DiskSizeGB: &diskSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
createOption := d.Get("create_option").(string)
|
||||||
|
|
||||||
|
creationData := &disk.CreationData{
|
||||||
|
CreateOption: disk.CreateOption(createOption),
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.EqualFold(createOption, disk.Import) {
|
||||||
|
if vhdUri := d.Get("vhd_uri").(string); vhdUri != "" {
|
||||||
|
creationData.SourceURI = vhdUri;
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("[ERROR] vhd_uri must be specified when create_option is `%s`", disk.Import)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createDisk.CreationData = creationData
|
||||||
|
|
||||||
|
_, diskErr := diskClient.CreateOrUpdate(resGroup, name, createDisk, make(chan struct{}))
|
||||||
|
if diskErr != nil {
|
||||||
|
return diskErr
|
||||||
|
}
|
||||||
|
|
||||||
|
read, err := diskClient.Get(resGroup, name)
|
||||||
|
if err != nil{
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if read.ID == nil {
|
||||||
|
return fmt.Errorf("[ERROR] Cannot read Disk %s (resource group %s) ID", name, resGroup)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId(*read.ID)
|
||||||
|
|
||||||
|
return resourceArmDiskRead(d, meta)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceArmDiskRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
diskClient := meta.(*ArmClient).diskClient
|
||||||
|
|
||||||
|
id, err := parseAzureResourceID(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resGroup := id.ResourceGroup
|
||||||
|
name := id.Path["disks"]
|
||||||
|
|
||||||
|
resp, err := diskClient.Get(resGroup, name)
|
||||||
|
if err != nil {
|
||||||
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
d.SetId("")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("[ERROR] Error making Read request on Azure Disk %s (resource group %s): %s", name, resGroup, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.Set("name", resp.Name)
|
||||||
|
d.Set("resource_group_name", resGroup)
|
||||||
|
d.Set("location", resp.Location)
|
||||||
|
|
||||||
|
if resp.Properties != nil {
|
||||||
|
if m, err := flattenAzureRmDiskProperties(resp.Properties); err != nil {
|
||||||
|
return fmt.Errorf("[DEBUG] Error setting disk properties: %#v", err)
|
||||||
|
} else {
|
||||||
|
d.Set("storage_account_type", m["storage_account_type"])
|
||||||
|
d.Set("disk_size_gb", m["disk_size_gb"])
|
||||||
|
if m["os_type"] != nil {
|
||||||
|
d.Set("os_type", m["os_type"])
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.CreationData != nil {
|
||||||
|
if m, err := flattenAzureRmDiskCreationData(resp.CreationData); err != nil {
|
||||||
|
return fmt.Errorf("[DEBUG] Error setting disk creation data: %#v", err)
|
||||||
|
} else {
|
||||||
|
d.Set("create_option", m["create_option"])
|
||||||
|
if m["vhd_uri"] != nil {
|
||||||
|
d.Set("vhd_uri", m["vhd_uri"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flattenAndSetTags(d, resp.Tags)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceArmDiskDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
diskClient := meta.(*ArmClient).diskClient
|
||||||
|
|
||||||
|
id, err := parseAzureResourceID(d.Id())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resGroup := id.ResourceGroup
|
||||||
|
name := id.Path["disks"]
|
||||||
|
|
||||||
|
if _, err = diskClient.Delete(resGroup, name, make(chan struct{})); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func flattenAzureRmDiskProperties(properties *disk.Properties) (map[string]interface{}, error) {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
result["storage_account_type"] = *properties.AccountType
|
||||||
|
result["disk_size_gb"] = *properties.DiskSizeGB
|
||||||
|
if properties.OsType != nil {
|
||||||
|
result["os_type"] = *properties.OsType
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func flattenAzureRmDiskCreationData(creationData *disk.CreationData) (map[string]interface{}, error) {
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
result["create_option"] = *creationData.CreateOption
|
||||||
|
if creationData.SourceURI != nil {
|
||||||
|
result["vhd_uri"] = *creationData.SourceURI
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package azurerm
|
|
@ -243,6 +243,7 @@ func resourceArmVirtualMachine() *schema.Resource {
|
||||||
Type: schema.TypeInt,
|
Type: schema.TypeInt,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
|
ValidateFunc: validateDiskSizeGB,
|
||||||
},
|
},
|
||||||
|
|
||||||
"lun": {
|
"lun": {
|
||||||
|
@ -483,15 +484,6 @@ func validateLicenseType(v interface{}, k string) (ws []string, errors []error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateDiskSizeGB(v interface{}, k string) (ws []string, errors []error) {
|
|
||||||
value := v.(int)
|
|
||||||
if value < 1 || value > 1023 {
|
|
||||||
errors = append(errors, fmt.Errorf(
|
|
||||||
"The `disk_size_gb` can only be between 1 and 1023"))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
client := meta.(*ArmClient)
|
client := meta.(*ArmClient)
|
||||||
vmClient := client.vmClient
|
vmClient := client.vmClient
|
||||||
|
|
|
@ -48,6 +48,14 @@
|
||||||
"revision": "8e625d1702a32d01cef05a9252198d231c4af113",
|
"revision": "8e625d1702a32d01cef05a9252198d231c4af113",
|
||||||
"revisionTime": "2017-02-08T01:01:20Z"
|
"revisionTime": "2017-02-08T01:01:20Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "iAZi+Mh1Tivk3bdBbAEz+bd5nPg=",
|
||||||
|
"path": "github.com/Azure/azure-sdk-for-go/arm/disk",
|
||||||
|
"revision": "ecf40e315d5ab0ca6d7b3b7f7fbb5c1577814813",
|
||||||
|
"revisionTime": "2017-03-02T00:14:02Z",
|
||||||
|
"version": "v8.1.0-beta",
|
||||||
|
"versionExact": "v8.1.0-beta"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "Amd1JuJVsI9wU3+KI4Ba4jBbxps=",
|
"checksumSHA1": "Amd1JuJVsI9wU3+KI4Ba4jBbxps=",
|
||||||
"comment": "v2.1.1-beta-8-gca4d906",
|
"comment": "v2.1.1-beta-8-gca4d906",
|
||||||
|
|
Loading…
Reference in New Issue