forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_scaleway_bootscript.go
119 lines (105 loc) · 2.76 KB
/
data_source_scaleway_bootscript.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
package scaleway
import (
"fmt"
"regexp"
"github.com/hashicorp/terraform/helper/schema"
"github.com/scaleway/scaleway-cli/pkg/api"
)
func dataSourceScalewayBootscript() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayBootscriptRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name_filter": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"architecture": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
},
// Computed values.
"organization": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"public": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"boot_cmd_args": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"dtb": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"initrd": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"kernel": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func bootscriptDescriptionAttributes(d *schema.ResourceData, script api.ScalewayBootscript) error {
d.Set("architecture", script.Arch)
d.Set("organization", script.Organization)
d.Set("public", script.Public)
d.Set("boot_cmd_args", script.Bootcmdargs)
d.Set("dtb", script.Dtb)
d.Set("initrd", script.Initrd)
d.Set("kernel", script.Kernel)
d.SetId(script.Identifier)
return nil
}
func dataSourceScalewayBootscriptRead(d *schema.ResourceData, meta interface{}) error {
scaleway := meta.(*Client).scaleway
scripts, err := scaleway.GetBootscripts()
if err != nil {
return err
}
var isMatch func(api.ScalewayBootscript) bool
if name, ok := d.GetOk("name"); ok {
isMatch = func(s api.ScalewayBootscript) bool {
return s.Title == name.(string)
}
} else if nameFilter, ok := d.GetOk("name_filter"); ok {
architecture := d.Get("architecture")
exp, err := regexp.Compile(nameFilter.(string))
if err != nil {
return err
}
isMatch = func(s api.ScalewayBootscript) bool {
nameMatch := exp.MatchString(s.Title)
architectureMatch := true
if architecture != "" {
architectureMatch = architecture == s.Arch
}
return nameMatch && architectureMatch
}
}
var matches []api.ScalewayBootscript
for _, script := range *scripts {
if isMatch(script) {
matches = append(matches, script)
}
}
if len(matches) > 1 {
return fmt.Errorf("The query returned more than one result. Please refine your query.")
}
if len(matches) == 0 {
return fmt.Errorf("The query returned no result. Please refine your query.")
}
return bootscriptDescriptionAttributes(d, matches[0])
}