UNPKG

5.22 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 assert = require('assert');
20const util = require('./util');
21const Logger = require('./logger');
22
23/**
24 * GTM constructor
25 *
26 * @class
27 * @classdesc
28 * Provides GTM functionality to a base BigIp object
29 *
30 * @param {Object} bigIpCore - Base BigIp object.
31 * @param {Object} [options] - Optional parameters.
32 * @param {Object} [options.logger] - Logger to use. Or, pass loggerOptions to get your own logger.
33 * @param {Object} [options.loggerOptions] - Options for the logger.
34 * See {@link module:logger.getLogger} for details.
35 */
36function BigIpGtm(bigIpCore, options) {
37 const logger = options ? options.logger : undefined;
38 let loggerOptions = options ? options.loggerOptions : undefined;
39
40 if (logger) {
41 this.logger = logger;
42 util.setLogger(logger);
43 } else {
44 loggerOptions = loggerOptions || { logLevel: 'none' };
45 loggerOptions.module = module;
46 this.logger = Logger.getLogger(loggerOptions);
47 util.setLoggerOptions(loggerOptions);
48 }
49
50 this.core = bigIpCore;
51 this.partition = 'Common';
52}
53
54/**
55 * Updates a GTM server
56 * @param {String} serverName - Name of the server to update
57 * @param {Object[]} virtualSservers - Array of virtual servers to set for the server
58 *
59 * {
60 * name: <name>
61 * ip: <ip_address>
62 * port: <port>
63 * }
64 * @param {Object} [options] - Optional parameters
65 * @param {String} [options.datacenter] - Datacenter for server
66 * @param {String} [options.monitor] - Full path to monitor for server
67 *
68 * @returns {Promise} A promise which is resolved if succssful or rejected if
69 * an error occurs
70 */
71BigIpGtm.prototype.updateServer = function updateServer(serverName, virtualServers, options) {
72 const servers = [];
73 const payload = {};
74
75 assert.equal(typeof serverName, 'string', 'serverName must be a string');
76 assert.equal(Array.isArray(virtualServers), true, 'virtualServers must be an array');
77
78 const datacenter = options ? options.datacenter : undefined;
79 const monitor = options ? options.monitor : undefined;
80
81 virtualServers.forEach((virtualServer) => {
82 servers.push({
83 name: virtualServer.name,
84 destination: `${virtualServer.ip}:${virtualServer.port}`
85 });
86 });
87 payload.virtualServers = servers;
88
89 if (datacenter) {
90 payload.datacenter = datacenter;
91 }
92
93 if (monitor) {
94 payload.monitor = monitor;
95 }
96
97 return this.core.modify(`/tm/gtm/server/~${this.partition}~${serverName}`, payload);
98};
99
100/**
101 * Updates the A record for a GTM pool with the list of virtual servers
102 *
103 * @param {String} poolName - Name of pool to update
104 * @param {String} serverName - Name of the server to update
105 * @param {Object[]} virtualSservers - Array of virtual servers to set for the pool
106 *
107 * {
108 * name: <name>
109 * }
110 * @param {Object} [options] - Optional parameters
111 * @param {String} [options.monitor] - Full path to monitor for pool
112 * @param {String} [options.loadBalancingMode] - Load balancing mode for pool
113 *
114 * @returns {Promise} A promise which is resolved if succssful or rejected if
115 * an error occurs
116 */
117BigIpGtm.prototype.updatePool = function updatePool(poolName, serverName, virtualServers, options) {
118 const members = [];
119 const payload = {};
120
121 assert.equal(typeof poolName, 'string', 'poolName must be a string');
122 assert.equal(typeof serverName, 'string', 'serverName must be a string');
123 assert.equal(Array.isArray(virtualServers), true, 'virtualServers must be an array');
124
125 const monitor = options ? options.monitor : undefined;
126 const loadBalancingMode = options ? options.loadBalancingMode : undefined;
127
128 virtualServers.forEach((virtualServer) => {
129 members.push({
130 name: `${serverName}:${virtualServer.name}`
131 });
132 });
133 payload.members = members;
134
135 if (monitor) {
136 payload.monitor = monitor;
137 }
138
139 if (loadBalancingMode) {
140 payload.loadBalancingMode = loadBalancingMode;
141 }
142
143 return this.core.modify(`/tm/gtm/pool/a/~${this.partition}~${poolName}`, payload);
144};
145
146/**
147 * Sets the partition to use for future requests on this instance
148 *
149 * @param {String} partition - The partition to use
150 */
151BigIpGtm.prototype.setPartition = function setPartition(partition) {
152 this.partition = partition;
153};
154
155module.exports = BigIpGtm;