-
Notifications
You must be signed in to change notification settings - Fork 132
/
data_cloud_project_regions.go
84 lines (70 loc) · 1.95 KB
/
data_cloud_project_regions.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
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCloudProjectRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectRegionsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the id of the cloud project.",
},
"has_services_up": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func dataSourceCloudProjectRegionsRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
log.Printf("[DEBUG] Will read public cloud regions for project: %s", serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/region",
url.PathEscape(serviceName),
)
names := make([]string, 0)
if err := config.OVHClient.Get(endpoint, &names); err != nil {
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
}
d.SetId(serviceName)
d.Set("service_name", serviceName)
var services []interface{}
if servicesVal, ok := d.GetOk("has_services_up"); ok {
services = servicesVal.([]interface{})
}
// no filtering on services
if len(services) < 1 {
d.Set("names", names)
return nil
}
filtered_names := make([]string, 0)
for _, n := range names {
region, err := getCloudProjectRegion(serviceName, n, config.OVHClient)
if err != nil {
return err
}
for _, service := range services {
if region.HasServiceUp(service.(string)) {
filtered_names = append(filtered_names, n)
}
}
}
d.Set("names", filtered_names)
return nil
}