Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code example rest gw + remove tenant resources #1276

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4127,7 +4127,7 @@ func UpdateIntegration(tenantName string, name string, keys map[string]interface
}

// User Functions
func UpdtaePendingUser(tenantName, username string, pending bool) error {
func UpdatePendingUser(tenantName, username string, pending bool) error {
ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second)
defer cancelfunc()
conn, err := MetadataDbClient.Client.Acquire(ctx)
Expand Down Expand Up @@ -4929,7 +4929,7 @@ func InsertNewTag(name string, color string, stationArr []int, schemaArr []int,
return newTag, nil
}

func InsertEntityToTag(tagName string, entity string, entity_id int, tenantName string) error {
func InsertEntityToTag(tagName string, entity string, entity_id int, tenantName, color string) error {
var entityDBList string
switch entity {
case "station":
Expand All @@ -4946,14 +4946,26 @@ func InsertEntityToTag(tagName string, entity string, entity_id int, tenantName
return err
}
defer conn.Release()
query := `UPDATE tags SET ` + entityDBList + ` = ARRAY_APPEND(` + entityDBList + `, $1) WHERE name = $2 AND tenant_name = $3`
stmt, err := conn.Conn().Prepare(ctx, "insert_entity_to_tag", query)
if err != nil {
return err
}
_, err = conn.Conn().Query(ctx, stmt.Name, entity_id, tagName, tenantName)
if err != nil {
return err
if color == "" {
query := `UPDATE tags SET ` + entityDBList + ` = ARRAY_APPEND(` + entityDBList + `, $1) WHERE name = $2 AND tenant_name = $3`
stmt, err := conn.Conn().Prepare(ctx, "insert_entity_to_tag", query)
if err != nil {
return err
}
_, err = conn.Conn().Query(ctx, stmt.Name, entity_id, tagName, tenantName)
if err != nil {
return err
}
} else {
query := `UPDATE tags SET ` + entityDBList + ` = ARRAY_APPEND(` + entityDBList + `, $1) , color = $4 WHERE name = $2 AND tenant_name = $3`
stmt, err := conn.Conn().Prepare(ctx, "insert_entity_to_tag", query)
if err != nil {
return err
}
_, err = conn.Conn().Query(ctx, stmt.Name, entity_id, tagName, tenantName, color)
if err != nil {
return err
}
}
return nil
}
Expand Down Expand Up @@ -6557,6 +6569,31 @@ func DeleteConfByTenantName(tenantName string) error {
return nil
}

func DeleteIntegrationsByTenantName(tenantName string) error {
ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second)
defer cancelfunc()

conn, err := MetadataDbClient.Client.Acquire(ctx)
if err != nil {
return err
}
defer conn.Release()

query := `DELETE FROM integrations
WHERE tenant_name=$1`

stmt, err := conn.Conn().Prepare(ctx, "remove_integrations_by_tenant_name", query)
if err != nil {
return err
}
tenantName = strings.ToLower(tenantName)
_, err = conn.Conn().Exec(ctx, stmt.Name, tenantName)
if err != nil {
return err
}
return nil
}

// Async tasks functions
func UpsertAsyncTask(task, brokerInCharge string, createdAt time.Time, tenantName string, stationId int, username string) (models.AsyncTask, error) {
ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second)
Expand Down
4 changes: 2 additions & 2 deletions server/memphis_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ func CreateDefaultSchema(username, tenantName string, userId int) (string, error

func CreateDefaultTags(tagType string, id int, tenantName string) error {
defaultTags := models.CreateTag{Name: "default"}

err := AddTagsToEntity([]models.CreateTag{defaultTags}, tagType, id, tenantName)
color := "0, 165, 255"
err := AddTagsToEntity([]models.CreateTag{defaultTags}, tagType, id, tenantName, color)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/memphis_handlers_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func (sh SchemasHandler) CreateNewSchema(c *gin.Context) {
}

if len(body.Tags) > 0 {
err = AddTagsToEntity(body.Tags, "schema", newSchema.ID, tenantName)
err = AddTagsToEntity(body.Tags, "schema", newSchema.ID, tenantName, "")
if err != nil {
serv.Errorf("[tenant: %v][user: %v]CreateNewSchema at AddTagsToEntity: Failed creating tag at schema %v: %v", user.TenantName, user.Username, schemaName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
Expand Down
2 changes: 1 addition & 1 deletion server/memphis_handlers_stations.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ func (sh StationsHandler) CreateStation(c *gin.Context) {
}

if len(body.Tags) > 0 {
err = AddTagsToEntity(body.Tags, "station", newStation.ID, newStation.TenantName)
err = AddTagsToEntity(body.Tags, "station", newStation.ID, newStation.TenantName, "")
if err != nil {
serv.Errorf("[tenant: %v][user: %v]CreateStation: : Station %v Failed adding tags: %v", user.TenantName, user.Username, body.Name, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
Expand Down
9 changes: 4 additions & 5 deletions server/memphis_handlers_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func CreateTag(name string, entity_type string, entity_id int, color string, ten
return nil
}

func AddTagsToEntity(tags []models.CreateTag, entity_type string, entity_id int, tenantName string) error {
func AddTagsToEntity(tags []models.CreateTag, entity_type string, entity_id int, tenantName, color string) error {
if len(tags) == 0 {
return nil
}
Expand All @@ -78,12 +78,11 @@ func AddTagsToEntity(tags []models.CreateTag, entity_type string, entity_id int,
return err
}
} else {
err = db.InsertEntityToTag(tagToCreate.Name, entity_type, entity_id, tenantName)
err = db.InsertEntityToTag(tagToCreate.Name, entity_type, entity_id, tenantName, color)
if err != nil {
return err
}
}

}

return nil
Expand Down Expand Up @@ -371,7 +370,7 @@ func (th TagsHandler) UpdateTagsForEntity(c *gin.Context) {
return
}
} else {
err = db.InsertEntityToTag(tag.Name, entity, entity_id, tenantName)
err = db.InsertEntityToTag(tag.Name, entity, entity_id, tenantName, "")
if err != nil {
serv.Errorf("[tenant: %v][user: %v]UpdateTagsForEntity at db.InsertEntityToTag: %v %v: %v", user.TenantName, user.Username, body.EntityType, body.EntityName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
Expand Down Expand Up @@ -443,7 +442,7 @@ func (th TagsHandler) UpdateTagsForEntity(c *gin.Context) {
return
}
if exist {
err = db.InsertEntityToTag(tag.Name, entity, entity_id, tenantName)
err = db.InsertEntityToTag(tag.Name, entity, entity_id, tenantName, "")
if err != nil {
serv.Errorf("[tenant: %v][user: %v]UpdateTagsForEntity at InsertEntityToTag: %v %v: %v", user.TenantName, user.Username, body.EntityType, body.EntityName, err.Error())
c.AbortWithStatusJSON(500, gin.H{"message": "Server error"})
Expand Down
9 changes: 8 additions & 1 deletion server/memphis_handlers_user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,18 @@ func removeTenantResources(tenantName string, user models.User) error {
return err
}

err = db.RemoveTenant(tenantName)
err = db.DeleteIntegrationsByTenantName(tenantName)
if err != nil {
return err
}

if tenantName != MEMPHIS_GLOBAL_ACCOUNT {
err = db.RemoveTenant(tenantName)
if err != nil {
return err
}
}

err = serv.memphisPurgeResourcesAccount(tenantName)
if err != nil {
if err != nil && !IsNatsErr(err, JSStreamNotFoundErr) {
Expand Down
2 changes: 2 additions & 0 deletions ui_src/src/components/sdkExsample/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ const SdkExample = ({ consumer, showTabs = true, stationName, username, connecti
codeEx.tokenGenerate = codeEx.tokenGenerate?.replaceAll('connection_token', 'password');
codeEx.tokenGenerate = codeEx.tokenGenerate?.replaceAll('<broker-token>', '<password>');
codeEx.tokenGenerate = codeEx.tokenGenerate?.replaceAll('memphis.ConnectionToken', 'memphis.Password');
codeEx.tokenGenerate = codeEx.tokenGenerate?.replaceAll("strings.NewReader('{", "strings.NewReader(`{");
codeEx.tokenGenerate = codeEx.tokenGenerate?.replaceAll("}')", " }`)");
}
setCodeExample(codeEx);
};
Expand Down
23 changes: 13 additions & 10 deletions ui_src/src/const/codeExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export const PROTOCOL_CODE_EXAMPLE = {
--data-raw '{
"username": "<application type username>",
"connection_token": "<broker-token>",
"accountId": "<account-id>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092\n}'`
},
Expand Down Expand Up @@ -503,13 +503,13 @@ export const PROTOCOL_CODE_EXAMPLE = {
url := "localhost/auth/authenticate"
method := "POST"

payload := strings.NewReader({
payload := strings.NewReader('{
"username": "<application type username>",
"connection_token": "<broker-token>",
"accountId": "<account-id>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092
})
}')

client := &http.Client {
}
Expand Down Expand Up @@ -565,7 +565,7 @@ axios(config)
var data = JSON.stringify({
"username": "<application type username>",
"connection_token": "<broker-token>",
"accountId": "<account-id>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092
});
Expand Down Expand Up @@ -615,7 +615,7 @@ url = "localhost/auth/authenticate"
payload = json.dumps({
"username": "<application type username>",
"connection_token": "<broker-token>",
"accountId": "<account-id>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092
})
Expand Down Expand Up @@ -644,7 +644,7 @@ Response response = client.newCall(request).execute();`,
tokenGenerate: `OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"username\": \"<application type username>\",\n\t\"connection_token\": \"<broker-token>\",\n \"token_expiry_in_minutes\": 123,\n \"refresh_token_expiry_in_minutes\": 10000092\n}");
RequestBody body = RequestBody.create(mediaType, "{\n \"username\": \"<application type username>\",\n\t\"connection_token\": \"<broker-token>\",\n \"token_expiry_in_minutes\": 123,\n \"refresh_token_expiry_in_minutes\": 10000092\n \"account_id\": \"<account-id>\"\n}");
Request request = new Request.Builder()
.url("localhost/auth/authenticate")
.method("POST", body)
Expand All @@ -654,7 +654,8 @@ Response response = client.newCall(request).execute();`
},
'JavaScript - Fetch': {
langCode: 'javascript',
producer: `var myHeaders = new Headers();
producer: `const fetch = require('node-fetch');
const myHeaders = new fetch.Headers();
myHeaders.append("Authorization", "Bearer <jwt>");
myHeaders.append("Content-Type", "application/json");

Expand All @@ -673,13 +674,14 @@ fetch("localhost/stations/<station-name>/produce/single", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));`,
tokenGenerate: `var myHeaders = new Headers();
tokenGenerate: `const fetch = require('node-fetch');
const myHeaders = new fetch.Headers();
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"username": "<application type username>",
"connection_token": "<broker-token>",
"accountId": "<account-id>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092
});
Expand Down Expand Up @@ -724,6 +726,7 @@ console.log(response);
"data": JSON.stringify({
"username": "<application type username>",
"connection_token": "<broker-token>",
"account_id": "<account-id>",
"token_expiry_in_minutes": 123,
"refresh_token_expiry_in_minutes": 10000092
}),
Expand Down