k2hr3-api 1.0.23 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/ChangeLog +12 -0
  2. package/app.js +63 -30
  3. package/bin/run.sh +14 -0
  4. package/config/default.json +3 -2
  5. package/lib/k2hr3config.js +12 -0
  6. package/lib/k2hr3dkc.js +903 -13
  7. package/lib/k2hr3keys.js +1 -0
  8. package/lib/k2hr3tokens.js +53 -0
  9. package/package.json +11 -6
  10. package/routes/tenant.js +1014 -0
  11. package/tests/auto_all_spec.js +4 -0
  12. package/tests/auto_extdata.js +2 -0
  13. package/tests/auto_tenant.js +989 -0
  14. package/tests/auto_tenant_spec.js +79 -0
  15. package/tests/auto_userdata.js +15 -12
  16. package/tests/manual_acr_delete.js +1 -0
  17. package/tests/manual_acr_get.js +1 -0
  18. package/tests/manual_acr_postput.js +1 -0
  19. package/tests/manual_allusertenant_get.js +58 -3
  20. package/tests/manual_extdata_get.js +1 -0
  21. package/tests/manual_list_gethead.js +1 -0
  22. package/tests/manual_policy_delete.js +1 -0
  23. package/tests/manual_policy_gethead.js +3 -1
  24. package/tests/manual_policy_postput.js +1 -0
  25. package/tests/manual_resource_delete.js +1 -0
  26. package/tests/manual_resource_gethead.js +1 -0
  27. package/tests/manual_resource_postput.js +1 -0
  28. package/tests/manual_role_delete.js +2 -0
  29. package/tests/manual_role_gethead.js +4 -0
  30. package/tests/manual_role_postput.js +2 -0
  31. package/tests/manual_service_delete.js +1 -0
  32. package/tests/manual_service_gethead.js +1 -0
  33. package/tests/manual_service_postput.js +1 -0
  34. package/tests/manual_tenant_delete.js +152 -0
  35. package/tests/manual_tenant_gethead.js +268 -0
  36. package/tests/manual_tenant_postput.js +293 -0
  37. package/tests/manual_test.sh +21 -7
  38. package/tests/manual_userdata_get.js +1 -0
  39. package/tests/manual_usertoken_gethead.js +1 -0
  40. package/tests/manual_usertoken_postput.js +1 -0
  41. package/tests/manual_version_get.js +1 -0
  42. package/tests/test.sh +2 -0
@@ -0,0 +1,79 @@
1
+ /*
2
+ * K2HR3 REST API
3
+ *
4
+ * Copyright 2023 Yahoo Japan Corporation.
5
+ *
6
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
7
+ * common management information for the cloud.
8
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
9
+ * These are stored as roles, resources, policies in K2hdkc, and the
10
+ * client system can dynamically read and modify these information.
11
+ *
12
+ * For the full copyright and license information, please view
13
+ * the license file that was distributed with this source code.
14
+ *
15
+ * AUTHOR: Takeshi Nakatani
16
+ * CREATE: Thu Jul 6 2023
17
+ * REVISION:
18
+ *
19
+ */
20
+
21
+ 'use strict';
22
+
23
+ var common = require('./auto_common'); // Common objects for Chai
24
+ var chai = common.chai; // eslint-disable-line no-unused-vars
25
+ var chaiHttp = common.chaiHttp; // eslint-disable-line no-unused-vars
26
+ var app = common.app; // eslint-disable-line no-unused-vars
27
+ var assert = common.assert; // eslint-disable-line no-unused-vars
28
+ var expect = common.expect; // eslint-disable-line no-unused-vars
29
+ var subproc = require('./auto_subprocesses');
30
+
31
+ //--------------------------------------------------------------
32
+ // Before in global section
33
+ //--------------------------------------------------------------
34
+ before(function(){ // eslint-disable-line no-undef
35
+ //
36
+ // Start all sub processes
37
+ //
38
+ subproc.start(this);
39
+ });
40
+
41
+ //--------------------------------------------------------------
42
+ // After in global section
43
+ //--------------------------------------------------------------
44
+ after(function(){ // eslint-disable-line no-undef
45
+ //
46
+ // Stop all sub processes
47
+ //
48
+ subproc.stop(this);
49
+ });
50
+
51
+ //--------------------------------------------------------------
52
+ // BeforeEach in global section
53
+ //--------------------------------------------------------------
54
+ beforeEach(function(){ // eslint-disable-line no-undef
55
+ // Nothing to do
56
+ });
57
+
58
+ //--------------------------------------------------------------
59
+ // AfterEach in global section
60
+ //--------------------------------------------------------------
61
+ afterEach(function(){ // eslint-disable-line no-undef
62
+ // Nothing to do
63
+ });
64
+
65
+ //--------------------------------------------------------------
66
+ // Sub describe section
67
+ //--------------------------------------------------------------
68
+ describe('SUB API TEST: TENANT', function(){ // eslint-disable-line no-undef
69
+ require('./auto_tenant');
70
+ });
71
+
72
+ /*
73
+ * Local variables:
74
+ * tab-width: 4
75
+ * c-basic-offset: 4
76
+ * End:
77
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
78
+ * vim<600: noexpandtab sw=4 ts=4
79
+ */
@@ -82,9 +82,9 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
82
82
  expect(res.files).to.be.an('object'); // response has some files from 'multipart/mixed'
83
83
  expect(res.files[null]).to.be.an('object'); // check only first(null) position( [TODO] checking another )
84
84
  expect(res.files[null].size).to.be.a('number'); // file has size member
85
- expect(res.files[null].path).to.be.an('string').to.not.empty; // file has temporary path
86
- expect(res.files[null].name).to.be.an('string').to.not.empty; // file has real name
87
- expect(res.files[null].type).to.be.an('string').to.not.empty; // file is set 'content-type'
85
+ expect(res.files[null].filepath).to.be.an('string').to.not.empty; // file has temporary path
86
+ expect(res.files[null].originalFilename).to.be.an('string').to.not.empty; // file has real name
87
+ expect(res.files[null].mimetype).to.be.an('string').to.not.empty; // file is set 'content-type'
88
88
  done();
89
89
  });
90
90
  });
@@ -109,9 +109,9 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
109
109
  expect(res.files).to.be.an('object'); // response has some files from 'multipart/mixed'
110
110
  expect(res.files[null]).to.be.an('object'); // check only first(null) position( [TODO] checking another )
111
111
  expect(res.files[null].size).to.be.a('number'); // file has size member
112
- expect(res.files[null].path).to.be.an('string').to.not.empty; // file has temporary path
113
- expect(res.files[null].name).to.be.an('string').to.not.empty; // file has real name
114
- expect(res.files[null].type).to.be.an('string').to.not.empty; // file is set 'content-type'
112
+ expect(res.files[null].filepath).to.be.an('string').to.not.empty; // file has temporary path
113
+ expect(res.files[null].originalFilename).to.be.an('string').to.not.empty; // file has real name
114
+ expect(res.files[null].mimetype).to.be.an('string').to.not.empty; // file is set 'content-type'
115
115
  done();
116
116
  });
117
117
  });
@@ -135,9 +135,9 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
135
135
  expect(res.files).to.be.an('object'); // response has some files from 'multipart/mixed'
136
136
  expect(res.files[null]).to.be.an('object'); // check only first(null) position( [TODO] checking another )
137
137
  expect(res.files[null].size).to.be.a('number'); // file has size member
138
- expect(res.files[null].path).to.be.an('string').to.not.empty; // file has temporary path
139
- expect(res.files[null].name).to.be.an('string').to.not.empty; // file has real name
140
- expect(res.files[null].type).to.be.an('string').to.not.empty; // file is set 'content-type'
138
+ expect(res.files[null].filepath).to.be.an('string').to.not.empty; // file has temporary path
139
+ expect(res.files[null].originalFilename).to.be.an('string').to.not.empty; // file has real name
140
+ expect(res.files[null].mimetype).to.be.an('string').to.not.empty; // file is set 'content-type'
141
141
  done();
142
142
  });
143
143
  });
@@ -148,6 +148,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
148
148
 
149
149
  chai.request(app)
150
150
  .get(uri)
151
+ .set('user-agent', '')
151
152
  .set('content-type', 'application/octet-stream')
152
153
  .set('accept-encoding', 'identity') // Chai send gzip encoding as default, thus we set 'identity'
153
154
  .end(function(err, res){
@@ -167,6 +168,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
167
168
 
168
169
  chai.request(app)
169
170
  .get(uri)
171
+ .set('user-agent', '')
170
172
  .set('accept-encoding', '') // Chai send gzip encoding as default, thus we set ''
171
173
  .end(function(err, res){
172
174
  expect(res).to.have.status(400);
@@ -191,7 +193,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
191
193
  .end(function(err, res){
192
194
  expect(res).to.have.status(200);
193
195
  expect(res).to.be.an('object');
194
- expect(res.body).to.be.an('object').to.be.empty; // body is empty because it must be 'application/json'
196
+ expect(res.body).be.instanceof(Buffer); // body is empty because it must be 'application/json'
195
197
  expect(res.text).to.be.a('undefined'); // text is empty because it must be 'text/plain'
196
198
  expect(res.files).to.be.a('undefined'); // files is empty because it must be 'text/plain'
197
199
  expect(res.header).to.be.an('object'); // check response header
@@ -219,7 +221,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
219
221
  .end(function(err, res){
220
222
  expect(res).to.have.status(200);
221
223
  expect(res).to.be.an('object');
222
- expect(res.body).to.be.an('object').to.be.empty; // body is empty because it must be 'application/json'
224
+ expect(res.body).be.instanceof(Buffer); // body is empty because it must be 'application/json'
223
225
  expect(res.text).to.be.a('undefined'); // text is empty because it must be 'text/plain'
224
226
  expect(res.files).to.be.a('undefined'); // files is empty because it must be 'text/plain'
225
227
  expect(res.header).to.be.an('object'); // check response header
@@ -246,7 +248,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
246
248
  .end(function(err, res){
247
249
  expect(res).to.have.status(200);
248
250
  expect(res).to.be.an('object');
249
- expect(res.body).to.be.an('object').to.be.empty; // body is empty because it must be 'application/json'
251
+ expect(res.body).be.instanceof(Buffer); // body is empty because it must be 'application/json'
250
252
  expect(res.text).to.be.a('undefined'); // text is empty because it must be 'text/plain'
251
253
  expect(res.files).to.be.a('undefined'); // files is empty because it must be 'text/plain'
252
254
  expect(res.header).to.be.an('object'); // check response header
@@ -265,6 +267,7 @@ describe('API : USERDATA', function(){ // eslint-disable-line no-undef
265
267
 
266
268
  chai.request(app)
267
269
  .get(uri)
270
+ .set('user-agent', '')
268
271
  .set('content-type', 'application/octet-stream')
269
272
  .set('accept-encoding', 'gzip')
270
273
  .end(function(err, res){
@@ -63,6 +63,7 @@ function rawDeleteV1Acr(token, service)
63
63
  if(null !== cacerts.ca){
64
64
  options.ca = cacerts.ca;
65
65
  }
66
+ options.rejectUnauthorized = false; // always insecure for this manual test
66
67
  options.agent = new https.Agent(options);
67
68
  httpobj = https;
68
69
  }else{
@@ -86,6 +86,7 @@ function rawGetV1Acr(token, service, inputdata)
86
86
  if(null !== cacerts.ca){
87
87
  options.ca = cacerts.ca;
88
88
  }
89
+ options.rejectUnauthorized = false; // always insecure for this manual test
89
90
  options.agent = new https.Agent(options);
90
91
  httpobj = https;
91
92
  }else{
@@ -78,6 +78,7 @@ function rawPutPostV1Acr(method, tenant, token, service)
78
78
  if(null !== cacerts.ca){
79
79
  options.ca = cacerts.ca;
80
80
  }
81
+ options.rejectUnauthorized = false; // always insecure for this manual test
81
82
  options.agent = new https.Agent(options);
82
83
  httpobj = https;
83
84
  }else{
@@ -22,14 +22,69 @@
22
22
 
23
23
  var k2hr3 = require('../lib/k2hr3dkc');
24
24
 
25
+ var apiutil = require('../lib/k2hr3apiutil');
26
+ var r3Conf = require('../lib/k2hr3config').r3ApiConfig;
27
+ var apiConf = new r3Conf();
28
+
25
29
  // Debug logging objects
26
- //var r3logger = require('../lib/dbglogging');
30
+ var r3logger = require('../lib/dbglogging');
27
31
 
28
- //
32
+ //---------------------------------------------------------
33
+ // Configuration and port number from Environment
34
+ //---------------------------------------------------------
35
+ var dkcconf = null;
36
+ var dkcport = null;
37
+ var dkccuk = null;
38
+ (function()
39
+ {
40
+ if(!apiutil.isSafeEntity(dkcconf)){
41
+ var tmpdkcconf = apiConf.getK2hdkcConfig();
42
+ if(!apiutil.checkFileExist(tmpdkcconf)){
43
+ r3logger.elog('k2hdkc slave configuration file(' + tmpdkcconf + ') specified in config json does not exist, then try to check K2HDKC_SLAVE_CONF environemnt.');
44
+
45
+ tmpdkcconf = apiutil.getSafeString(process.env.K2HDKC_SLAVE_CONF);
46
+ if(!apiutil.checkFileExist(tmpdkcconf)){
47
+ r3logger.elog('k2hdkc slave configuration file(' + tmpdkcconf + ') specified by K2HDKC_SLAVE_CONF environemnt does not exist, then use default path(/etc/k2hdkc/slave.ini).');
48
+ tmpdkcconf = '/etc/k2hdkc/slave.ini';
49
+ }
50
+ }
51
+ dkcconf = tmpdkcconf;
52
+ }
53
+ if(!apiutil.isSafeEntity(dkcport)){
54
+ var tmpdkcport = apiConf.getK2hdkcPort();
55
+ if(isNaN(tmpdkcport) || null === tmpdkcport){
56
+ r3logger.elog('k2hdkc slave port number(' + JSON.stringify(tmpdkcport) + ') specified in config json is something wrong, then try to check K2HDKC_SLAVE_PORT environemnt.');
57
+
58
+ tmpdkcport = apiutil.getSafeString(process.env.K2HDKC_SLAVE_PORT);
59
+ if(!apiutil.isSafeString(tmpdkcport) || isNaN(tmpdkcport)){
60
+ r3logger.elog('k2hdkc slave port number(' + JSON.stringify(tmpdkcport) + ') specified by K2HDKC_SLAVE_PORT environment is something wrong, then use default port number(8031).');
61
+ tmpdkcport = 8031;
62
+ }
63
+ }
64
+ dkcport = parseInt(tmpdkcport);
65
+ }
66
+ if(!apiutil.isSafeEntity(dkccuk)){
67
+ var tmpdkccuk = apiConf.getK2hdkcCuk();
68
+ if(null === tmpdkccuk){
69
+ r3logger.mlog('k2hdkc slave cuk is not specified. then try to check K2HDKC_SLAVE_CUK environemnt.');
70
+
71
+ tmpdkccuk = apiutil.getSafeString(process.env.K2HDKC_SLAVE_CUK);
72
+ if(!apiutil.isSafeString(tmpdkccuk)){
73
+ r3logger.mlog('k2hdkc slave cuk is not specified by K2HDKC_SLAVE_CUK environment is something wrong, then not use cuk(null).');
74
+ tmpdkccuk = null;
75
+ }
76
+ }
77
+ dkccuk = tmpdkccuk;
78
+ }
79
+ }());
80
+
81
+ //---------------------------------------------------------
29
82
  // call library function directly
30
- //
83
+ //---------------------------------------------------------
31
84
  function printAllUserTenantService()
32
85
  {
86
+ console.log('\n[NOTE] You need to run this as root user for attaching CHMPX memory.\n');
87
+
33
88
  //
34
89
  // This is not API, so access to k2hdkc directly.
35
90
  //
@@ -74,6 +74,7 @@ function getV1ExtData(uri, path, ua, is_gzip)
74
74
  if(null !== cacerts.ca){
75
75
  options.ca = cacerts.ca;
76
76
  }
77
+ options.rejectUnauthorized = false; // always insecure for this manual test
77
78
  options.agent = new https.Agent(options);
78
79
  httpobj = https;
79
80
  }else{
@@ -82,6 +82,7 @@ function getV1ChildrenList(method, token, type, service, path, is_expand)
82
82
  if(null !== cacerts.ca){
83
83
  options.ca = cacerts.ca;
84
84
  }
85
+ options.rejectUnauthorized = false; // always insecure for this manual test
85
86
  options.agent = new https.Agent(options);
86
87
  httpobj = https;
87
88
  }else{
@@ -64,6 +64,7 @@ function deleteV1Policy(token, name)
64
64
  if(null !== cacerts.ca){
65
65
  options.ca = cacerts.ca;
66
66
  }
67
+ options.rejectUnauthorized = false; // always insecure for this manual test
67
68
  options.agent = new https.Agent(options);
68
69
  httpobj = https;
69
70
  }else{
@@ -70,6 +70,7 @@ function getV1Policy(token, name, service)
70
70
  if(null !== cacerts.ca){
71
71
  options.ca = cacerts.ca;
72
72
  }
73
+ options.rejectUnauthorized = false; // always insecure for this manual test
73
74
  options.agent = new https.Agent(options);
74
75
  httpobj = https;
75
76
  }else{
@@ -133,6 +134,7 @@ function headV1Policy(tenant, name, action, resource)
133
134
  if(null !== cacerts.ca){
134
135
  options.ca = cacerts.ca;
135
136
  }
137
+ options.rejectUnauthorized = false; // always insecure for this manual test
136
138
  options.agent = new https.Agent(options);
137
139
  httpobj = https;
138
140
  }else{
@@ -208,7 +210,7 @@ cliutil.getConsoleInput('Method(GET/HEAD) : ', true, false, function
208
210
  });
209
211
  });
210
212
 
211
- }else if(!apiutil.compareCaseString('head', _method)){
213
+ }else if(apiutil.compareCaseString('head', _method)){
212
214
  cliutil.getConsoleInput('Tenant name(allow null) : ', true, false, function(isbreak, tenant)
213
215
  {
214
216
  if(isbreak){
@@ -126,6 +126,7 @@ function postV1Policy(method, token, name, effect, action, resource, alias)
126
126
  if(null !== cacerts.ca){
127
127
  options.ca = cacerts.ca;
128
128
  }
129
+ options.rejectUnauthorized = false; // always insecure for this manual test
129
130
  options.agent = new https.Agent(options);
130
131
  httpobj = https;
131
132
  }else{
@@ -139,6 +139,7 @@ function deleteV1Resource(token_type, token, name, data_type, keynames, aliases,
139
139
  if(null !== cacerts.ca){
140
140
  options.ca = cacerts.ca;
141
141
  }
142
+ options.rejectUnauthorized = false; // always insecure for this manual test
142
143
  options.agent = new https.Agent(options);
143
144
  httpobj = https;
144
145
  }else{
@@ -129,6 +129,7 @@ function getV1Resource(is_get_req, token_type, token, name, service, is_expand,
129
129
  if(null !== cacerts.ca){
130
130
  options.ca = cacerts.ca;
131
131
  }
132
+ options.rejectUnauthorized = false; // always insecure for this manual test
132
133
  options.agent = new https.Agent(options);
133
134
  httpobj = https;
134
135
  }else{
@@ -165,6 +165,7 @@ function postV1Resource(method, token, querypath, name, datatype, data, reskeys,
165
165
  if(null !== cacerts.ca){
166
166
  options.ca = cacerts.ca;
167
167
  }
168
+ options.rejectUnauthorized = false; // always insecure for this manual test
168
169
  options.agent = new https.Agent(options);
169
170
  httpobj = https;
170
171
  }else{
@@ -86,6 +86,7 @@ function deleteV1Role(token, name, target_host, port, cuk)
86
86
  if(null !== cacerts.ca){
87
87
  options.ca = cacerts.ca;
88
88
  }
89
+ options.rejectUnauthorized = false; // always insecure for this manual test
89
90
  options.agent = new https.Agent(options);
90
91
  httpobj = https;
91
92
  }else{
@@ -173,6 +174,7 @@ function deleteV1_IPByCuk(addrs, port, cuk)
173
174
  if(null !== cacerts.ca){
174
175
  options.ca = cacerts.ca;
175
176
  }
177
+ options.rejectUnauthorized = false; // always insecure for this manual test
176
178
  options.agent = new https.Agent(options);
177
179
  httpobj = https;
178
180
  }else{
@@ -64,6 +64,7 @@ function getV1Role(token, name, is_expand)
64
64
  if(null !== cacerts.ca){
65
65
  options.ca = cacerts.ca;
66
66
  }
67
+ options.rejectUnauthorized = false; // always insecure for this manual test
67
68
  options.agent = new https.Agent(options);
68
69
  httpobj = https;
69
70
  }else{
@@ -130,6 +131,7 @@ function getV1RoleToken(token, name, expire)
130
131
  if(null !== cacerts.ca){
131
132
  options.ca = cacerts.ca;
132
133
  }
134
+ options.rejectUnauthorized = false; // always insecure for this manual test
133
135
  options.agent = new https.Agent(options);
134
136
  httpobj = https;
135
137
  }else{
@@ -191,6 +193,7 @@ function getV1RoleTokenList(token, name, expand)
191
193
  if(null !== cacerts.ca){
192
194
  options.ca = cacerts.ca;
193
195
  }
196
+ options.rejectUnauthorized = false; // always insecure for this manual test
194
197
  options.agent = new https.Agent(options);
195
198
  httpobj = https;
196
199
  }else{
@@ -264,6 +267,7 @@ function headV1Role(token, roleyrn, port, cuk)
264
267
  if(null !== cacerts.ca){
265
268
  options.ca = cacerts.ca;
266
269
  }
270
+ options.rejectUnauthorized = false; // always insecure for this manual test
267
271
  options.agent = new https.Agent(options);
268
272
  httpobj = https;
269
273
  }else{
@@ -107,6 +107,7 @@ function postV1Role(method, token, name, policies, alias)
107
107
  if(null !== cacerts.ca){
108
108
  options.ca = cacerts.ca;
109
109
  }
110
+ options.rejectUnauthorized = false; // always insecure for this manual test
110
111
  options.agent = new https.Agent(options);
111
112
  httpobj = https;
112
113
  }else{
@@ -244,6 +245,7 @@ function postV1RoleHost(method, is_user_token, token, name, target_host, port, c
244
245
  if(null !== cacerts.ca){
245
246
  options.ca = cacerts.ca;
246
247
  }
248
+ options.rejectUnauthorized = false; // always insecure for this manual test
247
249
  options.agent = new https.Agent(options);
248
250
  httpobj = https;
249
251
  }else{
@@ -69,6 +69,7 @@ function rawDeleteV1Service(token, service, tenant)
69
69
  if(null !== cacerts.ca){
70
70
  options.ca = cacerts.ca;
71
71
  }
72
+ options.rejectUnauthorized = false; // always insecure for this manual test
72
73
  options.agent = new https.Agent(options);
73
74
  httpobj = https;
74
75
  }else{
@@ -69,6 +69,7 @@ function rawGetHeadV1Service(method, token, service, tenant)
69
69
  if(null !== cacerts.ca){
70
70
  options.ca = cacerts.ca;
71
71
  }
72
+ options.rejectUnauthorized = false; // always insecure for this manual test
72
73
  options.agent = new https.Agent(options);
73
74
  httpobj = https;
74
75
  }else{
@@ -142,6 +142,7 @@ function rawPutPostV1Service(method, token, service, is_create, verify, is_verif
142
142
  if(null !== cacerts.ca){
143
143
  options.ca = cacerts.ca;
144
144
  }
145
+ options.rejectUnauthorized = false; // always insecure for this manual test
145
146
  options.agent = new https.Agent(options);
146
147
  httpobj = https;
147
148
  }else{
@@ -0,0 +1,152 @@
1
+ /*
2
+ * K2HR3 REST API
3
+ *
4
+ * Copyright 2023 Yahoo Japan Corporation.
5
+ *
6
+ * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers
7
+ * common management information for the cloud.
8
+ * K2HR3 can dynamically manage information as "who", "what", "operate".
9
+ * These are stored as roles, resources, policies in K2hdkc, and the
10
+ * client system can dynamically read and modify these information.
11
+ *
12
+ * For the full copyright and license information, please view
13
+ * the license file that was distributed with this source code.
14
+ *
15
+ * AUTHOR: Takeshi Nakatani
16
+ * CREATE: Mon Jun 3 2023
17
+ * REVISION:
18
+ *
19
+ */
20
+
21
+ 'use strict';
22
+
23
+ var http = require('http');
24
+ var https = require('https');
25
+
26
+ var cacerts = require('../lib/cacerts');
27
+ var apiutil = require('../lib/k2hr3apiutil');
28
+ var cliutil = require('../lib/k2hr3cliutil');
29
+
30
+ // Debug logging objects
31
+ var r3logger = require('../lib/dbglogging');
32
+
33
+ //
34
+ // Hostname and port from env
35
+ //
36
+ var hostname = apiutil.getSafeString(process.env.APIHOST);
37
+ var hostport = apiutil.getSafeString(process.env.APIPORT);
38
+ var is_https = apiutil.compareCaseString('yes', process.env.HTTPS_ENV);
39
+
40
+ //
41
+ // Request API for test
42
+ //
43
+ function deleteV1Tenant(token, name, id)
44
+ {
45
+ var headers = {
46
+ 'Content-Type': 'application/json',
47
+ 'Content-Length': 0,
48
+ 'X-Auth-Token': token
49
+ };
50
+ var options = {
51
+ 'host': hostname,
52
+ 'port': hostport,
53
+ 'path': '/v1/tenant/' + name + encodeURI('?id=' + id),
54
+ 'method': 'DELETE',
55
+ 'headers': headers
56
+ };
57
+
58
+ r3logger.dlog('request options = ' + JSON.stringify(options));
59
+ r3logger.dlog('request headers = ' + JSON.stringify(headers));
60
+
61
+ var httpobj;
62
+ if(is_https){
63
+ if(null !== cacerts.ca){
64
+ options.ca = cacerts.ca;
65
+ }
66
+ options.rejectUnauthorized = false; // always insecure for this manual test
67
+ options.agent = new https.Agent(options);
68
+ httpobj = https;
69
+ }else{
70
+ options.agent = new http.Agent(options);
71
+ httpobj = http;
72
+ }
73
+
74
+ var req = httpobj.request(options, function(res)
75
+ {
76
+ var response = '';
77
+ console.log('RESPONSE CODE = ' + res.statusCode);
78
+ r3logger.dlog('response status = ' + res.statusCode);
79
+ r3logger.dlog('response header = ' + JSON.stringify(res.headers));
80
+ res.setEncoding('utf8');
81
+
82
+ res.on('data', function (chunk)
83
+ {
84
+ r3logger.dlog('response chunk = ' + chunk);
85
+ response += chunk;
86
+ });
87
+
88
+ res.on('end', function(result) // eslint-disable-line no-unused-vars
89
+ {
90
+ r3logger.mlog(r3logger.dump(response)); // response is object(or not)
91
+ console.log('RESPONSE BODY = ' + JSON.stringify(response));
92
+ process.exit(0);
93
+ });
94
+ });
95
+
96
+ req.on('error', function(e)
97
+ {
98
+ r3logger.elog('problem with request: ' + e.message);
99
+ });
100
+ req.end();
101
+ }
102
+
103
+ //
104
+ // run
105
+ //
106
+ cliutil.getConsoleInput('Unscoped(or Scoped) user token : ', true, false, function(isbreak, token)
107
+ {
108
+ if(isbreak){
109
+ process.exit(0);
110
+ }
111
+ var _token = token;
112
+
113
+ cliutil.getConsoleInput('Tenant name : ', true, false, function(isbreak, tenant)
114
+ {
115
+ if(isbreak){
116
+ process.exit(0);
117
+ }
118
+
119
+ if(!apiutil.isSafeString(tenant)){
120
+ console.log('method DELETE must specify tenant name');
121
+ process.exit(0);
122
+ }
123
+ var _tenant = apiutil.getSafeString(tenant);
124
+
125
+ cliutil.getConsoleInput('Tenant id : ', true, false, function(isbreak, id)
126
+ {
127
+ if(isbreak){
128
+ process.exit(0);
129
+ }
130
+
131
+ if(!apiutil.isSafeString(id)){
132
+ console.log('method DELETE must specify tenant id');
133
+ process.exit(0);
134
+ }
135
+ var _id = apiutil.getSafeString(id);
136
+
137
+ //
138
+ // Run
139
+ //
140
+ deleteV1Tenant(_token, _tenant, _id);
141
+ });
142
+ });
143
+ });
144
+
145
+ /*
146
+ * Local variables:
147
+ * tab-width: 4
148
+ * c-basic-offset: 4
149
+ * End:
150
+ * vim600: noexpandtab sw=4 ts=4 fdm=marker
151
+ * vim<600: noexpandtab sw=4 ts=4
152
+ */