UNPKG

3.27 kBJavaScriptView Raw
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'use strict';
18
19const q = require('q');
20const Logger = require('./logger');
21
22/**
23 * Constructor.
24 * @class
25 * @classdesc
26 * Abstract DNS provider implementation.
27 *
28 * This class should be inherited from to implement DNS
29 * specific implementations. Any method in this class that throws
30 * must be overridded. Methods that do not throw may be optionally
31 * overridden.
32 *
33 * @param {Ojbect} [options] - Options for the instance.
34 * @param {Object} [options.clOptions] - Command line options if called from a script.
35 * @param {Object} [options.logger] - Logger to use. Or, pass loggerOptions to get your own logger.
36 * @param {Object} [options.loggerOptions] - Options for the logger.
37 * See {@link module:logger.getLogger} for details.
38 */
39function DnsProvider(options) {
40 const logger = options ? options.logger : undefined;
41 let loggerOptions = options ? options.loggerOptions : undefined;
42
43 this.options = {};
44 if (options) {
45 Object.keys(options).forEach((option) => {
46 this.options[option] = options[option];
47 });
48 }
49
50 this.clOptions = {};
51 if (options && options.clOptions) {
52 Object.keys(this.options.clOptions).forEach((option) => {
53 this.clOptions[option] = options.clOptions[option];
54 });
55 }
56
57 if (logger) {
58 this.logger = logger;
59 } else {
60 loggerOptions = loggerOptions || { logLevel: 'none' };
61 loggerOptions.module = module;
62 this.logger = Logger.getLogger(loggerOptions);
63 this.loggerOptions = loggerOptions;
64 }
65}
66
67/**
68 * Initialize class
69 *
70 * Override for implementation specific initialization needs (read info
71 * from cloud provider, read database, etc.). Called at the start of
72 * processing.
73 *
74 * @param {Object} providerOptions - Provider specific options.
75 *
76 * @returns {Promise} A promise which will be resolved when init is complete.
77 */
78DnsProvider.prototype.init = function init(providerOptions) {
79 this.logger.debug('No override for DnsProvider.init', providerOptions);
80 return q();
81};
82
83/**
84 * Updates DNS records with the given instances
85 *
86 * @abstract
87 *
88 * @param {Object} instances - Array of instances, each having the form
89 *
90 * {
91 * name: name for instance,
92 * ip: ip address,
93 * port: port
94 * }
95 *
96 * @returns {Promise} A promise which will be resolved with the instance ID of the
97 * elected primary.
98 */
99DnsProvider.prototype.update = function update(instances) {
100 throw new Error('Unimplemented abstract method DnsProvider.update', instances);
101};
102
103module.exports = DnsProvider;