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

Commit

Permalink
Alter create service-accounts 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 964554c commit b9fe8e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 16 additions & 8 deletions server/src/server/wrapper/connected/CcloudServiceAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse, parseSideColumns } from "./../parser";
import { executeCli } from "./executeCli";
import { CliException, ServiceAccountAlreadyExistsException } from "./../model/error";
import { Deserializer, ConcatOutput } from "../utils";
import { ListServiceAccounts } from "../model/service-account";
import { ListServiceAccounts, ListServiceAccount } from "../model/service-account";

export class CcloudServiceAccount implements ServiceAccounts {
ccloud: CCloudCliWrapper;
Expand Down Expand Up @@ -30,14 +30,14 @@ export class CcloudServiceAccount implements ServiceAccounts {
async createServiceAccount(accountName: string, description: string = ""): Promise<ServiceAccount> {
let cliResult;
try {
cliResult = await executeCli(["service-account", "create", accountName, "--description", description]);
cliResult = await executeCli(["iam", "service-account", "create", accountName, "--description", description, "--output", "json"]);
}
catch (error) {
if (error.name.valueOf() !== "CliException") {
throw (error);
}

if (error.consoleLines.some((l: string): boolean => l.includes("Service name is already in use"))) {
if (error.consoleLines.some((l: string): boolean => l.includes("is already in use"))) {
let existingServicesAccounts = await this.getServiceAccounts();

let existingServicesAccount = existingServicesAccounts.find(s => s.Name === accountName);
Expand All @@ -54,15 +54,23 @@ export class CcloudServiceAccount implements ServiceAccounts {
throw (error);
}

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


let result = parseSideColumns(cliResult);

return (result as any) as ServiceAccount;
return {
Name: deserializedResult.name,
Id: deserializedResult.id,
Description: deserializedResult.description
};
}

async deleteServiceAccount(accountId: number): Promise<boolean> {
await executeCli(["service-account", "delete", accountId.toString()]);
await executeCli(["iam", "service-account", "delete", accountId.toString()]);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/server/wrapper/model/service-account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class ListServiceAccount {
export class ListServiceAccount {
id: number;
name: string;
description: string; // Currently isn't parsed properly (whitespace and space in general gets trimemd away)
Expand Down

0 comments on commit b9fe8e4

Please sign in to comment.