/
data_source_edgecenter_image.go
193 lines (175 loc) · 5.46 KB
/
data_source_edgecenter_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
package edgecenter
import (
"context"
"encoding/json"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
)
func dataSourceImage() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceImageRead,
Description: "A cloud image is a pre-configured virtual machine template that you can use to create new instances.",
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Description: "The name of the image. Use 'os-version', for example 'ubuntu-20.04'.",
Required: true,
},
"is_baremetal": {
Type: schema.TypeBool,
Description: "Set to true if need to get the baremetal image.",
Optional: true,
},
"min_disk": {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum disk space (in GB) required to launch an instance using this image.",
},
"min_ram": {
Type: schema.TypeInt,
Computed: true,
Description: "Minimum VM RAM (in MB) required to launch an instance using this image.",
},
"os_distro": {
Type: schema.TypeString,
Computed: true,
Description: "The distribution of the OS present in the image, e.g. Debian, CentOS, Ubuntu etc.",
},
"os_version": {
Type: schema.TypeString,
Computed: true,
Description: "The version of the OS present in the image. e.g. 19.04 (for Ubuntu) or 9.4 for Debian.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "A detailed description of the image.",
},
"metadata_k": {
Type: schema.TypeString,
Optional: true,
Description: "Filtration query opts (only key).",
},
"metadata_kv": {
Type: schema.TypeMap,
Optional: true,
Description: `Filtration query opts, for example, {offset = "10", limit = "10"}.`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"metadata_read_only": {
Type: schema.TypeList,
Computed: true,
Description: `A list of read-only metadata items, e.g. tags.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}
func dataSourceImageRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start Image reading")
name := d.Get("name").(string)
config := m.(*Config)
clientV2 := config.CloudClient
var err error
clientV2.Region, clientV2.Project, err = GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
listOpts := &edgecloudV2.ImageListOptions{}
if metadataK, ok := d.GetOk("metadata_k"); ok {
listOpts.MetadataK = metadataK.(string)
}
if metadataRaw, ok := d.GetOk("metadata_kv"); ok {
typedMetadataKV := make(map[string]string, len(metadataRaw.(map[string]interface{})))
for k, v := range metadataRaw.(map[string]interface{}) {
typedMetadataKV[k] = v.(string)
}
typedMetadataKVJson, err := json.Marshal(typedMetadataKV)
if err != nil {
return diag.FromErr(err)
}
listOpts.MetadataKV = string(typedMetadataKVJson)
}
var allImages []edgecloudV2.Image
if isBm, _ := d.Get("is_baremetal").(bool); isBm {
allImages, _, err = clientV2.Images.ImagesBaremetalList(ctx, listOpts)
if err != nil {
return diag.FromErr(err)
}
} else {
allImages, _, err = clientV2.Images.List(ctx, nil)
if err != nil {
return diag.FromErr(err)
}
}
var found bool
var image edgecloudV2.Image
for _, img := range allImages {
if strings.HasPrefix(strings.ToLower(img.Name), strings.ToLower(name)) {
image = img
found = true
break
}
}
if !found {
return diag.Errorf("image with name %s not found", name)
}
d.SetId(image.ID)
d.Set("project_id", clientV2.Project)
d.Set("region_id", clientV2.Region)
d.Set("min_disk", image.MinDisk)
d.Set("min_ram", image.MinRAM)
d.Set("os_distro", image.OSDistro)
d.Set("os_version", image.OSVersion)
d.Set("description", image.Description)
metadataReadOnly := PrepareMetadataReadonly(image.MetadataDetailed)
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
}
log.Println("[DEBUG] Finish Image reading")
return nil
}