forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cloud_project_containerregistry_ip_restrictions_registry.go
68 lines (58 loc) · 1.94 KB
/
data_cloud_project_containerregistry_ip_restrictions_registry.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
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCloudProjectContainerRegistryIPRestrictionsRegistry() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectContainerRegistryIPRestrictionsRegistryRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "Service name",
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"registry_id": {
Type: schema.TypeString,
Description: "Registry ID",
Required: true,
},
"ip_restrictions": {
Type: schema.TypeList,
Description: "List your IP restrictions applied on artifact manager component",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeMap,
Set: schema.HashString,
},
},
},
}
}
func dataSourceCloudProjectContainerRegistryIPRestrictionsRegistryRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
registryID := d.Get("registry_id").(string)
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s/ipRestrictions/registry",
url.PathEscape(serviceName),
url.PathEscape(registryID),
)
ipRestrictions := []CloudProjectContainerRegistryIPRestriction{}
log.Printf("[DEBUG] Will read Registry IP Restrictions from registry %s and project: %s", registryID, serviceName)
err := config.OVHClient.Get(endpoint, &ipRestrictions)
if err != nil {
return fmt.Errorf("calling get %s %w", endpoint, err)
}
mapIPRestrictions := make([]map[string]interface{}, len(ipRestrictions))
for i, ipRestriction := range ipRestrictions {
mapIPRestrictions[i] = ipRestriction.ToMap()
}
d.Set("ip_restrictions", mapIPRestrictions)
d.SetId(serviceName + "/" + registryID)
log.Printf("[DEBUG] Read Registry IP Restrictions %+v", mapIPRestrictions)
return nil
}