-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_cloud_project_region.go
114 lines (97 loc) · 2.71 KB
/
data_cloud_project_region.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
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/go-ovh/ovh"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func dataSourceCloudProjectRegion() *schema.Resource {
return &schema.Resource{
Read: dataSourceCloudProjectRegionRead,
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.",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"services": {
Type: schema.TypeSet,
Set: cloudServiceHash,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"continent_code": {
Type: schema.TypeString,
Computed: true,
},
"datacenter_location": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceCloudProjectRegionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
name := d.Get("name").(string)
log.Printf("[DEBUG] Will read public cloud region %s for project: %s", name, serviceName)
region, err := getCloudProjectRegion(serviceName, name, config.OVHClient)
if err != nil {
return err
}
d.Set("datacenter_location", region.DatacenterLocation)
d.Set("continent_code", region.ContinentCode)
services := &schema.Set{
F: cloudServiceHash,
}
for i := range region.Services {
service := map[string]interface{}{
"name": region.Services[i].Name,
"status": region.Services[i].Status,
}
services.Add(service)
}
d.Set("services", services)
d.Set("service_name", serviceName)
d.SetId(fmt.Sprintf("%s_%s", serviceName, name))
return nil
}
func getCloudProjectRegion(serviceName, region string, client *ovh.Client) (*CloudProjectRegionResponse, error) {
log.Printf("[DEBUG] Will read public cloud region %s for project: %s", region, serviceName)
response := &CloudProjectRegionResponse{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/region/%s",
url.PathEscape(serviceName),
url.PathEscape(region),
)
err := client.Get(endpoint, response)
if err != nil {
return nil, fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
}
return response, nil
}
func cloudServiceHash(v interface{}) int {
r := v.(map[string]interface{})
return hashcode.String(r["name"].(string))
}