def main(): # * Get existing argument parser from credentials parser = credentials.Parser parser.description = 'Intersight script to delete target by IP address' # * Place script specific arguments here parser.add_argument( '-t', '--target_host', required=True, help= 'Target host ip to delete. Deletes 1st target found with the specified IP(does not handle multiple targets with same IPs in an account' ) api_client = credentials.config_credentials() args = parser.parse_args() try: # * Start main code here # * Delete target with specified IP delete_target(api_client, args.target_host) except intersight.OpenApiException as exp: print("Exception when calling API: %s\n" % exp) traceback.print_exc() sys.exit(1) sys.exit(0)
def main(): client = credentials.config_credentials() try: #* Start main code here #* Get condition class instance api_instance = intersight.api.cond_api.CondApi(client) #* Find all Critical severity alarms within the last 30 days search_period = datetime.now() - timedelta(days=30) query_filter = f"Severity eq Critical and CreationTime gt {format_time(search_period)}" #* Only include CreationTime and Description in results query_select = "CreationTime,Description" #* Get alarms using query parameters alarm_query = api_instance.get_cond_alarm_list(filter=query_filter, select=query_select) #* Print table output of results ommiting uninteresting fields print_results_to_table(alarm_query.results, ignored_fields=['class_id', 'object_type']) except intersight.OpenApiException as e: logger.error("Exception when calling API: %s\n" % e) traceback.print_exc()
def main(): client = credentials.config_credentials() try: #* Start main code here #* Get aaa class instance api_instance = intersight.api.aaa_api.AaaApi(client) #* Find all aaa records for login attempts query_filter = "Event eq Login" #* Group results by Email and set count to property named `Total` query_apply = "groupby((Email), aggregate($count as Total))" #* Sort by Total in descending order query_order_by = "-Total" #* Get aaa records using query parameters aaa_query = api_instance.get_aaa_audit_record_list( filter=query_filter, apply=query_apply, orderby=query_order_by) #* Print table output of results print_results_to_table(aaa_query.results) except intersight.OpenApiException as e: logger.error("Exception when calling API: %s\n" % e) traceback.print_exc()
def main(): # Configure API key settings for authentication apiClient = credentials.config_credentials() try: # Get list of users get_users(apiClient) except intersight.OpenApiException as e: logger.error("Exception when calling API: %s\n" % e) traceback.print_exc()
def main(): # Configure API key settings for authentication api_client = credentials.config_credentials() try: # Get example time series data get_time_series(api_client) except intersight.OpenApiException as e: logger.error("Exception when calling API: %s\n" % e) traceback.print_exc()
def main(): # Configure API key settings for authentication api_client = credentials.config_credentials() try: # Get audit log data get_audit_logs(api_client) get_audit_logs_tag_summary(api_client) get_audit_log_aggregate_query(api_client) except intersight.OpenApiException as e: logger.error("Exception when calling API: %s\n" % e) traceback.print_exc()
def main(): # * Get existing argument parser from credentials parser = credentials.Parser parser.description = 'Intersight script to claim target' # * Place script specific arguments here parser.add_argument( '-t', '--targets', required=True, help= 'JSON file with target access information (hostname, username, password, and proxy settings if requred)' ) api_client = credentials.config_credentials() args = parser.parse_args() return_code = 0 if os.path.isfile(args.targets): with open(args.targets, 'r') as targets_file: targets_list = json.load(targets_file) else: # Argument targets can be a JSON string instead of file. # JSON string input can be used with Ansible to directly pass all info on the command line. targets_list = json.loads(args.targets) # Large try and generic exception handling with many APIs in use and exceptions possible try: for target in targets_list: result = dict(changed=False) result['msg'] = " Host: %s" % target['hostname'] # default access mode to allow control (Read-only False) and set to a boolean value if a string if not target.get('read_only'): target['read_only'] = False else: if target['read_only'] == 'True' or target[ 'read_only'] == 'true': target['read_only'] = True elif target['read_only'] == 'False' or target[ 'read_only'] == 'false': target['read_only'] = False # create target connector object based on target type if target['device_type'] == 'imc' or target[ 'device_type'] == 'ucs' or target[ 'device_type'] == 'ucsm' or target[ 'device_type'] == 'ucspe': dc_obj = device_connector.UcsDeviceConnector(target) elif target['device_type'] == 'hx': dc_obj = device_connector.HxDeviceConnector(target) else: result['msg'] += " Unknown device_type %s" % target[ 'device_type'] return_code = 1 print(json.dumps(result)) continue if not dc_obj.logged_in: result['msg'] += " Login error" return_code = 1 print(json.dumps(result)) continue ro_json = dc_obj.configure_connector() if not ro_json['AdminState']: return_code = 1 if ro_json.get('ApiError'): result['msg'] += ro_json['ApiError'] print(json.dumps(result)) continue # set access mode (ReadOnlyMode True/False) to desired state if (ro_json.get('ReadOnlyMode') is not None) and ( ro_json['ReadOnlyMode'] != target['read_only']): ro_json = dc_obj.configure_access_mode(ro_json) if ro_json.get('ApiError'): result['msg'] += ro_json['ApiError'] return_code = 1 print(json.dumps(result)) continue result['changed'] = True # configure proxy settings (changes reported in called function) ro_json = dc_obj.configure_proxy(ro_json, result) if ro_json.get('ApiError'): result['msg'] += ro_json['ApiError'] return_code = 1 print(json.dumps(result)) continue # wait for a connection to establish before checking claim state for _ in range(30): if ro_json['ConnectionState'] != 'Connected': sleep(1) ro_json = dc_obj.get_status() else: break result['msg'] += " AdminState: %s" % ro_json['AdminState'] result[ 'msg'] += " ConnectionState: %s" % ro_json['ConnectionState'] result['msg'] += " Previous claim state: %s" % ro_json[ 'AccountOwnershipState'] if ro_json['ConnectionState'] != 'Connected': return_code = 1 result['msg'] += " Not connected: unable to claim" print(json.dumps(result)) continue if ro_json['AccountOwnershipState'] != 'Claimed': # attempt to claim (claim_resp, device_id, claim_code) = dc_obj.get_claim_info(ro_json) if claim_resp.get('ApiError'): result['msg'] += claim_resp['ApiError'] return_code = 1 print(json.dumps(result)) continue result['msg'] += " Id: %s" % device_id result['msg'] += " Token: %s" % claim_code # Create Intersight API instance and post ID/claim code claim_target(api_client, device_id, claim_code) result['changed'] = True print(json.dumps(result)) # logout of any open sessions dc_obj.logout() except Exception as err: print("Exception:", str(err)) print('-' * 60) traceback.print_exc(file=sys.stdout) print('-' * 60) finally: # logout of any sessions active after exception handling if ('dc_obj' in locals() or 'dc_obj' in globals()): dc_obj.logout() sys.exit(return_code)