1 | 'use strict';
|
2 |
|
3 | const osLocale = require('os-locale');
|
4 | const MNSClient = require('@alicloud/mns');
|
5 | const hashedMachineId = require('node-machine-id').machineId;
|
6 | const pkg = require('../package.json');
|
7 | const CloudAPI = require('@alicloud/cloudapi');
|
8 | const TableStore = require('tablestore');
|
9 | const Log = require('@alicloud/log');
|
10 | const FC = require('@alicloud/fc2');
|
11 | const FnFClient = require('@alicloud/fnf-2019-03-15');
|
12 | const Pop = require('@alicloud/pop-core');
|
13 | const getProfile = require('./profile').getProfile;
|
14 | const OSS = require('ali-oss');
|
15 | const debug = require('debug');
|
16 | const {
|
17 | throwProcessedFCPermissionError,
|
18 | throwProcessedPopPermissionError,
|
19 | throwProcessedSLSPermissionError
|
20 | } = require('./error-message');
|
21 |
|
22 | const getRosClient = async () => {
|
23 | return await getPopClient('http://ros.aliyuncs.com', '2019-09-10');
|
24 | };
|
25 |
|
26 | const getOssClient = async (bucket) => {
|
27 | const profile = await getProfile();
|
28 |
|
29 | if (!bucket) {
|
30 | return OSS({
|
31 | region: 'oss-' + profile.defaultRegion,
|
32 | accessKeyId: profile.accessKeyId,
|
33 | accessKeySecret: profile.accessKeySecret
|
34 | });
|
35 | }
|
36 |
|
37 | const location = await OSS({
|
38 | accessKeyId: profile.accessKeyId,
|
39 | accessKeySecret: profile.accessKeySecret,
|
40 | bucket,
|
41 | region: 'oss-' + profile.defaultRegion
|
42 | }).getBucketLocation(bucket);
|
43 |
|
44 | debug('use bucket region %s', location.location);
|
45 |
|
46 | const client = OSS({
|
47 | accessKeyId: profile.accessKeyId,
|
48 | accessKeySecret: profile.accessKeySecret,
|
49 | bucket,
|
50 | region: location.location
|
51 | });
|
52 |
|
53 | return client;
|
54 | };
|
55 |
|
56 | const getFcClient = async (opts = {}) => {
|
57 | const profile = await getProfile();
|
58 |
|
59 | const locale = await osLocale();
|
60 |
|
61 | const mid = await hashedMachineId();
|
62 |
|
63 | FC.prototype.getAccountSettings = function(options = {}, headers = {}) {
|
64 | return this.get('/account-settings', options, headers);
|
65 | };
|
66 |
|
67 | const fc = new FC(profile.accountId, {
|
68 | accessKeyID: profile.accessKeyId,
|
69 | accessKeySecret: profile.accessKeySecret,
|
70 | endpoint: profile.fcEndpoint,
|
71 | region: profile.defaultRegion,
|
72 | timeout: opts.timeout || profile.timeout * 1000,
|
73 | secure: profile.protocol !== 'http',
|
74 | headers: {
|
75 | 'user-agent': `${pkg.name}/v${pkg.version} ( Node.js ${process.version}; OS ${process.platform} ${process.arch}; language ${locale}; mid ${mid})`
|
76 | }
|
77 | });
|
78 | const realRequest = fc.request.bind(fc);
|
79 | fc.request = async (method, path, query, body, headers, opts = {}) => {
|
80 | try {
|
81 | return await realRequest(method, path, query, body, headers || {}, opts || {});
|
82 | } catch (ex) {
|
83 | await throwProcessedFCPermissionError(ex, ...path.split('/').filter(p => !!p));
|
84 | throw ex;
|
85 | }
|
86 | };
|
87 |
|
88 | return fc;
|
89 | };
|
90 |
|
91 | const getFnFClient = async () => {
|
92 | const profile = await getProfile();
|
93 |
|
94 | return new FnFClient({
|
95 | endpoint: `https://${profile.accountId}.${profile.defaultRegion}.fnf.aliyuncs.com`,
|
96 | accessKeyId: profile.accessKeyId,
|
97 | accessKeySecret: profile.accessKeySecret
|
98 | });
|
99 | };
|
100 |
|
101 | const getPopClient = async (endpoint, apiVersion) => {
|
102 | const profile = await getProfile();
|
103 |
|
104 | const pop = new Pop({
|
105 | endpoint: endpoint,
|
106 | apiVersion: apiVersion,
|
107 | accessKeyId: profile.accessKeyId,
|
108 | accessKeySecret: profile.accessKeySecret,
|
109 | opts: {
|
110 | timeout: profile.timeout * 1000
|
111 | }
|
112 | });
|
113 |
|
114 | const realRequest = pop.request.bind(pop);
|
115 | pop.request = async (action, params, options) => {
|
116 | try {
|
117 | return await realRequest(action, params, options);
|
118 | } catch (ex) {
|
119 | await throwProcessedPopPermissionError(ex, action);
|
120 | throw ex;
|
121 | }
|
122 | };
|
123 |
|
124 | return pop;
|
125 | };
|
126 |
|
127 | const getOtsPopClient = async () => {
|
128 | const profile = await getProfile();
|
129 |
|
130 | return await getPopClient(`http://ots.${profile.defaultRegion}.aliyuncs.com`, '2016-06-20');
|
131 | };
|
132 |
|
133 | const getVpcPopClient = async () => {
|
134 | return await getPopClient('https://vpc.aliyuncs.com', '2016-04-28');
|
135 | };
|
136 |
|
137 | const getEcsPopClient = async () => {
|
138 | return await getPopClient('https://ecs.aliyuncs.com', '2014-05-26');
|
139 | };
|
140 |
|
141 | const getNasPopClient = async () => {
|
142 |
|
143 | const profile = await getProfile();
|
144 |
|
145 | return await getPopClient(`http://nas.${profile.defaultRegion}.aliyuncs.com`, '2017-06-26');
|
146 | };
|
147 |
|
148 | const getOtsClient = async (instanceName) => {
|
149 | const profile = await getProfile();
|
150 |
|
151 | var endpoint = `http://${instanceName}.${profile.defaultRegion}.ots.aliyuncs.com`;
|
152 | return new TableStore.Client({
|
153 | accessKeyId: profile.accessKeyId,
|
154 | secretAccessKey: profile.accessKeySecret,
|
155 | endpoint: endpoint,
|
156 | instancename: instanceName
|
157 | });
|
158 | };
|
159 |
|
160 | const getMnsClient = async (topicName, region) => {
|
161 | const profile = await getProfile();
|
162 |
|
163 | return new MNSClient(profile.accountId, {
|
164 | region: region,
|
165 | accessKeyId: profile.accessKeyId,
|
166 | accessKeySecret: profile.accessKeySecret,
|
167 |
|
168 | secure: false,
|
169 | internal: false,
|
170 | vpc: false
|
171 | });
|
172 | };
|
173 |
|
174 | const getCloudApiClient = async () => {
|
175 | const profile = await getProfile();
|
176 |
|
177 | return new CloudAPI({
|
178 | accessKeyId: profile.accessKeyId,
|
179 | accessKeySecret: profile.accessKeySecret,
|
180 | endpoint: `http://apigateway.${profile.defaultRegion}.aliyuncs.com`,
|
181 | opts: {
|
182 | timeout: profile.timeout * 1000
|
183 | }
|
184 | });
|
185 | };
|
186 |
|
187 | const getSlsClient = async () => {
|
188 | const profile = await getProfile();
|
189 |
|
190 | const log = new Log({
|
191 | region: profile.defaultRegion,
|
192 | accessKeyId: profile.accessKeyId,
|
193 | accessKeySecret: profile.accessKeySecret
|
194 | });
|
195 |
|
196 | const realRequest = log._request.bind(log);
|
197 | log._request = async (verb, projectName, path, queries, body, headers, options) => {
|
198 | try {
|
199 | return await realRequest(verb, projectName, path, queries, body, headers, options);
|
200 | } catch (ex) {
|
201 | await throwProcessedSLSPermissionError(ex);
|
202 | throw ex;
|
203 | }
|
204 | };
|
205 |
|
206 | return log;
|
207 | };
|
208 |
|
209 | module.exports = {
|
210 | getFcClient,
|
211 | getOtsClient,
|
212 | getOtsPopClient,
|
213 | getMnsClient,
|
214 | getCloudApiClient,
|
215 | getSlsClient,
|
216 | getPopClient,
|
217 | getVpcPopClient,
|
218 | getEcsPopClient,
|
219 | getNasPopClient,
|
220 | getOssClient,
|
221 | getRosClient,
|
222 | getFnFClient
|
223 | }; |
\ | No newline at end of file |