is there a way to clean up pending operations from...
# automation-api
q
is there a way to clean up pending operations from the state using automation api?
currently we stack export, clean them and stack import, but there's a big notice on unmarshalling state to versioned deployment
a
I don't think there is an easy way, but you could do export/clean/import in your automation program I think
w
pulumi refresh
clears pending operations, so I would think you should be able to use the refresh method on a stack in automation API to do so.
l
Yes, newer versions of the Pulumi CLI clean the delete operations, which were the only ones that you needed to use the export/clean/import workaround to deal with. These days, refresh should cover all bases.
👍 1
q
I'm running
3.35.3
and refresh doesn't modify state via automation api. It doesn't remove pending operations and doesn't reimport running resources, which is pretty annoying
w
It works for me. I have this inline program:
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an AWS resource (S3 Bucket)
const bucket = new aws.s3.Bucket("my-bucket", {
    versioning: {
        enabled: false
    }
});

// Export the name of the bucket
export const bucketName = bucket.id;
I have this automation API program:
Copy code
import { LocalProgramArgs, LocalWorkspace } from "@pulumi/pulumi/automation";
import * as upath from "upath";

const process = require('process');

const args = process.argv.slice(2);
let destroy = false;
if (args.length > 0 && args[0]) {
    destroy = args[0] === "destroy";
}

const run = async () => {
    

    // Create our stack using a local program
    // in the ../website directory
    const args: LocalProgramArgs = {
        stackName: "dev",
        workDir: upath.joinSafe(__dirname, "..", "bucket"),
    };

    // create (or select if one already exists) a stack that uses our local program
    const stack = await LocalWorkspace.createOrSelectStack(args);

    <http://console.info|console.info>("successfully initialized stack");
    <http://console.info|console.info>("setting up config");
    await stack.setConfig("aws:region", { value: "us-west-2" });
    <http://console.info|console.info>("config set");
    <http://console.info|console.info>("refreshing stack...");
    await stack.refresh({ onOutput: <http://console.info|console.info> });
    <http://console.info|console.info>("refresh complete");

    if (destroy) {
        <http://console.info|console.info>("destroying stack...");
        await stack.destroy({onOutput: <http://console.info|console.info>});
        <http://console.info|console.info>("stack destroy complete");
        process.exit(0);
    }

    <http://console.info|console.info>("updating stack...");
    const upRes = await stack.up({ onOutput: <http://console.info|console.info> });
    console.log(`update summary: \n${JSON.stringify(upRes.summary.resourceChanges, null, 4)}`);
};

run();
If I run the automation API program and then go into the AWS console and change versioning to Enabled and then run the automation API program again, versioning will then be disabled.
a
pulumi stack export | jq 'del(.deployment.pending_operations)' | pulumi stack import
in case you were still looking