Skip to content

Commit

Permalink
DEVEXP-411: Add suport for Access Control List
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch committed Apr 26, 2024
1 parent ec111e8 commit 1f85c89
Show file tree
Hide file tree
Showing 29 changed files with 1,420 additions and 12 deletions.
34 changes: 23 additions & 11 deletions examples/simple-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ FAX_CALLBACK_URL=callback url to override the one defined in the default service
FAX_EMAIL=email to associate with a phone number to use the fax-to-email functionality
## Elastic SIP Trunking API
SIP_TRUNK_ID=sipTrunkId to fill with one of the SIP trunk created with the Elastic SIP Trunking API
ACL_ID=accessControlList Id to fill with one of the access control list created with the Elastic SIP Trunking API
ACL_ID=accessControlListId to fill with one of the access control list created with the Elastic SIP Trunking API
IP_RANGE_ID=ipRangeId to fill with one of the IP range added to an ACL with the Elastic SIP Trunking API
```

**Note**: If you prefer using environment variables, the sample app is also supporting them: they take precedence over the value from the `.env` file.
Expand Down Expand Up @@ -275,13 +276,24 @@ yarn run numbers:regions:list

### Elastic SIP Trunk

| Service | Sample application name and location | Required parameters |
|------------|----------------------------------------------------------------------------------------------------------|----------------------|
| SIP Trunks | [./src/elastic-sip-trunking/sip-trunks/create.ts](./src/elastic-sip-trunking/sip-trunks/create.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/get.ts](./src/elastic-sip-trunking/sip-trunks/get.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/list.ts](./src/elastic-sip-trunking/sip-trunks/list.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/update.ts](./src/elastic-sip-trunking/sip-trunks/update.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/delete.ts](./src/elastic-sip-trunking/sip-trunks/delete.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/addACL.ts](./src/elastic-sip-trunking/sip-trunks/addACL.ts) | SIP_TRUNK_ID, ACL_ID |
| | [./src/elastic-sip-trunking/sip-trunks/listACLs.ts](./src/elastic-sip-trunking/sip-trunks/listACLs.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/deleteACL.ts](./src/elastic-sip-trunking/sip-trunks/deleteACL.ts) | SIP_TRUNK_ID, ACL_ID |
| Service | Sample application name and location | Required parameters |
|---------------------|----------------------------------------------------------------------------------------------------------------------------------------|----------------------|
| SIP Trunks | [./src/elastic-sip-trunking/sip-trunks/create.ts](./src/elastic-sip-trunking/sip-trunks/create.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/get.ts](./src/elastic-sip-trunking/sip-trunks/get.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/list.ts](./src/elastic-sip-trunking/sip-trunks/list.ts) | |
| | [./src/elastic-sip-trunking/sip-trunks/update.ts](./src/elastic-sip-trunking/sip-trunks/update.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/delete.ts](./src/elastic-sip-trunking/sip-trunks/delete.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/addACL.ts](./src/elastic-sip-trunking/sip-trunks/addACL.ts) | SIP_TRUNK_ID, ACL_ID |
| | [./src/elastic-sip-trunking/sip-trunks/listACLs.ts](./src/elastic-sip-trunking/sip-trunks/listACLs.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/sip-trunks/deleteACL.ts](./src/elastic-sip-trunking/sip-trunks/deleteACL.ts) | SIP_TRUNK_ID, ACL_ID |
| Access Control List | [./src/elastic-sip-trunking/access-control-list/create.ts](./src/elastic-sip-trunking/access-control-list/create.ts) | |
| | [./src/elastic-sip-trunking/access-control-list/list.ts](./src/elastic-sip-trunking/access-control-list/list.ts) | |
| | [./src/elastic-sip-trunking/access-control-list/update.ts](./src/elastic-sip-trunking/access-control-list/update.ts) | ACL_ID |
| | [./src/elastic-sip-trunking/access-control-list/delete.ts](./src/elastic-sip-trunking/access-control-list/delete.ts) | ACL_ID |
| | [./src/elastic-sip-trunking/access-control-list/addIpRange.ts](./src/elastic-sip-trunking/access-control-list/addIpRange.ts) | ACL_ID |
| | [./src/elastic-sip-trunking/access-control-list/listIpRanges.ts](./src/elastic-sip-trunking/access-control-list/listIpRanges.ts) | ACL_ID |
| | [./src/elastic-sip-trunking/access-control-list/updateIpRange.ts](./src/elastic-sip-trunking/access-control-list/updateIpRange.ts) | ACL_ID, IP_RANGE_ID |
| | [./src/elastic-sip-trunking/access-control-list/deleteIpRange.ts](./src/elastic-sip-trunking/access-control-list/deleteIpRange.ts) | ACL_ID, IP_RANGE_ID |
| | [./src/elastic-sip-trunking/access-control-list/addToTrunk.ts](./src/elastic-sip-trunking/access-control-list/addToTrunk.ts) | SIP_TRUNK_ID, ACL_ID |
| | [./src/elastic-sip-trunking/access-control-list/listForTrunk.ts](./src/elastic-sip-trunking/access-control-list/listForTrunk.ts) | SIP_TRUNK_ID |
| | [./src/elastic-sip-trunking/access-control-list/deleteFromTrunk.ts](./src/elastic-sip-trunking/access-control-list/deleteFromTrunk.ts) | SIP_TRUNK_ID, ACL_ID |
11 changes: 11 additions & 0 deletions examples/simple-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@
"elasticSipTrunks:sipTrunk:addACL": "ts-node src/elastic-sip-trunking/sip-trunks/addACL.ts",
"elasticSipTrunks:sipTrunk:listACLs": "ts-node src/elastic-sip-trunking/sip-trunks/listACLs.ts",
"elasticSipTrunks:sipTrunk:deleteACL": "ts-node src/elastic-sip-trunking/sip-trunks/deleteACL.ts",
"elasticSipTrunks:accessControlList:create": "ts-node src/elastic-sip-trunking/access-control-list/create.ts",
"elasticSipTrunks:accessControlList:list": "ts-node src/elastic-sip-trunking/access-control-list/list.ts",
"elasticSipTrunks:accessControlList:update": "ts-node src/elastic-sip-trunking/access-control-list/update.ts",
"elasticSipTrunks:accessControlList:delete": "ts-node src/elastic-sip-trunking/access-control-list/delete.ts",
"elasticSipTrunks:accessControlList:addIpRange": "ts-node src/elastic-sip-trunking/access-control-list/addIpRange.ts",
"elasticSipTrunks:accessControlList:listIpRanges": "ts-node src/elastic-sip-trunking/access-control-list/listIpRanges.ts",
"elasticSipTrunks:accessControlList:updateIpRange": "ts-node src/elastic-sip-trunking/access-control-list/updateIpRange.ts",
"elasticSipTrunks:accessControlList:deleteIpRange": "ts-node src/elastic-sip-trunking/access-control-list/deleteIpRange.ts",
"elasticSipTrunks:accessControlList:addToTrunk": "ts-node src/elastic-sip-trunking/access-control-list/addToTrunk.ts",
"elasticSipTrunks:accessControlList:listForTrunk": "ts-node src/elastic-sip-trunking/access-control-list/listForTrunk.ts",
"elasticSipTrunks:accessControlList:deleteFromTrunk": "ts-node src/elastic-sip-trunking/access-control-list/deleteFromTrunk.ts",
"fax:services:create": "ts-node src/fax/services/create.ts",
"fax:services:get": "ts-node src/fax/services/get.ts",
"fax:services:list": "ts-node src/fax/services/list.ts",
Expand Down
4 changes: 4 additions & 0 deletions examples/simple-examples/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export const getAccessControlListIdFromConfig = () => {
return readVariable('ACL_ID');
};

export const getIpRangeIdFromConfig = () => {
return readVariable('IP_RANGE_ID');
};

const readVariable = ( name: string): string => {
const value = process.env[name];
if (!value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getPrintFormat,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';

(async () => {
console.log('*******************************');
console.log('* addAccessControlListToTrunk *');
console.log('*******************************');

const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.AddIpRangeToAccessControlListRequestData = {
accessControlListId: aclId,
addIpRangeRequestBody: {
description: 'IP range to add with the Node.js SDK',
ipAddress: '11.12.13.14',
range: 27,
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.accessControlList.addIpRange(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The IP range with the ID ${response.id} associated to the ACL '${response.accessControlListId}' has been created at ${response.createTime?.toISOString()}`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getPrintFormat,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
printFullResponse,
} from '../../config';

(async () => {
console.log('*******************************');
console.log('* addAccessControlListToTrunk *');
console.log('*******************************');

const sipTrunkId = getSipTrunkIdFromConfig();
const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.AddAccessControlListToTrunkRequestData = {
trunkId: sipTrunkId,
addAccessControlListToTrunkRequestBody: {
accessControlListIds: [
aclId,
],
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.accessControlList.addToTrunk(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`The SIP trunk with the id '${requestData.trunkId}' contains the following ACL IDs:\n - ${response.accessControlListIds?.join('\n - ')}`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getPrintFormat, initElasticSipTrunkingService, printFullResponse } from '../../config';

(async () => {
console.log('***************************');
console.log('* createAccessControlList *');
console.log('***************************');

const requestData: ElasticSipTrunking.CreateAccessControlListRequestData = {
createAccessControlListBody: {
name: 'New ACL created with the Node.js SDK',
enabled: true,
ipRanges: [
{
description: 'Location 1',
ipAddress: '15.15.15.15',
range: 20,
},
],
},
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
const response = await elasticSipTrunkingService.accessControlList.create(requestData);

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(`New access control list created with the id '${response.id}' at '${response.createTime?.toISOString()}'`);
} else {
printFullResponse(response);
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getAccessControlListIdFromConfig, initElasticSipTrunkingService } from '../../config';

(async () => {
console.log('***************************');
console.log('* deleteAccessControlList *');
console.log('***************************');

const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.DeleteAccessControlListRequestData = {
id: aclId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
await elasticSipTrunkingService.accessControlList.delete(requestData);

console.log(`The SIP trunk with the id '${requestData.id}' has been deleted.`);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getSipTrunkIdFromConfig,
initElasticSipTrunkingService,
} from '../../config';

(async () => {
console.log('************************************');
console.log('* deleteAccessControlListFromTrunk *');
console.log('************************************');

const sipTrunkId = getSipTrunkIdFromConfig();
const aclId = getAccessControlListIdFromConfig();

const requestData: ElasticSipTrunking.DeleteAccessControlListFromTrunkRequestData = {
trunkId: sipTrunkId,
accessControlListId: aclId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
await elasticSipTrunkingService.accessControlList.deleteFromTrunk(requestData);

console.log(`The ACL '${requestData.accessControlListId}' has been removed fromm the SIP trunk '${requestData.trunkId}'`);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import {
getAccessControlListIdFromConfig,
getIpRangeIdFromConfig,
initElasticSipTrunkingService,
} from '../../config';

(async () => {
console.log('**************************************');
console.log('* deleteIpRangeFromAccessControlList *');
console.log('**************************************');

const aclId = getAccessControlListIdFromConfig();
const ipRangeId = getIpRangeIdFromConfig();

const requestData: ElasticSipTrunking.DeleteIpRangeFromAccessControlListRequestData = {
accessControlListId: aclId,
ipRangeId: ipRangeId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();
await elasticSipTrunkingService.accessControlList.deleteIpRange(requestData);

console.log(`The IP range '${requestData.ipRangeId}' has been removed fromm the ACL '${requestData.accessControlListId}'`);

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ElasticSipTrunking, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initElasticSipTrunkingService, printFullResponse } from '../../config';

const populateACLsList = (
aclPage: PageResult<ElasticSipTrunking.AccessControlList>,
aclsList: ElasticSipTrunking.AccessControlList[],
aclsDetailsList: string[],
) => {
aclPage.data.map((acl: ElasticSipTrunking.AccessControlList) => {
aclsList.push(acl);
aclsDetailsList.push(`${acl.id} - ${acl.name}`);
});
};

(async () => {
console.log('************************');
console.log('* getAccessControlList *');
console.log('************************');

const requestData: ElasticSipTrunking.ListAccessControlListRequestData = {};

const elasticSipTrunkingService = initElasticSipTrunkingService();

// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.accessControlList.list(requestData);
const aclsList: ElasticSipTrunking.AccessControlList[] = [];
const aclsDetailsList: string[] = [];

// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateACLsList(response, aclsList, aclsDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}

const printFormat = getPrintFormat(process.argv);

if (printFormat === 'pretty') {
console.log(aclsDetailsList.length > 0
? 'List of ACLs:\n' + aclsDetailsList.join('\n')
: 'Sorry, no ACLs were found.');
} else {
printFullResponse(aclsList);
}

// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const acl of elasticSipTrunkingService.accessControlList.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${acl.id} - ${acl.name}`);
} else {
console.log(acl);
}
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ElasticSipTrunking } from '@sinch/sdk-core';
import { getSipTrunkIdFromConfig, initElasticSipTrunkingService, printFullResponse } from '../../config';

(async () => {
console.log('*********************************');
console.log('* getAccessControlListsForTrunk *');
console.log('*********************************');

const sipTrunkId = getSipTrunkIdFromConfig();

const requestData: ElasticSipTrunking.ListAccessControlListsForTrunkRequestData = {
trunkId: sipTrunkId,
};

const elasticSipTrunkingService = initElasticSipTrunkingService();

// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await elasticSipTrunkingService.accessControlList.listForTrunk(requestData);

const aclList: string[] = [];

// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
aclList.push(...response.data);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}

printFullResponse(aclList);

// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const acl of elasticSipTrunkingService.accessControlList.listForTrunk(requestData)) {
console.log(acl);
}

})();
Loading

0 comments on commit 1f85c89

Please sign in to comment.