Skip to content

Commit

Permalink
feat(cli): add cloud secrets update command (#4804)
Browse files Browse the repository at this point in the history
* feat(cli): add cloud secrets update command

* chore: create missing secrets

* chore: add secrets from file and make update by name default

* chore: some cleanup

* chore: add upsert flag and some unit tests

* chore: regenerate docs

* chore: cleanup

* chore: typo

* chore: add missing override modifiers

* chore: remove detail from error

* chore: fix errors

* chore: apply copy suggestions from code review

* chore: address review comments

* chore: update docs
  • Loading branch information
shumailxyz committed Sep 26, 2023
1 parent a098a14 commit 8b8fc00
Show file tree
Hide file tree
Showing 7 changed files with 569 additions and 21 deletions.
14 changes: 13 additions & 1 deletion core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,19 @@ export class CloudApi {
method: "POST",
body: body || {},
headers: headers || {},
retry: retry === true ? true : false, // defaults to false unless true is explicitly passed
retry: !!retry, // defaults to false unless true is explicitly passed
retryDescription,
maxRetries,
})
}

async put<T>(path: string, opts: ApiFetchOptions & { body?: any } = {}) {
const { body, headers, retry, retryDescription, maxRetries } = opts
return this.apiFetch<T>(path, {
method: "PUT",
body: body || {},
headers: headers || {},
retry: !!retry, // defaults to false unless true is explicitly passed
retryDescription,
maxRetries,
})
Expand Down
8 changes: 5 additions & 3 deletions core/src/commands/cloud/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function handleBulkOperationResult<T>({
cmdLog: Log
results: T[]
errors: ApiCommandError[]
action: "create" | "delete"
action: "create" | "update" | "delete"
resource: "secret" | "user"
}): CommandResult<T[]> {
const successCount = results.length
Expand All @@ -122,7 +122,7 @@ export function handleBulkOperationResult<T>({
if (errors.length > 0) {
cmdLog.error("Error")

const actionVerb = action === "create" ? "creating" : "deleting"
const actionVerb = action === "create" ? "creating" : action === "update" ? "updating" : "deleting"
const errorMsgs = errors
.map((e) => {
// Identifier could be an ID, a name or empty.
Expand All @@ -146,7 +146,9 @@ export function handleBulkOperationResult<T>({
if (successCount > 0) {
const resourceStr = successCount === 1 ? resource : pluralize(resource)
log.info({
msg: `Successfully ${action === "create" ? "created" : "deleted"} ${successCount} ${resourceStr}!`,
msg: `Successfully ${
action === "create" ? "created" : action === "update" ? "updated" : "deleted"
} ${successCount} ${resourceStr}!`,
})
log.info("")
}
Expand Down
37 changes: 21 additions & 16 deletions core/src/commands/cloud/secrets/secrets-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,26 @@ import chalk from "chalk"
import { sortBy } from "lodash"
import { StringsParameter } from "../../../cli/params"
import { getCloudDistributionName } from "../../../util/util"
import { CloudProject } from "../../../cloud/api"
import { CloudApi, CloudProject } from "../../../cloud/api"
import { Log } from "../../../logger/log-entry"

export const fetchAllSecrets = async (api: CloudApi, projectId: string, log: Log): Promise<SecretResult[]> => {
let page = 0
let secrets: SecretResult[] = []
let hasMore = true
while (hasMore) {
log.debug(`Fetching page ${page}`)
const q = stringify({ projectId, offset: page * pageLimit, limit: pageLimit })
const res = await api.get<ListSecretsResponse>(`/secrets?${q}`)
if (res.data.length === 0) {
hasMore = false
} else {
secrets.push(...res.data.map((secret) => makeSecretFromResponse(secret)))
page++
}
}
return secrets
}

const pageLimit = 100

Expand Down Expand Up @@ -77,21 +96,7 @@ export class SecretsListCommand extends Command<{}, Opts> {
})
}

let page = 0
let secrets: SecretResult[] = []
let hasMore = true
while (hasMore) {
log.debug(`Fetching page ${page}`)
const q = stringify({ projectId: project.id, offset: page * pageLimit, limit: pageLimit })
const res = await api.get<ListSecretsResponse>(`/secrets?${q}`)
if (res.data.length === 0) {
hasMore = false
} else {
secrets.push(...res.data.map((secret) => makeSecretFromResponse(secret)))
page++
}
}

const secrets: SecretResult[] = await fetchAllSecrets(api, project.id, log)
log.info("")

if (secrets.length === 0) {
Expand Down

0 comments on commit 8b8fc00

Please sign in to comment.