forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cloud_project_containerregistries.go
116 lines (104 loc) · 2.87 KB
/
data_cloud_project_containerregistries.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
package ovh
import (
"fmt"
"log"
"net/url"
"sort"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func dataSourceCloudProjectContainerRegistries() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectContainerRegistriesRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"result": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"created_at": {
Type: schema.TypeString,
Description: "Registry creation date",
Computed: true,
},
"id": {
Type: schema.TypeString,
Description: "Registry ID",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Registry name",
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Description: "Project ID of your registry",
Computed: true,
},
"region": {
Type: schema.TypeString,
Description: "Region of the registry.",
Computed: true,
},
"size": {
Type: schema.TypeInt,
Description: "Current size of the registry (bytes)",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Registry status",
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Description: "Registry last update date",
Computed: true,
},
"url": {
Type: schema.TypeString,
Description: "Access url of the registry",
Computed: true,
},
"version": {
Type: schema.TypeString,
Description: "Version of your registry",
Computed: true,
},
},
},
},
},
}
}
func dataSourceCloudProjectContainerRegistriesRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
log.Printf("[DEBUG] Will read cloud project registries for project: %s", serviceName)
regs := []CloudProjectContainerRegistry{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry",
url.PathEscape(serviceName),
)
err := config.OVHClient.Get(endpoint, ®s)
if err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
mapregs := make([]map[string]interface{}, len(regs))
ids := make([]string, len(regs))
for i, reg := range regs {
mapregs[i] = reg.ToMap()
ids = append(ids, reg.Id)
}
// sort.Strings sorts in place, returns nothing
sort.Strings(ids)
d.SetId(hashcode.Strings(ids))
d.Set("result", mapregs)
return nil
}