|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const _ = require('lodash'); |
| 5 | + |
| 6 | +const constants = { |
| 7 | + providerName: 'google', |
| 8 | +}; |
| 9 | + |
| 10 | +class GoogleProvider { |
| 11 | + static getProviderName() { |
| 12 | + return constants.providerName; |
| 13 | + } |
| 14 | + |
| 15 | + constructor(serverless) { |
| 16 | + this.serverless = serverless; |
| 17 | + this.provider = this; // only load plugin in a Google service context |
| 18 | + this.serverless.setProvider(constants.providerName, this); |
| 19 | + |
| 20 | + /* eslint-disable global-require */ |
| 21 | + const GoogleCloud = require('google-cloud')({ |
| 22 | + projectId: this.serverless.service.provider.project, |
| 23 | + keyFilename: this.serverless.service.provider.credentials, |
| 24 | + }); |
| 25 | + |
| 26 | + const Google = require('googleapis'); |
| 27 | + /* eslint-enable global-require */ |
| 28 | + |
| 29 | + this.sdk = { |
| 30 | + Google, |
| 31 | + GoogleCloud, |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + request(service, method, params) { |
| 36 | + // use googleapis for functions because it's not supported in the SDK right now |
| 37 | + if (service === 'functions') { |
| 38 | + return this.googleCloudFunctionsWrapper(method, params); |
| 39 | + } |
| 40 | + |
| 41 | + const googleCloudService = this.sdk.GoogleCloud[service](); |
| 42 | + |
| 43 | + return new BbPromise((resolve, reject) => { |
| 44 | + googleCloudService[method](params, (error, data) => { |
| 45 | + if (error) { |
| 46 | + reject(new this.serverless.classes.Error(error.message, error.statusCode)); |
| 47 | + } else { |
| 48 | + resolve(data); |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /* eslint-disable no-shadow */ |
| 55 | + googleCloudFunctionsWrapper(method, params) { |
| 56 | + const discoveryURL = 'https://cloudfunctions.googleapis.com/$discovery/rest?version=v1beta2'; |
| 57 | + // eslint-disable-next-line global-require |
| 58 | + const key = require(this.serverless.service.provider.credentials); |
| 59 | + |
| 60 | + return new BbPromise((resolve, reject) => { |
| 61 | + const authClient = new this.sdk.Google.auth.JWT( |
| 62 | + key.client_email, |
| 63 | + null, |
| 64 | + key.private_key, |
| 65 | + ['https://www.googleapis.com/auth/cloud-platform'], |
| 66 | + null |
| 67 | + ); |
| 68 | + |
| 69 | + this.sdk.Google.discoverAPI(discoveryURL, (error, functions) => { |
| 70 | + authClient.authorize((error) => { |
| 71 | + if (error) { |
| 72 | + reject(new this.serverless.classes.Error(error.message, error.statusCode)); |
| 73 | + } |
| 74 | + |
| 75 | + const auth = authClient; |
| 76 | + const functionParams = { |
| 77 | + auth, |
| 78 | + }; |
| 79 | + |
| 80 | + // merge the params from the request call into the base functionParams |
| 81 | + _.merge(functionParams, params); |
| 82 | + |
| 83 | + functions.projects.locations.functions[method](functionParams, (error, body) => { |
| 84 | + if (error) { |
| 85 | + reject(new this.serverless.classes.Error(error.message, error.statusCode)); |
| 86 | + } else { |
| 87 | + resolve(body); |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + } |
| 94 | + /* eslint-enable no-shadow */ |
| 95 | +} |
| 96 | + |
| 97 | +module.exports = GoogleProvider; |
0 commit comments