variable rename to make it clearer

This commit is contained in:
mischa 2022-06-20 18:54:20 +02:00
parent bc169d8359
commit 2bcb69b951
2 changed files with 14 additions and 127 deletions

28
push.py
View File

@ -154,8 +154,8 @@ def alkira_service(session, connector_name):
response = alkira_post(session, f'/credentials/{credentials_url}', body) response = alkira_post(session, f'/credentials/{credentials_url}', body)
json_body = response.json() json_body = response.json()
if response.status_code == 200: if response.status_code == 200:
fw_id = json_body['id'] service_credentialid = json_body['id']
logging.debug(f'credentialId: {fw_id}') logging.debug(f'credentialId: {service_credentialid}')
logging.info('=== Create Instance Credentials') logging.info('=== Create Instance Credentials')
if connector_name in service_instance_credentials.keys(): if connector_name in service_instance_credentials.keys():
@ -164,10 +164,10 @@ def alkira_service(session, connector_name):
response = alkira_post(session, f'/credentials/{credentials_url}instance', body) response = alkira_post(session, f'/credentials/{credentials_url}instance', body)
json_body = response.json() json_body = response.json()
if response.status_code == 200: if response.status_code == 200:
instance_id = json_body['id'] service_instance_credentialid = json_body['id']
logging.debug(f'instance credentialId: {instance_id}') logging.debug(f'instance credentialId: {service_instance_credentialid}')
return fw_id, instance_id return service_credentialid, service_instance_credentialid
# Authenticate # Authenticate
s = alkira_login() s = alkira_login()
@ -193,8 +193,8 @@ for connector in config.sections():
config_path = (f'{connector_folder}/{connector_name}{connector_number}.txt') config_path = (f'{connector_folder}/{connector_name}{connector_number}.txt')
if 'service' in connector_name: if 'service' in connector_name:
fw_id, instance_id = alkira_service(s, connector_name) service_credentialid, service_instance_credentialid = alkira_service(s, connector_name)
logging.debug(f'Got credentialId: {fw_id} AND {instance_id}') logging.debug(f'Got credentialId: {service_credentialid} AND {service_instance_credentialid}')
with open (config_path, 'r') as f: with open (config_path, 'r') as f:
body = json.load(f) body = json.load(f)
@ -239,19 +239,19 @@ for connector in config.sections():
logging.debug(f'CONFIG size: {size}') logging.debug(f'CONFIG size: {size}')
body['size'] = size body['size'] = size
if 'credentialId' in body and 'fw_id' in locals(): if 'credentialId' in body and 'service_credentialid' in locals():
logging.debug(f'Set credentialId: {fw_id}') logging.debug(f'Set credentialId: {service_credentialid}')
body['credentialId'] = fw_id body['credentialId'] = service_credentialid
if 'instances' in body: if 'instances' in body:
if 'credentialId' in body['instances'][0] and 'instance_id' in locals(): if 'credentialId' in body['instances'][0] and 'service_instance_credentialid' in locals():
logging.debug(f'Set instance credentialId: {instance_id}') logging.debug(f'Set instance credentialId: {service_instance_credentialid}')
body['instances'][0]['credentialId'] = instance_id body['instances'][0]['credentialId'] = service_instance_credentialid
print(json.dumps(body)) print(json.dumps(body))
logging.debug(json.dumps(body)) logging.debug(json.dumps(body))
logging.info(f'=== Pushing {connector_name} to {cxp} (size: {size}; segment: {segments})') logging.info(f'=== Pushing {connector_name} to {cxp} (size: {size}; segment: {segments})')
logging.debug(f'CONNECTOR BEFORE AGAIN: {connector_name}') logging.debug(f'CONNECTOR BEFORE AGAIN: {connector_name}')
if connector_name in url_exceptions.keys(): if connector_name in url_exceptions.keys():
connector_name = url_exceptions[connector_name] connector_name = url_exceptions[connector_name]
logging.debug(f'CONNECTOR AFTER AGAIN: {connector_name}') logging.debug(f'CONNECTOR AFTER AGAIN: {connector_name}')

View File

@ -1,113 +0,0 @@
#!/usr/bin/env python3
#
# Copyright 2022, Mischa Peters <mischa AT alkira DOT net>, Alkira.
# wip-users.py
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
import os
import sys
import re
import json
import time
import logging
import requests
import configparser
###############################################
CONFIG_FILE = "/Users/mischa/Alkira/xDev/alkira.cnf"
if not os.path.isfile(CONFIG_FILE):
logging.error(f"The config file {CONFIG_FILE} doesn't exist")
sys.exit(1)
config = configparser.RawConfigParser()
config.read(CONFIG_FILE)
ALKIRA_TENANT = config.get('alkira', 'ALKIRA_TENANT')
ALKIRA_USERNAME = config.get('alkira', 'ALKIRA_USERNAME')
ALKIRA_PASSWORD = config.get('alkira', 'ALKIRA_PASSWORD')
ALKIRA_BASE_URI = f'https://{ALKIRA_TENANT}/api'
###############################################
# Set default headers
headers = {'Content-Type': "application/json"}
# Set logging.INFO to logging.DEBUG for debug information
logging.basicConfig(level=logging.DEBUG)
logging = logging.getLogger('AlkiraAPI')
def alkira_login():
body = {'userName': ALKIRA_USERNAME,
'password': ALKIRA_PASSWORD}
session = requests.session()
response = alkira_post(session, '/login', body)
return session
def alkira_post(session, uri, body):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.post(url, data=json.dumps(body), headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
def alkira_get(session, uri):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.get(url, headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
def alkira_delete(session, uri):
url = f'{ALKIRA_BASE_URI}{uri}'
try:
response = session.delete(url, headers=headers)
response.raise_for_status()
except Exception as e:
logging.error(f'Error: {str(e)}')
sys.exit(1)
return response
s = alkira_login()
logging.debug(s)
r = alkira_get(s, '/tenantnetworks')
#logging.debug(r.text)
data = r.json()
print(data[0]['name'])
print(data[0]['id'])
#body = {userName": "john", "email": "john@abcde.com", "firstName": "John", "lastName": "Doe", "password": "string", "roles": ["netadmin"], "userName": "john2"}
#r = alkira_post(s, '/users', body)
#print(r.status_code)
r = alkira_get(s, '/users')
data = r.json()
#print(json.dumps(data, indent=4))
for item in data:
if re.match(r'john', item.get('userName')):
user_id = item.get('id')
username = item.get('userName')
firstname = item.get('firstName')
lastname = item.get('lastName')
#print(f"First: {firstname}\n Last: {lastname}\n Login: {username}\n ID: {user_id}")
print(f'Deleting {username}')
r = alkira_delete(s, f'/users/{user_id}')
print(r.status_code)