egg.js+mongodb+openstack 公有云计费系统(三)OpenStack 对接 compute (一)

配额
控制器
'use strict';

const Controller = require('egg').Controller;

class IndexController extends Controller {
/**
* @name constructor
*
* @param {*} ctx
* @param {Function} DuFn 清理空字段函数
* @param {Object} auth 用户JWT
*
*/
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.DUFn = ctx.helper.deleteUndefined;
this.QPFn = ctx.helper.queryParamFn;
this.SERVICE = ctx.service.openstack.compute.index;
this.serverSERVICE = ctx.service.openstack.server.servers
}
/**
* name 计算相关配额
*
* @description
*
* @example GET /openstack/blcokstorage/limits
*
* @return {Object}
* - cores VCPU数量可用 颗
* - floatingIps 自定义IP可用 个
* - instances N/A
* - RAM 内存可用量 MB
* - securityGroups 实例数量 个
* - serverGroups 实例分组数量 个
*/
async limits() {
const { ctx, service } = this;
// if (!ctx.isAdmin()) return;
const DATAS = await this.SERVICE.limits(this.auth.id, ctx.isAdmin());
ctx.body = DATAS
}
}

module.exports = IndexController;
service
'use strict';
const ServerIndex = require('../index')
class ComputeIndexService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8774/v2.1/'
};
// 获取服务器相关配额
async limits(_ucid, isAdmin) {
try {
const DATAS = await this.OSAJax(`${this.actions}limits`, {
...!isAdmin && { _ucid }
});
const { maxImageMeta, maxPersonality, maxPersonalitySize, maxSecurityGroupRules, maxSecurityGroups, maxServerGroupMembers, maxServerGroups, maxServerMeta, maxTotalCores, maxTotalFloatingIps, maxTotalInstances, maxTotalKeypairs, maxTotalRAMSize, totalCoresUsed, totalFloatingIpsUsed, totalInstancesUsed, totalRAMUsed, totalSecurityGroupsUsed, totalServerGroupsUsed } = (await DATAS).limits.absolute
return {
data: {
maxTotalKeypairs,
'cores': maxTotalCores - totalCoresUsed,
'RAM': maxTotalRAMSize - totalRAMUsed,
maxTotalInstances,
totalInstancesUsed,
'instances': maxTotalInstances - totalInstancesUsed,
maxServerGroups,
'serverGroups': maxServerGroups - totalServerGroupsUsed,
}
}
} catch (error) {
return error
}
}
}

module.exports = ComputeIndexService;

?keypairs
?
控制器
'use strict';

const Controller = require('egg').Controller;

class OSKeypairController extends Controller {
/**
* @name constructor
*
* @param {*} ctx
* @param {Function} DuFn 清理空字段函数
* @param {Object} auth 用户JWT
*
*/
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.DUFn = ctx.helper.deleteUndefined;
this.QPFn = ctx.helper.queryParamFn;
this.SERVICE = ctx.service.openstack.compute.keypair;
this.MODEL = ctx.model.Openstack.Servers.Keypair
}
/**
* @name 获取用户所有密钥对
*
* @example
* - GET /openstack/compute/keypairs
*/
async index() {
const ctx = this.ctx;
// const DATAS = await this.SERVICE.index(this.auth.id, this.QPFn(ctx.query));
// ctx.body = DATAS
const RESULT = await this.MODEL.getAll(this.auth.id, this.QPFn({ ...ctx.query
}));
ctx.body = RESULT
}
/**
* @name 创建密钥对
*
* @param {String} name 密钥对名称
*
* @example
* - POST application/x-www-form-urlencoded /openstack/compute/keypairs
*/
async create() {
const ctx = this.ctx;
const {
name,
public_key,
description
} = ctx.request.body;
const BODYS = this.DUFn({
name,
public_key,
description
});
// const DATAS = await this.SERVICE.create(this.auth.id, BODYS);
// ctx.body = DATAS
const RESULT = await this.MODEL.createOne(this.auth.id, BODYS);
ctx.body = RESULT
}
/**
* @name 获取指定密钥对信息
*
* @param {String} ctx.params.id 密钥对名称
*
* @example
* - GET /openstack/compute/keypairs/{ctx.params.id}
*/
async show() {
const ctx = this.ctx;
const DATAS = await this.MODEL.getOne(this.auth.id, ctx.params.id);
ctx.body = DATAS
}
/**
* @name 删除密钥对
*
* @param {String} ctx.params.id 密钥对名称
*
* @example
* - DELETE /openstack/compute/keypairs/{ctx.params.id}
*/
async destroy() {
const ctx = this.ctx;
const DATAS = await this.MODEL.deleteOne(this.auth.id, ctx.params.id);
ctx.body = DATAS
}
/**
* @name 绑定ECS
*
* @param {String} name 密钥对名称
* @param {String} type 'bind|unbind' 方式
* @param {String} _ECS_id 服务器id
*
*/
async setBindECS() {
const ctx = this.ctx;
const {
name,
type,
_ECS_id
} = ctx.request.body;
const BODYS = this.DUFn({
name,
type,
_ECS_id
})
const DATAS = await this.MODEL.setBindEcs(this.auth.id, BODYS);
ctx.body = DATAS
}
}

module.exports = OSKeypairController;
service
'use strict';
const ServerIndex = require('../index')
class ComputeKeypairService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8774/v2.1/os-keypairs'
};
/**
* @name 列表
* @param {*} _ucid 用户id
*/
async index(_ucid, query) {
try {
const datas = await this.OSAJax(`${this.actions}`, {
_ucid,
body: { ...query.querys }
});
return {
data: {
'result': datas.keypairs,
'totalCount': datas.keypairs.length
},
}
} catch (error) {
return error
}
}
/**
* @name 获取
* @param {*} _ucid 用户id
* @param {*} keypair_name 密钥对名称
*/
async show(_ucid, keypair_name) {
try {
const datas = await this.OSAJax(`${this.actions}/${keypair_name}`, { _ucid });
return {
data: datas.keypair
}
} catch (error) {
return error
}
}
/**
* @name 创建
* @param {*} _ucid 用户id
* @param {*} body 需要提交的参数
*/
async create(_ucid, body) {
try {
const datas = await this.OSAJax(`${this.actions}`, {
_ucid,
method: 'POST',
body: {
'keypair': {
...body,
}
}
});
return {
...!!datas.conflictingRequest ? { code: 422, message: `密钥对名称为:${body.name}的密钥对已存在` } : { data: datas }
}
} catch (error) {
return error
}
}
/**
* @name 删除
* @param {*} _ucid 用户id
* @param {*} keypair_name 密钥对名称
*/
async destroy(_ucid, keypair_name) {
try {
const datas = await this.OSAJax(`${this.actions}/${keypair_name}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
code: datas.status,
message: datas.status === 404 && `${keypair_name}不存在或已删除`
}
} catch (error) {

}
}
}

module.exports = ComputeKeypairService;

0 个评论

要回复文章请先登录注册