Skip to content

Commit

Permalink
Merge branch 'release/7.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
disaster37 committed Feb 24, 2020
2 parents 7c3ee15 + 7fed999 commit 319a4c3
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- KIBANA_URL: "http://kb:5601"
- KIBANA_USERNAME: "elastic"
- KIBANA_PASSWORD: "changeme"
- image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
- image: docker.elastic.co/elasticsearch/elasticsearch:7.5.1
name: es
environment:
- cluster.name: "test"
Expand All @@ -22,7 +22,7 @@ jobs:
- xpack.security.enabled: "true"
- ES_JAVA_OPTS: "-Xms512m -Xmx512m"
- path.repo: "/tmp"
- image: docker.elastic.co/kibana/kibana:7.4.2
- image: docker.elastic.co/kibana/kibana:7.5.1
name: kb
environment:
ELASTICSEARCH_HOSTS: http://es:9200
Expand Down
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ You can see the API documentation: https://www.elastic.co/guide/en/kibana/master

***Sample:***
```tf
resource "kibana_role" "test" {
resource kibana_role "test" {
name = "terraform-test"
elasticsearch {
indices {
Expand Down Expand Up @@ -120,7 +120,7 @@ You can see the API documentation: https://www.elastic.co/guide/en/kibana/master

***Sample:***
```tf
resource "kibana_user_space" "test" {
resource kibana_user_space "test" {
name = "terraform-test"
description = "test"
initials = "tt"
Expand All @@ -147,7 +147,7 @@ You can see the API documentation: https://www.elastic.co/guide/en/kibana/master

***Sample:***
```tf
resource "kibana_object" "test" {
resource kibana_object "test" {
name = "terraform-test"
data = "${file("../fixtures/index-pattern.json")}"
deep_reference = "true"
Expand All @@ -168,6 +168,42 @@ resource "kibana_object" "test" {

---

### Copy saved object

This resource permit to copy objects from space to another spaces.
You can see the API documentation: https://www.elastic.co/guide/en/kibana/master/spaces-api.html

***Supported Kibana version:***
- v7

***Sample:***
```tf
resource kibana_copy_object "test" {
name = "terraform-test"
source_space = "default"
target_spaces = ["Team A"]
object {
id = "logstash-system-*"
type = "index-pattern"
}
}
```

***The following arguments are supported:***
- **name**: (required) The unique name
- **source_space**: (optional) The user space from copy objects. Default to `default`
- **target_spaces**: (required) The list of space where to copy objects
- **overwrite**: (optional) Overwrite existing objects. Default to `true`
- **object**: (optional) The list of object you should to copy
- **include_reference**: (optional) Include reference when copy objects. Default to `true`
- **force_update**: (optional) Force to copy objects each time you apply. Default to `true`

***object:***
- **id**: (required) The object ID
- **type**: (required) The object type

---

### Logstash pipeline management

This resource permit to manage logstash pipeline in Kibana.
Expand All @@ -178,7 +214,7 @@ You can see the API documentation: https://www.elastic.co/guide/en/kibana/master

***Sample:***
```tf
resource "kibana_logstash_pipeline" "test" {
resource kibana_logstash_pipeline "test" {
name = "terraform-test"
description = "test"
pipeline = "input { stdin {} } output { stdout {} }"
Expand Down
1 change: 1 addition & 0 deletions fixtures/test.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"test","type":"index-pattern","attributes":{"title":"test"}}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/disaster37/terraform-provider-kibana/v7
go 1.12

require (
github.com/disaster37/go-kibana-rest/v7 v7.4.2-2
github.com/disaster37/go-kibana-rest/v7 v7.5.1-1
github.com/hashicorp/terraform-plugin-sdk v1.1.1
github.com/mitchellh/gox v1.0.1
github.com/pkg/errors v0.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/disaster37/go-kibana-rest/v7 v7.4.2-2 h1:rB8+FoQaa1zpTpsz4xpfP1+doR1A
github.com/disaster37/go-kibana-rest/v7 v7.4.2-2/go.mod h1:Z87HQt6dkf7SvYqJinKPELp9HOPz15odL9ozK6iAXXI=
github.com/disaster37/go-kibana-rest/v7 v7.4.2 h1:ZD/qMxvfwoQlHTMbU2po9BuamTNQXTM/ZxmJVQpzK/4=
github.com/disaster37/go-kibana-rest/v7 v7.4.2/go.mod h1:Z87HQt6dkf7SvYqJinKPELp9HOPz15odL9ozK6iAXXI=
github.com/disaster37/go-kibana-rest/v7 v7.5.1-1 h1:/yK7P+7UNxt1jgOIjp1a9ezzXMus+X1gHtgLhVEUOYE=
github.com/disaster37/go-kibana-rest/v7 v7.5.1-1/go.mod h1:Z87HQt6dkf7SvYqJinKPELp9HOPz15odL9ozK6iAXXI=
github.com/disaster37/terraform-provider-kibana v0.0.0-20191011125519-b57968904215 h1:+NezComO7Ky50C8qsDlSFZu/BgHvTLM/rBK3Zj3EE58=
github.com/elastic/go-elasticsearch/v6 v6.8.2/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox93HoX8utj1kxD9aFUcI=
github.com/elastic/go-elasticsearch/v7 v7.4.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
Expand Down
1 change: 1 addition & 0 deletions kb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Provider() terraform.ResourceProvider {
"kibana_role": resourceKibanaRole(),
"kibana_object": resourceKibanaObject(),
"kibana_logstash_pipeline": resourceKibanaLogstashPipeline(),
"kibana_copy_object": resourceKibanaCopyObject(),
},

ConfigureFunc: providerConfigure,
Expand Down
2 changes: 1 addition & 1 deletion kb/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func init() {

// Init logger
logrus.SetFormatter(new(prefixed.TextFormatter))
logrus.SetLevel(logrus.DebugLevel)
logrus.SetLevel(logrus.InfoLevel)

// Init provider
testAccProvider = Provider().(*schema.Provider)
Expand Down
214 changes: 214 additions & 0 deletions kb/resource_kibana_copy_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Copy Kibana object from space to another spaces
// API documentation: https://www.elastic.co/guide/en/kibana/master/spaces-api-copy-saved-objects.html
// Supported version:
// - v7

package kb

import (
"fmt"

kibana "github.com/disaster37/go-kibana-rest/v7"
"github.com/disaster37/go-kibana-rest/v7/kbapi"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
log "github.com/sirupsen/logrus"
)

// Resource specification to handle kibana save object
func resourceKibanaCopyObject() *schema.Resource {
return &schema.Resource{
Create: resourceKibanaCopyObjectCreate,
Read: resourceKibanaCopyObjectRead,
Update: resourceKibanaCopyObjectUpdate,
Delete: resourceKibanaCopyObjectDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"source_space": {
Type: schema.TypeString,
Optional: true,
Default: "default",
},
"target_spaces": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"object": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"include_reference": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"overwrite": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"force_update": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}

// Copy objects in Kibana
func resourceKibanaCopyObjectCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)

err := copyObject(d, meta)
if err != nil {
return err
}

d.SetId(name)

log.Infof("Copy objects %s successfully", name)

return resourceKibanaCopyObjectRead(d, meta)
}

// Read object on kibana
func resourceKibanaCopyObjectRead(d *schema.ResourceData, meta interface{}) error {

id := d.Id()
sourceSpace := d.Get("source_space").(string)
targetSpaces := convertArrayInterfaceToArrayString(d.Get("target_spaces").(*schema.Set).List())
objects := buildCopyObjects(d.Get("object").(*schema.Set).List())
includeReference := d.Get("include_reference").(bool)
overwrite := d.Get("overwrite").(bool)
forceUpdate := d.Get("force_update").(bool)

log.Debugf("Resource id: %s", id)
log.Debugf("Source space: %s", sourceSpace)
log.Debugf("Target spaces: %+v", targetSpaces)
log.Debugf("Objects: %+v", objects)
log.Debugf("Include reference: %t", includeReference)
log.Debugf("Overwrite: %t", overwrite)
log.Debugf("force_update: %t", forceUpdate)

// @ TODO
// A good when is to check if already exported object is the same that original space
// To avoid this hard code, we juste use force_update and overwrite field
// It make same result but in bad way on terraform spirit

d.Set("name", id)
d.Set("source_space", sourceSpace)
d.Set("target_spaces", targetSpaces)
d.Set("object", objects)
d.Set("include_reference", includeReference)
d.Set("overwrite", overwrite)
d.Set("force_update", false)

log.Infof("Read resource %s successfully", id)

return nil
}

// Update existing object in Kibana
func resourceKibanaCopyObjectUpdate(d *schema.ResourceData, meta interface{}) error {
id := d.Id()

err := copyObject(d, meta)
if err != nil {
return err
}

log.Infof("Updated resource %s successfully", id)

return resourceKibanaCopyObjectRead(d, meta)
}

// Delete object in Kibana is not supported
// It just remove object from state
func resourceKibanaCopyObjectDelete(d *schema.ResourceData, meta interface{}) error {

d.SetId("")

log.Infof("Delete object in not supported")
fmt.Printf("[INFO] Delete object in not supported - just removing from state")
return nil

}

// Build list of object to export
func buildCopyObjects(raws []interface{}) []map[string]string {

results := make([]map[string]string, len(raws))

for i, raw := range raws {
m := raw.(map[string]interface{})
object := map[string]string{}
object["type"] = m["type"].(string)
object["id"] = m["id"].(string)
results[i] = object
}

return results
}

// Copy objects in Kibana
func copyObject(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
sourceSpace := d.Get("source_space").(string)
targetSpaces := convertArrayInterfaceToArrayString(d.Get("target_spaces").(*schema.Set).List())
objects := buildCopyObjects(d.Get("object").(*schema.Set).List())
includeReference := d.Get("include_reference").(bool)
overwrite := d.Get("overwrite").(bool)

log.Debugf("Source space: %s", sourceSpace)
log.Debugf("Target spaces: %+v", targetSpaces)
log.Debugf("Objects: %+v", objects)
log.Debugf("Include reference: %t", includeReference)
log.Debugf("Overwrite: %t", overwrite)

client := meta.(*kibana.Client)

objectsParameter := make([]kbapi.KibanaSpaceObjectParameter, 0, 1)
for _, object := range objects {
objectsParameter = append(objectsParameter, kbapi.KibanaSpaceObjectParameter{
ID: object["id"],
Type: object["type"],
})
}

parameter := &kbapi.KibanaSpaceCopySavedObjectParameter{
Spaces: targetSpaces,
Objects: objectsParameter,
IncludeReferences: includeReference,
Overwrite: overwrite,
}

err := client.API.KibanaSpaces.CopySavedObjects(parameter, sourceSpace)
if err != nil {
return err
}

log.Debugf("Copy object for resource successfully: %s", name)

return nil
}
Loading

0 comments on commit 319a4c3

Please sign in to comment.