Skip to content

Commit

Permalink
modified APIs to use https instead of http
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishal John Varghese committed Jan 29, 2025
1 parent 20d2b29 commit 8cbf1ac
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions config_migration/config_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, source, adm_type, adm_ip, adm_username, adm_password, adm_svc
self.tenant_name = ''

self.operation = operation
self.protocol = 'http'
self.protocol = 'https'
self.basepath = '/stylebook/nitro/v2/config'
self.endpoint = '/adc_configs/actions'
self.sessionid = None
Expand Down Expand Up @@ -232,15 +232,17 @@ def get_service_header(self, sessionid, isCloud):

def fetch_device_id(self, target):
try:
verify = True
if self.adm_type == 'service':
protocol = 'https'
headers = self.get_service_header(self.sessionid, 'True')
url = "{}://{}/nitro/v1/config/ns?filter=ip_address:{}".format(protocol, self.adm_svc_url, target)
else:
protocol = 'http'
protocol = 'https'
headers = self.get_onprem_header(self.sessionid, '')
url = "{}://{}/nitro/v1/config/ns?filter=ip_address:{}".format(protocol, self.adm_ip, target)
r = self.do_get(url, headers)
verify = False
r = self.do_get(url, headers, verify)
self.logger.info(r.status_code)
out = r.json()
target_id = out['ns'][0]['id']
Expand All @@ -255,12 +257,16 @@ def fetch_device_id(self, target):
else:
return target_id

def do_get(self, url, headers):
def do_get(self, url, headers, verify):
i = 0
reattempt_count = 5
while True:
try:
r = requests.get(url, headers=headers)
try:
r = requests.get(url, headers=headers, verify=verify)
except Exception as e:
self.logger.critical(e)
raise e
if r.status_code == 401:
self.logger.info("Session expired. Will relogin")
sessionid = self.login_to_adm()
Expand All @@ -269,6 +275,7 @@ def do_get(self, url, headers):
else:
headers = self.get_onprem_header(sessionid, '')
continue

return r
break
except (ConnectionError, ValueError):
Expand Down Expand Up @@ -630,10 +637,10 @@ def post_request(self, url, payload):
self.logger.critical(f"{url} - POST API request Failed")
raise e

def get_request(self, url):
def get_request(self, url, verify):
try:
headers = self.get_request_headers()
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers, verify=verify)
return self.parse_response(response)
except Exception as e:
self.logger.critical(f"{url} - GET API request Failed")
Expand All @@ -651,15 +658,17 @@ def delete_request(self, url):
def get_job_status(self, job_id, operation):
try:
self.logger.info("Fetching the job status")
verify = True
if self.adm_type == 'service':
request_url = 'https://' + self.adm_svc_url + self.basepath + '/jobs/' + job_id
else:
request_url = self.protocol + '://' + self.adm_ip + self.basepath + '/jobs/' + job_id
verify = False
#sometimes, JOB-ID is created with a little delay.. so introducing sleep
time.sleep(5)
last_progress = {}
while True:
result = self.get_request(request_url)
result = self.get_request(request_url, verify)
if self.error:
self.logger.critical(self.error)
return None, None
Expand Down Expand Up @@ -745,10 +754,11 @@ def extract_vservers(self, request_payload):
if self.adm_type == 'service':
request_url = 'https://' + self.adm_svc_url + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_service_header(self.sessionid, 'true')
result = requests.post(request_url, json=request_payload, headers=headers)
else:
request_url = self.protocol + '://' + self.adm_ip + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_onprem_header(self.sessionid, '')
result = requests.post(request_url, json=request_payload, headers=headers)
result = requests.post(request_url, json=request_payload, headers=headers, verify=False)
if self.error:
self.logger.critical(self.error)
return None, None
Expand Down Expand Up @@ -776,10 +786,11 @@ def extract_vservers_config(self, request_payload, vserver_names):
if self.adm_type == 'service':
request_url = 'https://' + self.adm_svc_url + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_service_header(self.sessionid, 'true')
result = requests.post(request_url, json=request_payload, headers=headers)
else:
request_url = self.protocol + '://' + self.adm_ip + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_onprem_header(self.sessionid, '')
result = requests.post(request_url, json=request_payload, headers=headers)
result = requests.post(request_url, json=request_payload, headers=headers, verify=False)

if self.error:
self.logger.critical(self.error)
Expand Down Expand Up @@ -810,11 +821,11 @@ def migrate_vservers_config(self, request_payload):
if self.adm_type == 'service':
request_url = 'https://' + self.adm_svc_url + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_service_header(self.sessionid, 'true')
result = requests.post(request_url, json=request_payload, headers=headers)
else:
request_url = self.protocol + '://' + self.adm_ip + self.basepath + self.endpoint + '/' + self.operation
headers = self.get_onprem_header(self.sessionid, '')

result = requests.post(request_url, json=request_payload, headers=headers)
result = requests.post(request_url, json=request_payload, headers=headers, verify=False)

if self.error:
self.logger.critical(self.error)
Expand Down Expand Up @@ -855,12 +866,12 @@ def logout_from_adm_svc(self):
def logout_from_adm_onprem(self):
try:
self.logger.info('Logging out from NetScaler Console.')
url = 'http://'+self.adm_ip+'/nitro/v1/config/login'
url = 'https://'+self.adm_ip+'/nitro/v1/config/login'
method = 'DELETE'
headers = {'Content-Type': 'application/json', 'Cookie': 'SESSID='+self.sessionid}

# Send the request
response = self.send_curl_request(url, method, None, headers)
response = self.send_curl_request(url, method, None, headers, verify=False)

# Check the response
if response.status_code == 200 and response.json()['username'] == adm_username and response.json()['tenant_id'] != "":
Expand Down Expand Up @@ -911,15 +922,15 @@ def login_to_adm_service(self):

def login_to_adm_onprem(self):
try:
url = 'http://'+self.adm_ip+'/nitro/v1/config/login'
url = 'https://'+self.adm_ip+'/nitro/v1/config/login'
method = 'POST'
data = {"login": {"username": self.adm_username, "password": self.adm_password}}
headers = {'Content-Type': 'application/json'}
# Convert data to JSON format and add "object=" prefix
payload = "object=" + json.dumps(data)

# Send the request
response = self.send_curl_request(url, method, payload, headers)
response = self.send_curl_request(url, method, payload, headers, verify=False)

# Check the response
if response.status_code == 200:
Expand All @@ -935,7 +946,7 @@ def login_to_adm_onprem(self):
self.logger.critical("Login failed. Status code:", response.status_code)
except Exception as e:
self.error = "Error in logging in to NetScaler Console On-prem. Check the NetScaler Console IP, username and password. Make sure the NetScaler Console is reachable."
self.logger.critical(self.error)
self.logger.critical(e)
raise e

def login_to_adm(self):
Expand All @@ -956,16 +967,16 @@ def get_session_id(self, cookies):
return cookie.value
return None

def send_curl_request(self, url, method='GET', data=None, headers=None):
def send_curl_request(self, url, method='GET', data=None, headers=None, verify=True):
if method.upper() == 'GET':
response = requests.get(url, params=data, headers=headers)
response = requests.get(url, params=data, headers=headers, verify=verify)
elif method.upper() == 'POST':
# For POST requests, the data parameter is used to send the payload
response = requests.post(url, data=data, headers=headers)
response = requests.post(url, data=data, headers=headers, verify=verify)
elif method.upper() == 'PUT':
response = requests.put(url, data=data, headers=headers)
response = requests.put(url, data=data, headers=headers, verify=verify)
elif method.upper() == 'DELETE':
response = requests.delete(url, data=data, headers=headers)
response = requests.delete(url, data=data, headers=headers, verify=verify)
else:
raise ValueError(f"Unsupported HTTP method: {method}")

Expand Down

0 comments on commit 8cbf1ac

Please sign in to comment.