/
internetoptions.py
197 lines (168 loc) · 7.45 KB
/
internetoptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------
# Copyright Commvault Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------------------------
"""File for setting internet options
InternetOptions: class for setting Internet options in CommServe
__init__(Commcell_object) -- initialise with object of CommCell
__repr__() -- returns the string to represent the instance of the
InternetOptions class
set_internet_gateway_client(clientname)
-- sets Internet gateway with provided client
set_metrics_internet_gateway() -- sets metrics server as internet gateway
set_no_gateway() -- Removes internet gateway
set_http_proxy(servername, port)-- sets HTTP proxy enabled with the provided server name and port
disable_http_proxy() -- Removes HTTP proxy
set_http_authentication(username, pwd)
-- sets authentication to HTTP proxy
disable_http_authentication() --Removes HTTP authentication
refresh() -- refresh the internet options
set_gateway_for_sendlogs --- sets internet gateway for sendlog files
"""
from __future__ import absolute_import
from __future__ import unicode_literals
from base64 import b64encode
from .exception import SDKException
class InternetOptions(object):
"""Class for setting Internet options in CommServe"""
def __init__(self, commcell_object):
self._commcell_object = commcell_object
self._INTERNET = self._commcell_object._services['INTERNET_PROXY']
self.refresh()
def __repr__(self):
"""Representation string for the instance of the UserGroups class."""
return "InternetOption class instance for Commcell: '{0}' with config '{1}'".format(
self._commcell_object.commserv_name,
self._internet_config
)
def _get_internet_config(self):
flag, response = self._commcell_object._cvpysdk_object.make_request(
'GET', self._INTERNET
)
if flag:
self._internet_config = response.json()
if self._internet_config and 'config' in self._internet_config:
self._config = self._internet_config['config']
else:
raise SDKException('Response', '102')
else:
raise SDKException('Response', '101', response.text)
def _save_config(self):
"""
updates the configuration of private Metrics
this must be called to save the configuration changes made in this object
Raises:
SDKException:
if response is not success
"""
flag, response = self._commcell_object._cvpysdk_object.make_request(
'POST', self._INTERNET, self._internet_config
)
if not flag:
raise SDKException('Response', '101', response.text)
def set_internet_gateway_client(self,
client_name=None,
cloud_metrics=True,
private_metrics=True
):
"""
set internet gateway with the client name provided.
Args:
client_name (str): client to be used as internet gateway
cloud_metrics (bool): True to enable gateway for cloud metrics
private_metrics(bool): True to enable gateway for Private metrics
Raises:
SDKException:
if client doesnt exist in CommServe
"""
if client_name is None:
raise SDKException('InternetOptions', '101', 'Client name is required')
clientid = int(self._commcell_object.clients.get(client_name).client_id)
self._config['proxyType'] = 2
self._config['proxyClient']['clientId'] = clientid
self._config['proxyClient']['clientName'] = client_name
if cloud_metrics:
self._config['useInternetGatewayPublic'] = True
else:
self._config['useInternetGatewayPublic'] = False
if private_metrics:
self._config['useInternetGatewayPrivate'] = True
else:
self._config['useInternetGatewayPrivate'] = False
self._save_config()
def set_gateway_for_sendlogs(self,
client_name
):
"""
set internet gateway with the client name provided for sendlogs.
Args:
client_name (str): client to be used as internet gateway
Raises:
SDKException:
if client doesnt exist in CommServe
"""
clientid = int(self._commcell_object.clients.get(client_name).client_id)
self._config['proxyType'] = 2
self._config['proxyClient']['clientId'] = clientid
self._config['proxyClient']['clientName'] = client_name
self._config['useInternetGatewaySendLogFile'] = True
self._save_config()
def set_metrics_internet_gateway(self):
"""sets metrics server as internet gateway"""
self._config['proxyType'] = 3
self._save_config()
def set_no_gateway(self):
"""Removes internet gateway"""
self._config['proxyType'] = 1
self._save_config()
def set_http_proxy(self, servername=None, port=None):
"""
sets HTTP proxy enabled with the provided server name and port
Args:
servername (str): hostname or IP of the HTTP proxy server
port (int): HTTP proxy server port
Raises:
SDKException:
if proxy server name and port are empty
"""
if servername is None or port is None:
raise SDKException('Response', '101', 'proxy server name and port cannot be empty')
self._config['useHttpProxy'] = True
self._config['proxyServer'] = servername
self._config['proxyPort'] = int(port)
self._save_config()
def disable_http_proxy(self):
"""Removes HTTP proxy"""
self._config['useHttpProxy'] = False
self._save_config()
def set_http_authentication(self, username='', pwd=''):
"""
sets authentication to HTTP proxy
Args:
username: username for proxy server
pwd: password for proxy server
"""
self._config['useProxyAuthentication'] = True
self._config['proxyCredentials']['userName'] = username
self._config['proxyCredentials']['password'] = b64encode(pwd.encode()).decode()
self._config['proxyCredentials']['confirmPassword'] = b64encode(pwd.encode()).decode()
self._save_config()
def disable_http_authentication(self):
"""Removes HTTP authentication"""
self._config['useProxyAuthentication'] = False
self._save_config()
def refresh(self):
"""Refresh the Internet Options."""
self._get_internet_config()