Skip to content

Instantly share code, notes, and snippets.

@stack72
Created October 27, 2024 21:16
Show Gist options
  • Save stack72/cbf973d3b136525c2d14e7a2f5c1bda7 to your computer and use it in GitHub Desktop.
Save stack72/cbf973d3b136525c2d14e7a2f5c1bda7 to your computer and use it in GitHub Desktop.
Examples of Functions as of Octover 27th 2024 - CRUD are actions, import is a new func kind called management
async function main(component: Input): Promise < Output > {
if (component.properties.resource?.payload) {
return {
status: "error",
message: "Resource already exists",
payload: component.properties.resource.payload,
};
}
const domain = component.properties.domain;
const params = [
"group",
"create",
"--name",
domain.Name,
"--location",
domain.Location
];
const tagArgs = Object.entries(domain.tags).map(([key, value]) => `${key}=${value}`);
params.push("--tags")
params.push(...tagArgs)
const child = await siExec.waitUntilEnd("az", params);
if (child.exitCode !== 0) {
console.error(child.stderr);
return {
status: "error",
message: `Unable to create; Azure CLI exited with non zero code: ${child.exitCode}`,
};
}
const response = JSON.parse(child.stdout);
return {
resourceId: response.name,
status: "ok",
};
}
async function main(component: Input): Promise < Output > {
let name = component.properties?.si?.resourceId;
const resource = component.properties.resource?.payload;
if (!name) {
name = resource.name;
}
if (!name) {
return {
status: component.properties.resource?.status ?? "error",
message: "Could not delete, no resourceId present for Resource Group component",
};
}
const child = await siExec.waitUntilEnd("az", [
"group",
"delete",
"--name",
name,
"-y",
"--no-wait"
]);
if (child.exitCode !== 0) {
console.error(child.stderr);
return {
status: "error",
payload: resource,
message: `Unable to delete Resource Group, AWS CLI 2 exited with non zero code: ${child.exitCode}`,
};
}
return {
payload: null,
status: "ok"
};
};
async function main({
thisComponent
}: Input): Promise < Output > {
const component = thisComponent.properties;
let name = _.get(component, ["si", "resourceId"]);
if (!name) {
name = _.get(component, ["resource", "payload", "name"]);
}
if (!name) {
return {
status: "error",
message: "No resourceId set, cannot import Resource Group"
}
}
const child = await siExec.waitUntilEnd("az", [
"group",
"show",
"--name",
name,
]);
if (child.exitCode !== 0) {
return {
status: "error",
message: `AZ CLI "az group show" returned non zero exit code(${ child.exitCode })`,
};
}
const resourceGroup = JSON.parse(child.stdout)
component["domain"].Location = resourceGroup.location;
component["domain"].Name = resourceGroup.name;
if (resourceGroup.tags.Name) {
component["si"]["name"] = resourceGroup.tags.Name;
const {
Name,
...otherTags
} = resourceGroup.tags; // Destructure to separate Name from other tags
component["domain"]["tags"] = otherTags;
} else {
component["domain"]["tags"] = resourceGroup.tags;
}
return {
status: "ok",
message: JSON.stringify(resourceGroup),
ops: {
update: {
self: {
properties: {
...component,
}
}
},
actions: {
self: {
remove: ["create"],
add: ["refresh"],
}
}
}
}
}
async function main(component: Input): Promise < Output > {
let name = component.properties?.si?.resourceId;
const resource = component.properties.resource?.payload;
if (!name) {
name = resource.name;
}
if (!name) {
return {
status: component.properties.resource?.status ?? "error",
message: "Could not refresh, no resourceId present for Resource Group component",
};
}
const child = await siExec.waitUntilEnd("az", [
"group",
"show",
"--name",
name,
]);
if (child.exitCode !== 0) {
console.log(child.stderr)
return {
payload: resource,
status: "error",
message: `Azure CLI "az group show" returned non zero exit code (${child.exitCode})`,
};
}
const object = JSON.parse(child.stdout);
return {
payload: object,
status: "ok"
};
}
async function main(component: Input): Promise < Output > {
const resource = component.properties.resource?.payload;
if (!resource) {
return {
status: component.properties.resource?.status ?? "ok",
message: component.properties.resource?.message,
};
}
const domain = component.properties.domain;
const params = [
"group",
"update",
"--name",
resource.name,
];
const tagArgs = Object.entries(domain.tags).map(([key, value]) => `${key}=${value}`);
params.push("--tags")
params.push(...tagArgs)
const child = await siExec.waitUntilEnd("az", params);
if (child.exitCode !== 0) {
console.error(child.stderr);
return {
status: "error",
message: `Unable to update; Azure CLI exited with non zero code: ${child.exitCode}`,
};
}
return {
payload: resource,
status: "ok",
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment