Hi guys, I have a node app that tries to create re...
# automation-api
b
Hi guys, I have a node app that tries to create resources on Azure cloud as it could be seen below,
Copy code
const projectName = "pulumi_over_http";
const createPulumiProgram = (content: string) => async () => {
    // Create a bucket and expose a website index document
    console.log("Resource group creation starts...")
    const resourceGroup = new azure.core.ResourceGroup('rg');
    console.log("Resource group creation ends...")

    console.log("Storage account creation starts...")
    const storage = new azure.storage.Account("storageaccount", {
        accountKind: 'StorageV2',
        accountReplicationType: 'LRS',
        accountTier: 'Standard',
        allowBlobPublicAccess: false,
        location: 'WestEurope',
        name: "storageaccount",
        resourceGroupName: resourceGroup.name,
    });
    console.log("Storage account creation ends...")

    console.log("Storage container creation starts...");
    const container = new azure.storage.Container(
    "storagecontainer",
    {
        containerAccessType: 'blob',
        name: "storagecontainer",
        storageAccountName: storage.name,
    });
    console.log("Storage container creation ends...");

    // Upload the files
    ["index.html", "404.html"].map(name =>
        new azure.storage.Blob(name, {
            contentType: 'text/html',
            // source: `./static/${page.name}`,
            name,
            storageAccountName: storage.name,
            storageContainerName: container.name,
            type: 'Block',
        }),
    );
};

const buildBaseRequestCopy = (
    baseRequest: BaseRequest,
  ) => ({
      ...baseRequest,
      purpose: 'pul',
    });

const init = async (
    kvName: string,
    baseRequest: BaseRequest, 
    stackName: string
) => {
    // await initBasicLocalWorkspaceConfig(kvName, baseRequest.environment, stackName);
    // await initAtlasLocalWorkspaceConfig(kvName, baseRequest.environment, stackName);

    // case AzureEnvVar.AZURE_CLIENT_ID:
    process.env.AZURE_CLIENT_ID = 'XXXXXXXXXXXX';
    process.env.AZURE_CLIENT_SECRET = 'XXXXXXXXXXXX'
    process.env.AZURE_TENANT_ID = 'XXXXXXXXXXXX'
    process.env.AZURE_CLIENT_OBJECT_ID = 'XXXXXXXXXXXX'
}

const baseRequest: BaseRequest = {
    application: 'atl',
    countryCode: '3se',
    domain: 'cld',
    environment: 'dev',
    project: '',
    team: '',
}

const request: AtlasRequest  = {
    ...baseRequest,
};

const baseRequestCopy: BaseRequest = buildBaseRequestCopy(
    baseRequest,
);

const stackName = 'atl-cld-dev-3se';
const kvName = 'hi3gkvpulatlclddev3se';
const strName = 'hi3gstgpulatlclddev3se'
// creates new sites
const createHandler: express.RequestHandler = async (req, res) => {
    const content = req.body.content as string;
    await init(kvName, baseRequestCopy, stackName);
        
    try {
        // create a new stack
        const stack = await LocalWorkspace.createStack({
            // generate our pulumi program on the fly from the POST body
            program: createPulumiProgram(content),
            projectName,
            stackName,
          },
          setLocalWorkspaceOptions(
            projectName,
            stackName,
            kvName,
            strName,
          ),
        );
        await stack.setConfig("azure:region", { value: "west-europe" });
        // deploy the stack, tailing the logs to console
        const upRes = await stack.up({ onOutput: <http://console.info|console.info> });
        res.json({ id: stackName, url: upRes.outputs.websiteUrl.value });
    } catch (e) {
        if (e instanceof StackAlreadyExistsError) {
            res.status(409).send(`stack "${stackName}" already exists`);
        } else {
            res.status(500).send(e);
        }
    }
};

// updates the content for an existing site
const updateHandler: express.RequestHandler = async (req, res) => {
    const stackName = req.params.id;
    const content = req.body.content as string;
    try {
        await init(kvName, baseRequestCopy, stackName);
        // create a new stack
        const stack = await LocalWorkspace.selectStack({
            // generate our pulumi program on the fly from the POST body
            program: createPulumiProgram(content),
            projectName,
            stackName,
          },
          setLocalWorkspaceOptions(
            projectName,
            stackName,
            kvName,
            strName,
          ),
        );
        await stack.setConfig("azure:region", { value: "west-europe" });
        // deploy the stack, tailing the logs to console
        const upRes = await stack.up({ onOutput: <http://console.info|console.info> });
        res.json({ id: stackName, url: upRes.outputs.websiteUrl.value });
    } catch (e) {
        if (e instanceof StackNotFoundError) {
            res.status(404).send(`stack "${stackName}" does not exist`);
        } else if (e instanceof ConcurrentUpdateError) {
            res.status(409).send(`stack "${stackName}" already has update in progress`)
        } else {
            res.status(500).send(e);
        }
    }
};
const ensurePlugins = async () => {
    const ws = await LocalWorkspace.create({});
    await ws.installPlugin("azure", "v4.6.0");
};

// install necessary plugins once upon boot
ensurePlugins();

// configure express
const app = express();
app.use(express.json());

// setup our RESTful routes for our Site resource
<http://app.post|app.post>("/atlas", createHandler);
app.put("/atlas/:id", updateHandler);

// start our http server
// start our http server
app.listen(1337, () => <http://console.info|console.info>("server running on :1337"));
l
Slack has a feature called "Create a Text Snippet" in the "+" menu, which makes collapsible text snippets for large chunks of code. It's helpful for reading the channel. Also, using threads can help with big posts like this. Describe the problem in a sentence or two, in a way that helps people decide whether or not they might be able to help; then post the finer details within the thread off that first post.
b
@little-cartoon-10569 thanks for the suggestion. I unfortunately don't see that feature on my org account but let me try to describe the problem in short. We are trying out Pulumi automation API in order to provision resources on Azure cloud and use self-managed backend with Azure blob storage. After taking reference from https://github.com/pulumi/automation-api-examples/tree/main/nodejs/pulumiOverHttp-ts, I wrote a similar program that deploys resources such as resource group, storage account, storage container and finally blob pages on Azure having Azure blob storage as Pulumi backend. When I try to hit create or update endpoints, it is unable to create any of the resources but throws an error containing the message
failed to register new resource rg [azure:core/resourceGroup:ResourceGroup]: Resource monitor is terminating
and that seems to be a lot.
Feels like, it ties to https://github.com/pulumi/pulumi/issues/8236 somehow as I could see it when I tried to catch all the unhandledPromiseRejections in my code.