apid to include basic apid/plugin details to edgex proxy.
diff --git a/apigee_sync.go b/apigee_sync.go index e02a790..a065389 100644 --- a/apigee_sync.go +++ b/apigee_sync.go
@@ -201,10 +201,15 @@ } uri.Path = path.Join(uri.Path, "/accesstoken") tokenActive = false - form := url.Values{} form.Set("grant_type", "client_credentials") form.Add("client_id", config.GetString(configConsumerKey)) + form.Add("display_name", ginstName) + form.Add("apid_instance_id", guuid) + form.Add("apid_cluster_Id", gapidConfigId) + form.Add("status", "ONLINE") + form.Add("created_at", time.Now().Format(time.RFC3339)) + form.Add("plugin_details", gpgInfo) form.Add("client_secret", config.GetString(configConsumerSecret)) req, err := http.NewRequest("POST", uri.String(), bytes.NewBufferString(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
diff --git a/apigee_sync_test.go b/apigee_sync_test.go index 55dc432..d6825b3 100644 --- a/apigee_sync_test.go +++ b/apigee_sync_test.go
@@ -17,6 +17,7 @@ var _ = Describe("api", func() { var server *httptest.Server + var plugInfo []pluginDetail BeforeSuite(func() { apid.Initialize(factory.DefaultServicesFactory()) @@ -45,6 +46,13 @@ err := req.ParseForm() Expect(err).NotTo(HaveOccurred()) Expect(req.Form.Get("grant_type")).To(Equal("client_credentials")) + Expect(req.Form.Get("status")).To(Equal("ONLINE")) + Expect(req.Form.Get("apid_cluster_Id")).To(Equal("bootstrap")) + Expect(req.Form.Get("display_name")).To(Equal("testhost")) + plinfo := []byte(req.Form.Get("plugin_details")) + err = json.Unmarshal(plinfo, &plugInfo) + Expect(err).NotTo(HaveOccurred()) + Expect(plugInfo[0].Name).To(Equal("apidApigeeSync")) res := oauthTokenResp{} res.AccessToken = "accesstoken" @@ -196,6 +204,8 @@ config.Set(configSnapServerBaseURI, server.URL) config.Set(configChangeServerBaseURI, server.URL) config.Set(configScopeId, "apid_config_scope_0") + config.Set(configName, "testhost") + config.Set(configSnapshotProtocol, "json") config.Set(configScopeId, scope) config.Set(configConsumerKey, key)
diff --git a/init.go b/init.go index 3d1dd85..dd290c1 100644 --- a/init.go +++ b/init.go
@@ -1,8 +1,11 @@ package apidApigeeSync import ( + "crypto/rand" + "encoding/json" "fmt" "github.com/30x/apid" + "time" ) const ( @@ -14,7 +17,7 @@ configConsumerSecret = "apigeesync_consumer_secret" configScopeId = "apigeesync_bootstrap_id" configSnapshotProtocol = "apigeesync_snapshot_proto" - configUnitTestMode = "apigeesync_UnitTest_mode" + configName = "apigeesync_instance_name" ApigeeSyncEventSelector = "ApigeeSync" ) @@ -24,22 +27,60 @@ data apid.DataService events apid.EventsService gapidConfigId string + guuid string + ginstName string + gpgInfo string ) +type pluginDetail struct { + Name string `json:"name"` + SchemaVersion string `json:"schemaVer"` +} + +/* + * generates a random uuid (mix of timestamp & crypto random string) + */ +func generate_uuid() string { + unix32bits := uint32(time.Now().UTC().Unix()) + buff := make([]byte, 12) + numRead, err := rand.Read(buff) + if numRead != len(buff) || err != nil { + panic(err) + } + return fmt.Sprintf("%x-%x-%x-%x-%x-%x\n", unix32bits, buff[0:2], buff[2:4], buff[4:6], buff[6:8], buff[8:]) +} + func init() { apid.RegisterPlugin(initPlugin) } func postInitPlugins(event apid.Event) { - + var plinfoDetails []pluginDetail if pie, ok := event.(apid.PluginsInitializedEvent); ok { - // todo: temporary example. do whatever registration logic is needed... + /* + * Store the plugin details in the heap. Needed during + * Bearer token generation request + */ for _, plugin := range pie.Plugins { name := plugin.Name version := plugin.Version - schemaVersion := plugin.ExtraData["schemaVersion"] - log.Debugf("plugin %s is version %s, schemaVersion: %s", name, version, schemaVersion) + log.Debugf("plugin %s is version %s, schemaVersion: %s", name, version) + if schemaVersion, ok := plugin.ExtraData["schemaVersion"].(string); ok { + inf := pluginDetail{ + Name: name, + SchemaVersion: schemaVersion} + plinfoDetails = append(plinfoDetails, inf) + } + } + if plinfoDetails == nil { + log.Panicf("No Plugins registered!") + } else { + pgInfo, err := json.Marshal(plinfoDetails) + if err != nil { + log.Panic("Unable to masrhal plugin data", err) + } + gpgInfo = (string(pgInfo[:])) } log.Debug("start post plugin init") @@ -62,6 +103,13 @@ config = services.Config() data = services.Data() events = services.Events() + guuid = generate_uuid() + + /* If The Instance has no name configured, just re-use UUID */ + ginstName = config.GetString(configName) + if ginstName == "" { + ginstName = guuid + } /* This callback function will get called, once all the plugins are * initialized (not just this plugin). This is needed because,
diff --git a/pluginData.go b/pluginData.go index d5ef00a..b65effc 100644 --- a/pluginData.go +++ b/pluginData.go
@@ -3,9 +3,9 @@ import "github.com/30x/apid" var pluginData = apid.PluginData{ - Name: "apidApigeeSync", + Name: "apidApigeeSync", Version: "0.0.1", ExtraData: map[string]interface{}{ - "syncSchemaVersion": "0.0.1", + "schemaVersion": "0.0.1", }, }