-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathresource_elasticsearch_opendistro_kibana_tenant.go
235 lines (199 loc) · 6.2 KB
/
resource_elasticsearch_opendistro_kibana_tenant.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package es
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/olivere/elastic/uritemplates"
elastic7 "github.com/olivere/elastic/v7"
)
func resourceElasticsearchOpenDistroKibanaTenant() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchOpenDistroKibanaTenantCreate,
Read: resourceElasticsearchOpenDistroKibanaTenantRead,
Update: resourceElasticsearchOpenDistroKibanaTenantUpdate,
Delete: resourceElasticsearchOpenDistroKibanaTenantDelete,
Schema: map[string]*schema.Schema{
"tenant_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"index": {
Type: schema.TypeString,
Computed: true,
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func resourceElasticsearchOpenDistroKibanaTenantCreate(d *schema.ResourceData, m interface{}) error {
if _, err := resourceElasticsearchPutOpenDistroKibanaTenant(d, m); err != nil {
log.Printf("[INFO] Failed to create OpenDistroKibanaTenant: %+v", err)
return err
}
name := d.Get("tenant_name").(string)
d.SetId(name)
return resourceElasticsearchOpenDistroKibanaTenantRead(d, m)
}
func resourceElasticsearchOpenDistroKibanaTenantRead(d *schema.ResourceData, m interface{}) error {
res, err := resourceElasticsearchGetOpenDistroKibanaTenant(d.Id(), m)
if err != nil {
if elastic7.IsNotFound(err) {
log.Printf("[WARN] OpenDistroKibanaTenant (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
if err := d.Set("tenant_name", d.Id()); err != nil {
return fmt.Errorf("error setting tenant_name: %s", err)
}
if err := d.Set("description", res.Description); err != nil {
return fmt.Errorf("error setting description: %s", err)
}
index, err := resourceElasticsearchOpenDistroKibanaComputeIndex(d.Id())
if err != nil {
return err
}
if err := d.Set("index", index); err != nil {
return fmt.Errorf("error setting index: %s", err)
}
return nil
}
func resourceElasticsearchOpenDistroKibanaComputeIndex(tenant string) (string, error) {
// Calc Hash
hashSum := int32(0)
for _, char := range tenant {
shift := (hashSum << 5)
hashSum = (shift - hashSum) + int32(char-0)
}
// remove all chars that are not alphanumeric
alphanumeric, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
return "", err
}
cleanedTenant := alphanumeric.ReplaceAllString(tenant, "")
// originalKibanaIndex+"_"+tenant.hashCode()+"_"+tenant.toLowerCase().replaceAll("[^a-z0-9]+", "")
return fmt.Sprintf(".kibana_%v_%v", hashSum, strings.ToLower(cleanedTenant)), nil
}
func resourceElasticsearchOpenDistroKibanaTenantUpdate(d *schema.ResourceData, m interface{}) error {
if _, err := resourceElasticsearchPutOpenDistroKibanaTenant(d, m); err != nil {
return err
}
return resourceElasticsearchOpenDistroKibanaTenantRead(d, m)
}
func resourceElasticsearchOpenDistroKibanaTenantDelete(d *schema.ResourceData, m interface{}) error {
path, err := uritemplates.Expand("/_opendistro/_security/api/tenants/{name}", map[string]string{
"name": d.Get("tenant_name").(string),
})
if err != nil {
return fmt.Errorf("error building URL path for tenant: %+v", err)
}
esClient, err := getClient(m.(*ProviderConf))
if err != nil {
return err
}
switch client := esClient.(type) {
case *elastic7.Client:
_, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "DELETE",
Path: path,
})
default:
err = errors.New("Creating tenants requires elastic v7 client")
}
return err
}
func resourceElasticsearchGetOpenDistroKibanaTenant(tenantID string, m interface{}) (TenantBody, error) {
var err error
tenant := new(TenantBody)
path, err := uritemplates.Expand("/_opendistro/_security/api/tenants/{name}", map[string]string{
"name": tenantID,
})
if err != nil {
return *tenant, fmt.Errorf("error building URL path for tenant: %+v", err)
}
var body json.RawMessage
esClient, err := getClient(m.(*ProviderConf))
if err != nil {
return *tenant, err
}
switch client := esClient.(type) {
case *elastic7.Client:
var res *elastic7.Response
res, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
body = res.Body
default:
err = errors.New("Creating tenants requires elastic v7 client")
}
if err != nil {
return *tenant, err
}
var tenantDefinition map[string]TenantBody
if err := json.Unmarshal(body, &tenantDefinition); err != nil {
return *tenant, fmt.Errorf("error unmarshalling tenant body: %+v: %+v", err, body)
}
*tenant = tenantDefinition[tenantID]
return *tenant, err
}
func resourceElasticsearchPutOpenDistroKibanaTenant(d *schema.ResourceData, m interface{}) (*TenantResponse, error) {
response := new(TenantResponse)
tenantsDefinition := TenantBody{
Description: d.Get("description").(string),
}
tenantJSON, err := json.Marshal(tenantsDefinition)
if err != nil {
return response, fmt.Errorf("Body Error : %s", tenantJSON)
}
path, err := uritemplates.Expand("/_opendistro/_security/api/tenants/{name}", map[string]string{
"name": d.Get("tenant_name").(string),
})
if err != nil {
return response, fmt.Errorf("error building URL path for tenant: %+v", err)
}
var body json.RawMessage
esClient, err := getClient(m.(*ProviderConf))
if err != nil {
return nil, err
}
switch client := esClient.(type) {
case *elastic7.Client:
var res *elastic7.Response
res, err = client.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "PUT",
Path: path,
Body: string(tenantJSON),
})
body = res.Body
default:
err = errors.New("Creating tenants requires elastic v7 client")
}
if err != nil {
return response, fmt.Errorf("error creating tenant: %+v: %+v", err, body)
}
if err := json.Unmarshal(body, response); err != nil {
return response, fmt.Errorf("error unmarshalling tenant body: %+v: %+v", err, body)
}
return response, nil
}
type TenantResponse struct {
Message string `json:"message"`
Status string `json:"status"`
}
type TenantBody struct {
Description string `json:"description"`
}