/
data_source_image.go
102 lines (85 loc) · 2.32 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
package profitbricks
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/profitbricks/profitbricks-sdk-go"
"strings"
)
func dataSourceImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceImageRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
},
"location": {
Type: schema.TypeString,
Optional: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceImageRead(d *schema.ResourceData, meta interface{}) error {
profitbricks.SetDepth("5")
images := profitbricks.ListImages()
if images.StatusCode > 299 {
return fmt.Errorf("An error occured while fetching ProfitBricks locations %s", images.Response)
}
name := d.Get("name").(string)
imageType, imageTypeOk := d.GetOk("type")
location, locationOk := d.GetOk("location")
version, versionOk := d.GetOk("version")
results := []profitbricks.Image{}
// if version value is present then concatenate name - version
// otherwise search by name or part of the name
if versionOk {
name_ver := fmt.Sprintf("%s-%s", name, version.(string))
for _, img := range images.Items {
if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name_ver)) {
results = append(results, img)
}
}
} else {
for _, img := range images.Items {
if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name)) {
results = append(results, img)
}
}
}
if imageTypeOk {
imageTypeResults := []profitbricks.Image{}
for _, img := range results {
if img.Properties.ImageType == imageType.(string) {
imageTypeResults = append(imageTypeResults, img)
}
}
results = imageTypeResults
}
if locationOk {
locationResults := []profitbricks.Image{}
for _, img := range results {
if img.Properties.Location == location.(string) {
locationResults = append(locationResults, img)
}
}
results = locationResults
}
if len(results) > 1 {
return fmt.Errorf("There is more than one image that match the search criteria")
}
if len(results) == 0 {
return fmt.Errorf("There are no images that match the search criteria")
}
d.Set("name", results[0].Properties.Name)
d.SetId(results[0].Id)
return nil
}