Skip to content

Commit 7ffaafd

Browse files
authored
chore: service accounts APIs (#305)
* fix: service accounts APIs * fix: add error codes
1 parent e5f85dc commit 7ffaafd

34 files changed

+2147
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ You can view all SDK packages directly in [npm](https://www.npmjs.com/search?q=k
2626
| Name | Package name | Status | Version | NPM | Docs |
2727
| ---------------- | :---------------------------------- | :----- | ------------------ | ------------------- | ------------------- |
2828
| kafka | **@rhoas/kafka-management-sdk** | beta | ![kafkashield] | [npm][kafkanpm] | [doc][kafkagit] |
29+
| service-accounts | **@rhoas/service-accounts-sdk** | alpha | ![sashield] | [npm][sanpm] | [doc][sagit]
2930
| connector | **@rhoas/connector-management-sdk** | alpha | ![connectorshield] | [npm][connectornpm] | [doc][connectorgit] |
3031
| service registry | **@rhoas/registry-management-sdk** | alpha | ![registryshield] | [npm][registrynpm] | [doc][registrygit] |
3132
| service accounts | **@rhoas/kafka-management-sdk** | alpha | ![kafkashield] | [npm][kafkanpm] | [doc][kafkagit] |
@@ -81,6 +82,9 @@ asapi
8182
[kafkagit]: https://github.com/redhat-developer/app-services-sdk-js/tree/main/packages/kafka-management-sdk
8283
[kafkanpm]: https://www.npmjs.com/package/@rhoas/kafka-management-sdk
8384
[kafkashield]: https://img.shields.io/npm/v/@rhoas/kafka-management-sdk
85+
[sagit]: https://github.com/redhat-developer/app-services-sdk-js/tree/main/packages/service-accounts-sdk
86+
[sanpm]: https://www.npmjs.com/package/@rhoas/service-accounts-sdk
87+
[sashield]: https://img.shields.io/npm/v/@rhoas/service-accounts-sdk
8488
[kinstancegit]: https://github.com/redhat-developer/app-services-sdk-js/tree/main/packages/kafka-instance-sdk
8589
[kinstancenpm]: https://www.npmjs.com/package/@rhoas/kafka-instance-sdk
8690
[kinstanceshield]: https://img.shields.io/npm/v/@rhoas/kafka-instance-sdk

examples/src/serviceAccount.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Configuration, SecurityApi, APIErrorCodes } from "../../packages/kafka-management-sdk/dist";
1+
import { Configuration, APIErrorCodes, ServiceAccountsApi } from "../../packages/service-accounts-sdk/dist";
22

33
const accessToken = process.env.CLOUD_API_TOKEN;
44
const basePath = "https://api.openshift.com";
@@ -8,12 +8,11 @@ const apiConfig = new Configuration({
88
basePath
99
})
1010

11-
const serviceAccountAPI = new SecurityApi(apiConfig)
11+
const serviceAccountAPI = new ServiceAccountsApi(apiConfig)
1212

1313
serviceAccountAPI.getServiceAccounts().then((data) => {
14-
console.log(data?.data.items)
14+
console.log(data?.data)
1515
}).catch((err) => {
1616
console.error(err.message)
17-
console.error("Service account fail error", err.response?.data.code == APIErrorCodes.ERROR_111)
1817
})
1918

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# empty npmignore to ensure all required files (e.g., in the dist folder) are published by npm
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Service Accounts SDK
2+
3+
RHOAS Service Accounts TypeScript/JavaScript SDK.
4+
5+
Service accounts are a special type of non-human privileged account used to execute applications and run automated services. Service accounts typically will allow you to connect to the services from your applications.
6+
7+
> NOTE: Creation of the service account doesn't automatically gives developers priviledge to
8+
use it with the service. Please refer to individual service API to see how to add permissions.
9+
For example for kafka-instance-sdk check "access-management" APIs.
10+
11+
### Getting Started
12+
13+
#### Install Package
14+
15+
```
16+
npm install @rhoas/service-accounts-sdk --save
17+
```
18+
19+
#### Usage
20+
21+
```ts
22+
import { Configuration, DefaultApi} from "@rhoas/service-accounts-sdk";
23+
24+
const accessToken = process.env.CLOUD_API_TOKEN;
25+
const basePath = "https://api.openshift.com";
26+
27+
const apiConfig = new Configuration({
28+
accessToken,
29+
basePath,
30+
});
31+
32+
const serviceAccountAPI = new ServiceAccountsApi(apiConfig)
33+
34+
serviceAccountAPI.getServiceAccounts().then((data) => {
35+
console.log(data?.data)
36+
}).catch((err) => {
37+
console.error(err.message)
38+
})
39+
```
40+
41+
See [./examples](https://github.com/redhat-developer/app-services-sdk-js/tree/main/examples) for full example
42+
43+
#### Security
44+
45+
API is using https://sso.redhat.com for OAuth Authentication.
46+
Provided token needs to be AccessToken returned from library like keycloak.js
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@rhoas/service-accounts-sdk",
3+
"version": "0.0.0-dev",
4+
"description": "RHOAS Service Accounts SDK",
5+
"author": "Red Hat Developers",
6+
"keywords": [
7+
"rhoas",
8+
"kafka",
9+
"redhat",
10+
"@rhoas/registry-management-sdk"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/redhat-developer/app-services-sdk-js"
15+
},
16+
"license": "Apache-2.0",
17+
"main": "./dist/index.js",
18+
"typings": "./dist/index.d.ts",
19+
"publishConfig": {
20+
"access": "public"
21+
},
22+
"scripts": {
23+
"build": "tsc --outDir dist/",
24+
"prepublishOnly": "npm run build"
25+
},
26+
"dependencies": {
27+
"axios": "0.26.0"
28+
},
29+
"devDependencies": {
30+
"@types/node": "16.11.26",
31+
"typescript": "4.6.2"
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AxiosError } from "axios";
2+
import { ModelError } from "./generated";
3+
4+
/**
5+
* Check if the error code originates from the API
6+
*
7+
* @param error generic error returned from fumction
8+
* @returns true if error originated from the API
9+
*/
10+
export const isServiceApiError = (error: unknown): error is AxiosError<ModelError> => {
11+
return (error as AxiosError<ModelError>).response?.data.error !== undefined;
12+
};
13+
14+
/**
15+
* Get the error code from the API error
16+
*
17+
* @param error generic error returned from fumction
18+
* @returns error code (one of fields of APIErrorCodes)
19+
*/
20+
export const getErrorCode = (error: unknown): string | undefined => {
21+
return (error as AxiosError<ModelError>).response?.data?.error;
22+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
/**
3+
srs error codes
4+
5+
```ts
6+
apiCall.then((data) => {
7+
console.log(data?.data.items)
8+
}).catch((err) => {
9+
if(APIErrorCodes.ERROR_5 == err.response?.data.code) {
10+
// Handle error
11+
}
12+
})
13+
```
14+
*/
15+
export const APIErrorCodes = {
16+
// TODO - Team to define specific error codes to handle
17+
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
/**
4+
* Service Accounts API Documentation
5+
* This is the API documentation for Service Accounts
6+
*
7+
* The version of the OpenAPI document: 1.0
8+
*
9+
*
10+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11+
* https://openapi-generator.tech
12+
* Do not edit the class manually.
13+
*/
14+
15+
16+
17+
export * from './api/service-accounts-api';
18+
export * from './api/serviceaccounts-api';
19+

0 commit comments

Comments
 (0)