Skip to content

Commit

Permalink
fc support tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Aug 28, 2019
1 parent 95ce2c6 commit edd01f3
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,22 +1,22 @@
language: node_js

node_js:
- "10"
- "8"
- "6"
- "4"

env:
- TEST_COV=true
- TEST_COV=false

matrix:
exclude:
- node_js: "4"
env: TEST_COV=true
- node_js: "6"
env: TEST_COV=true
- node_js: "8"
env: TEST_COV=false
- node_js: "10"
env: TEST_COV=false

script:
- 'make test-es5'
Expand Down
70 changes: 70 additions & 0 deletions es5/client.js
Expand Up @@ -350,6 +350,14 @@ var Client = function () {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var headers = arguments[1];

if (options.tags !== undefined) {
for (var k in options.tags) {
if (options.tags.hasOwnProperty(k)) {
options[`tag_${k}`] = options.tags[k];
}
}
delete options.tags;
}
return this.get('/services', options, headers);
}

Expand Down Expand Up @@ -963,6 +971,68 @@ var Client = function () {
return this.put(`/services/${serviceName}/aliases/${aliasName}`, options, headers);
}

/**
* 给fc资源打tag
*
* @param {String} resourceArn Resource ARN. Either full ARN or partial ARN.
* @param {Object} tags A list of tag keys. At least 1 tag is required. At most 20. Tag key is required, but tag value is optional.
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/

}, {
key: 'tagResource',
value: function tagResource(resourceArn, tags) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};

options.resourceArn = resourceArn;
options.tags = tags;

return this.post('/tag', options, headers);
}

/**
* 给fc资源取消tag
*
* @param {String} resourceArn Resource ARN. Either full ARN or partial ARN.
* @param {Object} tagkeys A list of tag keys. At least 1 tag key is required if all=false. At most 20.
* @param {Boolean} all Remove all tags at once. Default value is false. Accept value: true or false.
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/

}, {
key: 'untagResource',
value: function untagResource(resourceArn, tagKeys) {
var all = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var headers = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};

options.resourceArn = resourceArn;
options.tagKeys = tagKeys;
options.all = all;
return this.request('DELETE', '/tag', null, options, headers);
}

/**
* 获取某个资源的所有tag
*
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/

}, {
key: 'getResourceTags',
value: function getResourceTags() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var headers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

return this.get('/tag', options, headers);
}
/**
* 获得Header 签名
*
Expand Down
53 changes: 52 additions & 1 deletion lib/client.js
Expand Up @@ -135,7 +135,7 @@ class Client {

debug('response status: %s', response.statusCode);
debug('response headers: %j', response.headers);
var responseBody
var responseBody;
if (!opts['rawBuf'] || response.headers['x-fc-error-type']) {
responseBody = await httpx.read(response, 'utf8');
} else {
Expand Down Expand Up @@ -249,6 +249,14 @@ class Client {
* @return {Promise} 返回 Object(包含headers和data属性[Service 列表])
*/
listServices(options = {}, headers) {
if (options.tags !== undefined) {
for (var k in options.tags) {
if (options.tags.hasOwnProperty(k)) {
options[`tag_${k}`] = options.tags[k];
}
}
delete options.tags;
}
return this.get('/services', options, headers);
}

Expand Down Expand Up @@ -714,6 +722,49 @@ class Client {
return this.put(`/services/${serviceName}/aliases/${aliasName}`, options, headers);
}

/**
* 给fc资源打tag
*
* @param {String} resourceArn Resource ARN. Either full ARN or partial ARN.
* @param {Object} tags A list of tag keys. At least 1 tag is required. At most 20. Tag key is required, but tag value is optional.
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/
tagResource(resourceArn, tags, options = {}, headers = {}) {
options.resourceArn = resourceArn;
options.tags = tags;

return this.post('/tag', options, headers);
}

/**
* 给fc资源取消tag
*
* @param {String} resourceArn Resource ARN. Either full ARN or partial ARN.
* @param {Object} tagkeys A list of tag keys. At least 1 tag key is required if all=false. At most 20.
* @param {Boolean} all Remove all tags at once. Default value is false. Accept value: true or false.
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/
untagResource(resourceArn, tagKeys, all = false, options = {}, headers = {}) {
options.resourceArn = resourceArn;
options.tagKeys = tagKeys;
options.all = all;
return this.request('DELETE', '/tag', null, options, headers);
}

/**
* 获取某个资源的所有tag
*
* @param {Object} options
* @param {Object} headers
* @return {Promise} 返回 Object(包含headers和data属性)
*/
getResourceTags(options = {}, headers = {}) {
return this.get('/tag', options, headers);
}
/**
* 获得Header 签名
*
Expand Down
85 changes: 75 additions & 10 deletions test/client.test.js
Expand Up @@ -233,9 +233,9 @@ describe('client test', function () {
describe('function should ok', function () {
const functionName = 'hello-world';
const initFunctionName = 'counter';
const functionWithBufResp = "test-buf-resp"
const functionWithHandledErr = "test-func-with-handled-err"
const functionWithUnhandledErr = "test-func-with-unhandled-err"
const functionWithBufResp = 'test-buf-resp';
const functionWithHandledErr = 'test-func-with-handled-err';
const functionWithUnhandledErr = 'test-func-with-unhandled-err';
const client = new FunctionComputeClient(ACCOUNT_ID, {
accessKeyID: ACCESS_KEY_ID,
accessKeySecret: ACCESS_KEY_SECRET,
Expand All @@ -245,7 +245,7 @@ describe('client test', function () {
before(async function () {
// clean up
try {
await client.deleteService(serviceName)
await client.deleteService(serviceName);
} catch (ex) {
// Ignore
}
Expand Down Expand Up @@ -365,13 +365,13 @@ describe('client test', function () {

it('invokeFunction should faster', async function () {
const response = await client.invokeFunction(serviceName, functionName, Buffer.from('world'));
expect(response.data).a('string')
expect(response.data).a('string');
expect(response.data).to.be('hello world');
});

it('invokeFunction with rawBuf=false should return string', async function () {
const response = await client.invokeFunction(serviceName, functionName, Buffer.from('world'), {}, 'LATEST', {rawBuf:false});
expect(response.data).a('string')
expect(response.data).a('string');
expect(response.data).to.be('hello world');
});

Expand All @@ -391,7 +391,7 @@ describe('client test', function () {
expect(func.data).to.have.property('functionName', functionWithBufResp);

const response = await client.invokeFunction(serviceName, functionWithBufResp, Buffer.from('world'), {}, 'LATEST', {rawBuf: true});
expect(response.data).an(Buffer)
expect(response.data).an(Buffer);
expect(response.data.toString()).to.be('world');
});

Expand All @@ -411,7 +411,7 @@ describe('client test', function () {
expect(func.data).to.have.property('functionName', functionWithHandledErr);

const response = await client.invokeFunction(serviceName, functionWithHandledErr, Buffer.from('world'), {}, 'LATEST', {rawBuf: true});
expect(response.data).not.an(Buffer)
expect(response.data).not.an(Buffer);
expect(JSON.stringify(response.data)).to.be(JSON.stringify({ 'errorMessage': 'This is a handled error' }));
});

Expand All @@ -431,8 +431,8 @@ describe('client test', function () {
expect(func.data).to.have.property('functionName', functionWithUnhandledErr);

const response = await client.invokeFunction(serviceName, functionWithUnhandledErr, Buffer.from('world'), {}, 'LATEST', {rawBuf: true});
expect(response.data).not.an(Buffer)
expect(JSON.stringify(response.data)).contain(JSON.stringify({ 'errorMessage': 'Process exited unexpectedly before completing request' }).slice(0,-2))
expect(response.data).not.an(Buffer);
expect(JSON.stringify(response.data)).contain(JSON.stringify({ 'errorMessage': 'Process exited unexpectedly before completing request' }).slice(0,-2));
});

it('invokeFunction async should ok', async function () {
Expand Down Expand Up @@ -915,4 +915,69 @@ describe('client test', function () {
expect(res.data).to.be('');
});
});

describe('tag test should be ok', function () {
const client = new FunctionComputeClient(ACCOUNT_ID, {
accessKeyID: ACCESS_KEY_ID,
accessKeySecret: ACCESS_KEY_SECRET,
region: 'cn-shanghai'
});
const tagServiceName = `${serviceName}_tag_test`;
before(async function () {
const service = await client.createService(tagServiceName);
expect(service.data).to.be.ok();
expect(service.data).to.have.property('serviceName', tagServiceName);
});

after(async function () {
await client.deleteService(tagServiceName);
});

it('tagResource should ok', async function () {
await client.tagResource(`services/${tagServiceName}`, {k1:'v1', k2:'v2'});
const resp = await client.getResourceTags({
'resourceArn': `services/${tagServiceName}`
});
expect(resp.data).to.be.ok();
expect(resp.data.tags).to.be.ok();
expect(resp.data.tags.k1).to.be('v1');
expect(resp.data.tags.k2).to.be('v2');
var lresp = await client.listServices({
tags:{
k1: 'v1',
k2: 'v2',
}
});

expect(lresp.data).to.be.ok();
expect(lresp.data.services).to.be.ok();
expect(lresp.data.services.length).to.above(0);

lresp = await client.listServices({
tags: {
k3: 'v3'
}
});

expect(lresp.data).to.be.ok();
expect(lresp.data.services).to.be.ok();
expect(lresp.data.services.length).to.equal(0);
});

it('untagResource should ok', async function () {
await client.untagResource(`services/${tagServiceName}`, ['k1'], false);
var resp = await client.getResourceTags({
'resourceArn': `services/${tagServiceName}`
});
expect(resp.data).to.be.ok();
expect(resp.data.tags).to.be.ok();
expect(resp.data.tags.k1).to.be(undefined);
expect(resp.data.tags.k2).to.be('v2');
await client.untagResource(`services/${tagServiceName}`, [], true);
resp = await client.getResourceTags({
'resourceArn': `services/${tagServiceName}`
});
expect(Object.keys(resp.data.tags).length).to.equal(0);
});
});
});

0 comments on commit edd01f3

Please sign in to comment.