Created
January 18, 2018 10:47
-
-
Save furqanbaqai/f45c09738892944ebdfa33d468966ce1 to your computer and use it in GitHub Desktop.
Utility Class library for generating BMC Tokens - Login and Logout function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Utility Class library for generating BMC Tokens | |
BMC True Sight Vault | |
https://gist.github.com/furqanbaqai/07e250fc3f11e42071c8f4d8c2729562 | |
Authur: Muhammad Furqan Baqai [MFB] ([email protected]) | |
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