This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_source_image.go
144 lines (129 loc) · 3.66 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
package scaleway
import (
"fmt"
"log"
"regexp"
"sort"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
api "github.com/nicolai86/scaleway-sdk"
)
func dataSourceScalewayImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayImageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "exact name of the desired image",
},
"name_filter": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "partial name of the desired image to filter with",
ConflictsWith: []string{"most_recent"},
},
"architecture": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "architecture of the desired image",
},
"most_recent": {
Type: schema.TypeBool,
Optional: true,
Description: "select most recent image if multiple match",
ConflictsWith: []string{"name_filter"},
},
// Computed values.
"organization": {
Type: schema.TypeString,
Computed: true,
Description: "organization owning the bootscript",
},
"public": {
Type: schema.TypeBool,
Computed: true,
Description: "indication if the bootscript is public",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "date when the image was created",
},
},
}
}
func scalewayImageAttributes(d *schema.ResourceData, img *api.Image) error {
_ = d.Set("architecture", img.Arch)
_ = d.Set("organization", img.Organization)
_ = d.Set("public", img.Public)
_ = d.Set("creation_date", img.CreationDate)
_ = d.Set("name", img.Name)
d.SetId(img.Identifier)
return nil
}
type localImage struct {
api.MarketLocalImageDefinition
ModificationDate *time.Time
}
func dataSourceScalewayImageRead(d *schema.ResourceData, meta interface{}) error {
scaleway := meta.(*Meta).deprecatedClient
var nameMatch func(api.MarketImage) bool
if name, ok := d.GetOk("name"); ok {
nameMatch = func(img api.MarketImage) bool {
return img.Name == name.(string)
}
} else if nameFilter, ok := d.GetOk("name_filter"); ok {
exp, err := regexp.Compile(nameFilter.(string))
if err != nil {
return fmt.Errorf("invalid name_filter regular expression provided: %v", err)
}
nameMatch = func(img api.MarketImage) bool {
return exp.MatchString(img.Name)
}
}
imgs, err := scaleway.GetImages()
if err != nil {
return err
}
images := []localImage{}
for _, image := range *imgs {
if !nameMatch(image) {
continue
}
for _, v := range image.Versions {
for _, l := range v.LocalImages {
if l.Arch == d.Get("architecture").(string) && l.Zone == scaleway.Region {
t, err := time.Parse(time.RFC3339, v.ModificationDate)
if err != nil {
log.Printf("[WARNING] could not parse modification date: %v", err.Error())
}
images = append(images, localImage{
MarketLocalImageDefinition: l,
ModificationDate: &t,
})
}
}
}
}
mostRecent := d.Get("most_recent").(bool)
sort.Slice(images, func(i, j int) bool {
return images[i].ModificationDate.After(*images[j].ModificationDate)
})
log.Printf("[DEBUG] got %d images: %#v\n", len(images), images)
if len(images) > 1 && !mostRecent {
return fmt.Errorf("The query returned more than one result. Please refine your query.")
}
if len(images) == 0 {
return fmt.Errorf("The query returned no result. Please refine your query.")
}
img, err := scaleway.GetImage(images[0].ID)
if err != nil {
return err
}
return scalewayImageAttributes(d, img)
}