import { InstancesClient, ZoneOperationsClient } from "@google-cloud/compute";
type Gcloud = {
type: string;
project_id: string;
private_key_id: string;
private_key: string;
client_email: string;
client_id: string;
auth_uri: string;
token_uri: string;
auth_provider_x509_cert_url: string;
client_x509_cert_url: string;
universe_domain: string;
};
export async function main(
resource: Gcloud,
zone: string,
instanceName: string,
newInstanceStatus: "start" | "stop",
waitCompletion: boolean = false
) {
if (!["start", "stop"].includes(newInstanceStatus)) {
throw new Error("The new VM boot status must be 'start' or 'stop'.");
}
const instancesClient = new InstancesClient({
credentials: resource,
projectId: resource.project_id,
});
const [response] = await instancesClient[newInstanceStatus]({
project: resource.project_id,
zone,
instance: instanceName,
});
let operation: any = response.latestResponse;
if (waitCompletion) {
const operationsClient = new ZoneOperationsClient({
credentials: resource,
projectId: resource.project_id,
});
while (operation.status !== "DONE") {
[operation] = await operationsClient.wait({
operation: operation.name,
project: resource.project_id,
zone: operation.zone.split("/").pop(),
});
}
}
return operation;
}
Submitted by hugo697 692 days ago
import { InstancesClient, ZoneOperationsClient } from "@google-cloud/compute";
type Gcloud = {
type: string;
project_id: string;
private_key_id: string;
private_key: string;
client_email: string;
client_id: string;
auth_uri: string;
token_uri: string;
auth_provider_x509_cert_url: string;
client_x509_cert_url: string;
universe_domain: string;
};
export async function main(
resource: Gcloud,
zone: string,
instanceName: string,
newInstanceStatus: "start" | "stop",
waitCompletion: boolean = false
) {
if (!["start", "stop"].includes(newInstanceStatus)) {
throw new Error("The new VM boot status must be 'start' or 'stop'.");
}
const instancesClient = new InstancesClient({
credentials: resource,
projectId: resource.project_id,
});
const [response] = await instancesClient[newInstanceStatus]({
project: resource.project_id,
zone,
instance: instanceName,
});
let operation: any = response.latestResponse;
if (waitCompletion) {
const operationsClient = new ZoneOperationsClient({
credentials: resource,
projectId: resource.project_id,
});
while (operation.status !== "DONE") {
[operation] = await operationsClient.wait({
operation: operation.name,
project: resource.project_id,
zone: operation.zone.split("/").pop(),
});
}
}
return operation;
}
Submitted by hugo697 693 days ago
import { InstancesClient, ZoneOperationsClient } from '@google-cloud/compute'
type Gcloud = {
projectId: string
privateKey: string
clientEmail: string
}
export async function main(
resource: Gcloud,
zone: string,
instanceName: string,
newInstanceStatus: 'start' | 'stop',
waitCompletion: boolean = false
) {
if (!['start', 'stop'].includes(newInstanceStatus)) {
throw new Error("The new VM boot status must be 'start' or 'stop'.")
}
const credentials = {
client_email: resource.clientEmail,
private_key: resource.privateKey.replace(/\\n/g, '\n')
}
const instancesClient = new InstancesClient({ credentials, projectId: resource.projectId })
const [response] = await instancesClient[newInstanceStatus]({
project: resource.projectId,
zone,
instance: instanceName
})
let operation: any = response.latestResponse
if (waitCompletion) {
const operationsClient = new ZoneOperationsClient({
credentials,
projectId: resource.projectId
})
while (operation.status !== 'DONE') {
;[operation] = await operationsClient.wait({
operation: operation.name,
project: resource.projectId,
zone: operation.zone.split('/').pop()
})
}
}
return operation
}
Submitted by hugo697 693 days ago