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