Skip to content

Commit 0d76794

Browse files
authored
iam-ibm example snippet (#81)
* iam-ibm example snippet * chore: cleanup * chore: prettier
1 parent 14b26f6 commit 0d76794

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

rest/ibm-iam/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# IBM IAM Token
2+
3+
## Overview
4+
5+
Defines a field `Query.ibm_iam_token` that obtains an IBM Cloud IAM token.
6+
7+
This can be used in as the first step of a sequence to obtain a token for a subsequent execution
8+
of an `@rest` field in the sequence.
9+
10+
An example (`Query.usage`) is provided of accessing the IBM Cloud billing usage endpoint using
11+
a `@sequence` to fetch a token and then make the REST request.
12+
13+
The same technique can be used with any IAM system.
14+
15+
## Try it out
16+
17+
The schema must be deployed with the environment variable `STEPZEN_IBM_IAM_APIKEY` set
18+
to an IBM Cloud API key, typically by a CI/CD workflow.
19+
20+
Then a request such as this can be executed, replacing `<<account ID>>` with an IBM Cloud account ID
21+
that matches the IBM Cloud API key:
22+
23+
```
24+
stepzen request '{usage(account:"<<account ID>>" month:"2025-01") }'
25+
```

rest/ibm-iam/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
configurationset:
2+
- configuration:
3+
endpoint: https://iam.cloud.ibm.com/identity/token
4+
apikey: STEPZEN_IBM_IAM_APIKEY
5+
name: ibm-iam

rest/ibm-iam/ibm-iam.graphql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
extend type Query {
2+
"""
3+
Obtain an IBM Cloud bearer token.
4+
5+
Uses the [IBM Cloud API Key](https://cloud.ibm.com/docs/account?topic=account-userapikey&interface=ui#userapikey)
6+
or [service ID's API Key](https://cloud.ibm.com/docs/account?topic=account-serviceidapikeys&interface=ui)
7+
to [generate an IAM Token](https://cloud.ibm.com/docs/account?topic=account-iamtoken_from_apikey#iamtoken_from_apikey)
8+
"""
9+
ibm_iam_token: Secret
10+
@rest(
11+
endpoint: "$endpoint"
12+
method: POST
13+
contenttype: "x-www-form-urlencoded"
14+
postbody: """
15+
grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey={{ .Get "apikey"}}
16+
"""
17+
ecmascript: """
18+
function transformREST(body) {
19+
switch (status) {
20+
case 200:
21+
return body
22+
case 401:
23+
case 400: // returned for apikey not found
24+
throw new Error('unauthorized');
25+
default:
26+
throw new Error('unknown error');
27+
}
28+
}
29+
"""
30+
setters: { path: "access_token" }
31+
configuration: "ibm-iam"
32+
)
33+
}

rest/ibm-iam/index.graphql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
schema @sdl(files: ["ibm-iam.graphql"]) {
2+
query: Query
3+
}
4+
5+
"""
6+
Example use of `Query.ibm_iam_token`.
7+
8+
First fetches a token and then executes a REST request using the token.
9+
"""
10+
type Query {
11+
"""
12+
IBM Cloud account usage.
13+
"""
14+
usage(
15+
"""
16+
IBM Cloud account ID.
17+
"""
18+
account: String!
19+
"""
20+
Billing month with format `yyyy-mm`
21+
"""
22+
month: String!
23+
): JSON
24+
@sequence(
25+
steps: [
26+
{ query: "ibm_iam_token" }
27+
{
28+
query: "_usage"
29+
arguments: [
30+
{ name: "token", field: "§0" }
31+
{ name: "account", argument: "account" }
32+
{ name: "month", argument: "month" }
33+
]
34+
}
35+
]
36+
)
37+
38+
"""
39+
Fetch requesst against the usage endpoint.
40+
41+
https://cloud.ibm.com/apidocs/metering-reporting#get-account-usage
42+
"""
43+
_usage(token: Secret!, account: String!, month: String!): JSON
44+
@rest(
45+
endpoint: "https://billing.cloud.ibm.com/v4/accounts/$account/usage/$month"
46+
headers: { name: "Authorization", value: "Bearer $token" }
47+
)
48+
}

rest/ibm-iam/stepzen.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"endpoint": "api/miscellaneous"
3+
}

rest/ibm-iam/tests/Test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {
2+
deployAndRun,
3+
getTestDescription,
4+
} = require("../../../tests/gqltest.js");
5+
6+
testDescription = getTestDescription("snippets", __dirname);
7+
8+
describe(testDescription, function () {
9+
const tests = [];
10+
return deployAndRun(__dirname, tests);
11+
});

0 commit comments

Comments
 (0)