This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from scaleway/terraform-provider-scaleway
/
data_source_registry_image_beta.go
109 lines (101 loc) · 2.82 KB
/
data_source_registry_image_beta.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
package scaleway
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/registry/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func dataSourceScalewayRegistryImageBeta() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayRegistryImageBetaRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the registry image",
ConflictsWith: []string{"image_id"},
},
"image_id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the registry image",
ConflictsWith: []string{"name"},
ValidateFunc: validationUUIDorUUIDWithLocality(),
},
"namespace_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The namespace ID of the registry image",
ValidateFunc: validationUUIDorUUIDWithLocality(),
},
"size": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the registry image",
},
"visibility": {
Type: schema.TypeString,
Computed: true,
Description: "The visibility policy of the registry image",
},
"tags": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The tags associated with the registry image",
},
"region": regionSchema(),
"organization_id": organizationIDSchema(),
},
}
}
func dataSourceScalewayRegistryImageBetaRead(d *schema.ResourceData, m interface{}) error {
api, region, err := registryAPIWithRegion(d, m)
if err != nil {
return err
}
var image *registry.Image
imageID, ok := d.GetOk("image_id")
if !ok {
var namespaceID *string
if d.Get("namespace_id") != "" {
namespaceID = scw.StringPtr(expandID(d.Get("namespace_id")))
}
res, err := api.ListImages(®istry.ListImagesRequest{
Region: region,
Name: String(d.Get("name").(string)),
NamespaceID: namespaceID,
})
if err != nil {
return err
}
if len(res.Images) == 0 {
return fmt.Errorf("no images found with the name %s", d.Get("name"))
}
if len(res.Images) > 1 {
return fmt.Errorf("%d images found with the same name %s", len(res.Images), d.Get("name"))
}
image = res.Images[0]
} else {
res, err := api.GetImage(®istry.GetImageRequest{
Region: region,
ImageID: expandID(imageID),
})
if err != nil {
return err
}
image = res
}
d.SetId(datasourceNewRegionalizedID(image.ID, region))
_ = d.Set("image_id", image.ID)
_ = d.Set("name", image.Name)
_ = d.Set("namespace_id", image.NamespaceID)
_ = d.Set("visibility", image.Visibility.String())
_ = d.Set("size", image.Size)
_ = d.Set("tags", image.Tags)
return nil
}