Skip to content

Instantly share code, notes, and snippets.

@furqanbaqai
Created January 18, 2018 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furqanbaqai/f45c09738892944ebdfa33d468966ce1 to your computer and use it in GitHub Desktop.
Save furqanbaqai/f45c09738892944ebdfa33d468966ce1 to your computer and use it in GitHub Desktop.
Utility Class library for generating BMC Tokens - Login and Logout function
"""
Utility Class library for generating BMC Tokens
BMC True Sight Vault
https://gist.github.com/furqanbaqai/07e250fc3f11e42071c8f4d8c2729562
Authur: Muhammad Furqan Baqai [MFB] (baqai.furqan@gmail.com)
Change History
[MFB:2018-01-16] Initial checkin
"""
import json
import logging
from urllib.request import urlopen
from urllib.request import Request
class BMCTokenGenerator:
def __init__(self,bmcAPIUser, bmcAPIPass,bmc_tokenURL):
self.dictLogin = {"username": "","password": "","tenantName": "*"}
self.dictLogin["username"] = bmcAPIUser
self.dictLogin["password"] = bmcAPIPass
self.bmc_tokenURL = bmc_tokenURL
def getToken(self):
logger = logging.getLogger(__name__)
# Step#1: Creqte request
req = Request(url=self.bmc_tokenURL,data= json.dumps(self.dictLogin).encode('utf-8'),headers={'Content-type':'application/json'},method='POST')
logger.info("Sending request for fetching api token")
logger.debug("Sending request:"+json.dumps(self.dictLogin))
response = urlopen(req)
if response.getcode() == 200:
logger.info("Response received successfully")
dtResponse = json.loads(response.read().decode('utf-8'))
return dtResponse["response"]["authToken"]
return None
def logout(self, apitoken):
logger = logging.getLogger(__name__)
req = Request(url=self.bmc_tokenURL,data=None,headers={'authToken':apitoken, 'Content-type':'application/json'},method='DELETE')
logger.info("Logging out from True Sight")
response = urlopen(req)
if response.getcode() == 200:
logger.info("Loged-out successfully..")
else:
logger.error("Error response received while logging out. Response code:"+response.getcode())
logging.basicConfig(format='[%(levelname)s] %(asctime)s:%(message)s', level=logging.INFO)
if __name__ == "__main__":
tokenGen = BMCTokenGenerator('<userid>','<password>','http://<hostname>:8080/tsws/api/v10.1/token')
token = tokenGen.getToken()
logging.debug("Token received as:"+token)
tokenGen.logout(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment