forked from digitalocean/terraform-provider-digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_digitalocean_image.go
123 lines (101 loc) · 3.49 KB
/
datasource_digitalocean_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
package digitalocean
import (
"context"
"strconv"
"strings"
"github.com/digitalocean/godo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func dataSourceDigitalOceanImage() *schema.Resource {
recordSchema := imageSchema()
for _, f := range recordSchema {
f.Computed = true
}
recordSchema["id"].Optional = true
recordSchema["id"].ValidateFunc = validation.NoZeroValues
recordSchema["id"].ExactlyOneOf = []string{"id", "slug", "name"}
recordSchema["name"].Optional = true
recordSchema["name"].ValidateFunc = validation.StringIsNotEmpty
recordSchema["name"].ExactlyOneOf = []string{"id", "slug", "name"}
recordSchema["slug"].Optional = true
recordSchema["slug"].ValidateFunc = validation.StringIsNotEmpty
recordSchema["slug"].ExactlyOneOf = []string{"id", "slug", "name"}
recordSchema["source"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "user",
ValidateFunc: validation.StringInSlice([]string{"all", "applications", "distributions", "user"}, true),
ConflictsWith: []string{"id", "slug"},
}
return &schema.Resource{
ReadContext: dataSourceDigitalOceanImageRead,
Schema: recordSchema,
}
}
func dataSourceDigitalOceanImageRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*CombinedConfig).godoClient()
var foundImage *godo.Image
if id, ok := d.GetOk("id"); ok {
image, resp, err := client.Images.GetByID(context.Background(), id.(int))
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return diag.Errorf("image ID %d not found: %s", id.(int), err)
}
return diag.Errorf("Error retrieving image: %s", err)
}
foundImage = image
} else if slug, ok := d.GetOk("slug"); ok {
image, resp, err := client.Images.GetBySlug(context.Background(), slug.(string))
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return diag.Errorf("image not found: %s", err)
}
return diag.Errorf("Error retrieving image: %s", err)
}
foundImage = image
} else if name, ok := d.GetOk("name"); ok {
source := strings.ToLower(d.Get("source").(string))
var listImages imageListFunc
if source == "all" {
listImages = client.Images.List
} else if source == "distributions" {
listImages = client.Images.ListDistribution
} else if source == "applications" {
listImages = client.Images.ListApplication
} else if source == "user" {
listImages = client.Images.ListUser
} else {
return diag.Errorf("Illegal state: source=%s", source)
}
images, err := listDigitalOceanImages(listImages)
if err != nil {
return diag.FromErr(err)
}
var results []interface{}
for _, image := range images {
if image.(godo.Image).Name == name {
results = append(results, image)
}
}
if len(results) == 0 {
return diag.Errorf("no image found with name %s", name)
} else if len(results) > 1 {
return diag.Errorf("too many images found with name %s (found %d, expected 1)", name, len(results))
}
result := results[0].(godo.Image)
foundImage = &result
} else {
return diag.Errorf("Illegal state: one of id, name, or slug must be set")
}
flattenedImage, err := flattenDigitalOceanImage(*foundImage, meta, nil)
if err != nil {
return diag.FromErr(err)
}
if err := setResourceDataFromMap(d, flattenedImage); err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(foundImage.ID))
return nil
}