def list_mpgw(domain_name, dp_target, env_target, path): """ This command retrieves a list of MultiProtocol Gateways from a given target. """ dp_object = tools.load_datapower_object(config, dp_target, env_target) if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MultiProtocolGateway" mpgw_list_response = requests.get(url=link, auth=auth, verify=False) if int(int(mpgw_list_response.status_code) / 100) == 2: click.secho('Successfully retrieved a list of MultiProtocol Gateways', fg='green') mpgw_list_json = mpgw_list_response.json()["MultiProtocolGateway"] mpgw_list = [] if isinstance(mpgw_list_json, dict): mpgw_list.append(mpgw_list_json["name"]) elif isinstance(mpgw_list_json, list): for mpgw in mpgw_list_json: mpgw_list.append(mpgw["name"]) if path != None: file_name = os.path.join(path, "mpgw_list.json") click.secho("Saving output to '{0}' : ".format(file_name), fg='green') with codecs.open(file_name, "w", "utf-8") as w_file: json.dump(mpgw_list, w_file, sort_keys=True, indent=4, ensure_ascii=False) else: click.echo(str(mpgw_list)) return True else: click.secho('Failure. error: {0}.'.format(mpgw_list_response.json()['error']), fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MultiProtocolGateway" mpgw_list_response = requests.get(url=link, auth=auth, verify=False) if int(int(mpgw_list_response.status_code) / 100) == 2: click.secho('Datapower {0} : Successfully retrieved a list of MPGW and their state : '.format(datapower["name"]), fg='green') mpgw_list_json = mpgw_list_response.json()["MultiProtocolGateway"] mpgw_list = [] if isinstance(mpgw_list_json, dict): mpgw_list.append(mpgw_list_json["name"]) elif isinstance(mpgw_list_json, list): for mpgw in mpgw_list_json: mpgw_list.append(mpgw["name"]) if path != None: file_name = os.path.join(path, datapower["name"] + "_mpgw_list.json") click.secho("Saving output to '{0}' : ".format(file_name), fg='green') with codecs.open(file_name, "w", "utf-8") as w_file: json.dump(mpgw_list, w_file, sort_keys=True, indent=4, ensure_ascii=False) else: click.echo(str(mpgw_list)) return True else: click.secho('Datapower {0} : Failure. error: {1}.'.format(datapower["name"], mpgw_list_response.json()['error']), fg='red') return False
def create_http_fsh(name, listen_address, listen_port, domain_name, state, dp_target, env_target): """ This command creates an HTTP Front side handler """ click.secho("Creating a new HTTP FSH : {0}".format(name), fg='yellow') dp_object = tools.load_datapower_object(config, dp_target, env_target) http_fsh_object = { "HTTPSourceProtocolHandler" : { "name" : str(name), "mAdminState" : str(state), "AllowedFeatures" : { "CONNECT" : "off", "CmdExe" : "off", "CustomMethods" : "off", "DELETE" : "off", "DotDot" : "off", "FragmentIdentifiers" : "on", "GET" : "off", "HEAD" : "off", "HTTP-1.0" : "on", "HTTP-1.1" : "on", "OPTIONS" : "off", "POST" : "on", "PUT" : "on", "QueryString" : "on", "TRACE" : "off" }, "HTTPVersion" : "HTTP/1.1", "LocalAddress" : str(listen_address), "LocalPort" : str(listen_port), "PersistentConnections" : "on" } } if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "config/"+ str(domain_name) +"/HTTPSourceProtocolHandler" response = requests.post(url=link, data=json.dumps(http_fsh_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho("Success - HTTP FSH {0} has been created!.".format(name), fg='green') return True else: click.secho("Failure - HTTP FSH {0} could not been created! error: {1}.".format(name, response.json()['error']), fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "config/"+ str(domain_name) +"/HTTPSourceProtocolHandler" response = requests.post(url=link, data=json.dumps(http_fsh_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho("Datapower {0} : Success - HTTP FSH {1} has been created!.".format(datapower["name"], name), fg='green') return True else: click.secho("Datapower {0} : Failure - HTTP FSH {1} could not been created! error: {2}.".format(datapower["name"], name, response.json()['error']), fg='red') return False
def create_mq_fsh(name, queue_manager, queue_name, domain_name, state, dp_target, env_target, parse_properties, async_put): """ This command creates an IBM MQ Front sider handler """ click.echo("Creating a new MQ FSH : {0}".format(name)) dp_object = tools.load_datapower_object(config, dp_target, env_target) mq_fsh_object = { "MQSourceProtocolHandler" : { "name" : str(name), "mAdminState" : str(state), "QueueManager" : str(queue_manager), "GetQueue" : str(queue_name), "CodePage" : 1204, "GetMessageOptions" : 1, "ParseProperties" : str(parse_properties), "AsyncPut" : str(async_put), "ExcludeHeaders" : { "MQCIH" : "off", "MQDLH" : "off", "MQIIH" : "off", "MQRFH" : "off", "MQRFH2" : "off", "MQWIH" : "off" }, "ConcurrentConnections" : 1, "PollingInterval" : 30, "BatcjSize" : 0, "ContentTypeHeader" : "None", "RetrieveBackoutSettings" : "off", "UseQMNameInURL" : "off" } } if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MQSourceProtocolHandler" response = requests.post(url=link, data=json.dumps(mq_fsh_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho("Success - MQ FSH {0} has been created!.".format(name), fg='green') return True else: click.secho("Failure - MQ FSH {0} could not been created! error: {1}.".format(name, response.json()['error']), fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MQSourceProtocolHandler" response = requests.post(url=link, data=json.dumps(mq_fsh_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho("Datapower {0} : Success - MQ FSH {1} has been created!.".format(datapower["name"], name), fg='green') return True else: click.secho("Datapower {0} : Failure - MQ FSH {1} could not been created! error: {2}.".format(datapower["name"], name, response.json()['error']), fg='red') return False
def export_http_fsh(domain_name, fsh_target, export_type, directory_path, dp_target, env_target, all_files, include_internal_files): """ This command lets you export said HTTP Front Side Handler as either one of these options: JSON | XML | ZIP """ if not any(export_type.lower() in export for export in export_types): click.secho("The export type is not among the permitted types. Use --help to view possible types.", fg='red') return dp_object = tools.load_datapower_object(config, dp_target, env_target) click.secho("Exporting HTTP Front Side Handler : '{0}' to '{1}' as a {2} file...".format(fsh_target, directory_path, export_type.title()), fg='yellow') export_request_object = { "Export" : { "Format" : str(export_type).upper(), "AllFiles" : str("on" if all_files else "off"), "IncludeInternalFiles" : str("on" if include_internal_files else "off"), "Object" : { "class" : "HTTPSourceProtocolHandler", "name" : str(fsh_target), "ref-objects" : "on" } } } if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "actionqueue/"+ str(domain_name) action_response = requests.post(url=link, data=json.dumps(export_request_object), auth=auth, verify=False) export_response = tools.get_exported_json_object(dp_object, action_response, auth) if export_response != None: file_name = os.path.join(directory_path, str(fsh_target + "_HTTP_FSH_Export.json")) with codecs.open(file_name, "w", "utf-8") as w_file: json.dump(export_response, w_file, sort_keys=True, indent=4, ensure_ascii=False) click.secho("Success - Exported '{0}' to '{1}'.".format(fsh_target, file_name), fg='green') return True else: click.secho("Failure - Could'nt export '{0}'.".format(fsh_target), fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "actionqueue/"+ str(domain_name) action_response = requests.post(url=link, data=json.dumps(export_request_object), auth=auth, verify=False) export_response = tools.get_exported_json_object(datapower, action_response, auth) if export_response != None: file_name = os.path.join(directory_path, str(fsh_target + "_HTTP_FSH_Export_" + datapower["name"] + ".json")) with codecs.open(file_name, "w", "utf-8") as w_file: json.dump(export_response, w_file, sort_keys=True, indent=4, ensure_ascii=False) click.secho("Datapower {0} : Success - Exported '{1}' to '{2}'.".format(datapower["name"], fsh_target, file_name), fg='green') return True else: click.secho("Datapower {0} : Failure - Could'nt export '{1}'.".format(datapower["name"], fsh_target), fg='red') return False
def save_config(domain_name, dp_target, env_target): """ This command creates an IBM MQ Front sider handler """ click.secho("Saving Configuration", fg='yellow') dp_object = tools.load_datapower_object(config, dp_target, env_target) config_object = {"SaveConfig": ""} if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "actionqueue/" + str( domain_name) response = requests.post(url=link, data=json.dumps(config_object), auth=auth, verify=False) click.echo("{0} -- {1}".format(response.status_code, response.reason)) if int(int(response.status_code) / 100) == 2: click.secho('Success - Configuration saved.', fg='green') return True else: click.secho('Failure - Configuration not saved.', fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "actionqueue/" + str( domain_name) response = requests.post(url=link, data=json.dumps(config_object), auth=auth, verify=False) click.echo("{0} -- {1}".format(response.status_code, response.reason)) if int(int(response.status_code) / 100) == 2: click.secho('Success - Configuration saved for {0}.'.format( datapower["name"]), fg='green') return True else: click.secho( 'Failure - Configuration not saved for {0}.'.format( datapower["name"]), fg='red') return False
def list_directory(target_directory, domain_name, dp_target, env_target, file_target): """ This command lets you manipulate files from the datapower """ dp_object = tools.load_datapower_object(config, dp_target, env_target) if isinstance(dp_object, dict): click.secho("Searching for directory '{0}'".format(target_directory)) auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) folder_split = target_directory.split("/") if len(folder_split) > 1: link = str(dp_object["datapower_rest_url"]) + "filestore/"+ str(domain_name) +"/"+ str(target_directory) else: link = str(dp_object["datapower_rest_url"]) + "filestore/"+ str(domain_name) +"/"+ str(target_directory) response = requests.get(url=link, auth=auth, verify=False) response_json = response.json()["filestore"]["location"] if not file_target is None: with codecs.open(file_target, 'w', 'utf-8') as w_file: json.dump(response_json, w_file, sort_keys=True, indent=4, ensure_ascii=False) click.secho("Response saved to : '{0}'".format(file_target), fg="green") return True else: click.echo("Content : {0}".format(json.dumps(response_json, sort_keys=True, indent=4))) elif isinstance(dp_object, list): for datapower in dp_object: click.secho("Searching for directory '{0}' in datapower : {1}".format(target_directory, datapower["name"])) auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) folder_split = target_directory.split("/") if len(folder_split) > 1: link = str(datapower["datapower_rest_url"]) + "filestore/"+ str(domain_name) +"/"+ str(target_directory) else: link = str(datapower["datapower_rest_url"]) + "filestore/"+ str(domain_name) +"/"+ str(target_directory) response = requests.get(url=link, auth=auth, verify=False) response_json = response.json()["filestore"]["location"] if not file_target is None: with codecs.open(file_target + "_" + datapower["name"], 'w', 'utf-8') as w_file: json.dump(response_json, w_file, sort_keys=True, indent=4, ensure_ascii=False) click.secho("Response saved to : '{0}'".format(file_target), fg="green") return True else: click.echo("Datapower {0}, Content : {1}".format(datapower["name"],json.dumps(response_json, sort_keys=True, indent=4))) return False
def create_mpgw(mpgw_name, fsh_name, backend_url, backend_type, request_type, response_type, policy_name, domain_name, state, dp_target, env_target, xml_manager, host_rewrite, propagate_uri): """ This command creates a Multi-Protocol-Gateway Note: The 'backend_type' is an option but it is highly recommended to set a type. In case the 'backend_type' is 'static', use the option 'backend_url' as well or else the default value will be applied instead. """ if not any(request_type.lower() in req for req in request_respone_list): click.secho("The request type is not among the permitted types. Use --help to view possible types.", fg='red') return elif not any(response_type.lower() in res for res in request_respone_list): click.secho("The response type is not among the permitted types. Use --help to view possible types.", fg='red') return dp_object = tools.load_datapower_object(config, dp_target, env_target) click.secho("Creating MPGW : {0}".format(mpgw_name), fg='yellow') mpgw_object = { "MultiProtocolGateway": { "name": str(mpgw_name), # Name of the mpgw *required* "mAdminState": str(state), # The state (default enabled) *required* "FrontProtocol": { # The FSH list (either [] or {}) *required* "value" : str(fsh_name) }, "XMLManager": str(xml_manager), # The xml-manager (default default) *required* "BackendUrl": str(backend_url), # The backendUrl *required* "PropagateURI": str(propagate_uri), # Whether to propagate-url or not (default on) *required* "PersistentConnections": "on", "LoopDetection": "off", "DoHostRewriting": str(host_rewrite), # Should datapower rewrite the 'host' header (default on) "DoChunkedUpload": "off", "ProcessHTTPErrors": "on", "HTTPClientIPLabel": "X-Client-IP", "HTTPLogCorIDLabel": "X-Global-Transaction-ID", "InOrderMode": { "Request": "off", "Backend": "off", "Response": "off" }, "ForcePolicyExec": "off", "RewriteErrors": "on", "DelayErrors": "on", "DelayErrorsDuration": 1000, "RequestType": str(request_type), # The request type (preprocessed = non-xml) *required* "ResponseType": str(response_type), # The respone type (preprocessed = non-xml) *required* "FollowRedirects": "on", "RewriteLocationHeader": "off", "StylePolicy": str(policy_name), # The policy name *required* "Type": "{0}-backend".format(str(backend_type)), # The type of the backend (default static-backend) *required* "AllowCompression": "off", "AllowCacheControlHeader": "off", "PolicyAttachments": "Test", # ? "WSMAgentMonitor": "off", "WSMAgentMonitorPCM": "all-messages", "ProxyHTTPResponse": "off", "TransactionTimeout": 0 } } if isinstance(dp_object, dict): auth = (dp_object["credentials"]["username"], dp_object["credentials"]["password"]) link = str(dp_object["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MultiProtocolGateway" response = requests.post(url=link, data=json.dumps(mpgw_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho('Success - MPGW {0} has been created!.'.format(mpgw_name), fg='green') return True else: click.secho('Failure - MPGW {0} could not been created! error: {1}.'.format(mpgw_name, response.json()['error']), fg='red') elif isinstance(dp_object, list): for datapower in dp_object: auth = (datapower["credentials"]["username"], datapower["credentials"]["password"]) link = str(datapower["datapower_rest_url"]) + "config/"+ str(domain_name) +"/MultiProtocolGateway" response = requests.post(url=link, data=json.dumps(mpgw_object), auth=auth, verify=False) if int(int(response.status_code) / 100) == 2: click.secho('Datapower {0} : Success - MPGW {1} has been created!'.format(datapower["name"], mpgw_name), fg='green') return True else: click.secho('Datapower {0} : Failure - MPGW {1} could not been created ! error: {2}.'.format(datapower["name"], mpgw_name, response.json()['error']), fg='red') return False