Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating yaml for adding Oracle Cloud functionalities #159

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions generator/generators/Oracle/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import * as fs from "fs";
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript";
import { getAST } from "../../parsers/oracle/parser";
import { transform } from "../../transformers/do/transformer";
import { getDir,printFile } from "../lib/helper";

interface FunctionData {
functionName: string;
SDKFunctionName: string;
params: param[];
}

interface param {
name: string;
type: string;
typeName: string;
}

interface ClassData {
className: string;
functions: FunctionData[];
serviceName: string;
}

const dummyFile = process.cwd() + "/dummyClasses/do.js";

const dummyAst = createSourceFile(
dummyFile,
fs.readFileSync(dummyFile).toString(),
ScriptTarget.Latest,
true
);

export function extractSDKData(sdkClassAst, serviceClass) {
let methods: FunctionData[] = [];
const functions = [];

Object.keys(serviceClass).map((key, index) => {
functions.push(serviceClass[key].split(" ")[1]);
});

sdkClassAst.members.map(method => {
if (method.name && functions.includes(method.name.text)) {
let name;
Object.keys(serviceClass).map((key, index) => {
if (serviceClass[key].split(" ")[1] === method.name.text) {
name = key;
}
});

const parameters = [];
method.parameters.map(param => {
if (param.name.text !== "callback") {
const parameter = {
name: param.name.text,
optional: param.questionToken ? true : false,
type: SyntaxKind[param.type.kind],
typeName: null
};

if (parameter.type === "TypeReference" && param.type.typeName) {
parameter.typeName = param.type.typeName.text;
}

parameters.push(parameter);
}
});

methods.push({
functionName: name.toString(),
SDKFunctionName: method.name.text.toString(),
params: parameters
});
}
});

const classData: ClassData = {
className: sdkClassAst.name.text,
functions: methods,
serviceName: null
};

return classData;
}

export function generateOracleClass(serviceClass, serviceName) {
const sdkFile = serviceClass[Object.keys(serviceClass)[0]].split(" ")[0];
getAST(sdkFile).then(async result => {
const sdkClassAst = result;
try {
const classData: ClassData = extractSDKData(sdkClassAst, serviceClass);
classData.serviceName = serviceName;
const output = await transform(dummyAst, classData);
let filePath;
const dir = getDir(serviceName);
if (!fs.existsSync(process.cwd() + "/generatedClasses/DO/" + dir)) {
fs.mkdirSync(process.cwd() + "/generatedClasses/DO/" + dir);
}
if (/^[A-Z]*$/.test(serviceName)) {
filePath =
process.cwd() +
"/generatedClasses/DO/" +
dir +
"/do-" +
serviceName +
".js";
} else {
filePath =
process.cwd() +
"/generatedClasses/DO/" +
dir +
"/do-" +
serviceName.charAt(0).toLowerCase() +
serviceName.slice(1) +
".js";
}
printFile(filePath, output);
} catch (e) {
console.error(e);
}
});
}
5 changes: 4 additions & 1 deletion generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateAWSClass } from "./generators/aws/generator";
import { generateAzureClass } from "./generators/azure/generator";
import { generateDOClass } from "./generators/do/generator";
import { generateGCPClass } from "./generators/googleCloud/generator";
import {generateOracleClass} from "./generators/oracle/generator";

try {
const services = yaml.safeLoad(fs.readFileSync("node-cloud.yml", "utf8"));
Expand All @@ -18,7 +19,9 @@ try {
generateGCPClass(services[service][provider], service);
} else if (provider == "DO") {
generateDOClass(services[service][provider], service);
}
}else if (provider === "Oracle") {
generateOracleClass(services[service][provider], service);
}
});
});
} catch (error) {
Expand Down
41 changes: 39 additions & 2 deletions generator/node-cloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ StorageBucket:
list: storage storage.d.ts getBuckets
upload: storage bucket.d.ts upload
makePublic: storage file.d.ts makePublic
Oracle:
create: objectstorage createBucket
delete: objectstorage deleteBucket
list: objectstorage listBuckets
listObjects: objectstorage listObjects
deleteFile: objectstorage deleteObject

PaaS:
AWS:
Expand Down Expand Up @@ -118,6 +124,16 @@ Kubernetes:
deleteNodegroup: kubernetes.d.ts deleteNodePool
describeNodeGroup: kubernetes.d.ts getNodePoolById
listNodegroups: kubernetes.d.ts getNodePools
Oracle:
create: conatinerengine createCluster
delete: conatinerengine deleteCluster
listClusters: conatinerengine listClusters
describeCluster: conatinerengine getCluster
createNodeGroup: conatinerengine createNodePool
deleteNodegroup: conatinerengine deleteNodePool
describeNodeGroup: conatinerengine getNodePool
listNodegroups: conatinerengine listNodePools
describeNodeGroup: containerengine getNodePool

Monitoring:
GCP:
Expand All @@ -139,7 +155,12 @@ Monitoring:
updateAlarm: arm-monitor alertRules.d.ts update
listAlarms: arm-monitor alertRules.d.ts listBySubscription
getMetricData: arm-monitor alertRules.d.ts get

Oracle:
createAlarm: monitoring createAlarm
deleteAlarm: monitoring deleteAlarm
updateAlarm: monitoring updateAlarm
listAlarms: monitoring listAlarms
getMetricData: monitoring listMetrics
Container:
AWS:
create: ecs.d.ts createCluster
Expand Down Expand Up @@ -216,7 +237,12 @@ NoSql:
getClusters: databases.d.ts getAllClusters
getCluster: databases.d.ts getClusterById
updateCluster: databases.d.ts resizeCluster

Oracle:
createTable: nosql createTable
createItem: nosql createIndex
deleteItem: nosql deleteIndex
query: nosql query
deleteTable: nosql deleteTable
DNS:
AWS:
createZone: route53.d.ts createHostedZone
Expand All @@ -241,6 +267,12 @@ DNS:
getRecords: domains.d.ts getAllRecords
getRecord: domains.d.ts getRecord
changeRecordSets: domains.d.ts updateRecord
Oracle:
createZone: dns createZone
deleteZone: dns deleteZone
listZones: dns listZones
changeRecordSets: dns updateZoneRecords


LoadBalancer:
AWS:
Expand All @@ -260,6 +292,11 @@ LoadBalancer:
delete: load-balancers.d.ts delete
list: load-balancers.d.ts getAll
update: load-balancers.d.ts update
Oracle:
create: loadbalancer createLoadBalancer
delete: loadbalancer deleteLoadBalancer
list: loadbalancer listLoadBalancers
update: loadbalancer updateLoadBalancer

IAM:
AWS:
Expand Down
3 changes: 2 additions & 1 deletion generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "cross-env TS_NODE_FILES=true mocha --exit --require ts-node/register --colors test/**/*.ts",
"tool": "tsc main && node main",
"lint": "eslint .",
"lint-fix": "eslint --fix ."
"lint-fix": "eslint --fix ."
},
"dependencies": {
"@azure/arm-appservice": "^6.0.0",
Expand All @@ -37,6 +37,7 @@
"js-yaml": "^3.14.0",
"key-mirror": "^1.0.1",
"lodash": "^4.17.19",
"oci-sdk": "^2.54.0",
"typescript": "^3.9.3"
}
}
40 changes: 40 additions & 0 deletions generator/parsers/oracle/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as fs from "fs";
import * as path from "path";
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript";

export function getAST(sdkpkgName) {
return new Promise(async (resolve, reject) => {
try {
const file = path.join(
__dirname,
`../../../node_modules/oci-${sdkpkgName.toLowerCase()}/lib/client.d.ts`
);
const ast = createSourceFile(
file,
fs.readFileSync(file).toString(),
ScriptTarget.Latest,
true
);

let cloned = null;

await ast.forEachChild(child => {
if (SyntaxKind[child.kind] === "ClassDeclaration") {
cloned = Object.assign({}, child);
}
});

if (!cloned) {
reject(new Error("CLASS NOT FOUND"));
} else {
resolve(cloned);
}
} catch (error) {
if (error.code === "ENOENT") {
reject(new Error("PACKAGE NOT FOUND"));
} else {
reject(error);
}
}
});
}
35 changes: 35 additions & 0 deletions generator/test/parsers/oracle/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import { expect } from "chai";
import { SyntaxKind } from "typescript";

import { getAST } from "../../../parsers/oracle/parser";

describe("Oracle Cloud parser getAST", () => {
context("With existing file", () => { //checks if getAST returns the AST of the file when given a valid file.
it("Should return Abstract syntax tree of the class", async () => {
const ast: any = await getAST("dns");
expect(ast).to.be.an("object");
expect(SyntaxKind[ast.kind] === "ClassDeclaration").to.be.true;
});
});

context("With non-existing file", () => { //checking if throws error when a non existent file is passed.
it("should return File not found Error", async () => {
try {
await getAST("unknown.d.ts");
} catch (error) {
expect(error.message).to.eql("File not found!");
}
});
});

context("With wrong format file", () => { //if the file dors not contain a class declaration.
it("Should return class not found Error", async () => {
try {
await getAST("../types/common.d.ts");
} catch (error) {
expect(error.message).to.eql("Class not found!");
}
});
});
});
Loading