|
| 1 | +import axios from "axios"; |
| 2 | +import { getAccessToken } from "./auth.js"; |
| 3 | + |
| 4 | +const authorizationConfiguration = { |
| 5 | + clientId: process.env.ZEEBE_CLIENT_ID, |
| 6 | + clientSecret: process.env.ZEEBE_CLIENT_SECRET, |
| 7 | + audience: process.env.ZEEBE_AUDIENCE |
| 8 | +}; |
| 9 | + |
| 10 | +// An action that lists all roles. |
| 11 | +async function listRoles() { |
| 12 | + // Every request needs an access token. |
| 13 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 14 | + |
| 15 | + // These settings come from your .env file. |
| 16 | + const camundaApiUrl = process.env.ZEEBE_BASE_URL; |
| 17 | + |
| 18 | + // This is the API endpoint to query roles. |
| 19 | + const url = `${camundaApiUrl}/roles/search`; |
| 20 | + |
| 21 | + // Configure the API call. |
| 22 | + const options = { |
| 23 | + method: "POST", |
| 24 | + url, |
| 25 | + headers: { |
| 26 | + Accept: "application/json", |
| 27 | + Authorization: `Bearer ${accessToken}` |
| 28 | + }, |
| 29 | + // No filtering/paging/sorting, we want all roles. |
| 30 | + data: {} |
| 31 | + }; |
| 32 | + |
| 33 | + try { |
| 34 | + // Call the endpoint. |
| 35 | + const response = await axios(options); |
| 36 | + |
| 37 | + // Process the results from the API call. |
| 38 | + const results = response.data; |
| 39 | + |
| 40 | + // Emit roles to output. |
| 41 | + results.items.forEach(x => |
| 42 | + console.log(`Role Name: ${x.name}; key: ${x.key}`) |
| 43 | + ); |
| 44 | + } catch (error) { |
| 45 | + // Emit an error from the server. |
| 46 | + console.error(error.message); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// An action that creates a role. |
| 51 | +async function createRole([roleName]) { |
| 52 | + // Every request needs an access token. |
| 53 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 54 | + |
| 55 | + // These settings come from your .env file. |
| 56 | + const camundaApiUrl = process.env.ZEEBE_BASE_URL; |
| 57 | + |
| 58 | + // This is the API endpoint to add a new client to a cluster. |
| 59 | + const url = `${camundaApiUrl}/roles`; |
| 60 | + |
| 61 | + // Configure the API call. |
| 62 | + const options = { |
| 63 | + method: "POST", |
| 64 | + url, |
| 65 | + headers: { |
| 66 | + Accept: "application/json", |
| 67 | + Authorization: `Bearer ${accessToken}` |
| 68 | + }, |
| 69 | + data: { |
| 70 | + // The body contains information about the new role. |
| 71 | + name: roleName |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + try { |
| 76 | + const response = await axios(options); |
| 77 | + |
| 78 | + // Process the results from the API call. |
| 79 | + const newRole = response.data; |
| 80 | + |
| 81 | + // Emit new role to output. |
| 82 | + console.log(`Role added! Name: ${roleName}. Key: ${newRole.roleKey}.`); |
| 83 | + } catch (error) { |
| 84 | + // Emit an error from the server. |
| 85 | + console.error(error.message); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// An action that retrieves a role. |
| 90 | +async function getRole([roleKey]) { |
| 91 | + // Every request needs an access token. |
| 92 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 93 | + |
| 94 | + // These settings come from your .env file. |
| 95 | + const camundaApiUrl = process.env.ZEEBE_BASE_URL; |
| 96 | + |
| 97 | + // This is the API endpoint to get a specific role. |
| 98 | + const url = `${camundaApiUrl}/roles/${roleKey}`; |
| 99 | + |
| 100 | + // Configure the API call. |
| 101 | + const options = { |
| 102 | + method: "GET", |
| 103 | + url, |
| 104 | + headers: { |
| 105 | + Accept: "application/json", |
| 106 | + Authorization: `Bearer ${accessToken}` |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + try { |
| 111 | + // Call the endpoint. |
| 112 | + const response = await axios(options); |
| 113 | + |
| 114 | + // Process the results from the API call. |
| 115 | + const results = response.data; |
| 116 | + |
| 117 | + // Emit role to output. |
| 118 | + console.log( |
| 119 | + `Role Name: ${results.name}; Key: ${ |
| 120 | + results.key |
| 121 | + }; Members: ${JSON.stringify(results.assignedMemberKeys)}` |
| 122 | + ); |
| 123 | + } catch (error) { |
| 124 | + // Emit an error from the server. |
| 125 | + console.error(error.message); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// An action that deletes a role. |
| 130 | +async function deleteRole([roleKey]) { |
| 131 | + const accessToken = await getAccessToken(authorizationConfiguration); |
| 132 | + |
| 133 | + const camundaApiUrl = process.env.ZEEBE_BASE_URL; |
| 134 | + |
| 135 | + const url = `${camundaApiUrl}/roles/${roleKey}`; |
| 136 | + |
| 137 | + // Configure the API call. |
| 138 | + const options = { |
| 139 | + method: "DELETE", |
| 140 | + url, |
| 141 | + headers: { |
| 142 | + Accept: "application/json", |
| 143 | + Authorization: `Bearer ${accessToken}` |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + try { |
| 148 | + // Call the delete endpoint. |
| 149 | + const response = await axios(options); |
| 150 | + |
| 151 | + // Process the results from the API call. |
| 152 | + if (response.status === 204) { |
| 153 | + console.log("Role deleted!"); |
| 154 | + } else { |
| 155 | + // Emit an unexpected error message. |
| 156 | + console.error("Unable to delete this role!"); |
| 157 | + } |
| 158 | + } catch (error) { |
| 159 | + // Emit an error from the server. |
| 160 | + console.error(error.message); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// These functions are aliased to specific command names for terseness. |
| 165 | +// The name of each property translates to a method that can be called by the CLI. |
| 166 | +// e.g. if we export a function named `list`, you can run `npm run cli zeebe get`. |
| 167 | + |
| 168 | +export default { |
| 169 | + list: listRoles, |
| 170 | + create: createRole, |
| 171 | + view: getRole, |
| 172 | + delete: deleteRole |
| 173 | +}; |
0 commit comments