Skip to content

Commit

Permalink
Added new method for returning all security groups from openstack. Re…
Browse files Browse the repository at this point in the history
…-factored some code. Returning a common instance state instead of the state provided by openstack.
  • Loading branch information
Jamie Hall committed Sep 8, 2021
1 parent f2d496d commit da2d37a
Show file tree
Hide file tree
Showing 14 changed files with 1,295 additions and 92 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following environment variables are used to configure VISA Cloud Web Provide
| VISA_WEB_PROVIDER_OPENSTACK_IDENTITY_ENDPOINT | | The API endpoint to the OpenStack Identity API |
| VISA_WEB_PROVIDER_OPENSTACK_COMPUTE_ENDPOINT | | The API endpoint to the OpenStack Compute API |
| VISA_WEB_PROVIDER_OPENSTACK_IMAGE_ENDPOINT | | The API endpoint to the OpenStack Image API |
| VISA_WEB_PROVIDER_OPENSTACK_NETWORK_ENDPOINT | | The API endpoint to the OpenStack Network API |
| VISA_WEB_PROVIDER_OPENSTACK_APPLICATION_ID | | The application id for an OpenStack Application Credential |
| VISA_WEB_PROVIDER_OPENSTACK_APPLICATION_SECRET | | The application secret for an OpenStack Application Credential |
| VISA_WEB_PROVIDER_OPENSTACK_ADDRESS_PROVIDER | | The OpenStack network address provider |
Expand Down
1,153 changes: 1,114 additions & 39 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start:dev": "nodemon",
"clean": "rimraf ./dist",
"build": "rimraf ./dist && tsc",
"start": "npm run build && node index.js"
"start": "npm run build && node index.js",
},
"keywords": [],
"author": "",
Expand All @@ -20,6 +20,7 @@
"@types/node": "^14.11.8",
"@types/pg": "^8.6.0",
"@types/winston-syslog": "^2.0.3",
"concurrently": "^6.2.1",
"mocha": "^8.4.0",
"nodemon": "^2.0.4",
"rimraf": "^3.0.2",
Expand All @@ -28,7 +29,7 @@
"typescript": "^4.0.3"
},
"dependencies": {
"async-mutex": "^0.3.1",
"async-mutex": "^0.3.2",
"axios": "^0.21.1",
"chai": "^4.3.4",
"date-fns": "^2.23.0",
Expand Down
3 changes: 2 additions & 1 deletion src/application-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ApplicationConfig {
identityEndpoint: string;
computeEndpoint: string;
imageEndpoint: string;
networkEndpoint: string;
applicationId: string;
applicationSecret: string;
addressProvider: string;
Expand Down Expand Up @@ -55,6 +56,7 @@ export function APPLICATION_CONFIG(): ApplicationConfig {
identityEndpoint: process.env.VISA_WEB_PROVIDER_OPENSTACK_IDENTITY_ENDPOINT,
computeEndpoint: process.env.VISA_WEB_PROVIDER_OPENSTACK_COMPUTE_ENDPOINT,
imageEndpoint: process.env.VISA_WEB_PROVIDER_OPENSTACK_IMAGE_ENDPOINT,
networkEndpoint: process.env.VISA_WEB_PROVIDER_OPENSTACK_NETWORK_ENDPOINT,
applicationId: process.env.VISA_WEB_PROVIDER_OPENSTACK_APPLICATION_ID,
applicationSecret: process.env.VISA_WEB_PROVIDER_OPENSTACK_APPLICATION_SECRET,
addressProvider: process.env.VISA_WEB_PROVIDER_OPENSTACK_ADDRESS_PROVIDER,
Expand All @@ -63,6 +65,5 @@ export function APPLICATION_CONFIG(): ApplicationConfig {
}
};
}

return applicationConfig;
}
44 changes: 17 additions & 27 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import "reflect-metadata";
import {authenticationMiddleware, errorHandlerMiddleware, logger} from './utils';
import express from 'express'
import * as http from 'http';
import {FlavourController, ImageController, InstanceController, MetricsController} from "./controllers";
import {
FlavourController,
ImageController,
InstanceController,
MetricsController,
SecurityGroupController
} from "./controllers";
import {APPLICATION_CONFIG} from './application-config';
import {container} from "tsyringe";
import {OpenstackAuthenticator, OpenstackService} from "./services";
import {container} from "./ioc";

export class Application {

private _server: http.Server;

constructor() {
this.init();
}

async start(): Promise<null> {
Expand All @@ -23,7 +27,8 @@ export class Application {
const app = express();
const router = express.Router();
app.use(express.json());
app.use(authenticationMiddleware);

router.use(authenticationMiddleware);

/**
* Routing for instances
Expand All @@ -34,7 +39,7 @@ export class Application {
router.get('/instances/:id', (req, res, next) => container.resolve(InstanceController).get(req, res, next));
router.get('/instances/:id/security_groups', (req, res, next) => container.resolve(InstanceController).securityGroups(req, res, next));
router.post('/instances/:id/security_groups', (req, res, next) => container.resolve(InstanceController).addSecurityGroup(req, res, next));
router.delete('/instances/:id/security_groups', (req, res, next) => container.resolve(InstanceController).removeSecurityGroup(req, res, next));
router.post('/instances/:id/security_groups/remove', (req, res, next) => container.resolve(InstanceController).removeSecurityGroup(req, res, next));
router.get('/instances/:id/ip', (req, res, next) => container.resolve(InstanceController).ip(req, res, next));
router.delete('/instances/:id', (req, res, next) => container.resolve(InstanceController).remove(req, res, next));
router.post('/instances/:id/start', (req, res, next) => container.resolve(InstanceController).start(req, res, next));
Expand All @@ -53,6 +58,11 @@ export class Application {
router.get('/flavours', (req, res, next) => container.resolve(FlavourController).all(req, res, next));
router.get('/flavours/:id', (req, res, next) => container.resolve(FlavourController).get(req, res, next));

/**
* Routing for security groups
*/
router.get('/security_groups', (req, res, next) => container.resolve(SecurityGroupController).all(req, res, next));

/**
* Routing for metrics
*/
Expand Down Expand Up @@ -83,26 +93,6 @@ export class Application {
return null;
}

private init(): void {
container.register<OpenstackService>(OpenstackService, {
useValue: new OpenstackService(
{
computeEndpoint: APPLICATION_CONFIG().openstack.computeEndpoint,
imageEndpoint: APPLICATION_CONFIG().openstack.imageEndpoint
},
{
addressProvider: APPLICATION_CONFIG().openstack.addressProvider,
addressProviderUUID: APPLICATION_CONFIG().openstack.addressProviderUUID
},
new OpenstackAuthenticator(
APPLICATION_CONFIG().openstack.identityEndpoint,
APPLICATION_CONFIG().openstack.applicationId,
APPLICATION_CONFIG().openstack.applicationSecret,
APPLICATION_CONFIG().openstack.timeout
),
APPLICATION_CONFIG().openstack.timeout
)
});
}

}

1 change: 1 addition & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './instance.controller';
export * from './flavour.controller';
export * from './image.controller';
export * from './metrics.controller';
export * from './security-group.controller';
10 changes: 5 additions & 5 deletions src/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class InstanceController {
* Get a list of all instances
* @param request the http request
* @param response the http response
* @param next the next middleware handler
* @param next the next middleware handler#
*/
public async all(request: Request, response: Response, next: NextFunction) {
try {
Expand Down Expand Up @@ -119,7 +119,7 @@ export class InstanceController {
} else {
const {id} = request.params;
const {name} = json;
await this._openstack.addSecurityGroup(id, name);
await this._openstack.addSecurityGroupForInstance(id, name);
response.json({message: 'Added security group'});
}
} catch (error) {
Expand All @@ -136,7 +136,7 @@ export class InstanceController {
public async securityGroups(request: Request, response: Response, next: NextFunction) {
try {
const {id} = request.params;
const groups = await this._openstack.securityGroups(id);
const groups = await this._openstack.securityGroupsForInstance(id);
response.json(groups);
} catch (error) {
next(error);
Expand All @@ -161,7 +161,7 @@ export class InstanceController {
} else {
const {id} = request.params;
const {name} = json;
await this._openstack.removeSecurityGroup(id, name);
await this._openstack.removeSecurityGroupFromInstance(id, name);
response.json({message: 'Removed security group'});
}
} catch (error) {
Expand Down Expand Up @@ -193,7 +193,7 @@ export class InstanceController {
public async remove(request: Request, response: Response, next: NextFunction) {
try {
const {id} = request.params;
await this._openstack.delete(id);
await this._openstack.deleteInstance(id);
response.json({message: 'Deleted instance'});
} catch (error) {
next(error);
Expand Down
33 changes: 33 additions & 0 deletions src/controllers/security-group.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {NextFunction, Request, Response} from "express";
import {singleton} from "tsyringe";
import {OpenstackService} from "../services";

@singleton()
export class SecurityGroupController {

private readonly _openstack: OpenstackService;

/**
* Create a new security group controller
* @param openstack the openstack API http client
*/
constructor(readonly openstack: OpenstackService) {
this._openstack = openstack;
}

/**
* Get a list of security groups
* @param request the http request
* @param response the http response
* @param next the next middleware handler
*/
public async all(request: Request, response: Response, next: NextFunction) {
try {
const groups = await this._openstack.securityGroups();
response.json(groups);
} catch (error) {
next(error);
}
}

}
31 changes: 31 additions & 0 deletions src/ioc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {IocContainer} from '@tsoa/runtime';
import {container} from 'tsyringe';
import {OpenstackAuthenticator, OpenstackService} from "../services";
import {APPLICATION_CONFIG} from "../application-config";

container.register<OpenstackService>(OpenstackService, {
useValue: new OpenstackService(
{
computeEndpoint: APPLICATION_CONFIG().openstack.computeEndpoint,
imageEndpoint: APPLICATION_CONFIG().openstack.imageEndpoint,
networkEndpoint: APPLICATION_CONFIG().openstack.networkEndpoint
},
{
addressProvider: APPLICATION_CONFIG().openstack.addressProvider,
addressProviderUUID: APPLICATION_CONFIG().openstack.addressProviderUUID
},
new OpenstackAuthenticator(
APPLICATION_CONFIG().openstack.identityEndpoint,
APPLICATION_CONFIG().openstack.applicationId,
APPLICATION_CONFIG().openstack.applicationSecret,
APPLICATION_CONFIG().openstack.timeout
),
APPLICATION_CONFIG().openstack.timeout
)
});
export const iocContainer: IocContainer = {
get: <T>(controller: { prototype: T }): T =>
container.resolve<T>(controller as never),
};
export {container};
export default iocContainer;
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './instance-fault.model';
export * from './image.model';
export * from './flavour.model';
export * from './metrics.model';
export * from './instance-state.model';
12 changes: 12 additions & 0 deletions src/models/instance-state.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum CloudInstanceState {
UNKNOWN = 'UNKNOWN',
BUILDING = 'BUILDING',
STARTING = 'STARTING',
ACTIVE = 'ACTIVE',
STOPPING = 'STOPPING',
STOPPED = 'STOPPED',
REBOOTING = 'REBOOTING',
UNAVAILABLE = 'UNAVAILABLE',
ERROR = 'ERROR',
DELETED = 'DELETED'
}
5 changes: 3 additions & 2 deletions src/models/instance.model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {InstanceFault} from "./instance-fault.model";
import {CloudInstanceState} from "./instance-state.model";

export interface Instance {
id: string;
name: string;
state: string;
flavorId: string;
state: CloudInstanceState;
flavourId: string;
imageId: string;
createdAt: string;
address: string;
Expand Down
13 changes: 9 additions & 4 deletions src/services/cloud-provider.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ export interface CloudProvider {
* Get the security groups for a given instance identifier
* @param id the instance identifier
*/
securityGroups(id: string): Promise<string[]>;
securityGroupsForInstance(id: string): Promise<string[]>;

/**
* Remove a security for a given instance identifier
* @param id the instance identifier
* @param name the security group name
*/
removeSecurityGroup(id: string, name: string): Promise<void>;
removeSecurityGroupFromInstance(id: string, name: string): Promise<void>;

/**
* Add a security for a given instance identifier
* @param id the instance identifier
* @param name the security group name
*/
addSecurityGroup(id: string, name: string): Promise<void>;
addSecurityGroupForInstance(id: string, name: string): Promise<void>;

/**
* Create a new instance
Expand All @@ -58,7 +58,7 @@ export interface CloudProvider {
* Delete an instance
* @param id the instance identifier
*/
delete(id: string): Promise<void>;
deleteInstance(id: string): Promise<void>;

/**
* Shutdown an instance
Expand Down Expand Up @@ -105,4 +105,9 @@ export interface CloudProvider {
*/
metrics(): Promise<Metrics>;

/**
* Get all of the available security groups
*/
securityGroups(): Promise<string[]>;

}
Loading

0 comments on commit da2d37a

Please sign in to comment.