/
data_dbaas_logs_cluster.go
173 lines (156 loc) · 4.5 KB
/
data_dbaas_logs_cluster.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ovh
import (
"fmt"
"log"
"net/url"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func dataSourceDbaasLogsCluster() *schema.Resource {
return &schema.Resource{
Read: func(d *schema.ResourceData, meta interface{}) error {
return dataSourceDbaasLogsClusterRead(d, meta)
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Description: "The service name",
Required: true,
},
"cluster_id": {
Type: schema.TypeString,
Optional: true,
},
// Computed
"urn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_type": {
Type: schema.TypeString,
Description: "Cluster type",
Computed: true,
},
"dedicated_input_pem": {
Type: schema.TypeString,
Description: "PEM for dedicated inputs",
Computed: true,
Sensitive: true,
},
"archive_allowed_networks": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Allowed networks for ARCHIVE flow type",
Computed: true,
},
"direct_input_allowed_networks": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Allowed networks for DIRECT_INPUT flow type",
Computed: true,
},
"direct_input_pem": {
Type: schema.TypeString,
Description: "PEM for direct inputs",
Computed: true,
Sensitive: true,
},
"hostname": {
Type: schema.TypeString,
Description: "hostname",
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Description: "All content generated by given service will be placed on this cluster",
Computed: true,
},
"is_unlocked": {
Type: schema.TypeBool,
Description: "Allow given service to perform advanced operations on cluster",
Computed: true,
},
"query_allowed_networks": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Allowed networks for QUERY flow type",
Computed: true,
},
"region": {
Type: schema.TypeString,
Description: "Data center localization",
Computed: true,
},
},
}
}
func dbaasGetClusterID(config *Config, serviceName string) (string, error) {
res := []string{}
endpoint := fmt.Sprintf(
"/dbaas/logs/%s/cluster",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return "", fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
if len(res) == 1 {
return res[0], nil
}
/* Look for default clusterId */
for _, clusterId := range res {
res := make(map[string]interface{})
endpoint := fmt.Sprintf(
"/dbaas/logs/%s/cluster/%s",
url.PathEscape(serviceName), clusterId,
)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return "", fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
if res["isDefault"] == true {
return clusterId, nil
}
}
return "", fmt.Errorf("Can't find default clusterId")
}
func dataSourceDbaasLogsClusterRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
if clusterId == "" {
var err error
clusterId, err = dbaasGetClusterID(config, serviceName)
if err != nil {
return fmt.Errorf("Error retrieving clusterId for %s:\n\t %q", serviceName, err)
}
}
log.Printf("[DEBUG] Will read dbaas logs cluster %s/%s", serviceName, clusterId)
id := fmt.Sprintf("%s_%s", serviceName, clusterId)
d.SetId(id)
d.Set("urn", helpers.ServiceURN(config.Plate, "ldp", serviceName))
endpoint := fmt.Sprintf(
"/dbaas/logs/%s/cluster/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)
res := map[string]interface{}{}
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return fmt.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
d.Set("archive_allowed_networks", res["archiveAllowedNetworks"])
d.Set("cluster_type", res["clusterType"])
d.Set("dedicated_input_pem", res["dedicatedInputPEM"])
d.Set("direct_input_allowed_networks", res["directInputAllowedNetworks"])
d.Set("direct_input_pem", res["directInputPEM"])
d.Set("hostname", res["hostname"])
d.Set("is_default", res["isDefault"])
d.Set("is_unlocked", res["isUnlocked"])
d.Set("query_allowed_networks", res["queryAllowedNetworks"])
d.Set("region", res["region"])
return nil
}