Skip to content

Commit

Permalink
fix(pulumi): return response of ValidResultType for pulumi plugin com…
Browse files Browse the repository at this point in the history
…mands
  • Loading branch information
shumailxyz committed Oct 2, 2023
1 parent 72eb089 commit c9ee1ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
10 changes: 5 additions & 5 deletions core/src/graph/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ export class ProcessTaskNode<T extends Task = Task> extends TaskNode<T> {
const dependencyResults = this.getDependencyResults()

try {
const processResult: TaskResultType<T> | null = await this.task.process({
const processResult: TaskResultType<T> = await this.task.process({
status,
dependencyResults,
statusOnly: false,
})
this.task.emit("processed", { result: processResult ?? undefined })
if (processResult?.state === "ready") {
this.task.emit("processed", { result: processResult })
if (processResult.state === "ready") {
const msg = `${this.task.getDescription()} is ready.`
this.statusOnly || this.task.type === "resolve-action" ? this.task.log.debug(msg) : this.task.log.verbose(msg)
}
Expand Down Expand Up @@ -308,11 +308,11 @@ export class StatusTaskNode<T extends Task = Task> extends TaskNode<T> {
const dependencyResults = this.getDependencyResults()

try {
const result: TaskResultType<T> | null = await this.task.getStatus({
const result: TaskResultType<T> = await this.task.getStatus({
statusOnly: this.statusOnly,
dependencyResults,
})
this.task.emit("statusResolved", { result: result ?? undefined })
this.task.emit("statusResolved", { result: result })
if (!this.task.force && result?.state === "ready") {
const msg = `${this.task.getDescription()} status is ready.`
this.statusOnly || this.task.type === "resolve-action" ? this.task.log.debug(msg) : this.task.log.verbose(msg)
Expand Down
27 changes: 19 additions & 8 deletions plugins/pulumi/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { ActionConfigContext } from "@garden-io/core/build/src/config/template-c

type PulumiBaseParams = Omit<PulumiParams, "action">

type PulumiRunFn = (params: PulumiParams) => Promise<any>
type PulumiRunFn = (params: PulumiParams) => Promise<PulumiCommandResult>

interface PulumiCommandSpec {
name: string
Expand Down Expand Up @@ -109,13 +109,19 @@ const pulumiCommandSpecs: PulumiCommandSpec[] = [
await copy(planPath, modifiedPlanPath)
log.debug(`Copied plan to ${modifiedPlanPath}`)
return {
affectedResourcesCount,
operationCounts,
modifiedPlanPath,
previewUrl,
state: "ready",
outputs: {
affectedResourcesCount,
operationCounts,
modifiedPlanPath,
previewUrl,
}
}
} else {
return null
return {
state: "ready",
outputs: {}
}
}
},
// TODO-G2-thor: Re-enable and test when 0.13 is stable enough to run commands.
Expand Down Expand Up @@ -164,7 +170,12 @@ const pulumiCommandSpecs: PulumiCommandSpec[] = [
commandDescription: "pulumi destroy",
runFn: async (params) => {
if (params.action.getSpec("allowDestroy")) {
await deletePulumiDeploy!(params)
return await deletePulumiDeploy!(params)
}
// do nothing and return ready if allowDestory is not set
return {
state: "ready",
outputs: {}
}
},
},
Expand Down Expand Up @@ -197,7 +208,7 @@ interface PulumiPluginCommandTaskParams {
pulumiParams: PulumiBaseParams
}

interface PulumiCommandResult extends ValidResultType {}
export interface PulumiCommandResult extends ValidResultType {}

@Profile()
class PulumiPluginCommandTask extends PluginActionTask<PulumiDeploy, PulumiCommandResult> {
Expand Down
24 changes: 21 additions & 3 deletions plugins/pulumi/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { PulumiProvider } from "./provider"
import { dedent, deline, naturalList } from "@garden-io/sdk/build/src/util/string"
import { Resolved } from "@garden-io/core/build/src/actions/types"
import { ActionLog } from "@garden-io/core/build/src/logger/log-entry"
import { PulumiCommandResult } from "./commands"

export interface PulumiParams {
ctx: PluginContext
Expand Down Expand Up @@ -371,7 +372,7 @@ export function countAffectedResources(plan: PulumiPlan): number {
/**
* Wrapper for `pulumi cancel --yes`. Does not throw on error, since we may also want to cancel other updates upstream.
*/
export async function cancelUpdate({ action, ctx, provider, log }: PulumiParams): Promise<void> {
export async function cancelUpdate({ action, ctx, provider, log }: PulumiParams): Promise<PulumiCommandResult> {
const res = await pulumi(ctx, provider).exec({
log,
ignoreError: true,
Expand All @@ -383,13 +384,22 @@ export async function cancelUpdate({ action, ctx, provider, log }: PulumiParams)

if (res.exitCode !== 0) {
log.warn(chalk.yellow(`pulumi cancel failed:\n${res.stderr}`))
return {
state: "failed",
outputs: {},
}
}

return {
state: "ready",
outputs: {},
}
}

/**
* Wrapper for `pulumi refresh --yes`.
*/
export async function refreshResources(params: PulumiParams): Promise<void> {
export async function refreshResources(params: PulumiParams): Promise<PulumiCommandResult> {
const { action, ctx, provider, log } = params
const configPath = await applyConfig(params)

Expand All @@ -401,12 +411,16 @@ export async function refreshResources(params: PulumiParams): Promise<void> {
cwd: getActionStackRoot(action),
})
log.info(res.stdout)
return {
state: "ready",
outputs: {},
}
}

/**
* Wrapper for `pulumi stack export|pulumi stack import`.
*/
export async function reimportStack(params: PulumiParams): Promise<void> {
export async function reimportStack(params: PulumiParams): Promise<PulumiCommandResult> {
const { action, ctx, provider, log } = params
const cwd = getActionStackRoot(action)

Expand All @@ -426,6 +440,10 @@ export async function reimportStack(params: PulumiParams): Promise<void> {
env: ensureEnv({ log, ctx, provider, action }),
cwd,
})
return {
state: "ready",
outputs: {},
}
}

// Lower-level helpers
Expand Down

0 comments on commit c9ee1ac

Please sign in to comment.