forked from hashicorp/terraform-provider-azurerm
/
data_source_managed_disk.go
98 lines (75 loc) · 2.07 KB
/
data_source_managed_disk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package azurerm
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmManagedDisk() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmManagedDiskRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"zones": zonesSchemaComputed(),
"storage_account_type": {
Type: schema.TypeString,
Computed: true,
},
"source_uri": {
Type: schema.TypeString,
Computed: true,
},
"source_resource_id": {
Type: schema.TypeString,
Computed: true,
},
"os_type": {
Type: schema.TypeString,
Computed: true,
},
"disk_size_gb": {
Type: schema.TypeInt,
Computed: true,
},
"create_option": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).diskClient
ctx := meta.(*ArmClient).StopContext
resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Managed Disk %q (Resource Group %q) was not found", name, resGroup)
}
return fmt.Errorf("[ERROR] Error making Read request on Azure Managed Disk %q (Resource Group %q): %s", name, resGroup, err)
}
d.SetId(*resp.ID)
if sku := resp.Sku; sku != nil {
d.Set("storage_account_type", string(sku.Name))
}
if props := resp.DiskProperties; props != nil {
if diskSize := props.DiskSizeGB; diskSize != nil {
d.Set("disk_size_gb", *diskSize)
}
if osType := props.OsType; osType != "" {
d.Set("os_type", string(osType))
}
}
if resp.CreationData != nil {
flattenAzureRmManagedDiskCreationData(d, resp.CreationData)
}
d.Set("zones", resp.Zones)
flattenAndSetTags(d, resp.Tags)
return nil
}