/
data_source_platform_image.go
67 lines (53 loc) · 1.53 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
package azurerm
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmPlatformImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPlatformImageRead,
Schema: map[string]*schema.Schema{
"location": locationSchema(),
"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).vmImageClient
ctx := meta.(*ArmClient).StopContext
location := azureRMNormalizeLocation(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", azureRMNormalizeLocation(*location))
}
d.Set("publisher", publisher)
d.Set("offer", offer)
d.Set("sku", sku)
d.Set("version", latestVersion.Name)
return nil
}