1 | 'use strict';
|
2 |
|
3 | const BbPromise = require('bluebird');
|
4 | const openwhisk = require('openwhisk')
|
5 | const Credentials = require('./credentials');
|
6 |
|
7 | const constants = {
|
8 | providerName: 'openwhisk',
|
9 | };
|
10 |
|
11 | const credentials = ['apihost', 'auth'];
|
12 |
|
13 | class OpenwhiskProvider {
|
14 | static getProviderName() {
|
15 | return constants.providerName;
|
16 | }
|
17 |
|
18 | constructor(serverless) {
|
19 | this.serverless = serverless;
|
20 | this.provider = this;
|
21 | this.serverless.setProvider(constants.providerName, this);
|
22 | this.sdk = openwhisk
|
23 | }
|
24 |
|
25 | client() {
|
26 | if (this._client) return BbPromise.resolve(this._client)
|
27 |
|
28 | const ignore_certs = this.serverless.service.provider.ignore_certs || false
|
29 | return this.props().then(this.hasValidCreds).then(wskProps => {
|
30 | this._client = openwhisk({ apihost: wskProps.apihost, api_key: wskProps.auth, namespace: wskProps.namespace, ignore_certs, apigw_token: wskProps.apigw_access_token });
|
31 | return this._client
|
32 | })
|
33 | }
|
34 |
|
35 | props() {
|
36 | if (this._props) return BbPromise.resolve(this._props)
|
37 |
|
38 | return Credentials.getWskProps().then(wskProps => {
|
39 | this._props = wskProps;
|
40 | return this._props;
|
41 | })
|
42 | }
|
43 |
|
44 | hasValidCreds(creds) {
|
45 | credentials.forEach(prop => {
|
46 | if (!creds[prop]) {
|
47 | throw new Error(`Missing mandatory openwhisk configuration property: OW_${prop.toUpperCase()}.` +
|
48 | ' Check .wskprops file or set environment variable?');
|
49 | }
|
50 | });
|
51 | return creds;
|
52 | }
|
53 | }
|
54 |
|
55 | module.exports = OpenwhiskProvider;
|