Skip to content

Commit

Permalink
fix: fix some bug on diffJson. Fix issue #21
Browse files Browse the repository at this point in the history
Signed-off-by: disaster37 <[email protected]>
  • Loading branch information
disaster37 committed Mar 9, 2022
1 parent e4574b2 commit bd865c7
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 87 deletions.
94 changes: 33 additions & 61 deletions kb/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kb

import (
"fmt"
"strings"

"github.com/elastic/go-ucfg"
Expand All @@ -13,14 +14,22 @@ import (

// suppressEquivalentJSON permit to compare json string
func suppressEquivalentJSON(k, old, new string, d *schema.ResourceData) bool {
if old == "" {
old = `{}`
}
if new == "" {
new = `{}`
}
confOld, err := ucfgjson.NewConfig([]byte(old), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentJSON: %s", err.Error())
fmt.Printf("[ERR] Error when converting current Json: %s\ndata: %s", err.Error(), old)
log.Errorf("Error when converting current Json: %s\ndata: %s", err.Error(), old)
return false
}
confNew, err := ucfgjson.NewConfig([]byte(new), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentJSON: %s", err.Error())
fmt.Printf("[ERR] Error when converting new Json: %s\ndata: %s", err.Error(), new)
log.Errorf("Error when converting new Json: %s\ndata: %s", err.Error(), new)
return false
}

Expand All @@ -44,9 +53,13 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {

// Convert string line to JSON
for i, oldJSON := range oldSlice {
if oldJSON == "" {
oldJSON = `{}`
}
config, err := ucfgjson.NewConfig([]byte(oldJSON), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
fmt.Printf("[ERR] Error when converting current Json: %s\ndata: %s", err.Error(), oldJSON)
log.Errorf("Error when converting current Json: %s\ndata: %s", err.Error(), oldJSON)
return false
}
config.Remove("version", -1)
Expand All @@ -55,9 +68,13 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {
oldObjSlice[i] = config
}
for i, newJSON := range newSlice {
if newJSON == "" {
newJSON = `{}`
}
config, err := ucfgjson.NewConfig([]byte(newJSON), ucfg.PathSep("."))
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
fmt.Printf("[ERR] Error when converting new Json: %s\ndata: %s", err.Error(), newJSON)
log.Errorf("Error when converting new Json: %s\ndata: %s", err.Error(), newJSON)
return false
}
config.Remove("version", -1)
Expand All @@ -67,17 +84,25 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {
}

// Compare json obj
for _, oldConfig := range oldObjSlice {
for i, oldConfig := range oldObjSlice {
isFound := false
if !oldConfig.HasField("id") {
return false
}
oldId, err := oldConfig.String("id", -1)
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
log.Errorf("Error when get ID on current Json: %s\ndata: %s", err.Error(), oldSlice[i])
fmt.Printf("[ERR] Error when get ID on current Json: %s\ndata: %s", err.Error(), oldSlice[i])
return false
}
for _, newConfig := range newObjSlice {
for j, newConfig := range newObjSlice {
if !newConfig.HasField("id") {
return false
}
newId, err := newConfig.String("id", -1)
if err != nil {
log.Errorf("Error on suppressEquivalentNDJSON: %s", err.Error())
log.Errorf("Error when get ID on new Json: %s\ndata: %s", err.Error(), newSlice[j])
fmt.Printf("[ERR] Error when get ID on new Json: %s\ndata: %s", err.Error(), newSlice[j])
return false
}
if oldId == newId {
Expand All @@ -97,58 +122,5 @@ func suppressEquivalentNDJSON(k, old, new string, d *schema.ResourceData) bool {
}
}

/*
// NDJSON mean sthat each line correspond to JSON struct
oldSlice := strings.Split(old, "\n")
newSlice := strings.Split(new, "\n")
oldObjSlice := make([]map[string]interface{}, len(oldSlice))
newObjSlice := make([]map[string]interface{}, len(newSlice))
if len(oldSlice) != len(newSlice) {
return false
}
// Convert string line to JSON
for i, oldJSON := range oldSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldJSON), &jsonObj); err != nil {
return false
}
delete(jsonObj, "version")
delete(jsonObj, "updated_at")
oldObjSlice[i] = jsonObj
}
for i, newJSON := range newSlice {
jsonObj := make(map[string]interface{})
if err := json.Unmarshal([]byte(newJSON), &jsonObj); err != nil {
return false
}
delete(jsonObj, "version")
delete(jsonObj, "updated_at")
newObjSlice[i] = jsonObj
}
// Compare json obj
for _, oldJSON := range oldObjSlice {
isFound := false
for _, newJSON := range newObjSlice {
if oldJSON["id"].(string) == newJSON["id"].(string) {
if reflect.DeepEqual(oldJSON, newJSON) == false {
return false
}
isFound = true
break
}
}
if isFound == false {
return false
}
}
*/

return true

}
8 changes: 1 addition & 7 deletions kb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ func Provider() *schema.Provider {

func providerConfigure(d *schema.ResourceData) (interface{}, error) {

var (
relevantClient interface{}
)

URL := d.Get("url").(string)
insecure := d.Get("insecure").(bool)
cacertFiles := convertArrayInterfaceToArrayString(d.Get("cacert_files").(*schema.Set).List())
Expand Down Expand Up @@ -144,9 +140,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {

if vCurrent.LessThan(*vMinimal) {
return nil, errors.New("Kibana is older than 7.0.0")
} else {
relevantClient = client
}

return relevantClient, nil
return client, nil
}
2 changes: 1 addition & 1 deletion kb/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func init() {

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

// Init provider
testAccProvider = Provider()
Expand Down
6 changes: 6 additions & 0 deletions kb/resource_kibana_copy_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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/v2/helper/schema"
Expand Down Expand Up @@ -90,6 +92,7 @@ func resourceKibanaCopyObjectCreate(d *schema.ResourceData, meta interface{}) er
d.SetId(name)

log.Infof("Copy objects %s successfully", name)
fmt.Printf("[INFO] Copy objects %s successfully", name)

return resourceKibanaCopyObjectRead(d, meta)
}
Expand Down Expand Up @@ -130,6 +133,7 @@ func resourceKibanaCopyObjectRead(d *schema.ResourceData, meta interface{}) erro
d.Set("force_update", false)

log.Infof("Read resource %s successfully", id)
fmt.Printf("[INFO] Read resource %s successfully", id)

return nil
}
Expand All @@ -144,6 +148,7 @@ func resourceKibanaCopyObjectUpdate(d *schema.ResourceData, meta interface{}) er
}

log.Infof("Updated resource %s successfully", id)
fmt.Printf("[INFO] Updated resource %s successfully", id)

return resourceKibanaCopyObjectRead(d, meta)
}
Expand All @@ -155,6 +160,7 @@ func resourceKibanaCopyObjectDelete(d *schema.ResourceData, meta interface{}) er
d.SetId("")

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

}
Expand Down
1 change: 1 addition & 0 deletions kb/resource_kibana_copy_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ resource kibana_object "test" {
resource kibana_user_space "test" {
uid = "terraform-test2"
name = "terraform-test2"
}
resource kibana_copy_object "test" {
Expand Down
13 changes: 10 additions & 3 deletions kb/resource_kibana_logstash_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package kb

import (
"fmt"

kibana "github.com/disaster37/go-kibana-rest/v7"
kbapi "github.com/disaster37/go-kibana-rest/v7/kbapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -35,9 +37,8 @@ func resourceKibanaLogstashPipeline() *schema.Resource {
Optional: true,
},
"pipeline": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppressEquivalentJSON,
Type: schema.TypeString,
Required: true,
},
"username": {
Type: schema.TypeString,
Expand Down Expand Up @@ -65,6 +66,7 @@ func resourceKibanaLogstashPipelineCreate(d *schema.ResourceData, meta interface
d.SetId(logstashPipeline.ID)

log.Infof("Created logstash pipeline %s successfully", logstashPipeline.ID)
fmt.Printf("[INFO] Created logstash pipeline %s successfully", logstashPipeline.ID)

return resourceKibanaLogstashPipelineRead(d, meta)
}
Expand All @@ -85,6 +87,7 @@ func resourceKibanaLogstashPipelineRead(d *schema.ResourceData, meta interface{}

if logstashPiepeline == nil {
log.Warnf("Logstash piepline %s not found - removing from state", id)
fmt.Printf("[WARN] Logstash piepline %s not found - removing from state", id)
d.SetId("")
return nil
}
Expand All @@ -98,6 +101,7 @@ func resourceKibanaLogstashPipelineRead(d *schema.ResourceData, meta interface{}
d.Set("settings", logstashPiepeline.Settings)

log.Infof("Read logstash pipeline %s successfully", id)
fmt.Printf("[INFO] Read logstash pipeline %s successfully", id)

return nil
}
Expand All @@ -111,6 +115,7 @@ func resourceKibanaLogstashPipelineUpdate(d *schema.ResourceData, meta interface
}

log.Infof("Updated logstash piepeline %s successfully", logstashPipeline.ID)
fmt.Printf("[INFO] Updated logstash piepeline %s successfully", logstashPipeline.ID)

return resourceKibanaLogstashPipelineRead(d, meta)
}
Expand All @@ -127,6 +132,7 @@ func resourceKibanaLogstashPipelineDelete(d *schema.ResourceData, meta interface
if err != nil {
if err.(kbapi.APIError).Code == 404 {
log.Warnf("Logstash pipeline %s not found - removing from state", id)
fmt.Printf("[WARN] Logstash pipeline %s not found - removing from state", id)
d.SetId("")
return nil
}
Expand All @@ -137,6 +143,7 @@ func resourceKibanaLogstashPipelineDelete(d *schema.ResourceData, meta interface
d.SetId("")

log.Infof("Deleted logstash pipeline %s successfully", id)
fmt.Printf("[INFO] Deleted logstash pipeline %s successfully", id)
return nil

}
Expand Down
7 changes: 7 additions & 0 deletions kb/resource_kibana_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package kb

import (
"fmt"

kibana "github.com/disaster37/go-kibana-rest/v7"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -80,6 +82,7 @@ func resourceKibanaObjectCreate(d *schema.ResourceData, meta interface{}) error
d.SetId(name)

log.Infof("Imported objects %s successfully", name)
fmt.Printf("[INFO] Imported objects %s successfully", name)

return resourceKibanaObjectRead(d, meta)
}
Expand Down Expand Up @@ -107,6 +110,7 @@ func resourceKibanaObjectRead(d *schema.ResourceData, meta interface{}) error {

if len(data) == 0 {
log.Warnf("Export object %s not found - removing from state", id)
fmt.Printf("[WARN] Export object %s not found - removing from state", id)
d.SetId("")
return nil
}
Expand All @@ -120,6 +124,7 @@ func resourceKibanaObjectRead(d *schema.ResourceData, meta interface{}) error {
d.Set("export_objects", exportObjects)

log.Infof("Export object %s successfully", id)
fmt.Printf("[INFO] Export object %s successfully", id)

return nil
}
Expand All @@ -134,6 +139,7 @@ func resourceKibanaObjectUpdate(d *schema.ResourceData, meta interface{}) error
}

log.Infof("Updated object %s successfully", id)
fmt.Printf("[INFO] Updated object %s successfully", id)

return resourceKibanaObjectRead(d, meta)
}
Expand All @@ -145,6 +151,7 @@ func resourceKibanaObjectDelete(d *schema.ResourceData, meta interface{}) error
d.SetId("")

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

}
Expand Down
6 changes: 6 additions & 0 deletions kb/resource_kibana_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func resourceKibanaRoleCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(name)

log.Infof("Created role %s successfully", name)
fmt.Printf("[INFO] Created role %s successfully", name)

return resourceKibanaRoleRead(d, meta)
}
Expand All @@ -171,6 +172,7 @@ func resourceKibanaRoleRead(d *schema.ResourceData, meta interface{}) error {

if role == nil {
log.Warnf("Role %s not found - removing from state", id)
fmt.Printf("[WARN] Role %s not found - removing from state", id)
d.SetId("")
return nil
}
Expand Down Expand Up @@ -202,6 +204,7 @@ func resourceKibanaRoleRead(d *schema.ResourceData, meta interface{}) error {
}

log.Infof("Read role %s successfully", id)
fmt.Printf("[INFO] Read role %s successfully", id)

return nil
}
Expand All @@ -216,6 +219,7 @@ func resourceKibanaRoleUpdate(d *schema.ResourceData, meta interface{}) error {
}

log.Infof("Updated role %s successfully", id)
fmt.Printf("[INFO] Updated role %s successfully", id)

return resourceKibanaRoleRead(d, meta)
}
Expand All @@ -232,6 +236,7 @@ func resourceKibanaRoleDelete(d *schema.ResourceData, meta interface{}) error {
if err != nil {
if err.(kbapi.APIError).Code == 404 {
log.Warnf("Role %s not found - removing from state", id)
fmt.Printf("[WARN] Role %s not found - removing from state", id)
d.SetId("")
return nil
}
Expand All @@ -242,6 +247,7 @@ func resourceKibanaRoleDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")

log.Infof("Deleted role %s successfully", id)
fmt.Printf("[INFO] Deleted role %s successfully", id)
return nil

}
Expand Down
Loading

0 comments on commit bd865c7

Please sign in to comment.