1 | /**
|
2 | * Copyright 2018 F5 Networks, Inc.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | ;
|
18 |
|
19 | module.exports = {
|
20 | /**
|
21 | * Creates a cloud provider for the given name or match option
|
22 | *
|
23 | * @param {String} name - Name of the provider
|
24 | * @param {Ojbect} [options] - Options for the instance.
|
25 | * @param {Object} [options.clOptions] - Command line options if called from a script.
|
26 | * @param {Object} [options.logger] - Logger to use.
|
27 | * Or, pass loggerOptions to get your own logger.
|
28 | * @param {Object} [options.loggerOptions] - Options for the logger.
|
29 | * See {@link module:logger.getLogger} for details.
|
30 | * @param {Object} [matchOptions] - String matching options for determing Cloud Provider
|
31 | * @param {String} [matchOptions.storageUri] - Provider specific storage URI
|
32 | */
|
33 | getCloudProvider(name, options, matchOptions) {
|
34 | let ProviderImplementation;
|
35 | const matchedProvider = matchProvider(matchOptions);
|
36 |
|
37 | if (!name && !matchedProvider) {
|
38 | throw new Error('Unavailable cloud provider');
|
39 | }
|
40 |
|
41 | if (name === 'generic') {
|
42 | // eslint-disable-next-line global-require
|
43 | ProviderImplementation = require('./genericNodeProvider');
|
44 | } else {
|
45 | // eslint-disable-next-line global-require
|
46 | ProviderImplementation = require(`../../f5-cloud-libs-${name || matchedProvider}`).provider;
|
47 | }
|
48 |
|
49 | if (ProviderImplementation) {
|
50 | return new ProviderImplementation(options);
|
51 | }
|
52 |
|
53 | throw new Error('Unsupported cloud provider');
|
54 | }
|
55 | };
|
56 |
|
57 | function matchProvider(opts) {
|
58 | if (opts && opts.storageUri) {
|
59 | const uri = opts.storageUri;
|
60 |
|
61 | const awsPrefix = 'arn';
|
62 | const azureRegex = /https:\/\/[a-z0-9]+\.blob\.core\.windows\.net/;
|
63 | const gcePrefix = 'gs://';
|
64 |
|
65 | if (uri.startsWith(awsPrefix)) {
|
66 | return 'aws';
|
67 | } else if (uri.match(azureRegex)) {
|
68 | return 'azure';
|
69 | } else if (uri.startsWith(gcePrefix)) {
|
70 | return 'gce';
|
71 | }
|
72 | }
|
73 | return null;
|
74 | }
|