node

node

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。
javascript/jQuery

javascript/jQuery

一种直译式脚本语言,是一种动态类型、弱类型、基于原型的语言,内置支持类型。
MongoDB

MongoDB

MongoDB 是一个基于分布式文件存储的数据库
openstack

openstack

OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目。
VUE

VUE

一套构建用户界面的渐进式框架。与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计。
bootstrap

bootstrap

Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web.
HTML

HTML

超文本标记语言,标准通用标记语言下的一个应用。
CSS/SASS/SCSS/Less

CSS/SASS/SCSS/Less

层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
PHP

PHP

PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执
每天进步一点点

每天进步一点点

乌法把门的各累笑寂静
求职招聘

求职招聘

猎头招聘专用栏目
Python

Python

一种解释型、面向对象、动态数据类型的高级程序设计语言。

Egg 配置mongoose

Nodejslopo1983 发表了文章 • 0 个评论 • 2115 次浏览 • 2019-08-31 08:55 • 来自相关话题

1.安裝npm i egg-mongoose --save2./config/plugin.jsexports.mongoose = {
enable: true,
package: 'egg-mongoose',
};3./config/config.default.js
 
單一exports.mongoose = {
url: 'mongodb://127.0.0.1/example',
options: {},
plugins: [createdPlugin, [updatedPlugin, pluginOptions]],
};多個exports.mongoose = {
clients: {
// clientId, access the client instance by app.mongooseDB.get('clientId')
db1: {
url: 'mongodb://127.0.0.1/example1',
options: {},
},
db2: {
url: 'mongodb://127.0.0.1/example2',
options: {},
},
},
};4.使用(案例參考)
model/user/address (model文件夾 在app目錄下自己創建)module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const {
ctx,
helper,
service
} = app.createAnonymousContext();
const {
objToQuery
} = helper;
const UserAddressSchema = new Schema({
// 用户id
_uid: {
type: Schema.Types.ObjectId,
ref: 'account_user',
required: true,
index: true
},
default_id: '',
addresses: [{
// 数组ID
_id: mongoose.Schema.Types.ObjectId,
// 收件人名称
name: '',
// 聯係電話
phone: '',
// 省市区地址
Region: '',
// 城市id
RegionId: '',
// 郵編
postcode: '',
// 详细地址
location: '',
// 地址标签
tag: ''
}]
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
UserAddressSchema.statics = {
// 添加地址
addAddress: async function(_uid, BODYS) {
const { isDefault } = BODYS;
const _id = mongoose.Types.ObjectId();
const MODEL = this.findOneAndUpdate({
_uid
}, {
$push: {
'addresses': {
...BODYS,
_id
}
},
...isDefault && { default_id: _id }
}, {
new: true,
fields: 'addresses default_id -_id'
})
try {
const DATAS = await MODEL;
return !!DATAS.addresses.length
} catch (error) {
console.log(error)
return error
}
},
// 删除地址
deleteAddress: async function(_uid, _id) {
const MODEL = this.findOneAndUpdate({
_uid
}, {
$pull: {
'addresses': {
_id
}
}
})
try {
const DATAS = await MODEL;
console.log(DATAS)
return DATAS
} catch (error) {
console.log(error)
return error
}
},
// 更新地址
updateAddress: async function(_uid, BODYS) {
const { _id, isDefault } = BODYS;
delete BODYS._id;
delete BODYS.isDefault;
const MODEL = this.findOneAndUpdate({
_uid,
'addresses._id': _id
}, {
$set: {...objToQuery(BODYS, 'addresses')
}
}, {
fields: 'addresses default_id -_id'
});
try {
// 修改数据
const DATAS = await MODEL;

// 修改默认
!!isDefault && await this.setDefault(_uid, isDefault, _id);
return !!DATAS.addresses.length
} catch (error) {
console.log(error)
return error
}
},
// 設置默認
setDefault: async function(_uid, isDefaults, _id) {
const default_id = !!isDefaults ? _id : '';
const MODEL = this.findOneAndUpdate({ _uid }, { default_id });
try {
const MB = await MODEL;
return MB
} catch (error) {
console.log(error);
return error
}
},
// 获取单一地址
getOne: async function(_uid, _id) {
const MODEL = this.findOne({
_uid,
}, {
'addresses': {
'$elemMatch': {
_id
}
}
}, {
fields: 'addresses default_id -_id'
});
console.log(_id)
try {
if (_id !== "default") {
const DATAS = await MODEL;
return DATAS.addresses[0]
}
} catch (error) {
console.log(error)
return error
}
},
// 获取全部地址
getAllAddress: async function(_uid, query) {
const MODEL1 = this.findOneAndUpdate({ _uid }, { _uid, query }, { new: true, upsert: true, fields: 'addresses default_id -_id' });
// const MODEL2 = this.create({ _uid })
try {
const DATAS = await MODEL1;
return DATAS
// console.log(!!DATAS._uid)
// return DATAS._uid ? DATAS.addresses : (await MODEL2).addresses
} catch (error) {
console.log(error)
return error
}
},
// 獲取默認地址
getDefaultAddress: async function(_uid) {
const MODEL = this.findOne({ _uid }, {}, {
fields: 'default_id -_id'
});
try {
const { default_id } = await MODEL;
return await this.getOne(_uid, default_id)
} catch (error) {
console.log(error);
return error
}
}
}
return mongoose.model('user_address', UserAddressSchema);
}controller/user/address (RESTful 風格)const indexController = require('../index');
class invoiceAddressController extends indexController {
constructor(ctx) {
super(ctx);
this.MODEL = ctx.model.User.Address;
};
/**
* @name 获取所有用户地址列表
*
*/
async index() {
const ctx = this.ctx;
const isDefault = Object.keys(ctx.query).includes('default');
const RESULT = !!isDefault ?
await this.MODEL.getDefaultAddress(ctx._uid()) :
await this.MODEL.getAllAddress(ctx._uid());
ctx.body = RESULT
};
/**
* @name 获取用户地址列表
*/
async show() {
const ctx = this.ctx;
const RESULT = await this.MODEL.getOne(ctx._uid(), ctx.params.id);
ctx.body = RESULT
};
/**
* @name 创建地址
*/
async create() {
const ctx = this.ctx;
const {
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag
} = ctx.request.body;
const BODYS = this.DUFN({
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag
})
const RESULT = await this.MODEL.addAddress(ctx._uid(), BODYS);
ctx.body = RESULT
};
/**
* @name 更新
*
*/
async update() {
const {
ctx
} = this;
const _id = ctx.params.id
const {
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag,
action
} = ctx.request.body;
const BODYS = this.DUFN({
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag,
action,
_id
})
const RESULT = action === 'setDefault' ?
await this.MODEL.setDefault(ctx._uid(), isDefault, _id) :
await this.MODEL.updateAddress(ctx._uid(), BODYS);
ctx.body = RESULT
};
/**
* @name 删除
*/
async destroy() {
const ctx = this.ctx;
const RESULT = await this.MODEL.deleteAddress(ctx._uid(), ctx.params.id);
ctx.body = RESULT
}
}
module.exports = invoiceAddressControllerrouter.jsrouter.resources('user_address', '/user/address', app.jwt, controller.user.address); 查看全部
1.安裝
npm i egg-mongoose --save
2./config/plugin.js
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
};
3./config/config.default.js
 
單一
exports.mongoose = {
url: 'mongodb://127.0.0.1/example',
options: {},
plugins: [createdPlugin, [updatedPlugin, pluginOptions]],
};
多個
exports.mongoose = {
clients: {
// clientId, access the client instance by app.mongooseDB.get('clientId')
db1: {
url: 'mongodb://127.0.0.1/example1',
options: {},
},
db2: {
url: 'mongodb://127.0.0.1/example2',
options: {},
},
},
};
4.使用(案例參考)
model/user/address (model文件夾 在app目錄下自己創建)
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const {
ctx,
helper,
service
} = app.createAnonymousContext();
const {
objToQuery
} = helper;
const UserAddressSchema = new Schema({
// 用户id
_uid: {
type: Schema.Types.ObjectId,
ref: 'account_user',
required: true,
index: true
},
default_id: '',
addresses: [{
// 数组ID
_id: mongoose.Schema.Types.ObjectId,
// 收件人名称
name: '',
// 聯係電話
phone: '',
// 省市区地址
Region: '',
// 城市id
RegionId: '',
// 郵編
postcode: '',
// 详细地址
location: '',
// 地址标签
tag: ''
}]
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
UserAddressSchema.statics = {
// 添加地址
addAddress: async function(_uid, BODYS) {
const { isDefault } = BODYS;
const _id = mongoose.Types.ObjectId();
const MODEL = this.findOneAndUpdate({
_uid
}, {
$push: {
'addresses': {
...BODYS,
_id
}
},
...isDefault && { default_id: _id }
}, {
new: true,
fields: 'addresses default_id -_id'
})
try {
const DATAS = await MODEL;
return !!DATAS.addresses.length
} catch (error) {
console.log(error)
return error
}
},
// 删除地址
deleteAddress: async function(_uid, _id) {
const MODEL = this.findOneAndUpdate({
_uid
}, {
$pull: {
'addresses': {
_id
}
}
})
try {
const DATAS = await MODEL;
console.log(DATAS)
return DATAS
} catch (error) {
console.log(error)
return error
}
},
// 更新地址
updateAddress: async function(_uid, BODYS) {
const { _id, isDefault } = BODYS;
delete BODYS._id;
delete BODYS.isDefault;
const MODEL = this.findOneAndUpdate({
_uid,
'addresses._id': _id
}, {
$set: {...objToQuery(BODYS, 'addresses')
}
}, {
fields: 'addresses default_id -_id'
});
try {
// 修改数据
const DATAS = await MODEL;

// 修改默认
!!isDefault && await this.setDefault(_uid, isDefault, _id);
return !!DATAS.addresses.length
} catch (error) {
console.log(error)
return error
}
},
// 設置默認
setDefault: async function(_uid, isDefaults, _id) {
const default_id = !!isDefaults ? _id : '';
const MODEL = this.findOneAndUpdate({ _uid }, { default_id });
try {
const MB = await MODEL;
return MB
} catch (error) {
console.log(error);
return error
}
},
// 获取单一地址
getOne: async function(_uid, _id) {
const MODEL = this.findOne({
_uid,
}, {
'addresses': {
'$elemMatch': {
_id
}
}
}, {
fields: 'addresses default_id -_id'
});
console.log(_id)
try {
if (_id !== "default") {
const DATAS = await MODEL;
return DATAS.addresses[0]
}
} catch (error) {
console.log(error)
return error
}
},
// 获取全部地址
getAllAddress: async function(_uid, query) {
const MODEL1 = this.findOneAndUpdate({ _uid }, { _uid, query }, { new: true, upsert: true, fields: 'addresses default_id -_id' });
// const MODEL2 = this.create({ _uid })
try {
const DATAS = await MODEL1;
return DATAS
// console.log(!!DATAS._uid)
// return DATAS._uid ? DATAS.addresses : (await MODEL2).addresses
} catch (error) {
console.log(error)
return error
}
},
// 獲取默認地址
getDefaultAddress: async function(_uid) {
const MODEL = this.findOne({ _uid }, {}, {
fields: 'default_id -_id'
});
try {
const { default_id } = await MODEL;
return await this.getOne(_uid, default_id)
} catch (error) {
console.log(error);
return error
}
}
}
return mongoose.model('user_address', UserAddressSchema);
}
controller/user/address (RESTful 風格)
const indexController = require('../index');
class invoiceAddressController extends indexController {
constructor(ctx) {
super(ctx);
this.MODEL = ctx.model.User.Address;
};
/**
* @name 获取所有用户地址列表
*
*/
async index() {
const ctx = this.ctx;
const isDefault = Object.keys(ctx.query).includes('default');
const RESULT = !!isDefault ?
await this.MODEL.getDefaultAddress(ctx._uid()) :
await this.MODEL.getAllAddress(ctx._uid());
ctx.body = RESULT
};
/**
* @name 获取用户地址列表
*/
async show() {
const ctx = this.ctx;
const RESULT = await this.MODEL.getOne(ctx._uid(), ctx.params.id);
ctx.body = RESULT
};
/**
* @name 创建地址
*/
async create() {
const ctx = this.ctx;
const {
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag
} = ctx.request.body;
const BODYS = this.DUFN({
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag
})
const RESULT = await this.MODEL.addAddress(ctx._uid(), BODYS);
ctx.body = RESULT
};
/**
* @name 更新
*
*/
async update() {
const {
ctx
} = this;
const _id = ctx.params.id
const {
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag,
action
} = ctx.request.body;
const BODYS = this.DUFN({
name,
phone,
postcode,
Region,
RegionId,
location,
isDefault,
tag,
action,
_id
})
const RESULT = action === 'setDefault' ?
await this.MODEL.setDefault(ctx._uid(), isDefault, _id) :
await this.MODEL.updateAddress(ctx._uid(), BODYS);
ctx.body = RESULT
};
/**
* @name 删除
*/
async destroy() {
const ctx = this.ctx;
const RESULT = await this.MODEL.deleteAddress(ctx._uid(), ctx.params.id);
ctx.body = RESULT
}
}
module.exports = invoiceAddressController
router.js
router.resources('user_address', '/user/address', app.jwt, controller.user.address);

egg-jwt配置

Nodejslopo1983 发表了文章 • 0 个评论 • 2285 次浏览 • 2019-08-30 18:00 • 来自相关话题

1. 安裝npm i egg-jwt --save
 
2.config/config.default config.jwt = {
secret: appInfo.name + '_1539582326426_4353',
unless: {
path:
}
};
 
3.config/pluginexports.jwt = {
enable: true,
package: "egg-jwt"
};
 4.service/apps/jwt.jsclass jwtService extends Service {
constructor(ctx) {
super(ctx)
};
// jwt 加密 使用HS256
/**
*
* @param {Object} parmas 需封装的参数
* @param {String} type 类型 default:'web' ->24hrs
* @param {Boolean} expires 是否设置过期 default:true
*/
async sign(parmas, type = 'web', expires = true) {
const app = this.app;
return await app.jwt.sign({ ...parmas }, app.config.jwt.secret, { algorithm: 'HS256', ...(expires && { expiresIn: `${type === 'web' ? '24 hrs' : '30 days'}` }) })
}
// JWT 解密
async verify(token) {
const { app } = this;
return await app.jwt.verify(token, app.config.jwt.secret, { algorithm: 'HS256' })
}
}
module.exports = jwtService5.使用class weChatTemplateService extends indexService {
constructor(ctx) {
super(ctx);
this.userMODEL = ctx.model.Account.User;
this.tokenSERVICE = ctx.service.account.jwt
};
...
setToken(params, other) {
return { 'token': this.tokenSERVICE.create(params), ...other }
};
...
}6.鑒權
egg-jwt已经封装了verify 不需要再做 verify
获取token 数据 使用 this.ctx.state.user 获取
亦可在context封装鉴权
/extend/contextmodule.exports = {
isAdmin() {
return this.state.user.group === 1 ? false : true;
},
_uid() {
return this.state.user._id
}
};在controller 下面可以直接用
ctx.isAdmin() ctx._uid() 来获取 查看全部
1. 安裝
npm i egg-jwt --save

 
2.config/config.default
    config.jwt = {
secret: appInfo.name + '_1539582326426_4353',
unless: {
path:
}
};

 
3.config/plugin
exports.jwt = {
enable: true,
package: "egg-jwt"
};

 4.service/apps/jwt.js
class jwtService extends Service {
constructor(ctx) {
super(ctx)
};
// jwt 加密 使用HS256
/**
*
* @param {Object} parmas 需封装的参数
* @param {String} type 类型 default:'web' ->24hrs
* @param {Boolean} expires 是否设置过期 default:true
*/
async sign(parmas, type = 'web', expires = true) {
const app = this.app;
return await app.jwt.sign({ ...parmas }, app.config.jwt.secret, { algorithm: 'HS256', ...(expires && { expiresIn: `${type === 'web' ? '24 hrs' : '30 days'}` }) })
}
// JWT 解密
async verify(token) {
const { app } = this;
return await app.jwt.verify(token, app.config.jwt.secret, { algorithm: 'HS256' })
}
}
module.exports = jwtService
5.使用
class weChatTemplateService extends indexService {
constructor(ctx) {
super(ctx);
this.userMODEL = ctx.model.Account.User;
this.tokenSERVICE = ctx.service.account.jwt
};
...
setToken(params, other) {
return { 'token': this.tokenSERVICE.create(params), ...other }
};
...
}
6.鑒權
egg-jwt已经封装了verify 不需要再做 verify
获取token 数据 使用 this.ctx.state.user 获取
亦可在context封装鉴权
/extend/context
module.exports = {
isAdmin() {
return this.state.user.group === 1 ? false : true;
},
_uid() {
return this.state.user._id
}
};
在controller 下面可以直接用
ctx.isAdmin() ctx._uid() 来获取

eggjs 设置跨域

Nodejslopo1983 发表了文章 • 0 个评论 • 1900 次浏览 • 2019-08-30 17:57 • 来自相关话题

1.egg-corsnpm install egg-cors --save2.config/plugin.jsexports.cors: {
enable: true,
package: 'egg-cors'
}3.config/config.default.jsconfig.security = {
  csrf: {
    enable: false
  },
  domainWhiteList: [ '*' ]
};

config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
}; 查看全部
1.egg-cors
npm install  egg-cors --save
2.config/plugin.js
exports.cors: {
enable: true,
package: 'egg-cors'
}
3.config/config.default.js
config.security = {
  csrf: {
    enable: false
  },
  domainWhiteList: [ '*' ]
};

config.cors = {
origin: '*',
allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS'
};

eggjs 上传文件

Nodejslopo1983 发表了文章 • 0 个评论 • 2985 次浏览 • 2019-08-30 17:52 • 来自相关话题

'use strict';
const Controller = require('egg').Controller;
// 文件存储
const fs = require('fs');
const path = require('path');
const awaitWriteStream = require('await-stream-ready').write;
const sendToWormhole = require('stream-wormhole');

class UploadController extends Controller {
/**
*
* @param {*} stream 传入的Buffer流
* @param {*} paths 保存的路径
* @param {*} multiple 是否多文件
* @param {*} files 多文件返回
*/
async upload(stream, paths = "app/public/img", multiple = false, files = []) {
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
const target = path.join(this.config.baseDir, paths, filename);
const writeStream = fs.createWriteStream(target);
try {
await awaitWriteStream(stream.pipe(writeStream));
return !!multiple ? files.push(filename) : filename;
} catch (err) {
await sendToWormhole(stream);
return { code: 422, message: '上传失败,请重试!' }
}
};
// 单文件
async create() {
const ctx = this.ctx;
// 获取流
const stream = await ctx.getFileStream();
// 生成文件名
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
// pipe流写入信息
const target = path.join(this.config.baseDir, 'app/public/img', filename);
const writeStream = fs.createWriteStream(target);
try {
// 保存
await awaitWriteStream(stream.pipe(writeStream));
} catch (err) {
// 保存失败销毁stream 不然接口会pending到超时
await sendToWormhole(stream);
this.ctx.body = { code: 422, message: '上传失败,请重试!' }
throw err;
}
this.ctx.body = {
data: filename
};
};
// 多文件
async creates() {
const ctx = this.ctx;
// 获取文件流组
const streams = ctx.multipart();
let stream;
// 保存返回的文件信息
let files = [];
// 其他form 参数
let fields = {}
while ((stream = await streams()) != null) {
// 检查是否有其他参数 如果有写入 这里做案例 不做处理
if (stream.length) {
fields[stream[0]] = stream[1]
} else {
// 空文件处理
if (!stream.filename) {
return;
}
// 设置文件名称
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
// pipe 设置
const target = path.join(this.config.baseDir, 'app/public/img', filename);
const writeStream = fs.createWriteStream(target);
try {
// 保存
await awaitWriteStream(stream.pipe(writeStream));
// 写入数组
files.push({ filename, path: `/img/${filename}` })
} catch (err) {
await sendToWormhole(stream);
this.ctx.body = { code: 422, message: '上传失败,请重试!' }
throw err;
}
}
}
this.ctx.body = {
data: { files, fields }
};
}
}

module.exports = UploadController 查看全部
'use strict';
const Controller = require('egg').Controller;
// 文件存储
const fs = require('fs');
const path = require('path');
const awaitWriteStream = require('await-stream-ready').write;
const sendToWormhole = require('stream-wormhole');

class UploadController extends Controller {
/**
*
* @param {*} stream 传入的Buffer流
* @param {*} paths 保存的路径
* @param {*} multiple 是否多文件
* @param {*} files 多文件返回
*/
async upload(stream, paths = "app/public/img", multiple = false, files = []) {
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
const target = path.join(this.config.baseDir, paths, filename);
const writeStream = fs.createWriteStream(target);
try {
await awaitWriteStream(stream.pipe(writeStream));
return !!multiple ? files.push(filename) : filename;
} catch (err) {
await sendToWormhole(stream);
return { code: 422, message: '上传失败,请重试!' }
}
};
// 单文件
async create() {
const ctx = this.ctx;
// 获取流
const stream = await ctx.getFileStream();
// 生成文件名
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
// pipe流写入信息
const target = path.join(this.config.baseDir, 'app/public/img', filename);
const writeStream = fs.createWriteStream(target);
try {
// 保存
await awaitWriteStream(stream.pipe(writeStream));
} catch (err) {
// 保存失败销毁stream 不然接口会pending到超时
await sendToWormhole(stream);
this.ctx.body = { code: 422, message: '上传失败,请重试!' }
throw err;
}
this.ctx.body = {
data: filename
};
};
// 多文件
async creates() {
const ctx = this.ctx;
// 获取文件流组
const streams = ctx.multipart();
let stream;
// 保存返回的文件信息
let files = [];
// 其他form 参数
let fields = {}
while ((stream = await streams()) != null) {
// 检查是否有其他参数 如果有写入 这里做案例 不做处理
if (stream.length) {
fields[stream[0]] = stream[1]
} else {
// 空文件处理
if (!stream.filename) {
return;
}
// 设置文件名称
const filename = Math.random().toString(36).substr(2) + new Date().getTime() + path.extname(stream.filename).toLocaleLowerCase();
// pipe 设置
const target = path.join(this.config.baseDir, 'app/public/img', filename);
const writeStream = fs.createWriteStream(target);
try {
// 保存
await awaitWriteStream(stream.pipe(writeStream));
// 写入数组
files.push({ filename, path: `/img/${filename}` })
} catch (err) {
await sendToWormhole(stream);
this.ctx.body = { code: 422, message: '上传失败,请重试!' }
throw err;
}
}
}
this.ctx.body = {
data: { files, fields }
};
}
}

module.exports = UploadController

js偏函数

javascript/jQuerylopo1983 发表了文章 • 0 个评论 • 1771 次浏览 • 2019-06-10 09:50 • 来自相关话题

有一种函数叫偏函数( 左倾 ),其原理是将一些函数组合封装到一个函数中,调用时可以按顺序实现全部功能。使用时大多需要配合reduceRight
const compose = (...args) => x => args.reduceRight((result, cb) => cb(res), x); 查看全部

有一种函数叫偏函数( 左倾 ),其原理是将一些函数组合封装到一个函数中,调用时可以按顺序实现全部功能。使用时大多需要配合reduceRight


const compose = (...args) => x => args.reduceRight((result, cb) => cb(res), x);

mongodb 聚合去重

mongodblopo1983 发表了文章 • 0 个评论 • 2119 次浏览 • 2019-06-05 19:20 • 来自相关话题

db.dbname.aggregate([
// _id 以你可以排除认定为重复kv
{
$group:{_id:{title:'$title'},count:{$sum:1},dups:{$addToSet:'$_id'}}
},
{
$match:{count:{$gt:1}}
}

]).forEach(function(it){

it.dups.shift();
db.dbname.remove({_id: {$in: it.dups}});

});
当然数据太大的时候配合 limit 来选取 查看全部
db.dbname.aggregate([
// _id 以你可以排除认定为重复kv
{
$group:{_id:{title:'$title'},count:{$sum:1},dups:{$addToSet:'$_id'}}
},
{
$match:{count:{$gt:1}}
}

]).forEach(function(it){

it.dups.shift();
db.dbname.remove({_id: {$in: it.dups}});

});

当然数据太大的时候配合 limit 来选取


MongoDB4.x 远程连接及用户名密码认证登陆配置

回复

mongodblopo1983 发起了问题 • 1 人关注 • 0 个回复 • 3090 次浏览 • 2019-06-02 13:23 • 来自相关话题

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

Nodejslopo1983 发表了文章 • 0 个评论 • 1765 次浏览 • 2019-03-19 11:48 • 来自相关话题

配额
控制器
'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; 查看全部
配额
控制器
'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;

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

Nodejslopo1983 发表了文章 • 0 个评论 • 1546 次浏览 • 2019-03-15 11:27 • 来自相关话题

磁盘相关
控制器'use strict';

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

class OPBlcokStorageVolumeController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QMFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.volumeMODEL = ctx.model.Openstack.BlcokStorage.Volumes;
this.volumeSERVICE = ctx.service.openstack.blcokStorage.volume;
};
/**
* @name 磁盘列表
*
* @param {String} detail 不需要参数
*
* ----------------------------------- 分页 -------------------------------------
*
* @param {String} limit 分页>>条数/页
* @param {String} marker markerID(上页最后条id)
*
* ----------------------------------- 排序 -------------------------------------
*
* @param {String} sort_dir a|b 排序方式 ['asc':default,'desc']
* @param {String} sort_key 排序字段 [status,name,created_at]
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume?{detail}
*
*/
async index() {
const { ctx } = this;
const RESULT = await this.volumeMODEL.getUsers(this.auth.id, this.QMFN({...ctx.query }));
ctx.body = RESULT;
};
/**
* @name 获取单一磁盘
*
* @description
*
* @example GET /openstack/blcokstorage/volume/{id}
*
*/
async show() {
const ctx = this.ctx;
// const datas = await this.volumeSERVICE.show(ctx.params.id, this.auth.id);
// ctx.body = datas;
const RESULT = await this.volumeMODEL.getOne(this.auth.id, ctx.params.id, ctx.isAdmin())
ctx.body = RESULT;
};
/**
* @name 创建磁盘
*
* @description
*
* @example POST /openstack/blcokstorage/volume
*
* @param {String} size 大小
* @param {String} availability_zone Optional 可用区域
* @param {String} snapshot_id Optional 快照ID FN从快照创建
* @param {String} backup_id Optional 备份ID FN从备份创建
* @param {String} imageRef Optional 镜像ID FN从镜像创建
* @param {String} volume_type Optional 磁盘类型 默认HDD
*
*/
async create() {
const { ctx } = this;
const { availability_zone, snapshot_id, backup_id, imageRef, size = 5, volume_type = "hdd" } = ctx.request.body;
const BODYS = this.DUFN({ availability_zone, snapshot_id, backup_id, imageRef, size, volume_type })
const datas = await this.volumeMODEL.createOne(this.auth.id, BODYS)
ctx.body = datas
};
/**
* @name 调整磁盘
*
* @description
*
* @example POST /openstack/blcokstorage/volume
*
* @param {String} size 大小
* @param {String} availability_zone Optional 可用区域
* @param {String} snapshot_id Optional 快照ID FN从快照创建
* @param {String} backup_id Optional 备份ID FN从备份创建
* @param {String} imageRef Optional 镜像ID FN从镜像创建
* @param {String} volume_type Optional 磁盘类型 默认HDD
*
*/
async update() {
const ctx = this.ctx;
const BODYS = ctx.request.body;
const { action, name } = BODYS;
delete BODYS.action;
const datas = !!action ? await this.volumeSERVICE.action(this.auth.id, this.auth.ProjectID, ctx.params.id, action, BODYS) : await this.serverSERVICE.update(ctx.params.id, this.auth.id, this.DUFn({ name }));
ctx.body = datas
};
/**
* @name 删除磁盘
*
* @description
*
*/
async destroy() {
const ctx = this.ctx;
const DATAS = await this.volumeSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = DATAS
}

}

module.exports = OPBlcokStorageVolumeController;modelmodule.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const {
helper,
service
} = app.createAnonymousContext();
const {
NumToStr
} = ctx.helper;
const OSblockStorageVolumeSchema = new Schema({
// 配置
config: {},
// 公司id
_comp: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true,
index: true
},
// 支付方式
payment: {
type: String,
default: 'prepay'
},
// 到期时间
endTime: {
type: Date
},
// 后台备注描述
_description: {

},
// 源信息
_metadata: {
type: String,
index: true
},
/*---------------------------------------------------*/
"status": {
type: 'String',
default: 'creating'
},
"migration_status": {},
"user_id": {},
"attachments": ,
"availability_zone": {},
"bootable": {},
"encrypted": {},
"created_at": {},
"description": {},
"updated_at": {},
"volume_type": {},
"name": {},
"replication_status": {},
"consistencygroup_id": {},
"source_volid": {},
"imageRef": {},
"backup_id": {},
"snapshot_id": {},
"multiattach": {},
"metadata": {},
"id": {
type: String,
index: true
},
"size": {},
"os-vol-host-attr:host": {},
"os-vol-tenant-attr:tenant_id": {}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSblockStorageVolumeSchema.statics = {
getUsers: async function(_comp, queryMix) {
const serverSERVICE = ctx.service.openstack.server.servers;
const {
querys,
select,
sort,
limit,
marker
} = queryMix;
const withDetail = querys.detail === '' || querys.detail;
delete querys.detail;
let QUERTS = {};
for (const key in querys) {
QUERTS[key] = eval(`/${querys[key]}.*/i`)
}
const MODELS = (count = false) => this.find({
_comp,
...QUERTS,
...(marker && !count && {
'_id': {
"$lt": marker
}
}),
}, {
...select,
}, {
...!count && {
limit
},
'sort': {
_id: -1,
...sort
}
});
try {
const datas = await MODELS()
return {
data: {
result: datas,
limit,
marker: datas.length ? [...datas].pop()._id : '',
totalCount: await MODELS(true).count()
}
}
} catch (error) {
return error
}
},
/**
* @name 创建硬盘
* @param {*} _id
* @param {*} body
*/
createOne: async function(_id, bodys) {
try {
const {
endTime,
payment,
_metadata
} = bodys;
delete bodys._metadata;
delete bodys.endTime;
delete bodys.payment;
//
const DATAS = await service.openstack.blcokStorage.volume.create(_id, {
...bodys,
... {
"name": `volume-${Math.random().toString(36).slice(2, 8)}`
}
});
const {
id: _openstack_id
} = DATAS.data.volume;
!!DATAS.data.volume && await ctx.model.Schedule.Openstack.Volume.addOne({
_comp: _id,
_openstack_id,
type: 'volume',
active: 'creating',
_metadata
})
return !!DATAS.data.volume ? {
data: await this.create({
_comp: _id,
endTime,
payment,
_metadata,
...DATAS.data.volume
})
} : {
'code': 422,
'message': '创建失败'
};
} catch (error) {
return error
}
},
// 获取全部
getAll: async function(queryMix) {
const {
querys,
select,
pages,
sort,
dates
} = queryMix;
const MODELS = this.find({
'status': {
'$ne': 0
},
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return {
data: {
result: await MODELS,
totalCount: await MODELS.count()
}
};
},
// 查看单个
getOne: async function(_comp, id, isAdmin) {
const MODELS = this.findOne({...isAdmin ? {
id
} : {
_comp,
id
}
});
try {
const RESULT = await MODELS;
return {
data: RESULT
}
} catch (error) {
return error
}
},
// 绑定到磁盘
/**
* @name 绑定到磁盘
* @param {*} _comp 用户UUID
* @param {*} id 磁盘ID
* @param {*} type 'bind'|'unbind' 绑定方式
*/
bindToECS: async function({
_comp,
id,
type = "bind"
} = {}, bodys) {
const MODEL = this.findOneAndUpdate({
_comp,
id
}, {
'attachments': type === 'bind' ? [bodys] : ,
'status': type === 'bind' ? 'in-use' : 'available'
});
try {
const datas = await MODEL;
} catch (error) {
return error
}
}
}
return mongoose.model('openstack_block_storage_volume', OSblockStorageVolumeSchema)
}service'use strict';
const ServerIndex = require('../index')
//
class VolumeService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default;
this.actions = ':8776/v3';
};
// 获取磁盘列表
async list(querys, _ucid, project_id, isAdmin) {
try {
const detail = Object.keys(querys).includes('detail');
delete querys.detail;
let SORT = {}
if (querys.sort_key) {
SORT = { 'sort': `${querys.sort_key}:${querys.sort_dir}` };
delete querys.sort_key;
delete querys.sort_dir;
}
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : project_id}/volumes${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: {
...querys,
...querys.sort_key && SORT,
...isAdmin && { 'all_tenants': true },
},
});
return {
data: {
result: datas.volumes.map(e => {
!!detail && e.attachments.length && (e.attachment = e.attachments[0].server_id)
delete e.links;
delete e.attachments;
return e
}).filter(e => e.status != 'deleting'),
totalCount: datas.volumes.length
}
};
} catch (error) {
return error
}
}
// 获取磁盘详情
async show(id, _ucid, _description) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/volumes/${id}`, {
..._ucid !== 'admin' && { _ucid }
});
return _ucid === 'admin' ? { ...datas.volume, _description } : { data: datas.volume };
} catch (error) {
return error
}
}
// 创建磁盘
async create(_ucid, bodys, order = "false") {
const DATAS = this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/volumes`, {
body: {
'volume': {
... {
"availability_zone": null,
"source_volid": null,
"description": null,
"multiattach": false,
"snapshot_id": null,
"backup_id": null,
"imageRef": null,
"metadata": {},
"consistencygroup_id": null
},
...bodys
}
},
method: 'POST',
_ucid
});
try {
const datas = await DATAS
return !!order ? { data: datas } : datas
} catch (error) {
return error
}

}
// 删除磁盘
async destroy(_ucid, project_id, volume_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
...datas.status === 404 && {
message: `${volume_id}不存在或已删除`
}
}
} catch (error) {

}
}
// 磁盘操作
// {'os-extend':'磁盘扩容','revert':'镜像回滚'}
async action(_ucid, project_id, volume_id, type, bodys) {
const isIn = (e) => ['os-extend', 'revert'].includes(e);
if (!isIn(type)) return { code: 422 };
const actionMAP = (e) => {
const OBJ = {};
OBJ[e] = !bodys ? null : bodys;
return OBJ;
}
try {
const DATAS = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
body: actionMAP(type),
method: 'POST',
_ucid, full: true
});
return {
code: DATAS.status
}
} catch (error) {
return error
}
}
async update(_ucid, project_id, volume_id, bodys) {
try {
const DATAS = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}`, {
_ucid,
bodys: {
...bodys
},
method: 'PUT'
});
return {
data: DATAS
}
} catch (error) {
return error
}
}
}
module.exports = VolumeService;
scheduleconst Subscription = require('egg').Subscription;

class getVolumeStatus extends Subscription {
constructor(ctx) {
super(ctx);
this.SERVICE = ctx.service.openstack.blcokStorage.volume;
this.MODELS = ctx.model.Schedule.Openstack.Volume;
this.volumeMODEL = ctx.model.Openstack.BlcokStorage.Volumes;
}
static get schedule() {
return {
interval: '10s',
type: 'worker',
};
}
async subscribe() {
const ctx = this.ctx;
const lists = await this.MODELS.getALL();
if (lists.length) {
const datas = await this.checkItem(lists);
}
}
async checkItem(result) {
// 调取记录
const checkFN = result.map(e => {
// 记录尝试次数
this.MODELS.retry(e._openstack_id)
return this.SERVICE.show(e._openstack_id, 'admin', e);
})
let DATAS = await Promise.all(checkFN);
// 检查ACTION
if (!!DATAS.length) {
const endOrder = DATAS.map(e => {
const { _comp, _openstack_id: id } = e._description;
delete e._description
delete e.links
if (e.status === 'available' || e.status === 'in-use') {
return this.volumeMODEL.findOneAndUpdate({ id }, { ...e });
}
})
DATAS = await Promise.all(endOrder);
};
// 清除已完成任务
if (DATAS.length) {
const clearSchedule = DATAS.map(e => {
if (!!e) {
const { id: _openstack_id } = e;
return this.MODELS.deleteOne({ '_openstack_id': _openstack_id })
}
})
DATAS = await Promise.all(clearSchedule);
}
}
}

module.exports = getVolumeStatus;
schedule model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const scheduleOSVolumeSchema = new Schema({
// 类型
type: {
type: String,
},
// 尝试次数
retry: {
type: Number,
dafault: 0
},
// 是否创建成功
status: {
type: Boolean,
default: false
},
// 操作方式 [BUILD:创建,DELETE:删除]
active: {
type: String,
},
// 数据ID
_openstack_id: {
type: String,
index: true
},
// 公司ID
_comp: {
type: String
},
// 其他配置参数
/**
* ecs:Number
*/
_description: {

},
// 其他配置元信息
_metadata: {

}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
scheduleOSVolumeSchema.statics = {
getALL: async function (limit = 50) {
const MODEL = this.find().limit(limit);
try {
return await MODEL;
} catch (error) {
return error
}
},
addOne: async function (bodys) {
const MODEL = this.create({ ...bodys });
try {
const result = await MODEL;
return { code: !!result ? 201 : 404 }
} catch (error) {
return error
}
},
// destroyOne: async function (id) {
// const MODEL = this.deleteOne(id);
// try {
// const result = await MODEL
// return result
// } catch (error) {
// return error
// }
// },
retry: async function (_openstack_id) {
const MODEL = this.findOneAndUpdate({ _openstack_id }, {
'$inc': { retry: 1 }
});
try {
await MODEL
} catch (error) {
return error
}
}
}
return mongoose.model('schedule_openstack_volume', scheduleOSVolumeSchema);
} 查看全部
磁盘相关
控制器
'use strict';

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

class OPBlcokStorageVolumeController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QMFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.volumeMODEL = ctx.model.Openstack.BlcokStorage.Volumes;
this.volumeSERVICE = ctx.service.openstack.blcokStorage.volume;
};
/**
* @name 磁盘列表
*
* @param {String} detail 不需要参数
*
* ----------------------------------- 分页 -------------------------------------
*
* @param {String} limit 分页>>条数/页
* @param {String} marker markerID(上页最后条id)
*
* ----------------------------------- 排序 -------------------------------------
*
* @param {String} sort_dir a|b 排序方式 ['asc':default,'desc']
* @param {String} sort_key 排序字段 [status,name,created_at]
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume?{detail}
*
*/
async index() {
const { ctx } = this;
const RESULT = await this.volumeMODEL.getUsers(this.auth.id, this.QMFN({...ctx.query }));
ctx.body = RESULT;
};
/**
* @name 获取单一磁盘
*
* @description
*
* @example GET /openstack/blcokstorage/volume/{id}
*
*/
async show() {
const ctx = this.ctx;
// const datas = await this.volumeSERVICE.show(ctx.params.id, this.auth.id);
// ctx.body = datas;
const RESULT = await this.volumeMODEL.getOne(this.auth.id, ctx.params.id, ctx.isAdmin())
ctx.body = RESULT;
};
/**
* @name 创建磁盘
*
* @description
*
* @example POST /openstack/blcokstorage/volume
*
* @param {String} size 大小
* @param {String} availability_zone Optional 可用区域
* @param {String} snapshot_id Optional 快照ID FN从快照创建
* @param {String} backup_id Optional 备份ID FN从备份创建
* @param {String} imageRef Optional 镜像ID FN从镜像创建
* @param {String} volume_type Optional 磁盘类型 默认HDD
*
*/
async create() {
const { ctx } = this;
const { availability_zone, snapshot_id, backup_id, imageRef, size = 5, volume_type = "hdd" } = ctx.request.body;
const BODYS = this.DUFN({ availability_zone, snapshot_id, backup_id, imageRef, size, volume_type })
const datas = await this.volumeMODEL.createOne(this.auth.id, BODYS)
ctx.body = datas
};
/**
* @name 调整磁盘
*
* @description
*
* @example POST /openstack/blcokstorage/volume
*
* @param {String} size 大小
* @param {String} availability_zone Optional 可用区域
* @param {String} snapshot_id Optional 快照ID FN从快照创建
* @param {String} backup_id Optional 备份ID FN从备份创建
* @param {String} imageRef Optional 镜像ID FN从镜像创建
* @param {String} volume_type Optional 磁盘类型 默认HDD
*
*/
async update() {
const ctx = this.ctx;
const BODYS = ctx.request.body;
const { action, name } = BODYS;
delete BODYS.action;
const datas = !!action ? await this.volumeSERVICE.action(this.auth.id, this.auth.ProjectID, ctx.params.id, action, BODYS) : await this.serverSERVICE.update(ctx.params.id, this.auth.id, this.DUFn({ name }));
ctx.body = datas
};
/**
* @name 删除磁盘
*
* @description
*
*/
async destroy() {
const ctx = this.ctx;
const DATAS = await this.volumeSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = DATAS
}

}

module.exports = OPBlcokStorageVolumeController;
model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const {
helper,
service
} = app.createAnonymousContext();
const {
NumToStr
} = ctx.helper;
const OSblockStorageVolumeSchema = new Schema({
// 配置
config: {},
// 公司id
_comp: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true,
index: true
},
// 支付方式
payment: {
type: String,
default: 'prepay'
},
// 到期时间
endTime: {
type: Date
},
// 后台备注描述
_description: {

},
// 源信息
_metadata: {
type: String,
index: true
},
/*---------------------------------------------------*/
"status": {
type: 'String',
default: 'creating'
},
"migration_status": {},
"user_id": {},
"attachments": ,
"availability_zone": {},
"bootable": {},
"encrypted": {},
"created_at": {},
"description": {},
"updated_at": {},
"volume_type": {},
"name": {},
"replication_status": {},
"consistencygroup_id": {},
"source_volid": {},
"imageRef": {},
"backup_id": {},
"snapshot_id": {},
"multiattach": {},
"metadata": {},
"id": {
type: String,
index: true
},
"size": {},
"os-vol-host-attr:host": {},
"os-vol-tenant-attr:tenant_id": {}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSblockStorageVolumeSchema.statics = {
getUsers: async function(_comp, queryMix) {
const serverSERVICE = ctx.service.openstack.server.servers;
const {
querys,
select,
sort,
limit,
marker
} = queryMix;
const withDetail = querys.detail === '' || querys.detail;
delete querys.detail;
let QUERTS = {};
for (const key in querys) {
QUERTS[key] = eval(`/${querys[key]}.*/i`)
}
const MODELS = (count = false) => this.find({
_comp,
...QUERTS,
...(marker && !count && {
'_id': {
"$lt": marker
}
}),
}, {
...select,
}, {
...!count && {
limit
},
'sort': {
_id: -1,
...sort
}
});
try {
const datas = await MODELS()
return {
data: {
result: datas,
limit,
marker: datas.length ? [...datas].pop()._id : '',
totalCount: await MODELS(true).count()
}
}
} catch (error) {
return error
}
},
/**
* @name 创建硬盘
* @param {*} _id
* @param {*} body
*/
createOne: async function(_id, bodys) {
try {
const {
endTime,
payment,
_metadata
} = bodys;
delete bodys._metadata;
delete bodys.endTime;
delete bodys.payment;
//
const DATAS = await service.openstack.blcokStorage.volume.create(_id, {
...bodys,
... {
"name": `volume-${Math.random().toString(36).slice(2, 8)}`
}
});
const {
id: _openstack_id
} = DATAS.data.volume;
!!DATAS.data.volume && await ctx.model.Schedule.Openstack.Volume.addOne({
_comp: _id,
_openstack_id,
type: 'volume',
active: 'creating',
_metadata
})
return !!DATAS.data.volume ? {
data: await this.create({
_comp: _id,
endTime,
payment,
_metadata,
...DATAS.data.volume
})
} : {
'code': 422,
'message': '创建失败'
};
} catch (error) {
return error
}
},
// 获取全部
getAll: async function(queryMix) {
const {
querys,
select,
pages,
sort,
dates
} = queryMix;
const MODELS = this.find({
'status': {
'$ne': 0
},
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return {
data: {
result: await MODELS,
totalCount: await MODELS.count()
}
};
},
// 查看单个
getOne: async function(_comp, id, isAdmin) {
const MODELS = this.findOne({...isAdmin ? {
id
} : {
_comp,
id
}
});
try {
const RESULT = await MODELS;
return {
data: RESULT
}
} catch (error) {
return error
}
},
// 绑定到磁盘
/**
* @name 绑定到磁盘
* @param {*} _comp 用户UUID
* @param {*} id 磁盘ID
* @param {*} type 'bind'|'unbind' 绑定方式
*/
bindToECS: async function({
_comp,
id,
type = "bind"
} = {}, bodys) {
const MODEL = this.findOneAndUpdate({
_comp,
id
}, {
'attachments': type === 'bind' ? [bodys] : ,
'status': type === 'bind' ? 'in-use' : 'available'
});
try {
const datas = await MODEL;
} catch (error) {
return error
}
}
}
return mongoose.model('openstack_block_storage_volume', OSblockStorageVolumeSchema)
}
service
'use strict';
const ServerIndex = require('../index')
//
class VolumeService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default;
this.actions = ':8776/v3';
};
// 获取磁盘列表
async list(querys, _ucid, project_id, isAdmin) {
try {
const detail = Object.keys(querys).includes('detail');
delete querys.detail;
let SORT = {}
if (querys.sort_key) {
SORT = { 'sort': `${querys.sort_key}:${querys.sort_dir}` };
delete querys.sort_key;
delete querys.sort_dir;
}
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : project_id}/volumes${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: {
...querys,
...querys.sort_key && SORT,
...isAdmin && { 'all_tenants': true },
},
});
return {
data: {
result: datas.volumes.map(e => {
!!detail && e.attachments.length && (e.attachment = e.attachments[0].server_id)
delete e.links;
delete e.attachments;
return e
}).filter(e => e.status != 'deleting'),
totalCount: datas.volumes.length
}
};
} catch (error) {
return error
}
}
// 获取磁盘详情
async show(id, _ucid, _description) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/volumes/${id}`, {
..._ucid !== 'admin' && { _ucid }
});
return _ucid === 'admin' ? { ...datas.volume, _description } : { data: datas.volume };
} catch (error) {
return error
}
}
// 创建磁盘
async create(_ucid, bodys, order = "false") {
const DATAS = this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/volumes`, {
body: {
'volume': {
... {
"availability_zone": null,
"source_volid": null,
"description": null,
"multiattach": false,
"snapshot_id": null,
"backup_id": null,
"imageRef": null,
"metadata": {},
"consistencygroup_id": null
},
...bodys
}
},
method: 'POST',
_ucid
});
try {
const datas = await DATAS
return !!order ? { data: datas } : datas
} catch (error) {
return error
}

}
// 删除磁盘
async destroy(_ucid, project_id, volume_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
...datas.status === 404 && {
message: `${volume_id}不存在或已删除`
}
}
} catch (error) {

}
}
// 磁盘操作
// {'os-extend':'磁盘扩容','revert':'镜像回滚'}
async action(_ucid, project_id, volume_id, type, bodys) {
const isIn = (e) => ['os-extend', 'revert'].includes(e);
if (!isIn(type)) return { code: 422 };
const actionMAP = (e) => {
const OBJ = {};
OBJ[e] = !bodys ? null : bodys;
return OBJ;
}
try {
const DATAS = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
body: actionMAP(type),
method: 'POST',
_ucid, full: true
});
return {
code: DATAS.status
}
} catch (error) {
return error
}
}
async update(_ucid, project_id, volume_id, bodys) {
try {
const DATAS = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}`, {
_ucid,
bodys: {
...bodys
},
method: 'PUT'
});
return {
data: DATAS
}
} catch (error) {
return error
}
}
}
module.exports = VolumeService;

schedule
const Subscription = require('egg').Subscription;

class getVolumeStatus extends Subscription {
constructor(ctx) {
super(ctx);
this.SERVICE = ctx.service.openstack.blcokStorage.volume;
this.MODELS = ctx.model.Schedule.Openstack.Volume;
this.volumeMODEL = ctx.model.Openstack.BlcokStorage.Volumes;
}
static get schedule() {
return {
interval: '10s',
type: 'worker',
};
}
async subscribe() {
const ctx = this.ctx;
const lists = await this.MODELS.getALL();
if (lists.length) {
const datas = await this.checkItem(lists);
}
}
async checkItem(result) {
// 调取记录
const checkFN = result.map(e => {
// 记录尝试次数
this.MODELS.retry(e._openstack_id)
return this.SERVICE.show(e._openstack_id, 'admin', e);
})
let DATAS = await Promise.all(checkFN);
// 检查ACTION
if (!!DATAS.length) {
const endOrder = DATAS.map(e => {
const { _comp, _openstack_id: id } = e._description;
delete e._description
delete e.links
if (e.status === 'available' || e.status === 'in-use') {
return this.volumeMODEL.findOneAndUpdate({ id }, { ...e });
}
})
DATAS = await Promise.all(endOrder);
};
// 清除已完成任务
if (DATAS.length) {
const clearSchedule = DATAS.map(e => {
if (!!e) {
const { id: _openstack_id } = e;
return this.MODELS.deleteOne({ '_openstack_id': _openstack_id })
}
})
DATAS = await Promise.all(clearSchedule);
}
}
}

module.exports = getVolumeStatus;

schedule model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const scheduleOSVolumeSchema = new Schema({
// 类型
type: {
type: String,
},
// 尝试次数
retry: {
type: Number,
dafault: 0
},
// 是否创建成功
status: {
type: Boolean,
default: false
},
// 操作方式 [BUILD:创建,DELETE:删除]
active: {
type: String,
},
// 数据ID
_openstack_id: {
type: String,
index: true
},
// 公司ID
_comp: {
type: String
},
// 其他配置参数
/**
* ecs:Number
*/
_description: {

},
// 其他配置元信息
_metadata: {

}
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
scheduleOSVolumeSchema.statics = {
getALL: async function (limit = 50) {
const MODEL = this.find().limit(limit);
try {
return await MODEL;
} catch (error) {
return error
}
},
addOne: async function (bodys) {
const MODEL = this.create({ ...bodys });
try {
const result = await MODEL;
return { code: !!result ? 201 : 404 }
} catch (error) {
return error
}
},
// destroyOne: async function (id) {
// const MODEL = this.deleteOne(id);
// try {
// const result = await MODEL
// return result
// } catch (error) {
// return error
// }
// },
retry: async function (_openstack_id) {
const MODEL = this.findOneAndUpdate({ _openstack_id }, {
'$inc': { retry: 1 }
});
try {
await MODEL
} catch (error) {
return error
}
}
}
return mongoose.model('schedule_openstack_volume', scheduleOSVolumeSchema);
}

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

Nodejslopo1983 发表了文章 • 0 个评论 • 1482 次浏览 • 2019-03-15 11:22 • 来自相关话题

磁盘备份
控制器
'use strict';

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

class OPBlcokStorageVolumeBackupsController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QPFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.snapshotMODEL = ctx.model.Openstack.BlcokStorage.Snapshot;
this.backupSERVICE = ctx.service.openstack.blcokStorage.backup;
}
/**
* @name 磁盘备份列表
*
* @param {String} ctx.query 不需要参数
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume
*
*/
async index() {
const { ctx } = this;
// const datas = !!ctx.isAdmin() ? await this.snapshotMODEL.getAll(this.QPFN(ctx.query)) : await this.snapshotMODEL.getUsers(this.auth.id, this.QPFN(ctx.query));
const datas = await this.backupSERVICE.list(this.QPFN(ctx.query), this.auth.id, ctx.isAdmin())
ctx.body = datas;
}
/**
* @name 获取单一磁盘备份
*
* @description
*
* @example GET /openstack/blcokstorage/snapshot/{id}
*
*/
async show() {
const ctx = this.ctx;
const datas = await this.backupSERVICE.show(ctx.params.id, this.auth.id);
ctx.body = datas;
}
/**
* @name 创建磁盘备份
*
* @param {String} name Optional 磁盘备份名称
* @param {String} description Optional 备注
* @param {String} volume_id 磁盘ID
* @param {Boolean} force Optional 是否备份
* @param {Object} metadata Optional One or more metadata key and value pairs for the snapshot.
*
* @description
*
* @example POST /openstack/blcokstorage/snapshot
*
*/
async create() {
const ctx = this.ctx;
const { name, description, volume_id, force, metadata } = ctx.request.body;
const BODYS = this.DUFN({ name, description, volume_id, force, metadata });
const datas = await this.snapshotMODEL.createOne(this.auth.id, BODYS);
ctx.body = datas
}
/**
* @name 删除备份
*
* @param {*} snapshot_id 备份ID
*
*/
async destroy() {
const ctx = this.ctx;
const datas = await this.backupSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = datas
}
}

module.exports = OPBlcokStorageVolumeBackupsController;
service
'use strict';
const ServerIndex = require('../index')
//
class VolumeSnapshotService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default
this.actions = ':8776/v3'
};
// 获取磁盘备份
async list(query, _ucid, isAdmin) {
const detail = Object.keys(query).includes('detail');
delete query.detail
try {
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : await this.getProject(_ucid)}/backups${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: { ...isAdmin && { 'all_tenants': true } },
full: true
});
return {
data: {
result: datas,
// totalCount: datas.snapshots.length
}
};
} catch (error) {
return error
}
};
// 获取磁盘备份详情
async show(id, _ucid) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/backups/${id}`, { _ucid });
return { data: datas.snapshot };
} catch (error) {
return error
}
}
// 创建磁盘备份
async create(_ucid, bodys, type = 'manual') {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/backups`, {
body: {
'backups': {
... {
"name": `${!bodys.name && `backups-${type}-${Math.random().toString(36).slice(2, 8)}`}`,
"force": true,
"metadata": null
},
...bodys
}
},
method: 'POST',
_ucid
});
return datas
} catch (error) {
return error
}
}
/**
* @name 删除
* @param {*} _ucid 用户id
* @param {*} project_id 密钥对名称
* @param {*} snapshot_id 备份ID
*/
async destroy(_ucid, project_id, snapshot_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/backups/${snapshot_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
message: datas.status === 404 && `${keypair_name}不存在或已删除`
}
} catch (error) {

}
}
}
module.exports = VolumeSnapshotService;磁盘辅助操作配置
控制器
'use strict';

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

class OPBlcokStorageController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.DUFN = ctx.helper.deleteUndefined;
this.SERVICE = ctx.service.openstack.blcokStorage.index
};
/**
* name 储存相关配额
*
* @description
*
* @example GET /openstack/blcokstorage/limits
*
* @return {Object}
* - BackupGigabytes 备份可用磁盘可用容量 GB
* - Backups 备份可用个数 个
* - Gigabytes 磁盘可用量 GB
* - Snapshots 磁盘快照可用个数 个
* - Volumes 磁盘可用个数 个
*/
async limits() {
const { ctx } = this;
const DATAS = await this.SERVICE.limits(this.auth.ProjectID, this.auth.id);
ctx.body = DATAS
};
/**
* @name 加载磁盘到服务器
*
* @description
*
* @param {String} instance_uuid 服务器ID
* @param {String} :id 磁盘ID
*
*/
async attach_volume() {
const ctx = this.ctx;
const { instance_uuid, mode } = ctx.request.body;
const BODYS = this.DUFN({ instance_uuid, mode });
const DATAS = await this.SERVICE.attachVolume(this.auth.id, ctx.params.id, BODYS, this.auth.ProjectID);
ctx.body = DATAS;
};
/**
* @name 卸载磁盘
*
* @description
*
* @param {String} attachment_id 服务器ID
* @param {String} :id 磁盘ID
*
*/
async detach_volume() {
const ctx = this.ctx;
const { attachment_id } = ctx.request.body;
const DATAS = await this.SERVICE.detachVolume(this.auth.id, this.auth.ProjectID, ctx.params.id, attachment_id);
ctx.body = DATAS;
};
/**
* @name 创建磁盘镜像
*
* @description
*
* @param {String} image_name 镜像名称
* @param {String} volume_id 磁盘ID
*
*/
async create_image() {
const ctx = this.ctx;
const { image_name, volume_id } = ctx.request.body;
const DATAS = await this.SERVICE.createImage(this.auth.id, this.auth.ProjectID, image_name, volume_id);
ctx.body = DATAS
};
/**
* @name 磁盘扩容
*
* @param {Number} new_size 扩容容量
*/
async new_size_volume() {
const ctx = this.ctx;
const { volumeID } = ctx.params;
const { new_size } = ctx.request.body;
const DATAS = await this.SERVICE.newSizeVolume(this.auth.id, this.auth.ProjectID, volumeID, new_size);
ctx.body = DATAS
};
}

module.exports = OPBlcokStorageController;service
'use strict';
const ServerIndex = require('../index')
//
class volumeService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.nextChars = ctx.helper.nextChars;
this.actions = ':8776/v3'
};
// 获取用户磁盘配额
async limits(project_id, _ucid) {
const DATAS = this.OSAJax(`${this.actions}/${project_id}/limits`, { _ucid })
try {
const { maxTotalBackupGigabytes, maxTotalBackups, maxTotalSnapshots, maxTotalVolumeGigabytes, maxTotalVolumes, totalBackupGigabytesUsed, totalBackupsUsed, totalGigabytesUsed, totalSnapshotsUsed, totalVolumesUsed } = (await DATAS).limits.absolute;
return {
data: {
maxTotalBackupGigabytes,
totalBackupGigabytesUsed,
'BackupGigabytes': maxTotalBackupGigabytes - totalBackupGigabytesUsed,
maxTotalBackups,
totalBackupsUsed,
'Backups': maxTotalBackups - totalBackupsUsed,
maxTotalVolumeGigabytes,
totalGigabytesUsed,
'Gigabytes': maxTotalVolumeGigabytes - totalGigabytesUsed,
maxTotalSnapshots,
totalSnapshotsUsed,
'Snapshots': maxTotalSnapshots - totalSnapshotsUsed,
maxTotalVolumes,
totalVolumesUsed,
'Volumes': maxTotalVolumes - totalVolumesUsed
}
}
} catch (error) {
console.log(error)
return error
}
};
// 磁盘挂载到服务器
async attachVolume(_ucid, volume_uuid, body, project_id) {
// const charCount = (await this.OSAJax(`:8774/v2.1/servers/${body.instance_uuid}`, { _ucid })).server['os-extended-volumes:volumes_attached'].length;
// console.log(charCount)

const DATAS = this.OSAJax(`${this.actions}/${!!project_id ? project_id : await this.getProject(_ucid)}/attachments/`, {
body: {
'attachment': {
...body,
volume_uuid
}
},
full: true,
method: 'POST',
_ucid
});
try {
const datas = await DATAS;
return {
code: datas.status,
data: datas.data
}
} catch (error) {}
};
// 磁盘从服务器卸载
async detachVolume(_ucid, ProjectID, volume_id, attachment_id) {
const DATAS = this.OSAJax(`${this.actions}/${ProjectID}/volumes/${volume_id}/action`, {
body: {
"os-detach": {
attachment_id
}
},
full: true,
method: 'POST',
_ucid
});
try {
const datas = await DATAS;
return {
code: datas.status,
}
} catch (error) {

}
};
// 扩容
async newSizeVolume(_ucid, project_id, volume_id, new_size) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
_ucid,
method: 'POST',
body: {
"os-extend": {
new_size
}
},
full: true
});
return {
code: datas.status,
message: datas.status === 400 && `${datas.data.badRequest.message}`
}
} catch (error) {
return error
}
};
// 创建磁盘镜像
async createImage(_ucid, project_id, image_name, volume_id) {
const datas = this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
_ucid,
method: 'POST',
body: {
'os-volume_upload_image': {
... {
// "force": false,
// "disk_format": "raw",
// "container_format": "bare",
"visibility": "private",
// "protected": false
},
image_name
}
}
});
try {
throw new Error('无权限')
return {
data: (await datas)['os-volume_upload_image']
}
} catch (error) {
return error
}
};
}
module.exports = volumeService;磁盘快照
控制器
'use strict';

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

class OPBlcokStorageVolumeSnapshotsController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QPFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.snapshotMODEL = ctx.model.Openstack.BlcokStorage.Snapshot;
this.snapshotSERVICE = ctx.service.openstack.blcokStorage.snapshots;
}
/**
* @name 磁盘快照列表
*
* @param {String} ctx.query 不需要参数
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume
*
*/
async index() {
const { ctx } = this;
// const datas = !!ctx.isAdmin() ? await this.snapshotMODEL.getAll(this.QPFN(ctx.query)) : await this.snapshotMODEL.getUsers(this.auth.id, this.QPFN(ctx.query));
const datas = await this.snapshotSERVICE.list(this.QPFN(ctx.query), this.auth.id, ctx.isAdmin())
ctx.body = datas;
}
/**
* @name 获取单一磁盘快照
*
* @description
*
* @example GET /openstack/blcokstorage/snapshot/{id}
*
*/
async show() {
const ctx = this.ctx;
const datas = await this.snapshotSERVICE.show(ctx.params.id, this.auth.id);
ctx.body = datas;
}
/**
* @name 创建磁盘快照
*
* @param {String} name Optional 磁盘快照名称
* @param {String} description Optional 备注
* @param {String} volume_id 磁盘ID
* @param {Boolean} force Optional 是否备份
* @param {Object} metadata Optional One or more metadata key and value pairs for the snapshot.
*
* @description
*
* @example POST /openstack/blcokstorage/snapshot
*
*/
async create() {
const ctx = this.ctx;
const { name, description, volume_id, force, metadata } = ctx.request.body;
const BODYS = this.DUFN({ name, description, volume_id, force, metadata });
const datas = await this.snapshotMODEL.createOne(this.auth.id, BODYS);
ctx.body = datas
}
/**
* @name 删除快照
*
* @param {*} snapshot_id 快照ID
*
*/
async destroy() {
const ctx = this.ctx;
const datas = await this.snapshotSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = datas
}
}

module.exports = OPBlcokStorageVolumeSnapshotsController;
service
'use strict';
const ServerIndex = require('../index')
//
class VolumeSnapshotService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default
this.actions = ':8776/v3'
};
// 获取磁盘快照
async list(query, _ucid, isAdmin) {
const detail = Object.keys(query).includes('detail');
delete query.detail
try {
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : await this.getProject(_ucid)}/snapshots${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: { ...isAdmin && { 'all_tenants': true } }
});
return {
data: {
result: datas.snapshots,
totalCount: datas.snapshots.length
}
};
} catch (error) {
return error
}
};
// 获取磁盘快照详情
async show(id, _ucid) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/snapshots/${id}`, { _ucid });
return { data: datas.snapshot };
} catch (error) {
return error
}
}
// 创建磁盘快照
async create(_ucid, bodys, type = 'manual') {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/snapshots`, {
body: {
'snapshot': {
... {
"name": `${!bodys.name && `Snapshot-${type}-${Math.random().toString(36).slice(2, 8)}`}`,
"force": true,
"metadata": null
},
...bodys
}
},
method: 'POST',
_ucid
});
return datas
} catch (error) {
return error
}
}
/**
* @name 删除
* @param {*} _ucid 用户id
* @param {*} project_id 密钥对名称
* @param {*} snapshot_id 快照ID
*/
async destroy(_ucid, project_id, snapshot_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/snapshots/${snapshot_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
message: datas.status === 404 && `${keypair_name}不存在或已删除`
}
} catch (error) {

}
}
}
module.exports = VolumeSnapshotService;model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const { helper, service } = app.createAnonymousContext();
const { NumToStr } = ctx.helper;
const OSblockStorageSnapshotSchema = new Schema({
// 快照类型
type: String,
// 快照状态
// status: String,
// 快照大小
size: Number,
// 快照元信息
metadata: {},
// 快照名称
name: String,
// 快照所属磁盘ID
volume_id: String,
// 创建时间
created_at: Date,
// 描述 prjectID Date
description: String,
// 快照ID
id: String,
// 更新时间
updated_at: Date,
// 公司id
_comp: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true,
index: true
},
// 状态 ['删除','正常','锁定'] 0,1,2
_status: {
type: Number,
default: 1,
get: v => NumToStr(['删除', '正常', '锁定'], v, true),
},
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSblockStorageSnapshotSchema.statics = {
getUsers: async function (_id, queryMix) {
const { querys, select, pages, sort, dates } = queryMix;
const MODELS = this.find({
'_comp': _id,
...querys,
}, {
...select,
})
.limit(!!pages ? pages.limit : 10)
.sort({
'_id': -1,
...sort
});
return {
data: {
result: await MODELS,
totalCount: await MODELS.count()
}
}
},
createOne: async function (_id, body, type) {
const DATAS = await service.openstack.blcokStorage.snapshots.create(_id, body, type);
const MODEL = this.create({
_comp: _id,
...DATAS['snapshot']
});
try {
return { data: await MODEL };
} catch (error) {
return error
}
},
// 获取全部
getAll: async function (queryMix) {
const { querys, select, pages, sort, dates } = queryMix;
const MODELS = this.find({
'status': { '$ne': 0 },
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return { data: { result: await MODELS, totalCount: await MODELS.count() } };
}
}
return mongoose.model('openstack_block_storage_snapshot', OSblockStorageSnapshotSchema)
}

磁盘类型
控制器
'use strict';

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

class blcokStorageTypesController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QMFN = ctx.helper.queryParamFn;
this.deleteUndefined = ctx.helper.deleteUndefined;
this.volumeTypesMODEL = ctx.model.Openstack.BlcokStorage.Types;
this.volumeTypesSERVICE = ctx.service.openstack.blcokStorage.types
}
/**
* @name servers type 列表
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/
*
*/
async index() {
const {
ctx,
service
} = this;
const datas = await this.volumeTypesMODEL.getAll(this.auth.id, ctx.isAdmin());
// const datas = await this.volumeTypesSERVICE.list(this.auth.id, this.auth.ProjectID, ctx.isAdmin())
ctx.body = datas;
}
}

module.exports = blcokStorageTypesController;service
'use strict';
const ServerIndex = require('../index')
//
class volumeTypesService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8776/v3'
};
// 获取磁盘类型列表
async list(_ucid, project_id, isAdmin) {
const {
config
} = this;
const datas = await this.OSAJax(`${this.actions}/${project_id || config.openstack.projectID.default}/types`, {
...!isAdmin && _ucid
});
try {
return {
data: {
result: datas.volume_types,
totalCount: datas.volume_types.length
}
};
} catch (error) {
return error
}
}
}
module.exports = volumeTypesService;MODEL
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const { NumToStr } = ctx.helper;
const OSStorageTypeSchema = new Schema({
// opID
id: String,
// 是否公用
is_public: Boolean,
// 磁盘名称
name: String,
// 磁盘描述备注
description: String,
// 状态 ['删除','正常','锁定'] 0,1,2
status: {
type: Number,
default: 1,
get: v => NumToStr(['删除', '正常', '锁定'], v, true),
},
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSStorageTypeSchema.statics = {
// 同步openstack 实例配置
syncFN: async function () {
try {
const datas = (await ctx.service.openstack.blcokStorage.types.list()).data;
if (datas.totalCount) {
let updateModel = datas.result.map(e => this.findOneAndUpdate({ 'id': e.id }, { ...e }, { 'upsert': true, 'new': true }));
let result = await Promise.all(updateModel);
return result;
}
} catch (error) {
return error
}
},
// 获取全部
getAll: async function (queryMix, isAdmin) {
const { querys, select, pages, sort, dates } = queryMix;
const models = this.find({
'status': { '$ne': 0 },
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
...!isAdmin && {
'__v': 0,
'created': 0,
'updated': 0,
'_id': 0
}
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return { data: { result: await models, totalCount: await models.count() } };
}
}
return mongoose.model('openstack_block_storage_types', OSStorageTypeSchema)
} 查看全部
磁盘备份
控制器
'use strict';

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

class OPBlcokStorageVolumeBackupsController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QPFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.snapshotMODEL = ctx.model.Openstack.BlcokStorage.Snapshot;
this.backupSERVICE = ctx.service.openstack.blcokStorage.backup;
}
/**
* @name 磁盘备份列表
*
* @param {String} ctx.query 不需要参数
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume
*
*/
async index() {
const { ctx } = this;
// const datas = !!ctx.isAdmin() ? await this.snapshotMODEL.getAll(this.QPFN(ctx.query)) : await this.snapshotMODEL.getUsers(this.auth.id, this.QPFN(ctx.query));
const datas = await this.backupSERVICE.list(this.QPFN(ctx.query), this.auth.id, ctx.isAdmin())
ctx.body = datas;
}
/**
* @name 获取单一磁盘备份
*
* @description
*
* @example GET /openstack/blcokstorage/snapshot/{id}
*
*/
async show() {
const ctx = this.ctx;
const datas = await this.backupSERVICE.show(ctx.params.id, this.auth.id);
ctx.body = datas;
}
/**
* @name 创建磁盘备份
*
* @param {String} name Optional 磁盘备份名称
* @param {String} description Optional 备注
* @param {String} volume_id 磁盘ID
* @param {Boolean} force Optional 是否备份
* @param {Object} metadata Optional One or more metadata key and value pairs for the snapshot.
*
* @description
*
* @example POST /openstack/blcokstorage/snapshot
*
*/
async create() {
const ctx = this.ctx;
const { name, description, volume_id, force, metadata } = ctx.request.body;
const BODYS = this.DUFN({ name, description, volume_id, force, metadata });
const datas = await this.snapshotMODEL.createOne(this.auth.id, BODYS);
ctx.body = datas
}
/**
* @name 删除备份
*
* @param {*} snapshot_id 备份ID
*
*/
async destroy() {
const ctx = this.ctx;
const datas = await this.backupSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = datas
}
}

module.exports = OPBlcokStorageVolumeBackupsController;
service
'use strict';
const ServerIndex = require('../index')
//
class VolumeSnapshotService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default
this.actions = ':8776/v3'
};
// 获取磁盘备份
async list(query, _ucid, isAdmin) {
const detail = Object.keys(query).includes('detail');
delete query.detail
try {
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : await this.getProject(_ucid)}/backups${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: { ...isAdmin && { 'all_tenants': true } },
full: true
});
return {
data: {
result: datas,
// totalCount: datas.snapshots.length
}
};
} catch (error) {
return error
}
};
// 获取磁盘备份详情
async show(id, _ucid) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/backups/${id}`, { _ucid });
return { data: datas.snapshot };
} catch (error) {
return error
}
}
// 创建磁盘备份
async create(_ucid, bodys, type = 'manual') {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/backups`, {
body: {
'backups': {
... {
"name": `${!bodys.name && `backups-${type}-${Math.random().toString(36).slice(2, 8)}`}`,
"force": true,
"metadata": null
},
...bodys
}
},
method: 'POST',
_ucid
});
return datas
} catch (error) {
return error
}
}
/**
* @name 删除
* @param {*} _ucid 用户id
* @param {*} project_id 密钥对名称
* @param {*} snapshot_id 备份ID
*/
async destroy(_ucid, project_id, snapshot_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/backups/${snapshot_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
message: datas.status === 404 && `${keypair_name}不存在或已删除`
}
} catch (error) {

}
}
}
module.exports = VolumeSnapshotService;
磁盘辅助操作配置
控制器
'use strict';

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

class OPBlcokStorageController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.DUFN = ctx.helper.deleteUndefined;
this.SERVICE = ctx.service.openstack.blcokStorage.index
};
/**
* name 储存相关配额
*
* @description
*
* @example GET /openstack/blcokstorage/limits
*
* @return {Object}
* - BackupGigabytes 备份可用磁盘可用容量 GB
* - Backups 备份可用个数 个
* - Gigabytes 磁盘可用量 GB
* - Snapshots 磁盘快照可用个数 个
* - Volumes 磁盘可用个数 个
*/
async limits() {
const { ctx } = this;
const DATAS = await this.SERVICE.limits(this.auth.ProjectID, this.auth.id);
ctx.body = DATAS
};
/**
* @name 加载磁盘到服务器
*
* @description
*
* @param {String} instance_uuid 服务器ID
* @param {String} :id 磁盘ID
*
*/
async attach_volume() {
const ctx = this.ctx;
const { instance_uuid, mode } = ctx.request.body;
const BODYS = this.DUFN({ instance_uuid, mode });
const DATAS = await this.SERVICE.attachVolume(this.auth.id, ctx.params.id, BODYS, this.auth.ProjectID);
ctx.body = DATAS;
};
/**
* @name 卸载磁盘
*
* @description
*
* @param {String} attachment_id 服务器ID
* @param {String} :id 磁盘ID
*
*/
async detach_volume() {
const ctx = this.ctx;
const { attachment_id } = ctx.request.body;
const DATAS = await this.SERVICE.detachVolume(this.auth.id, this.auth.ProjectID, ctx.params.id, attachment_id);
ctx.body = DATAS;
};
/**
* @name 创建磁盘镜像
*
* @description
*
* @param {String} image_name 镜像名称
* @param {String} volume_id 磁盘ID
*
*/
async create_image() {
const ctx = this.ctx;
const { image_name, volume_id } = ctx.request.body;
const DATAS = await this.SERVICE.createImage(this.auth.id, this.auth.ProjectID, image_name, volume_id);
ctx.body = DATAS
};
/**
* @name 磁盘扩容
*
* @param {Number} new_size 扩容容量
*/
async new_size_volume() {
const ctx = this.ctx;
const { volumeID } = ctx.params;
const { new_size } = ctx.request.body;
const DATAS = await this.SERVICE.newSizeVolume(this.auth.id, this.auth.ProjectID, volumeID, new_size);
ctx.body = DATAS
};
}

module.exports = OPBlcokStorageController;
service
'use strict';
const ServerIndex = require('../index')
//
class volumeService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.nextChars = ctx.helper.nextChars;
this.actions = ':8776/v3'
};
// 获取用户磁盘配额
async limits(project_id, _ucid) {
const DATAS = this.OSAJax(`${this.actions}/${project_id}/limits`, { _ucid })
try {
const { maxTotalBackupGigabytes, maxTotalBackups, maxTotalSnapshots, maxTotalVolumeGigabytes, maxTotalVolumes, totalBackupGigabytesUsed, totalBackupsUsed, totalGigabytesUsed, totalSnapshotsUsed, totalVolumesUsed } = (await DATAS).limits.absolute;
return {
data: {
maxTotalBackupGigabytes,
totalBackupGigabytesUsed,
'BackupGigabytes': maxTotalBackupGigabytes - totalBackupGigabytesUsed,
maxTotalBackups,
totalBackupsUsed,
'Backups': maxTotalBackups - totalBackupsUsed,
maxTotalVolumeGigabytes,
totalGigabytesUsed,
'Gigabytes': maxTotalVolumeGigabytes - totalGigabytesUsed,
maxTotalSnapshots,
totalSnapshotsUsed,
'Snapshots': maxTotalSnapshots - totalSnapshotsUsed,
maxTotalVolumes,
totalVolumesUsed,
'Volumes': maxTotalVolumes - totalVolumesUsed
}
}
} catch (error) {
console.log(error)
return error
}
};
// 磁盘挂载到服务器
async attachVolume(_ucid, volume_uuid, body, project_id) {
// const charCount = (await this.OSAJax(`:8774/v2.1/servers/${body.instance_uuid}`, { _ucid })).server['os-extended-volumes:volumes_attached'].length;
// console.log(charCount)

const DATAS = this.OSAJax(`${this.actions}/${!!project_id ? project_id : await this.getProject(_ucid)}/attachments/`, {
body: {
'attachment': {
...body,
volume_uuid
}
},
full: true,
method: 'POST',
_ucid
});
try {
const datas = await DATAS;
return {
code: datas.status,
data: datas.data
}
} catch (error) {}
};
// 磁盘从服务器卸载
async detachVolume(_ucid, ProjectID, volume_id, attachment_id) {
const DATAS = this.OSAJax(`${this.actions}/${ProjectID}/volumes/${volume_id}/action`, {
body: {
"os-detach": {
attachment_id
}
},
full: true,
method: 'POST',
_ucid
});
try {
const datas = await DATAS;
return {
code: datas.status,
}
} catch (error) {

}
};
// 扩容
async newSizeVolume(_ucid, project_id, volume_id, new_size) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
_ucid,
method: 'POST',
body: {
"os-extend": {
new_size
}
},
full: true
});
return {
code: datas.status,
message: datas.status === 400 && `${datas.data.badRequest.message}`
}
} catch (error) {
return error
}
};
// 创建磁盘镜像
async createImage(_ucid, project_id, image_name, volume_id) {
const datas = this.OSAJax(`${this.actions}/${project_id}/volumes/${volume_id}/action`, {
_ucid,
method: 'POST',
body: {
'os-volume_upload_image': {
... {
// "force": false,
// "disk_format": "raw",
// "container_format": "bare",
"visibility": "private",
// "protected": false
},
image_name
}
}
});
try {
throw new Error('无权限')
return {
data: (await datas)['os-volume_upload_image']
}
} catch (error) {
return error
}
};
}
module.exports = volumeService;
磁盘快照
控制器
'use strict';

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

class OPBlcokStorageVolumeSnapshotsController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QPFN = ctx.helper.queryParamFn;
this.DUFN = ctx.helper.deleteUndefined;
this.snapshotMODEL = ctx.model.Openstack.BlcokStorage.Snapshot;
this.snapshotSERVICE = ctx.service.openstack.blcokStorage.snapshots;
}
/**
* @name 磁盘快照列表
*
* @param {String} ctx.query 不需要参数
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/volume
*
*/
async index() {
const { ctx } = this;
// const datas = !!ctx.isAdmin() ? await this.snapshotMODEL.getAll(this.QPFN(ctx.query)) : await this.snapshotMODEL.getUsers(this.auth.id, this.QPFN(ctx.query));
const datas = await this.snapshotSERVICE.list(this.QPFN(ctx.query), this.auth.id, ctx.isAdmin())
ctx.body = datas;
}
/**
* @name 获取单一磁盘快照
*
* @description
*
* @example GET /openstack/blcokstorage/snapshot/{id}
*
*/
async show() {
const ctx = this.ctx;
const datas = await this.snapshotSERVICE.show(ctx.params.id, this.auth.id);
ctx.body = datas;
}
/**
* @name 创建磁盘快照
*
* @param {String} name Optional 磁盘快照名称
* @param {String} description Optional 备注
* @param {String} volume_id 磁盘ID
* @param {Boolean} force Optional 是否备份
* @param {Object} metadata Optional One or more metadata key and value pairs for the snapshot.
*
* @description
*
* @example POST /openstack/blcokstorage/snapshot
*
*/
async create() {
const ctx = this.ctx;
const { name, description, volume_id, force, metadata } = ctx.request.body;
const BODYS = this.DUFN({ name, description, volume_id, force, metadata });
const datas = await this.snapshotMODEL.createOne(this.auth.id, BODYS);
ctx.body = datas
}
/**
* @name 删除快照
*
* @param {*} snapshot_id 快照ID
*
*/
async destroy() {
const ctx = this.ctx;
const datas = await this.snapshotSERVICE.destroy(this.auth.id, this.auth.ProjectID, ctx.params.id);
ctx.body = datas
}
}

module.exports = OPBlcokStorageVolumeSnapshotsController;
service
'use strict';
const ServerIndex = require('../index')
//
class VolumeSnapshotService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.adminprojectID = this.config.openstack.projectID.default
this.actions = ':8776/v3'
};
// 获取磁盘快照
async list(query, _ucid, isAdmin) {
const detail = Object.keys(query).includes('detail');
delete query.detail
try {
const datas = await this.OSAJax(`${this.actions}/${!!isAdmin ? this.adminprojectID : await this.getProject(_ucid)}/snapshots${detail ? '/detail' : ''}`, {
...!isAdmin && { _ucid },
body: { ...isAdmin && { 'all_tenants': true } }
});
return {
data: {
result: datas.snapshots,
totalCount: datas.snapshots.length
}
};
} catch (error) {
return error
}
};
// 获取磁盘快照详情
async show(id, _ucid) {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/snapshots/${id}`, { _ucid });
return { data: datas.snapshot };
} catch (error) {
return error
}
}
// 创建磁盘快照
async create(_ucid, bodys, type = 'manual') {
try {
const datas = await this.OSAJax(`${this.actions}/${await this.getProject(_ucid)}/snapshots`, {
body: {
'snapshot': {
... {
"name": `${!bodys.name && `Snapshot-${type}-${Math.random().toString(36).slice(2, 8)}`}`,
"force": true,
"metadata": null
},
...bodys
}
},
method: 'POST',
_ucid
});
return datas
} catch (error) {
return error
}
}
/**
* @name 删除
* @param {*} _ucid 用户id
* @param {*} project_id 密钥对名称
* @param {*} snapshot_id 快照ID
*/
async destroy(_ucid, project_id, snapshot_id) {
try {
const datas = await this.OSAJax(`${this.actions}/${project_id}/snapshots/${snapshot_id}`, {
_ucid,
method: 'DELETE',
full: true
});
return {
message: datas.status === 404 && `${keypair_name}不存在或已删除`
}
} catch (error) {

}
}
}
module.exports = VolumeSnapshotService;
model
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const { helper, service } = app.createAnonymousContext();
const { NumToStr } = ctx.helper;
const OSblockStorageSnapshotSchema = new Schema({
// 快照类型
type: String,
// 快照状态
// status: String,
// 快照大小
size: Number,
// 快照元信息
metadata: {},
// 快照名称
name: String,
// 快照所属磁盘ID
volume_id: String,
// 创建时间
created_at: Date,
// 描述 prjectID Date
description: String,
// 快照ID
id: String,
// 更新时间
updated_at: Date,
// 公司id
_comp: {
type: Schema.Types.ObjectId,
ref: 'Company',
required: true,
index: true
},
// 状态 ['删除','正常','锁定'] 0,1,2
_status: {
type: Number,
default: 1,
get: v => NumToStr(['删除', '正常', '锁定'], v, true),
},
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSblockStorageSnapshotSchema.statics = {
getUsers: async function (_id, queryMix) {
const { querys, select, pages, sort, dates } = queryMix;
const MODELS = this.find({
'_comp': _id,
...querys,
}, {
...select,
})
.limit(!!pages ? pages.limit : 10)
.sort({
'_id': -1,
...sort
});
return {
data: {
result: await MODELS,
totalCount: await MODELS.count()
}
}
},
createOne: async function (_id, body, type) {
const DATAS = await service.openstack.blcokStorage.snapshots.create(_id, body, type);
const MODEL = this.create({
_comp: _id,
...DATAS['snapshot']
});
try {
return { data: await MODEL };
} catch (error) {
return error
}
},
// 获取全部
getAll: async function (queryMix) {
const { querys, select, pages, sort, dates } = queryMix;
const MODELS = this.find({
'status': { '$ne': 0 },
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return { data: { result: await MODELS, totalCount: await MODELS.count() } };
}
}
return mongoose.model('openstack_block_storage_snapshot', OSblockStorageSnapshotSchema)
}

磁盘类型
控制器
'use strict';

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

class blcokStorageTypesController extends Controller {
constructor(ctx) {
super(ctx);
this.auth = ctx.state.user;
this.QMFN = ctx.helper.queryParamFn;
this.deleteUndefined = ctx.helper.deleteUndefined;
this.volumeTypesMODEL = ctx.model.Openstack.BlcokStorage.Types;
this.volumeTypesSERVICE = ctx.service.openstack.blcokStorage.types
}
/**
* @name servers type 列表
*
* @description query参数detail存在则显示实力配置列表详细信息,若不传仅显示概要列表
*
* @example GET /openstack/blcokstorage/
*
*/
async index() {
const {
ctx,
service
} = this;
const datas = await this.volumeTypesMODEL.getAll(this.auth.id, ctx.isAdmin());
// const datas = await this.volumeTypesSERVICE.list(this.auth.id, this.auth.ProjectID, ctx.isAdmin())
ctx.body = datas;
}
}

module.exports = blcokStorageTypesController;
service
'use strict';
const ServerIndex = require('../index')
//
class volumeTypesService extends ServerIndex {
constructor(ctx) {
super(ctx);
this.actions = ':8776/v3'
};
// 获取磁盘类型列表
async list(_ucid, project_id, isAdmin) {
const {
config
} = this;
const datas = await this.OSAJax(`${this.actions}/${project_id || config.openstack.projectID.default}/types`, {
...!isAdmin && _ucid
});
try {
return {
data: {
result: datas.volume_types,
totalCount: datas.volume_types.length
}
};
} catch (error) {
return error
}
}
}
module.exports = volumeTypesService;
MODEL
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const ctx = app.createAnonymousContext();
const { NumToStr } = ctx.helper;
const OSStorageTypeSchema = new Schema({
// opID
id: String,
// 是否公用
is_public: Boolean,
// 磁盘名称
name: String,
// 磁盘描述备注
description: String,
// 状态 ['删除','正常','锁定'] 0,1,2
status: {
type: Number,
default: 1,
get: v => NumToStr(['删除', '正常', '锁定'], v, true),
},
}, {
timestamps: {
createdAt: 'created',
updatedAt: 'updated'
}
});
OSStorageTypeSchema.statics = {
// 同步openstack 实例配置
syncFN: async function () {
try {
const datas = (await ctx.service.openstack.blcokStorage.types.list()).data;
if (datas.totalCount) {
let updateModel = datas.result.map(e => this.findOneAndUpdate({ 'id': e.id }, { ...e }, { 'upsert': true, 'new': true }));
let result = await Promise.all(updateModel);
return result;
}
} catch (error) {
return error
}
},
// 获取全部
getAll: async function (queryMix, isAdmin) {
const { querys, select, pages, sort, dates } = queryMix;
const models = this.find({
'status': { '$ne': 0 },
...querys,
// ...(!!pages && !!pages.marker && { '_id': { "$lt": pages.marker } }),
}, {
...select,
...!isAdmin && {
'__v': 0,
'created': 0,
'updated': 0,
'_id': 0
}
})
// .limit(!!pages ? pages.limit : 10)
.sort({
// '_id': -1,
'id': 1,
...sort
});
return { data: { result: await models, totalCount: await models.count() } };
}
}
return mongoose.model('openstack_block_storage_types', OSStorageTypeSchema)
}