Add schema type check in APID plugins. Add bulk updates for snapshot for ApidVerifyKey.
diff --git a/api_test.go b/api_test.go index d310faa..566897f 100644 --- a/api_test.go +++ b/api_test.go
@@ -242,7 +242,7 @@ } srvItems["tenant_id"] = scv - res := insertCreateDeveloper(srvItems, db) + res := insertDeveloper(srvItems, db) Expect(res).Should(BeTrue()) } @@ -289,7 +289,7 @@ Type: 1, } srvItems["tenant_id"] = scv - res := insertCreateApplication(srvItems, db) + res := insertApplication(srvItems, db) Expect(res).Should(BeTrue()) } k = j @@ -328,7 +328,7 @@ Type: 1, } srvItems["tenant_id"] = scv - res := insertCreateCredential(srvItems, db) + res := insertCredential(srvItems, db) Expect(res).Should(BeTrue()) } @@ -369,7 +369,7 @@ Type: 1, } srvItems["tenant_id"] = scv - res := insertApiProductMapper(srvItems, db) + res := insertAPIProductMapper(srvItems, db) Expect(res).Should(BeTrue()) }
diff --git a/listener.go b/listener.go index ad662da..91eb2a2 100644 --- a/listener.go +++ b/listener.go
@@ -33,6 +33,7 @@ func processSnapshot(snapshot *common.Snapshot) { + res := true log.Debugf("Process Snapshot data") db, err := data.DB() @@ -40,32 +41,293 @@ panic("Unable to access Sqlite DB") } + /* + * Iterate the tables, and insert the rows, + * Commit them in bulk. + */ for _, payload := range snapshot.Tables { - switch payload.Name { - case "developer": - for _, row := range payload.Rows { - insertCreateDeveloper(row, db) - } - case "app": - for _, row := range payload.Rows { - insertCreateApplication(row, db) - } - case "app_credential": - for _, row := range payload.Rows { - insertCreateCredential(row, db) - } - case "api_product": - for _, row := range payload.Rows { - insertAPIproduct(row, db) - } - case "app_credential_apiproduct_mapper": - for _, row := range payload.Rows { - insertApiProductMapper(row, db) - } - + case "kms.developer": + res = insertDevelopers(payload.Rows, db) + case "kms.app": + res = insertApplications(payload.Rows, db) + case "kms.app_credential": + res = insertCredentials(payload.Rows, db) + case "kms.api_product": + res = insertAPIproducts(payload.Rows, db) + case "kms.app_credential_apiproduct_mapper": + res = insertApiProductMappers(payload.Rows, db) + } + if res == false { + log.Error("Error encountered in Downloading Snapshot for VerifyApiKey") + return } } + log.Debug("Downloading Snapshot for VerifyApiKey complete") +} + +/* + * Performs bulk insert of credentials + */ +func insertCredentials(rows []common.Row, db *sql.DB) bool { + + var scope, id, appId, consumerSecret, appstatus, status, tenantId string + var issuedAt int64 + + prep, err := db.Prepare("INSERT INTO APP_CREDENTIAL (_apid_scope, id, app_id, consumer_secret, app_status, status, issued_at, tenant_id)VALUES($1,$2,$3,$4,$5,$6,$7,$8);") + if err != nil { + log.Error("INSERT Cred Failed: ", err) + return false + } + + txn, err := db.Begin() + for _, ele := range rows { + ele.Get("_apid_scope", &scope) + ele.Get("id", &id) + ele.Get("app_id", &appId) + ele.Get("consumer_secret", &consumerSecret) + ele.Get("app_status", &appstatus) + ele.Get("status", &status) + ele.Get("issued_at", &issuedAt) + ele.Get("tenant_id", &tenantId) + _, err = txn.Stmt(prep).Exec( + scope, + id, + appId, + consumerSecret, + appstatus, + status, + issuedAt, + tenantId) + + if err != nil { + log.Error("INSERT CRED Failed: ", id, ", ", scope, ")", err) + txn.Rollback() + return false + } else { + log.Debug("INSERT CRED Success: (", id, ", ", scope, ")") + } + } + txn.Commit() + return true +} + +/* + * Performs Bulk insert of Applications + */ +func insertApplications(rows []common.Row, db *sql.DB) bool { + + var scope, EntityIdentifier, DeveloperId, CallbackUrl, Status, AppName, AppFamily, tenantId, CreatedBy, LastModifiedBy string + var CreatedAt, LastModifiedAt int64 + + prep, err := db.Prepare("INSERT INTO APP (_apid_scope, id, developer_id,callback_url,status, name, app_family, created_at, created_by,updated_at, updated_by,tenant_id) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") + if err != nil { + log.Error("INSERT APP Failed: ", err) + return false + } + + txn, err := db.Begin() + for _, ele := range rows { + + ele.Get("_apid_scope", &scope) + ele.Get("id", &EntityIdentifier) + ele.Get("developer_id", &DeveloperId) + ele.Get("callback_url", &CallbackUrl) + ele.Get("status", &Status) + ele.Get("name", &AppName) + ele.Get("app_family", &AppFamily) + ele.Get("created_at", &CreatedAt) + ele.Get("created_by", &CreatedBy) + ele.Get("updated_at", &LastModifiedAt) + ele.Get("updated_by", &LastModifiedBy) + ele.Get("tenant_id", &tenantId) + + _, err = txn.Stmt(prep).Exec( + scope, + EntityIdentifier, + DeveloperId, + CallbackUrl, + Status, + AppName, + AppFamily, + CreatedAt, + CreatedBy, + LastModifiedAt, + LastModifiedBy, + tenantId) + + if err != nil { + log.Error("INSERT APP Failed: (", EntityIdentifier, ", ", tenantId, ")", err) + txn.Rollback() + return false + } else { + log.Debug("INSERT APP Success: (", EntityIdentifier, ", ", tenantId, ")") + } + } + txn.Commit() + return true + +} + +/* + * Performs bulk insert of Developers + */ +func insertDevelopers(rows []common.Row, db *sql.DB) bool { + + var scope, EntityIdentifier, Email, Status, UserName, FirstName, LastName, tenantId, CreatedBy, LastModifiedBy, Username string + var CreatedAt, LastModifiedAt int64 + + prep, err := db.Prepare("INSERT INTO DEVELOPER (_apid_scope,email,id,tenant_id,status,username,first_name,last_name,created_at,created_by,updated_at,updated_by) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);") + if err != nil { + log.Error("INSERT DEVELOPER Failed: ", err) + return false + } + + txn, err := db.Begin() + for _, ele := range rows { + + ele.Get("_apid_scope", &scope) + ele.Get("email", &Email) + ele.Get("id", &EntityIdentifier) + ele.Get("tenant_id", &tenantId) + ele.Get("status", &Status) + ele.Get("username", &Username) + ele.Get("first_name", &FirstName) + ele.Get("last_name", &LastName) + ele.Get("created_at", &CreatedAt) + ele.Get("created_by", &CreatedBy) + ele.Get("updated_at", &LastModifiedAt) + ele.Get("updated_by", &LastModifiedBy) + + _, err = txn.Stmt(prep).Exec( + scope, + Email, + EntityIdentifier, + tenantId, + Status, + UserName, + FirstName, + LastName, + CreatedAt, + CreatedBy, + LastModifiedAt, + LastModifiedBy) + + if err != nil { + log.Error("INSERT DEVELOPER Failed: (", EntityIdentifier, ", ", scope, ")", err) + txn.Rollback() + return false + } else { + log.Debug("INSERT DEVELOPER Success: (", EntityIdentifier, ", ", scope, ")") + } + } + txn.Commit() + return true +} + +/* + * Performs Bulk insert of API products + */ +func insertAPIproducts(rows []common.Row, db *sql.DB) bool { + + var scope, apiProduct, res, env, tenantId string + + prep, err := db.Prepare("INSERT INTO API_PRODUCT (id, api_resources, environments, tenant_id,_apid_scope) VALUES($1,$2,$3,$4,$5)") + if err != nil { + log.Error("INSERT API_PRODUCT Failed: ", err) + return false + } + + txn, err := db.Begin() + for _, ele := range rows { + + ele.Get("_apid_scope", &scope) + ele.Get("id", &apiProduct) + ele.Get("api_resources", &res) + ele.Get("environments", &env) + ele.Get("tenant_id", &tenantId) + + _, err = txn.Stmt(prep).Exec( + apiProduct, + res, + env, + tenantId, + scope) + + if err != nil { + log.Error("INSERT API_PRODUCT Failed: (", apiProduct, ", ", tenantId, ")", err) + txn.Rollback() + return false + } else { + log.Debug("INSERT API_PRODUCT Success: (", apiProduct, ", ", tenantId, ")") + } + } + txn.Commit() + return true +} + +/* + * Performs a bulk insert of all APP_CREDENTIAL_APIPRODUCT_MAPPER rows + */ +func insertApiProductMappers(rows []common.Row, db *sql.DB) bool { + + var ApiProduct, AppId, EntityIdentifier, tenantId, Scope, Status string + + prep, err := db.Prepare("INSERT INTO APP_CREDENTIAL_APIPRODUCT_MAPPER(apiprdt_id, app_id, appcred_id, tenant_id, _apid_scope, status) VALUES($1,$2,$3,$4,$5,$6);") + if err != nil { + log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: ", err) + return false + } + + txn, err := db.Begin() + for _, ele := range rows { + + ele.Get("apiprdt_id", &ApiProduct) + ele.Get("app_id", &AppId) + ele.Get("appcred_id", &EntityIdentifier) + ele.Get("tenant_id", &tenantId) + ele.Get("_apid_scope", &Scope) + ele.Get("status", &Status) + + /* + * If the credentials has been successfully inserted, insert the + * mapping entries associated with the credential + */ + + _, err = txn.Stmt(prep).Exec( + ApiProduct, + AppId, + EntityIdentifier, + tenantId, + Scope, + Status) + + if err != nil { + log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: (", + ApiProduct, ", ", + AppId, ", ", + EntityIdentifier, ", ", + tenantId, ", ", + Scope, ", ", + Status, + ")", + err) + + txn.Rollback() + return false + } else { + log.Debug("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER Success: (", + ApiProduct, ", ", + AppId, ", ", + EntityIdentifier, ", ", + tenantId, ", ", + Scope, ", ", + Status, + ")") + } + } + txn.Commit() + return true } func processChange(changes *common.ChangeList) { @@ -80,35 +342,53 @@ for _, payload := range changes.Changes { switch payload.Table { - case "kms.developer", "public.developer": + case "kms.developer": switch payload.Operation { case 1: - insertCreateDeveloper(payload.NewRow, db) + insertDeveloper(payload.NewRow, db) + case 2: + updateDeveloper(payload.NewRow, payload.OldRow, db) + case 3: + deleteDeveloper(payload.OldRow, db) + } + case "kms.app": + switch payload.Operation { + case 1: + insertApplication(payload.NewRow, db) + case 2: + updateApplication(payload.NewRow, payload.OldRow, db) + case 3: + deleteApplication(payload.OldRow, db) } - case "kms.app", "public.app": + case "kms.app_credential": switch payload.Operation { case 1: - insertCreateApplication(payload.NewRow, db) + insertCredential(payload.NewRow, db) + case 2: + updateCredential(payload.NewRow, payload.OldRow, db) + case 3: + deleteCredential(payload.OldRow, db) } - - case "kms.app_credential", "public.app_credential": - switch payload.Operation { - case 1: - insertCreateCredential(payload.NewRow, db) - } - case "kms.api_product", "public.api_product": + case "kms.api_product": switch payload.Operation { case 1: insertAPIproduct(payload.NewRow, db) + case 2: + updateAPIproduct(payload.NewRow, payload.OldRow, db) + case 3: + deleteAPIproduct(payload.OldRow, db) } - case "kms.app_credential_apiproduct_mapper", "public.app_credential_apiproduct_mapper": + case "kms.app_credential_apiproduct_mapper": switch payload.Operation { case 1: - insertApiProductMapper(payload.NewRow, db) + insertAPIProductMapper(payload.NewRow, db) + case 2: + updateAPIproductMapper(payload.NewRow, payload.OldRow, db) + case 3: + deleteAPIproductMapper(payload.OldRow, db) } - } } } @@ -116,7 +396,7 @@ /* * INSERT INTO APP_CREDENTIAL op */ -func insertCreateCredential(ele common.Row, db *sql.DB) bool { +func insertCredential(ele common.Row, db *sql.DB) bool { var scope, id, appId, consumerSecret, appstatus, status, tenantId string var issuedAt int64 @@ -154,7 +434,8 @@ } } -func insertApiProductMapper(ele common.Row, db *sql.DB) bool { + +func insertAPIProductMapper(ele common.Row, db *sql.DB) bool { var ApiProduct, AppId, EntityIdentifier, tenantId, Scope, Status string @@ -208,13 +489,6 @@ } /* - * DELETE CRED - */ -func deleteCredential(ele common.Row, db *sql.DB) bool { - return true -} - -/* * INSERT INTO API product op */ func insertAPIproduct(ele common.Row, db *sql.DB) bool { @@ -252,7 +526,7 @@ /* * INSERT INTO APP op */ -func insertCreateApplication(ele common.Row, db *sql.DB) bool { +func insertApplication(ele common.Row, db *sql.DB) bool { var scope, EntityIdentifier, DeveloperId, CallbackUrl, Status, AppName, AppFamily, tenantId, CreatedBy, LastModifiedBy string var CreatedAt, LastModifiedAt int64 @@ -302,7 +576,7 @@ /* * INSERT INTO DEVELOPER op */ -func insertCreateDeveloper(ele common.Row, db *sql.DB) bool { +func insertDeveloper(ele common.Row, db *sql.DB) bool { var scope, EntityIdentifier, Email, Status, UserName, FirstName, LastName, tenantId, CreatedBy, LastModifiedBy, Username string var CreatedAt, LastModifiedAt int64 @@ -347,3 +621,73 @@ return true } } + +/* + * DELETE APP + */ +func deleteApplication(ele common.Row, db *sql.DB) bool { + return true +} + +/* + * DELETE CRED + */ +func deleteCredential(ele common.Row, db *sql.DB) bool { + return true +} + +/* + * DELETE developer + */ +func deleteDeveloper(ele common.Row, db *sql.DB) bool { + return true +} + +/* + * DELETE API product + */ +func deleteAPIproduct(ele common.Row, db *sql.DB) bool { + return true +} + +/* + * DELETE APIPRDT MAPPER + */ +func deleteAPIproductMapper(ele common.Row, db *sql.DB) bool { + return true +} + +/* + * UPDATE APP + */ +func updateApplication(ele common.Row, ele2 common.Row, db *sql.DB) bool { + return true +} + +/* + * UPDATE CRED + */ +func updateCredential(ele common.Row, ele2 common.Row, db *sql.DB) bool { + return true +} + +/* + * UPDATE developer + */ +func updateDeveloper(ele common.Row, ele2 common.Row, db *sql.DB) bool { + return true +} + +/* + * UPDATE API product + */ +func updateAPIproduct(ele common.Row, ele2 common.Row, db *sql.DB) bool { + return true +} + +/* + * UPDATE APIPRDT MAPPER + */ +func updateAPIproductMapper(ele common.Row, ele2 common.Row, db *sql.DB) bool { + return true +}
diff --git a/listener_test.go b/listener_test.go index 2a91b90..bd425c6 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -170,27 +170,27 @@ event.Changes = []common.Change{ { - Table: "public.api_product", + Table: "kms.api_product", NewRow: srvItems, Operation: 1, }, { - Table: "public.developer", + Table: "kms.developer", NewRow: devItems, Operation: 1, }, { - Table: "public.app", + Table: "kms.app", NewRow: appItems, Operation: 1, }, { - Table: "public.app_credential", + Table: "kms.app_credential", NewRow: credItems, Operation: 1, }, { - Table: "public.app_credential_apiproduct_mapper", + Table: "kms.app_credential_apiproduct_mapper", NewRow: mpItems, Operation: 1, },