github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_managed_disk.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Azure/azure-sdk-for-go/arm/disk"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/helper/validation"
     8  	"log"
     9  	"net/http"
    10  	"strings"
    11  )
    12  
    13  func resourceArmManagedDisk() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmManagedDiskCreate,
    16  		Read:   resourceArmManagedDiskRead,
    17  		Update: resourceArmManagedDiskCreate,
    18  		Delete: resourceArmManagedDiskDelete,
    19  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"location": locationSchema(),
    31  
    32  			"resource_group_name": {
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"storage_account_type": {
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  				ValidateFunc: validation.StringInSlice([]string{
    42  					string(disk.PremiumLRS),
    43  					string(disk.StandardLRS),
    44  				}, true),
    45  			},
    46  
    47  			"create_option": {
    48  				Type:     schema.TypeString,
    49  				Required: true,
    50  				ForceNew: true,
    51  				ValidateFunc: validation.StringInSlice([]string{
    52  					string(disk.Import),
    53  					string(disk.Empty),
    54  					string(disk.Copy),
    55  				}, true),
    56  			},
    57  
    58  			"source_uri": {
    59  				Type:     schema.TypeString,
    60  				Optional: true,
    61  				Computed: true,
    62  				ForceNew: true,
    63  			},
    64  
    65  			"source_resource_id": {
    66  				Type:     schema.TypeString,
    67  				Optional: true,
    68  				ForceNew: true,
    69  			},
    70  
    71  			"os_type": {
    72  				Type:     schema.TypeString,
    73  				Optional: true,
    74  				ValidateFunc: validation.StringInSlice([]string{
    75  					string(disk.Windows),
    76  					string(disk.Linux),
    77  				}, true),
    78  			},
    79  
    80  			"disk_size_gb": {
    81  				Type:         schema.TypeInt,
    82  				Required:     true,
    83  				ValidateFunc: validateDiskSizeGB,
    84  			},
    85  
    86  			"tags": tagsSchema(),
    87  		},
    88  	}
    89  }
    90  
    91  func validateDiskSizeGB(v interface{}, k string) (ws []string, errors []error) {
    92  	value := v.(int)
    93  	if value < 1 || value > 1023 {
    94  		errors = append(errors, fmt.Errorf(
    95  			"The `disk_size_gb` can only be between 1 and 1023"))
    96  	}
    97  	return
    98  }
    99  
   100  func resourceArmManagedDiskCreate(d *schema.ResourceData, meta interface{}) error {
   101  	client := meta.(*ArmClient)
   102  	diskClient := client.diskClient
   103  
   104  	log.Printf("[INFO] preparing arguments for Azure ARM Managed Disk creation.")
   105  
   106  	name := d.Get("name").(string)
   107  	location := d.Get("location").(string)
   108  	resGroup := d.Get("resource_group_name").(string)
   109  	tags := d.Get("tags").(map[string]interface{})
   110  	expandedTags := expandTags(tags)
   111  
   112  	createDisk := disk.Model{
   113  		Name:     &name,
   114  		Location: &location,
   115  		Tags:     expandedTags,
   116  	}
   117  
   118  	storageAccountType := d.Get("storage_account_type").(string)
   119  	osType := d.Get("os_type").(string)
   120  
   121  	createDisk.Properties = &disk.Properties{
   122  		AccountType: disk.StorageAccountTypes(storageAccountType),
   123  		OsType:      disk.OperatingSystemTypes(osType),
   124  	}
   125  
   126  	if v := d.Get("disk_size_gb"); v != 0 {
   127  		diskSize := int32(v.(int))
   128  		createDisk.Properties.DiskSizeGB = &diskSize
   129  	}
   130  	createOption := d.Get("create_option").(string)
   131  
   132  	creationData := &disk.CreationData{
   133  		CreateOption: disk.CreateOption(createOption),
   134  	}
   135  
   136  	if strings.EqualFold(createOption, string(disk.Import)) {
   137  		if sourceUri := d.Get("source_uri").(string); sourceUri != "" {
   138  			creationData.SourceURI = &sourceUri
   139  		} else {
   140  			return fmt.Errorf("[ERROR] source_uri must be specified when create_option is `%s`", disk.Import)
   141  		}
   142  	} else if strings.EqualFold(createOption, string(disk.Copy)) {
   143  		if sourceResourceId := d.Get("source_resource_id").(string); sourceResourceId != "" {
   144  			creationData.SourceResourceID = &sourceResourceId
   145  		} else {
   146  			return fmt.Errorf("[ERROR] source_resource_id must be specified when create_option is `%s`", disk.Copy)
   147  		}
   148  	}
   149  
   150  	createDisk.CreationData = creationData
   151  
   152  	_, diskErr := diskClient.CreateOrUpdate(resGroup, name, createDisk, make(chan struct{}))
   153  	if diskErr != nil {
   154  		return diskErr
   155  	}
   156  
   157  	read, err := diskClient.Get(resGroup, name)
   158  	if err != nil {
   159  		return err
   160  	}
   161  	if read.ID == nil {
   162  		return fmt.Errorf("[ERROR] Cannot read Managed Disk %s (resource group %s) ID", name, resGroup)
   163  	}
   164  
   165  	d.SetId(*read.ID)
   166  
   167  	return resourceArmManagedDiskRead(d, meta)
   168  }
   169  
   170  func resourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error {
   171  	diskClient := meta.(*ArmClient).diskClient
   172  
   173  	id, err := parseAzureResourceID(d.Id())
   174  	if err != nil {
   175  		return err
   176  	}
   177  	resGroup := id.ResourceGroup
   178  	name := id.Path["disks"]
   179  
   180  	resp, err := diskClient.Get(resGroup, name)
   181  	if err != nil {
   182  		if resp.StatusCode == http.StatusNotFound {
   183  			d.SetId("")
   184  			return nil
   185  		}
   186  		return fmt.Errorf("[ERROR] Error making Read request on Azure Managed Disk %s (resource group %s): %s", name, resGroup, err)
   187  	}
   188  
   189  	d.Set("name", resp.Name)
   190  	d.Set("resource_group_name", resGroup)
   191  	d.Set("location", resp.Location)
   192  
   193  	if resp.Properties != nil {
   194  		flattenAzureRmManagedDiskProperties(d, resp.Properties)
   195  	}
   196  
   197  	if resp.CreationData != nil {
   198  		flattenAzureRmManagedDiskCreationData(d, resp.CreationData)
   199  	}
   200  
   201  	flattenAndSetTags(d, resp.Tags)
   202  
   203  	return nil
   204  }
   205  
   206  func resourceArmManagedDiskDelete(d *schema.ResourceData, meta interface{}) error {
   207  	diskClient := meta.(*ArmClient).diskClient
   208  
   209  	id, err := parseAzureResourceID(d.Id())
   210  	if err != nil {
   211  		return err
   212  	}
   213  	resGroup := id.ResourceGroup
   214  	name := id.Path["disks"]
   215  
   216  	if _, err = diskClient.Delete(resGroup, name, make(chan struct{})); err != nil {
   217  		return err
   218  	}
   219  
   220  	return nil
   221  }
   222  
   223  func flattenAzureRmManagedDiskProperties(d *schema.ResourceData, properties *disk.Properties) {
   224  	d.Set("storage_account_type", string(properties.AccountType))
   225  	if properties.DiskSizeGB != nil {
   226  		d.Set("disk_size_gb", *properties.DiskSizeGB)
   227  	}
   228  	if properties.OsType != "" {
   229  		d.Set("os_type", string(properties.OsType))
   230  	}
   231  }
   232  
   233  func flattenAzureRmManagedDiskCreationData(d *schema.ResourceData, creationData *disk.CreationData) {
   234  	d.Set("create_option", string(creationData.CreateOption))
   235  	if creationData.SourceURI != nil {
   236  		d.Set("source_uri", *creationData.SourceURI)
   237  	}
   238  }