Skip to content

Commit

Permalink
dry run: Add parameter to disable TLS check
Browse files Browse the repository at this point in the history
  • Loading branch information
katsel committed May 6, 2021
1 parent a52b244 commit c4fcf45
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/cnaas_nms/tools/template_dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import os
import argparse
try:
import requests
import jinja2
Expand All @@ -16,7 +17,7 @@

api_url = os.environ['CNAASURL']
headers = {"Authorization": "Bearer "+os.environ['JWT_AUTH_TOKEN']}

verify_tls = True

def get_entrypoint(platform, device_type):
mapfile = os.path.join(platform, 'mapping.yml')
Expand All @@ -31,15 +32,15 @@ def get_entrypoint(platform, device_type):
def get_device_details(hostname):
r = requests.get(
f"{api_url}/api/v1.0/device/{hostname}",
verify=False,
verify=verify_tls,
headers=headers)
if r.status_code != 200:
raise Exception("Could not query device API")
device_data = r.json()['data']['devices'][0]

r = requests.get(
f"{api_url}/api/v1.0/device/{hostname}/generate_config",
verify=False,
verify=verify_tls,
headers=headers)
if r.status_code != 200:
raise Exception("Could not query generate_config API")
Expand Down Expand Up @@ -90,7 +91,7 @@ def schedule_apply_dryrun(hostname, config):
r = requests.post(
f"{api_url}/api/v1.0/device/{hostname}/apply_config",
headers=headers,
verify=False,
verify=verify_tls,
json=data
)
if r.status_code != 200:
Expand All @@ -99,11 +100,15 @@ def schedule_apply_dryrun(hostname, config):


def main():
if len(sys.argv) != 2:
print("Usage: template_dry_run.py <hostname>")
sys.exit(1)

hostname = sys.argv[1]
parser = argparse.ArgumentParser()
parser.add_argument("hostname")
parser.add_argument("-k", "--skip-verify", help="skip TLS cert verification", action="store_true")
args = parser.parse_args()

hostname = args.hostname
if args.skip_verify:
global verify_tls
verify_tls = False
try:
device_type, platform, variables, old_config = get_device_details(hostname)
except Exception as e:
Expand All @@ -117,7 +122,7 @@ def main():
print(new_config)

try:
input("Start apply_config dry run? Ctrl-c to abort or enter to continue...")
input("Start apply_config dry run? Ctrl-C to abort or enter to continue...")
except KeyboardInterrupt:
print("Exiting...")
else:
Expand Down

0 comments on commit c4fcf45

Please sign in to comment.