File tree Expand file tree Collapse file tree 6 files changed +125
-0
lines changed
Expand file tree Collapse file tree 6 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 1+ configurationset :
2+ - configuration :
3+ endpoint : https://iam.cloud.ibm.com/identity/token
4+ apikey : STEPZEN_IBM_IAM_APIKEY
5+ name : ibm-iam
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ {
2+ "endpoint" : " api/miscellaneous"
3+ }
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments