Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Alter api-keys to utilize json output
Browse files Browse the repository at this point in the history
  • Loading branch information
wcarlsen committed Apr 28, 2022
1 parent b9fe8e4 commit f1eebcc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion server/src/server/api/api-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class ApiKeysInterface {

try {
let apiKey = await apiKeys.createApiKey(
parseInt(req.body.serviceAccountId),
req.body.serviceAccountId,
req.body.description
);
res.json(apiKey);
Expand Down
47 changes: 36 additions & 11 deletions server/src/server/wrapper/connected/CcloudApiKeys.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
import { parse, parseSideColumns } from "./../parser";
import {executeCli } from "./executeCli";
import { GetConfig } from "../../config";
import { Deserializer, ConcatOutput } from "../utils";
import { CreateApiKey, ListApiKeys } from "../model/api-keys";

export class CcloudApiKeys implements ApiKeys {
ccloud: CCloudCliWrapper;

async createApiKey(serviceAccountId: number, description: string): Promise<ApiKeySet> {
async createApiKey(serviceAccountId: string, description: string): Promise<ApiKeySet> {
let config = GetConfig();

let cliOutput = await executeCli([
"api-key",
"create",
"--resource", config.clusterId,
"--environment", config.environmentId,
"--service-account", serviceAccountId + "",
"--description", description]
"--service-account", serviceAccountId,
"--description", description, "--output", "json"]
);

let cliObjects: any = parseSideColumns(cliOutput);
let apiKeySet: ApiKeySet = { Key: cliObjects.APIKey, Secret: cliObjects.Secret }

let combinedResult = ConcatOutput(cliOutput);
let deserializedResult : CreateApiKey;
try {
deserializedResult = Deserializer<CreateApiKey>(combinedResult);
} catch (error) {
return error;
}

return apiKeySet;
return {
Key: deserializedResult.key,
Secret: deserializedResult.secret
};
}

async deleteApiKey(key: string): Promise<void> {
await executeCli(["api-key", "delete", key]);
}

async getApiKeys(): Promise<ApiKey[]> {
let cliOutput = await executeCli(["api-key", "list"]);
let cliObjects = parse(cliOutput);
let cliOutput = await executeCli(["api-key", "list", "--output", "json"]);

let apiKeys = cliObjects.map(function (obj) {
return { Key: obj.Key, Description: obj.Description, Owner: obj.Owner, Resource: obj.ResourceID } as ApiKey
});

return apiKeys;
let combinedResult = ConcatOutput(cliOutput);
let deserializedResult : ListApiKeys;
try {
deserializedResult = Deserializer<ListApiKeys>(combinedResult);
} catch (error) {
return error;
}

return deserializedResult.map(t => {
let obj = {
Key: t.key,
Description: t.description,
Owner: t.owner_resource_id,
Resource: t.resource_id
};
return obj;
})
}

constructor(ccloud: CCloudCliWrapper) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/server/wrapper/definitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface ServiceAccounts {

interface ApiKeys {
createApiKey(
serviceAccountId: number,
serviceAccountId: string,
description: string
): Promise<ApiKeySet>;

Expand Down
16 changes: 16 additions & 0 deletions server/src/server/wrapper/model/api-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export class CreateApiKey {
key: string
secret: string
}

export class ListApiKey{
created: string
description: string
key: string
owner_email: string
owner_resource_id: string
resource_id: string
resource_type: string
}

export type ListApiKeys = Array<ListApiKey>
6 changes: 3 additions & 3 deletions server/src/server/wrapper/model/service-account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class ListServiceAccount {
id: number;
name: string;
description: string; // Currently isn't parsed properly (whitespace and space in general gets trimemd away)
id: number
name: string
description: string // Currently isn't parsed properly (whitespace and space in general gets trimemd away)
}

export type ListServiceAccounts = Array<ListServiceAccount>;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class NotConnectedApiKeys implements ApiKeys {
constructor() {
this.apiKeys = [];
}
async createApiKey(serviceAccountId: number, description: string): Promise<ApiKeySet> {
async createApiKey(serviceAccountId: string, description: string): Promise<ApiKeySet> {
let key = NotConnectedApiKeys.createRandomString(16, true);

let apiKey: ApiKey = {
Expand Down

0 comments on commit f1eebcc

Please sign in to comment.