-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_elasticsearch_index_data.go
92 lines (79 loc) · 2.4 KB
/
resource_elasticsearch_index_data.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
_ "github.com/hashicorp/terraform/terraform"
"log"
"fmt"
"io/ioutil"
_ "errors"
)
func resourceElasticsearchIndexData() *schema.Resource {
return &schema.Resource{
Create: resourceElasticsearchIndexDataCreate,
Read: resourceElasticsearchIndexDataRead,
Update: resourceElasticsearchIndexDataUpdate,
Delete: resourceElasticsearchIndexDataDelete,
Schema: map[string]*schema.Schema{
"index_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"file_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"file_url": &schema.Schema{
Type: schema.TypeString,
ConflictsWith: []string{"file_path"},
Optional: true,
},
"content_type": &schema.Schema{
Type: schema.TypeString,
Default: "application/x-ndjson",
Optional: true,
},
},
}
}
func resourceElasticsearchIndexDataCreate(d *schema.ResourceData, meta interface{}) error {
url := meta.(*ElasticInfo).elasticsearchUrl
username := meta.(*ElasticInfo).elasticsearchUsername
password := meta.(*ElasticInfo).elasticsearchPassword
index := d.Get("index_name").(string)
contentType := d.Get("content_type").(string)
reader, err := getFileOrUrlReader(d)
if err != nil {
return err
}
url = fmt.Sprintf("%v/%v/doc/_bulk?pretty", url, index)
respBody, err := streamingRequest("POST", d, meta, url, contentType, username, password, reader)
if err != nil {
return err
}
body, err := ioutil.ReadAll(respBody)
log.Printf("Respose: %s", body)
if err != nil {
return err
}
d.SetId("a")
return err
}
func resourceElasticsearchIndexDataRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceElasticsearchIndexDataUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceElasticsearchIndexDataDelete(d *schema.ResourceData, meta interface{}) error {
url := meta.(*ElasticInfo).elasticsearchUrl
username := meta.(*ElasticInfo).elasticsearchUsername
password := meta.(*ElasticInfo).elasticsearchPassword
index := d.Get("index_name").(string)
url = fmt.Sprintf("%v/%v", url, index)
_, err := deleteKibRequest(d, meta, url, username, password)
if err != nil {
return err
}
d.SetId("")
return nil
}