Created
October 27, 2024 21:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment