Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api/manage-instances/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Programmatically Manage Instances

This script demonstrates how to programmatically query for all active instances through Prismatic's GraphQL API, and delete or disable them.
Documentation for the API can be found at [prismatic.io/docs](https://prismatic.io/docs/api/api-overview/).

`index.ts` creates an authenticated GraphQL client and uses that client to make requests to the Prismatic API.
Files in the `queries/` directory wrap various GraphQL queries and mutations to do things like list instances or disable or delete instances.

## Running this script

To run this script, you will need to supply an API key and an action you would like to perform on an instance, and the ID or name of the instance.
Those values can be shared as environment variables `PRISMATIC_API_KEY`,`ACTION`,and `IDENTIFIER` respectively.
If you have the [prism CLI tool](https://www.npmjs.com/package/@prismatic-io/prism) installed, you can use it to fetch a valid token:

```bash
PRISMATIC_API_KEY=$(prism me:token) ACTION=disable IDENTIFIER=SW5zdGFuY2U6OT... npm run start
```

## Extending this script

This script is meant to be used as an example to illustrate how to programmatically list instances, and disable or delete instances.
You can extend this script's logic however you like.
For example, there may only be a set of instances you want to disable, each of those instances have a specific label you can query for.
Or, you may want to loop through all instances and disable specific ones by a particular Id.
Use this script as a jumping-off point to build your own logic to manage your instances.
58 changes: 58 additions & 0 deletions api/manage-instances/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { GraphQLClient } = require("graphql-request");
const {
GET_INSTANCES,
DISABLE_INSTANCE,
DELETE_INSTANCE,
} = require("./queries");

const PRISMATIC_API_KEY = process.env.PRISMATIC_API_KEY;

if (!PRISMATIC_API_KEY) {
throw new Error("You must set a PRISMATIC_API_KEY environment variable.");
}

const identifier = process.env.IDENTIFIER;
const action = process.env.ACTION;

const API_ENDPOINT = process.env.PRISMATIC_URL
? `${process.env.PRISMATIC_URL}/api`
: "https://app.prismatic.io/api";

const client = new GraphQLClient(API_ENDPOINT, {
headers: {
Authorization: `Bearer ${PRISMATIC_API_KEY}`,
},
});

async function manageInstances(identifier, action) {
try {
/** Get a list of all enabled instances */
const instancesList = await client.request(GET_INSTANCES);

const instances = instancesList.instances.nodes;

/** Loop through all nodes and find the instance Id or name that matches the identifier */
const instance = instances.find(
(i) => i.id === identifier || i.name === identifier
);
if (!instance) {
console.log(`No instance found with ID or name "${identifier}".`);
return;
}

/** Disable or Delete the found instance depending on the action */
if (action === "disable") {
const res = await client.request(DISABLE_INSTANCE, { id: instance.id });
console.log(`Disabled: ${res.updateInstance.instance.name}`);
} else if (action === "delete") {
const res = await client.request(DELETE_INSTANCE, { id: instance.id });
console.log(`🗑️ Deleted: ${res.deleteInstance.instance.name}`);
} else {
console.log('Unknown action. Use "disable" or "delete".');
}
} catch (err) {
console.error("GraphQL error:", err.message);
}
}

manageInstances(identifier, action.toLowerCase());
249 changes: 249 additions & 0 deletions api/manage-instances/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions api/manage-instances/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Manage Instances",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "ts-node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"graphql-request": "^7.2.0",
"ts-node": "^10.9.2"
}
}
Loading