-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
data_source_platform_image.go
76 lines (60 loc) · 1.86 KB
/
data_source_platform_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
package azurerm
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/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmPlatformImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPlatformImageRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"location": azure.SchemaLocation(),
"publisher": {
Type: schema.TypeString,
Required: true,
},
"offer": {
Type: schema.TypeString,
Required: true,
},
"sku": {
Type: schema.TypeString,
Required: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceArmPlatformImageRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Compute.VMImageClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
location := azure.NormalizeLocation(d.Get("location").(string))
publisher := d.Get("publisher").(string)
offer := d.Get("offer").(string)
sku := d.Get("sku").(string)
result, err := client.List(ctx, location, publisher, offer, sku, "", utils.Int32(int32(1000)), "name")
if err != nil {
return fmt.Errorf("Error reading Platform Images: %+v", err)
}
// the last value is the latest, apparently.
latestVersion := (*result.Value)[len(*result.Value)-1]
d.SetId(*latestVersion.ID)
if location := latestVersion.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("publisher", publisher)
d.Set("offer", offer)
d.Set("sku", sku)
d.Set("version", latestVersion.Name)
return nil
}