/
data_source_managed_disk.go
148 lines (115 loc) · 3.76 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package compute
import (
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmManagedDisk() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmManagedDiskRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"create_option": {
Type: schema.TypeString,
Computed: true,
},
"disk_encryption_set_id": {
Type: schema.TypeString,
Computed: true,
},
"disk_iops_read_write": {
Type: schema.TypeInt,
Computed: true,
},
"disk_mbps_read_write": {
Type: schema.TypeInt,
Computed: true,
},
"disk_size_gb": {
Type: schema.TypeInt,
Computed: true,
},
"os_type": {
Type: schema.TypeString,
Computed: true,
},
"source_resource_id": {
Type: schema.TypeString,
Computed: true,
},
"source_uri": {
Type: schema.TypeString,
Computed: true,
},
"storage_account_id": {
Type: schema.TypeString,
Computed: true,
},
"storage_account_type": {
Type: schema.TypeString,
Computed: true,
},
"tags": tags.Schema(),
"zones": azure.SchemaZonesComputed(),
},
}
}
func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DisksClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
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)
d.Set("name", name)
d.Set("resource_group_name", resGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if sku := resp.Sku; sku != nil {
d.Set("storage_account_type", string(sku.Name))
}
if props := resp.DiskProperties; props != nil {
if creationData := props.CreationData; creationData != nil {
d.Set("create_option", string(creationData.CreateOption))
imageReferenceID := ""
if creationData.ImageReference != nil && creationData.ImageReference.ID != nil {
imageReferenceID = *creationData.ImageReference.ID
}
d.Set("image_reference_id", imageReferenceID)
d.Set("source_resource_id", creationData.SourceResourceID)
d.Set("source_uri", creationData.SourceURI)
d.Set("storage_account_id", creationData.StorageAccountID)
}
d.Set("disk_size_gb", props.DiskSizeGB)
d.Set("disk_iops_read_write", props.DiskIOPSReadWrite)
d.Set("disk_mbps_read_write", props.DiskMBpsReadWrite)
d.Set("os_type", props.OsType)
diskEncryptionSetId := ""
if props.Encryption != nil && props.Encryption.DiskEncryptionSetID != nil {
diskEncryptionSetId = *props.Encryption.DiskEncryptionSetID
}
d.Set("disk_encryption_set_id", diskEncryptionSetId)
}
d.Set("zones", utils.FlattenStringSlice(resp.Zones))
return tags.FlattenAndSet(d, resp.Tags)
}