-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
data_source_image.go
213 lines (183 loc) · 5.3 KB
/
data_source_image.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package azurerm
import (
"fmt"
"log"
"regexp"
"sort"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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 dataSourceArmImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmImageRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name_regex": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.ValidateRegexp,
ConflictsWith: []string{"name"},
},
"sort_descending": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"name": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"name_regex"},
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"location": azure.SchemaLocationForDataSource(),
"zone_resilient": {
Type: schema.TypeBool,
Computed: true,
},
"os_disk": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"blob_uri": {
Type: schema.TypeString,
Computed: true,
},
"caching": {
Type: schema.TypeString,
Computed: true,
},
"managed_disk_id": {
Type: schema.TypeString,
Computed: true,
},
"os_state": {
Type: schema.TypeString,
Computed: true,
},
"os_type": {
Type: schema.TypeString,
Computed: true,
},
"size_gb": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"data_disk": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"blob_uri": {
Type: schema.TypeString,
Computed: true,
},
"caching": {
Type: schema.TypeString,
Computed: true,
},
"lun": {
Type: schema.TypeInt,
Computed: true,
},
"managed_disk_id": {
Type: schema.TypeString,
Computed: true,
},
"size_gb": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"tags": tags.SchemaDataSource(),
},
}
}
func dataSourceArmImageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Compute.ImagesClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
nameRegex, nameRegexOk := d.GetOk("name_regex")
if name == "" && !nameRegexOk {
return fmt.Errorf("[ERROR] either name or name_regex is required")
}
var img compute.Image
if !nameRegexOk {
var err error
if img, err = client.Get(ctx, resGroup, name, ""); err != nil {
if utils.ResponseWasNotFound(img.Response) {
return fmt.Errorf("Error: Image %q (Resource Group %q) was not found", name, resGroup)
}
return fmt.Errorf("[ERROR] Error making Read request on Azure Image %q (resource group %q): %+v", name, resGroup, err)
}
} else {
r := regexp.MustCompile(nameRegex.(string))
list := make([]compute.Image, 0)
resp, err := client.ListByResourceGroupComplete(ctx, resGroup)
if err != nil {
if utils.ResponseWasNotFound(resp.Response().Response) {
return fmt.Errorf("Error: Unable to list images for Resource Group %q", resGroup)
}
return fmt.Errorf("[ERROR] Error getting list of images (resource group %q): %+v", resGroup, err)
}
for resp.NotDone() {
img = resp.Value()
if r.Match(([]byte)(*img.Name)) {
list = append(list, img)
}
err = resp.Next()
if err != nil {
return err
}
}
if len(list) < 1 {
d.SetId("")
return nil
}
if len(list) > 1 {
desc := d.Get("sort_descending").(bool)
log.Printf("[DEBUG] arm_image - multiple results found and `sort_descending` is set to: %t", desc)
sort.Slice(list, func(i, j int) bool {
return (!desc && *list[i].Name < *list[j].Name) ||
(desc && *list[i].Name > *list[j].Name)
})
}
img = list[0]
}
d.SetId(*img.ID)
d.Set("name", img.Name)
d.Set("resource_group_name", resGroup)
if location := img.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if profile := img.StorageProfile; profile != nil {
if disk := profile.OsDisk; disk != nil {
if err := d.Set("os_disk", flattenAzureRmImageOSDisk(disk)); err != nil {
return fmt.Errorf("[DEBUG] Error setting AzureRM Image OS Disk error: %+v", err)
}
}
if disks := profile.DataDisks; disks != nil {
if err := d.Set("data_disk", flattenAzureRmImageDataDisks(disks)); err != nil {
return fmt.Errorf("[DEBUG] Error setting AzureRM Image Data Disks error: %+v", err)
}
}
d.Set("zone_resilient", profile.ZoneResilient)
}
return tags.FlattenAndSet(d, img.Tags)
}