Skip to content

Commit

Permalink
Add platform API prefix (#32)
Browse files Browse the repository at this point in the history
* Add platform API prefix

* readme

* readme change
  • Loading branch information
NiraimathiGunalan authored May 21, 2024
1 parent 7398e5f commit 27ddafd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See [action.yml](action.yml)
The following parameters are always required:

- API_HOST - Domain name of the ApiConnect instance where discovered APIs will be sent.<br /> &nbsp; Example : `us-east.apiconnect.automation.ibm.com`
- PLATFORM_API_PREFIX - Platform API prefix has a default value of `platform-api` which is the same as the prefix value for ApiConnect on Cloud. It can be changed to match your systems set up if different from the default
- PROVIDER_ORG - The provider org name of the apiconnect manager
- API_FILES - One or more file names of the APIs to sync with apiconnect discovery repo separated by comma.<br /> &nbsp; Example : `gmail-api.json,gmail-api.yaml,mit-api.json,APIfolder/petstore-exp.json`
- API_FOLDERS - One or more folder names containing APIs to sync with apiconnect discovery repo separated by comma. <br /> &nbsp; Example : `APIFiles,APIFolders`
Expand All @@ -38,6 +39,7 @@ on: [pull_request, workflow_dispatch, push]
env:
API_HOST: <host-name>
PLATFORM_API_PREFIX: <platform-api-prefix>
PROVIDER_ORG: <porg-name>
API_FILES: <file/files name>
Expand All @@ -54,6 +56,7 @@ jobs:
id: discover-apis
with:
api_host: ${{ env.API_HOST }}
platform_api_prefix: ${{ env.PLATFORM_API_PREFIX }}
provider_org: ${{ env.PROVIDER_ORG }}
api_key: ${{ secrets.apicApikey }}
if: env.API_FILES
Expand All @@ -80,6 +83,7 @@ on: [pull_request, workflow_dispatch, push]
env:
API_HOST: <host-name>
PLATFORM_API_PREFIX: <platform-api-prefix>
PROVIDER_ORG: <porg-name>
API_FILES: <file/files name>
Expand Down Expand Up @@ -119,6 +123,7 @@ jobs:
id: discover-apis
with:
api_host: ${{ env.API_HOST }}
platform_api_prefix: ${{ env.PLATFORM_API_PREFIX }}
provider_org: ${{ env.PROVIDER_ORG }}
api_key: ${{ secrets.apicApikey }}
if: env.API_FILES
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ inputs:
api_host:
description: 'Domain name of the ApiConnect instance where discovered APIs will be sent'
required: true
platform_api_prefix:
description: 'Platform API Prefix'
required: false
api_key:
description: 'APIC API key'
required: false
Expand Down
24 changes: 12 additions & 12 deletions discovery-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ const RECORD_API_VERSION = '1.0';
const zip = new AdmZip();
const outputFile = 'multipleAPIfiles.zip';

let createOrUpdateDiscoveredApi = async function(workspacePath, apihost, apikey, porg, apisLocation, dataSourceLocation, dataSourceCheck, isFolder) {
let createOrUpdateDiscoveredApi = async function(workspacePath, apihost, platformApiPrefix, apikey, porg, apisLocation, dataSourceLocation, dataSourceCheck, isFolder) {
if (!apisLocation) {
return { status: 400, message: [ 'Error: create Or Update Discovered Api not run as API files or API folders parameter is missing or Empty' ] };
}
const apisArray = apisLocation.split(',');
const isMultiple = apisArray.length > 1;
let resp; let stateUpdateContent;
let curlUrl = `https://platform-api.${apihost}/discovery/orgs/${porg}/discovered-apis`;
let curlUrl = `https://${platformApiPrefix}.${apihost}/discovery/orgs/${porg}/discovered-apis`;
if (!apikey) {
return { status: 304, message: [ 'Warning: create Or Update Discovered Api not run as apikey is missing' ] };
}
var token = await getAuthToken(apihost, apikey);
var token = await getAuthToken(apihost, platformApiPrefix, apikey);
if (dataSourceCheck) {
await checkAndRegisterDataSource(apihost, token, porg, dataSourceLocation);
await checkAndRegisterDataSource(apihost, platformApiPrefix, token, porg, dataSourceLocation);
}
if (!isFolder && !isMultiple) {
let stats = fs.statSync(path.resolve(apisLocation));
Expand All @@ -45,7 +45,7 @@ let createOrUpdateDiscoveredApi = async function(workspacePath, apihost, apikey,

if (resp.status !== 200 && resp.status !== 201) {
stateUpdateContent = JSON.stringify({ state: 'unhealthy', message: resp.message.message });
datasourceStateUpdate(apihost, stateUpdateContent, token, porg, dataSourceLocation);
datasourceStateUpdate(apihost, platformApiPrefix, stateUpdateContent, token, porg, dataSourceLocation);
}
return resp;

Expand Down Expand Up @@ -130,11 +130,11 @@ let createOrUpdateApiInternal = async function(curlUrl, token, bodyContent, meth
}
};

let datasourceStateUpdate = async function(apihost, bodyContent, token, porg, dataSourceLocation) {
let datasourceStateUpdate = async function(apihost, platformApiPrefix, bodyContent, token, porg, dataSourceLocation) {
let resp;
try {
dataSourceLocation = dataSourceLocation.replaceAll('/', '-');
resp = await axios.patch(`https://platform-api.${apihost}/discovery/orgs/${porg}/data-sources/${dataSourceLocation}`, bodyContent, {
resp = await axios.patch(`https://${platformApiPrefix}.${apihost}/discovery/orgs/${porg}/data-sources/${dataSourceLocation}`, bodyContent, {
headers: {
Authorization: 'Bearer ' + token,
Accept: 'application/json',
Expand All @@ -150,12 +150,12 @@ let datasourceStateUpdate = async function(apihost, bodyContent, token, porg, da
return { status: 500, message: error };
}
};
let checkAndRegisterDataSource = async function(apihost, token, porg, dataSourceLocation) {
let checkAndRegisterDataSource = async function(apihost, platformApiPrefix, token, porg, dataSourceLocation) {
// Use this function to perform the datasource registration. If the dataSource doesn't exist create it
let resp;
try {
dataSourceLocation = dataSourceLocation.replaceAll('/', '-');
resp = await axios.get(`https://platform-api.${apihost}/discovery/orgs/${porg}/data-sources/${dataSourceLocation}`, {
resp = await axios.get(`https://${platformApiPrefix}.${apihost}/discovery/orgs/${porg}/data-sources/${dataSourceLocation}`, {
headers: {
Authorization: 'Bearer ' + token,
Accept: 'application/json',
Expand All @@ -164,7 +164,7 @@ let checkAndRegisterDataSource = async function(apihost, token, porg, dataSource
}).then(response => {
if (response.data.status === 404) {
const bodyContent = JSON.stringify({ title: dataSourceLocation, collector_type: COLLECTOR_TYPE });
resp = axios.post(`https://platform-api.${apihost}/discovery/orgs/${porg}/data-sources`, bodyContent, {
resp = axios.post(`https://${platformApiPrefix}.${apihost}/discovery/orgs/${porg}/data-sources`, bodyContent, {
headers: {
Authorization: 'Bearer ' + token,
Accept: 'application/json',
Expand All @@ -179,10 +179,10 @@ let checkAndRegisterDataSource = async function(apihost, token, porg, dataSource

};

let getAuthToken = async function(apihost, apikey) {
let getAuthToken = async function(apihost, platformApiPrefix, apikey) {

var bodyContent = JSON.stringify({ grant_type: 'api_key', api_key: apikey, realm: 'provider/default-idp-2' });
const token = await axios.post(`https://platform-api.${apihost}/discovery/token`, bodyContent, {
const token = await axios.post(`https://${platformApiPrefix}.${apihost}/discovery/token`, bodyContent, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
Expand Down
32 changes: 16 additions & 16 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run() {
const datasourceCheck = core.getInput('resync_check');
const apisLocation = core.getInput('api_files') || core.getInput('api_folders');
const filesChanged = core.getInput('git_diff');

const platformApiPrefix = core.getInput('platform_api_prefix') ? core.getInput('platform_api_prefix') : 'platform-api';
if (core.getInput('api_files')) {
isFolder = false;
} else if (core.getInput('api_folders')) {
Expand All @@ -36,7 +36,7 @@ async function run() {
}
}
if (checkChanges) {
await execution(apihost, porg, isFolder, apisLocation, datasourceCheck, workspacePath, apikey, githubServer, repoLocation);
await execution(apihost, platformApiPrefix, porg, isFolder, apisLocation, datasourceCheck, workspacePath, apikey, githubServer, repoLocation);
} else {
core.setOutput('action-result', 'No files changed from the previous commit to send to Discovery Service');
}
Expand All @@ -48,14 +48,14 @@ async function run() {
}
}

async function execution(apihost, porg, isFolder, apisLocation, datasourceCheck, workspacePath, apikey, githubServer, repoLocation) {
async function execution(apihost, platformApiPrefix, porg, isFolder, apisLocation, datasourceCheck, workspacePath, apikey, githubServer, repoLocation) {
try {
core.info(`apihost ${apihost}`);
core.info(`porg ${porg}`);
isFolder && core.info(`apifolders ${apisLocation}`) || core.info(`apifiles ${apisLocation}`);
core.info(`datasourceCheck ${datasourceCheck}`);

var resp = await createOrUpdateDiscoveredApi(workspacePath, apihost, apikey, porg, apisLocation, githubServer + '/' + repoLocation, datasourceCheck, isFolder);
var resp = await createOrUpdateDiscoveredApi(workspacePath, apihost, platformApiPrefix, apikey, porg, apisLocation, githubServer + '/' + repoLocation, datasourceCheck, isFolder);
core.info(`response: status: ${resp.status}, message: ${resp.message[0]}`);

core.setOutput('action-result', `response: status: ${resp.status}, message: ${resp.message[0]}`);
Expand Down
4 changes: 2 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ test('test runs', () => {
process.env['INPUT_API_HOST'] = 'd-h01.apiconnect.dev.automation.ibm.com';
process.env['INPUT_API_KEY'] = '';
process.env['INPUT_PROVIDER_ORG'] = 'ruairi_h01_b';
// process.env['INPUT_GIT_DIFF'] = 'APIfolder/gmail-api.json mit-api.json new-api.yaml';
process.env['INPUT_GIT_DIFF'] = 'APIfolder/gmail-api.json mit-api.json new-api.yaml';

process.env['INPUT_API_FILES'] = [ 'APIfolder/gmail-api.json', 'APIfiles/mit-api.json' ];
// process.env['INPUT_API_FILES'] = [ 'gmail-api-2.json' ];
// process.env['INPUT_API_FOLDERS'] = [ 'APIfiles' ];
// process.env['INPUT_API_FOLDERS'] = [ 'APIfiles', 'APIfolder' ];
process.env['INPUT_RESYNC_CHECK'] = true;

process.env['INPUT_PLATFORM_API_PREFIX'] = 'api';
const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
console.log(result);
Expand Down

0 comments on commit 27ddafd

Please sign in to comment.