Merge pull request #8 from 30x/apid_displayname

Apid displayname
diff --git a/apigeeSync_suite_test.go b/apigeeSync_suite_test.go
index 99c0ecc..be0f820 100644
--- a/apigeeSync_suite_test.go
+++ b/apigeeSync_suite_test.go
@@ -4,17 +4,17 @@
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
 
-	"testing"
+	"encoding/json"
 	"github.com/30x/apid"
 	"github.com/30x/apid/factory"
+	"github.com/apigee-labs/transicator/common"
 	"io/ioutil"
+	"net/http"
 	"net/http/httptest"
 	"os"
-	"encoding/json"
-	"net/http"
-	"github.com/apigee-labs/transicator/common"
-	"time"
 	"strconv"
+	"testing"
+	"time"
 )
 
 var (
@@ -59,6 +59,9 @@
 		Expect(req.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded; param=value"))
 
 		err := req.ParseForm()
+		// TODO: Test framework cannot handle this assertions and
+		// this handler just stops and sends back ""
+		// we need to handle it differently
 		Expect(err).NotTo(HaveOccurred())
 		Expect(req.Form.Get("grant_type")).To(Equal("client_credentials"))
 		Expect(req.Header.Get("status")).To(Equal("ONLINE"))
@@ -378,7 +381,6 @@
 	Expect(err).NotTo(HaveOccurred())
 })
 
-
 var _ = AfterSuite(func() {
 	apid.Events().Close()
 	if testServer != nil {
diff --git a/apigee_sync.go b/apigee_sync.go
index 230705c..ba9f876 100644
--- a/apigee_sync.go
+++ b/apigee_sync.go
@@ -18,7 +18,7 @@
 var lastSequence string
 
 func addHeaders(req *http.Request) {
-	req.Header.Add("Authorization", "Bearer " + token)
+	req.Header.Add("Authorization", "Bearer "+token)
 	req.Header.Set("apid_instance_id", apidInfo.InstanceID)
 	req.Header.Set("apid_cluster_Id", apidInfo.ClusterID)
 	req.Header.Set("updated_at_apid", time.Now().Format(time.RFC3339))
@@ -68,7 +68,7 @@
 		}
 		endTime := time.Second
 		// Gradually increase retry interval, and max at some level
-		if endTime - startTime <= 1 {
+		if endTime-startTime <= 1 {
 			if times < pollInterval {
 				times++
 			} else {
@@ -259,6 +259,7 @@
 	}
 
 	var oauthResp oauthTokenResp
+	log.Debugf("Response: %s ", body)
 	err = json.Unmarshal(body, &oauthResp)
 	if err != nil {
 		log.Error(err)
@@ -361,7 +362,7 @@
 	/* Get the bearer token */
 	status := getBearerToken()
 	if status == false {
-		log.Panic("Unable to get Bearer token or is Invalid")
+		log.Errorf("Unable to get Bearer token or is Invalid")
 	}
 	snapshotUri, err := url.Parse(config.GetString(configSnapServerBaseURI))
 	if err != nil {
diff --git a/apigee_sync_test.go b/apigee_sync_test.go
index dea238c..5a0ddc7 100644
--- a/apigee_sync_test.go
+++ b/apigee_sync_test.go
@@ -1,10 +1,10 @@
 package apidApigeeSync
 
 import (
-	. "github.com/onsi/ginkgo"
-	. "github.com/onsi/gomega"
 	"github.com/30x/apid"
 	"github.com/apigee-labs/transicator/common"
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
 )
 
 var _ = Describe("listener", func() {
diff --git a/data.go b/data.go
index e93971b..6618531 100644
--- a/data.go
+++ b/data.go
@@ -1,12 +1,12 @@
 package apidApigeeSync
 
 import (
+	"crypto/rand"
 	"database/sql"
+	"errors"
+	"fmt"
 	"github.com/30x/apid"
 	"sync"
-	"fmt"
-	"crypto/rand"
-	"errors"
 )
 
 var (
@@ -16,12 +16,12 @@
 
 type dataApidCluster struct {
 	ChangeSelector, ID, Name, OrgAppName, CreatedBy, UpdatedBy, Description string
-	Updated, Created string
+	Updated, Created                                                        string
 }
 
 type dataDataScope struct {
 	ChangeSelector, ID, ClusterID, Scope, Org, Env, CreatedBy, UpdatedBy string
-	Updated, Created string
+	Updated, Created                                                     string
 }
 
 /*
@@ -244,6 +244,8 @@
 }
 
 func getApidInstanceInfo() (info apidInstanceInfo, err error) {
+	info.InstanceName = config.GetString(configName)
+	info.ClusterID = config.GetString(configApidClusterId)
 
 	// always use default database for this
 	var db apid.DB
@@ -266,14 +268,6 @@
 			db.Exec("INSERT INTO APID (instance_id) VALUES (?)", info.InstanceID)
 		}
 	}
-
-	// if name not explicitly configured, just use InstanceID
-	config.SetDefault(configName, info.InstanceID)
-	info.InstanceName = config.GetString(configName)
-
-	// not stored in DB
-	info.ClusterID = config.GetString(configApidClusterId)
-
 	return
 }
 
@@ -304,6 +298,8 @@
 /*
  * generates a random uuid (mix of timestamp & crypto random string)
  */
+
+//TODO: Change to https://tools.ietf.org/html/rfc4122 based implementation such as https://github.com/google/uuid
 func generateUUID() string {
 
 	buff := make([]byte, 16)
diff --git a/glide.yaml b/glide.yaml
index df2b636..c07a74e 100644
--- a/glide.yaml
+++ b/glide.yaml
@@ -1,6 +1,8 @@
 package: github.com/30x/apidApigeeSync
 import:
 - package: github.com/30x/apid
+  version: master
 testImport:
 - package: github.com/onsi/ginkgo/ginkgo
 - package: github.com/onsi/gomega
+  version: master
diff --git a/init.go b/init.go
index 22371fa..ff19bed 100644
--- a/init.go
+++ b/init.go
@@ -4,6 +4,7 @@
 	"encoding/json"
 	"fmt"
 	"github.com/30x/apid"
+	"os"
 )
 
 const (
@@ -24,8 +25,7 @@
 	config            apid.ConfigService
 	data              apid.DataService
 	events            apid.EventsService
-
-	apidInfo	  apidInstanceInfo
+	apidInfo          apidInstanceInfo
 	apidPluginDetails string
 )
 
@@ -42,12 +42,27 @@
 	apid.RegisterPlugin(initPlugin)
 }
 
+func initDefaults() {
+	config.SetDefault(configPollInterval, 120)
+	name, errh := os.Hostname()
+	if (errh != nil) && (len(config.GetString(configName)) == 0) {
+		log.Errorf("Not able to get hostname for kernel. Please set '%s' property in config", configName)
+		name = "Undefined"
+	}
+	config.SetDefault(configName, name)
+	log.Debugf("Using %s as display name", config.GetString(configName))
+}
+
+func SetLogger(logger apid.LogService) {
+	log = logger
+}
+
 func initPlugin(services apid.Services) (apid.PluginData, error) {
-	log = services.Log().ForModule("apigeeSync")
+	SetLogger(services.Log().ForModule("apigeeSync"))
 	log.Debug("start init")
 
 	config = services.Config()
-	config.SetDefault(configPollInterval, 120)
+	initDefaults()
 
 	data = services.Data()
 	events = services.Events()
@@ -129,4 +144,3 @@
 		log.Debug("Done post plugin init")
 	}
 }
-
diff --git a/init_test.go b/init_test.go
new file mode 100644
index 0000000..6ff98c2
--- /dev/null
+++ b/init_test.go
@@ -0,0 +1,25 @@
+package apidApigeeSync
+
+import (
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+)
+
+var _ = Describe("init", func() {
+
+	Context("Apid Instance display name", func() {
+
+		It("should be hostname by defauls", func() {
+			initDefaults()
+			Expect(apidInfo.InstanceName).To(Equal("testhost"))
+		})
+		It("accept display name from config", func() {
+			config.Set(configName, "aa01")
+			initDefaults()
+			var apidInfoLatest apidInstanceInfo
+			apidInfoLatest , _ = getApidInstanceInfo()
+			Expect(apidInfoLatest.InstanceName).To(Equal("aa01"))
+		})
+
+	})
+})
diff --git a/listener.go b/listener.go
index 47c58a7..2a95d6f 100644
--- a/listener.go
+++ b/listener.go
@@ -6,8 +6,8 @@
 )
 
 const (
-	LISTENER_TABLE_APID_CLUSTER  = "edgex.apid_cluster"
-	LISTENER_TABLE_DATA_SCOPE    = "edgex.data_scope"
+	LISTENER_TABLE_APID_CLUSTER = "edgex.apid_cluster"
+	LISTENER_TABLE_DATA_SCOPE   = "edgex.data_scope"
 )
 
 type handler struct {
@@ -122,7 +122,7 @@
 				log.Panicf("illegal operation: %s for %s", change.Operation, change.Table)
 			}
 		}
-		if err != nil{
+		if err != nil {
 			log.Panicf("Error processing ChangeList: %v", err)
 		}
 	}
@@ -167,4 +167,3 @@
 
 	return ds
 }
-
diff --git a/listener_test.go b/listener_test.go
index 893e81a..ad2c272 100644
--- a/listener_test.go
+++ b/listener_test.go
@@ -17,7 +17,7 @@
 
 			event := common.Snapshot{
 				SnapshotInfo: "test_snapshot",
-				Tables: []common.Table{},
+				Tables:       []common.Table{},
 			}
 
 			handler.Handle(&event)
@@ -54,15 +54,15 @@
 						Name: LISTENER_TABLE_APID_CLUSTER,
 						Rows: []common.Row{
 							{
-								"id": &common.ColumnVal{Value: "i"},
-								"_change_selector": &common.ColumnVal{Value: "c"},
-								"name": &common.ColumnVal{Value: "n"},
+								"id":                    &common.ColumnVal{Value: "i"},
+								"_change_selector":      &common.ColumnVal{Value: "c"},
+								"name":                  &common.ColumnVal{Value: "n"},
 								"umbrella_org_app_name": &common.ColumnVal{Value: "o"},
-								"created": &common.ColumnVal{Value: "c"},
-								"created_by": &common.ColumnVal{Value: "c"},
-								"updated": &common.ColumnVal{Value: "u"},
-								"updated_by": &common.ColumnVal{Value: "u"},
-								"description": &common.ColumnVal{Value: "d"},
+								"created":               &common.ColumnVal{Value: "c"},
+								"created_by":            &common.ColumnVal{Value: "c"},
+								"updated":               &common.ColumnVal{Value: "u"},
+								"updated_by":            &common.ColumnVal{Value: "u"},
+								"description":           &common.ColumnVal{Value: "d"},
 							},
 						},
 					},
@@ -70,16 +70,16 @@
 						Name: LISTENER_TABLE_DATA_SCOPE,
 						Rows: []common.Row{
 							{
-								"id": &common.ColumnVal{Value: "i"},
+								"id":               &common.ColumnVal{Value: "i"},
 								"_change_selector": &common.ColumnVal{Value: "c"},
-								"apid_cluster_id": &common.ColumnVal{Value: "a"},
-								"scope": &common.ColumnVal{Value: "s"},
-								"org": &common.ColumnVal{Value: "o"},
-								"env": &common.ColumnVal{Value: "e"},
-								"created": &common.ColumnVal{Value: "c"},
-								"created_by": &common.ColumnVal{Value: "c"},
-								"updated": &common.ColumnVal{Value: "u"},
-								"updated_by": &common.ColumnVal{Value: "u"},
+								"apid_cluster_id":  &common.ColumnVal{Value: "a"},
+								"scope":            &common.ColumnVal{Value: "s"},
+								"org":              &common.ColumnVal{Value: "o"},
+								"env":              &common.ColumnVal{Value: "e"},
+								"created":          &common.ColumnVal{Value: "c"},
+								"created_by":       &common.ColumnVal{Value: "c"},
+								"updated":          &common.ColumnVal{Value: "u"},
+								"updated_by":       &common.ColumnVal{Value: "u"},
 							},
 						},
 					},
@@ -172,7 +172,7 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Insert,
-							Table: LISTENER_TABLE_APID_CLUSTER,
+							Table:     LISTENER_TABLE_APID_CLUSTER,
 						},
 					},
 				}
@@ -187,7 +187,7 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Update,
-							Table: LISTENER_TABLE_APID_CLUSTER,
+							Table:     LISTENER_TABLE_APID_CLUSTER,
 						},
 					},
 				}
@@ -206,18 +206,18 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Insert,
-							Table: LISTENER_TABLE_DATA_SCOPE,
+							Table:     LISTENER_TABLE_DATA_SCOPE,
 							NewRow: common.Row{
-								"id": &common.ColumnVal{Value: "i"},
+								"id":               &common.ColumnVal{Value: "i"},
 								"_change_selector": &common.ColumnVal{Value: "c"},
-								"apid_cluster_id": &common.ColumnVal{Value: "a"},
-								"scope": &common.ColumnVal{Value: "s"},
-								"org": &common.ColumnVal{Value: "o"},
-								"env": &common.ColumnVal{Value: "e"},
-								"created": &common.ColumnVal{Value: "c"},
-								"created_by": &common.ColumnVal{Value: "c"},
-								"updated": &common.ColumnVal{Value: "u"},
-								"updated_by": &common.ColumnVal{Value: "u"},
+								"apid_cluster_id":  &common.ColumnVal{Value: "a"},
+								"scope":            &common.ColumnVal{Value: "s"},
+								"org":              &common.ColumnVal{Value: "o"},
+								"env":              &common.ColumnVal{Value: "e"},
+								"created":          &common.ColumnVal{Value: "c"},
+								"created_by":       &common.ColumnVal{Value: "c"},
+								"updated":          &common.ColumnVal{Value: "u"},
+								"updated_by":       &common.ColumnVal{Value: "u"},
 							},
 						},
 					},
@@ -263,18 +263,18 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Insert,
-							Table: LISTENER_TABLE_DATA_SCOPE,
+							Table:     LISTENER_TABLE_DATA_SCOPE,
 							NewRow: common.Row{
-								"id": &common.ColumnVal{Value: "i"},
+								"id":               &common.ColumnVal{Value: "i"},
 								"_change_selector": &common.ColumnVal{Value: "c"},
-								"apid_cluster_id": &common.ColumnVal{Value: "a"},
-								"scope": &common.ColumnVal{Value: "s"},
-								"org": &common.ColumnVal{Value: "o"},
-								"env": &common.ColumnVal{Value: "e"},
-								"created": &common.ColumnVal{Value: "c"},
-								"created_by": &common.ColumnVal{Value: "c"},
-								"updated": &common.ColumnVal{Value: "u"},
-								"updated_by": &common.ColumnVal{Value: "u"},
+								"apid_cluster_id":  &common.ColumnVal{Value: "a"},
+								"scope":            &common.ColumnVal{Value: "s"},
+								"org":              &common.ColumnVal{Value: "o"},
+								"env":              &common.ColumnVal{Value: "e"},
+								"created":          &common.ColumnVal{Value: "c"},
+								"created_by":       &common.ColumnVal{Value: "c"},
+								"updated":          &common.ColumnVal{Value: "u"},
+								"updated_by":       &common.ColumnVal{Value: "u"},
 							},
 						},
 					},
@@ -287,8 +287,8 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Delete,
-							Table: LISTENER_TABLE_DATA_SCOPE,
-							OldRow: insert.Changes[0].NewRow,
+							Table:     LISTENER_TABLE_DATA_SCOPE,
+							OldRow:    insert.Changes[0].NewRow,
 						},
 					},
 				}
@@ -309,7 +309,7 @@
 					Changes: []common.Change{
 						{
 							Operation: common.Update,
-							Table: LISTENER_TABLE_DATA_SCOPE,
+							Table:     LISTENER_TABLE_DATA_SCOPE,
 						},
 					},
 				}
diff --git a/pluginData.go b/pluginData.go
index ef93e2e..9368f70 100644
--- a/pluginData.go
+++ b/pluginData.go
@@ -4,7 +4,7 @@
 
 var pluginData = apid.PluginData{
 	Name:    "apidApigeeSync",
-	Version: "0.0.2",
+	Version: "0.0.3",
 	ExtraData: map[string]interface{}{
 		"schemaVersion": "0.0.2",
 	},