def get_parent_organization_group_details(organization_group_uuid): """ To get the list of parent organization group uuid for particular OG :param organization_group_uuid: organization group uuid :return: list of parent organization group details """ api_url = get_organization_group_details_url(organization_group_uuid) headers = RequestHeader().header try: response = requests.get(api_url, headers=headers) if not response.ok: log.error(response.status_code, response.reason, response.content) # HTTP return response else: log.info(response.content) response_data = json.loads(response.content) return response_data except Exception as e: log.error( 'Organization Group Details Search failed for organization group uuid {} with exception {}' .format(organization_group_uuid, str(e))) return e
def search_product_with_id(product_id): """ Searches for Product based on product id :param product_id: Product ID :returns : True or False indicating Success or Failure and product associated with id """ api_url = get_product_search_url(product_id) headers = RequestHeader().header try: response = requests.get(api_url, headers=headers) if not response.ok: log.error(response.status_code, response.reason, response.content) # HTTP return False, response else: log.info(response.content) response_data = json.loads(response.content) return True, response_data except Exception as e: log.error( 'Product Search failed for product id {} with exception {}'.format( product_id, str(e))) return False, e
def search_product(params): """ Searches for Products based on the filter criteria :param params: Search Parameters :returns: True or False indicating Success or Failure and list of products """ api_url = get_product_extensive_search_url() headers = RequestHeader().header api_params = params try: response = requests.get(api_url, headers=headers, params=api_params) if not response.ok: log.error(response.status_code, response.reason, response.content) # HTTP return False, response else: log.info(response.content) response_data = json.loads(response.content) return True, response_data except Exception as e: log.error( 'Product Search failed for params {} with exception {}'.format( params, str(e))) return False, e
def create_app(transaction_data): """ Creates a new application based on the transaction data model :param transaction_data: Transaction data model :return: Returns True/False indicating Success/Failure and Application ID(0 in case of failure) """ api_url = get_create_internal_app_from_blob_url() headers = RequestHeader().header api_body = { 'Description': transaction_data.description, 'BlobId': str(transaction_data.blob_id), 'PushMode': transaction_data.push_mode, 'ApplicationName': transaction_data.application_name, 'FileName': transaction_data.file_name, 'DeviceType': transaction_data.device_type, 'EnableProvisioning': transaction_data.enable_provisioning, 'UploadViaLink': transaction_data.upload_via_link, 'LocationGroupId': config.TENANT_GROUP_ID, 'SupportedModels': transaction_data.supported_models, 'ActualFileVersion': None, 'AppVersion': transaction_data.app_version } payload = json.dumps(api_body) try: response = requests.post(api_url, headers=headers, data=payload) if not response.ok: log.error( f'{response.status_code}, {response.reason}, {response.content}' ) # HTTP return False, 0, 0, '', '' else: response_data = json.loads(response.content) app_version = response_data['AppVersion'] app_name = response_data['ApplicationName'] bundle_id = response_data['BundleId'] log.info('Application saved with Application ID {id}'.format( id=response_data['Id']['Value'])) return True, response_data['Id'][ 'Value'], app_version, app_name, bundle_id except Exception as e: log.error('Application creation failed for transactionId: {}'.format( transaction_data.transaction_id, str(e)))
def blob_upload(file_source_path, file_name): """ Uploads the blob to Airwatch Server :param file_source_path : File to be uploaded :param file_name : Name of the file :return : True/False indicating Success/Failure and Transaction ID, if successful """ file_size = os.path.getsize(file_source_path) api_url = get_blob_upload_url() api_url = "{api_url}?fileName={fileName}&organizationgroupid={ogid}&moduleType={moduleType}"\ .format(api_url=api_url, fileName=file_name, ogid=config.TENANT_GROUP_ID, moduleType=MODULE_TYPE) headers = RequestHeader().header with open(file_source_path, 'rb') as file: start = 0 file.seek(start) chunk_data = file.read(file_size) try: response = requests.post(api_url, headers=headers, data=chunk_data) if not response.ok: log.error(f'{response.status_code}, {response.reason}') log.debug(f'{response.content}') return False, 0 else: response_data = json.loads(response.content) if response_data['Value'] > 0: blob_id = response_data['Value'] return True, blob_id else: return False, 0 except Exception as e: log.error('Upload blob failed for file path {} with exception: {}'. format(file_source_path, str(e)))
def edit_app_assignment(app_id, app_assignment_model, assignment_group_for_deletion): """ Edits the app assignment for given Application ID :param app_id: Application ID :param app_assignment_model: App assignment model :param assignment_group_for_deletion: Smartgroup IDs that has to be deleted from assignment :return: True/False indicating Success/Failure """ api_url = get_edit_assignment_url(app_id) headers = RequestHeader().header api_body = { 'SmartGroupIds': app_assignment_model.smart_group_ids, 'SmartGroupIdsForDeletion': assignment_group_for_deletion, 'DeploymentParameters': app_assignment_model.deployment_parameters } payload = json.dumps(api_body) try: response = requests.put(api_url, headers=headers, data=payload) log.debug(f'{response.status_code}, {response.reason}') if not response.ok: return False else: log.debug( 'App(AppID : {id}) assignment updated with smartgroups {groups}' .format(id=app_id, groups=app_assignment_model.smart_group_ids)) return True except Exception as e: log.error( 'Application edit assignment failed for Application: {id} with error {e}' .format(id=app_id, e=str(e)))
def activate_product(product_id): """ Activates the product with the specified product_id :param product_id: Product Id of the product to be activated :returns bool: indicating Success or Failure """ api_url = get_product_activate_url(product_id) headers = RequestHeader().header try: response = requests.post(api_url, headers=headers) if not response.ok: log.error(response.status_code, response.reason, response.content) # HTTP return False else: return True except Exception as e: log.error( 'Product Activation failed for product id {} with exception {}'. format(product_id, str(e))) return False
def get_app_details(app_id): """ Get details of an internal app by using app id :param app_id: App ID :return: True/False indicating Success/Failure and app_details json that contains details of that app """ api_url = get_internal_app(app_id) headers = RequestHeader().header try: response = requests.get(api_url, headers=headers) if not response.ok: log.error(f'{response.status_code}, {response.reason}, {response.content}') # HTTP return False, 0 else: app_details = json.loads(response.content) return True, app_details except Exception as e: log.error('Get application details failed for app_id {} with exception {}'.format(app_id, str(e))) return False, 0
def search_application(bundle_id): """ Search for applications with the given Bundle ID :param bundle_id: Bundle ID (App Identifier) :return: True/False indicating Success/Failure and Application_list that matches the given Bundle ID """ api_url = get_apps_search_url() headers = RequestHeader().header api_params = { 'type': 'App', 'applicationtype': 'Internal', 'bundleid': bundle_id, 'locationgroupid': config.TENANT_GROUP_ID, 'productcomponentappsonly': 'False' } try: response = requests.get(api_url, headers=headers, params=api_params) if not response.ok: log.error( f'{response.status_code}, {response.reason}, {response.content}' ) # HTTP return False, 0 else: response_data = json.loads(response.content) app_list = response_data['Application'] return True, app_list except Exception as e: log.error('Application Search failed: {}'.format(str(e))) return False
def search_product_with_application_id(app_id): """ Searches for Products based on application id association :param app_id: application id that is being used in products :return : True or False Indicates Success (Product Found) or Failure (Product Not Found) and ProductID """ api_url = get_product_application_url(app_id) headers = RequestHeader().header try: response = requests.get(api_url, headers=headers) if not response.ok: log.error(response.status_code, response.reason, response.content) # HTTP return False, response else: log.info(response.content) response_data = json.loads(response.content) return True, response_data[0]['ProductID'] except Exception as e: log.error('Product Search failed for app id {} with exception {}'.format(app_id, str(e))) return False, response
def retire_app(app_id): """ Retires the app based on the Application ID :param app_id: Application ID :return: True/False indicating Success/Failure """ api_url = get_retire_app_url(app_id) headers = RequestHeader().header try: response = requests.post(api_url, headers=headers) log.debug( f'{response.status_code}, {response.reason}, {response.content}') if not response.ok: return False else: return True except Exception as e: log.error('Application creation failed for transactionId: {}'.format( str(e)))
def add_assignments(app_id, app_assignment_model): """ Assigns the app created to specified smart groups :param app_id: Application ID :param app_assignment_model: Assignment Model :return: True/False indicating Success/Failure """ api_url = get_internal_app_assignment_url(app_id) headers = RequestHeader().header api_body = { 'SmartGroupIds': app_assignment_model.smart_group_ids, 'DeploymentParameters': app_assignment_model.deployment_parameters } payload = json.dumps(api_body) try: response = requests.post(api_url, headers=headers, data=payload) log.debug( f'{response.status_code}, {response.reason}, {response.content}') if not response.ok: return False else: log.debug( 'App with ID: {id} assigned to smartgroups {groups}'.format( id=app_id, groups=app_assignment_model.smart_group_ids)) return True except Exception as e: log.error( 'Application assignment failed for Application : {id} with error {e}' .format(id=app_id, e=str(e)))
def chunk_upload(file_source_path): """ Uploads the file in chunks to Airwatch Server :param file_source_path: File to be uploaded :return: True/False indicating Success/Failure and Transaction ID, if successful """ file_size = os.path.getsize(file_source_path) api_url = get_chunk_upload_url() headers = RequestHeader().header with open(file_source_path, 'rb') as file: start = 0 chunk_count = math.ceil(file_size / config.MAX_UPLOAD_BYTE_LENGTH) retry_timeout = 0.300 # milliseconds sent_chunk_count = 0 transaction_id = '' log.debug( 'File {} Total chunk count:{count} with transaction {id}'.format( file_source_path, count=chunk_count, id=transaction_id)) while True: current_chunk_count = sent_chunk_count + 1 log.debug('Uploading chunk number: {}'.format(current_chunk_count)) end = min(file_size, start + config.MAX_UPLOAD_BYTE_LENGTH) file.seek(start) chunk_data = file.read(end) base64_file = str(base64.b64encode(chunk_data))[2:-1] internal_app_chunk_value = { 'TransactionId': str(transaction_id), 'ChunkData': base64_file, 'ChunkSize': end - start, 'ChunkSequenceNumber': current_chunk_count, 'TotalApplicationSize': file_size } payload = json.dumps(internal_app_chunk_value) start = start + end try: response = requests.post(api_url, headers=headers, data=payload) if not response.ok: log.error(f'{response.status_code}, {response.reason}') log.debug(f'{response.content}') return False, 0 else: response_data = json.loads(response.content) if response_data['UploadSuccess']: log.debug('{}. chunk sent to server'.format( current_chunk_count)) sent_chunk_count = current_chunk_count transaction_id = response_data['TranscationId'] else: return False, 0 except Exception as e: log.error('Upload chunk failed with exception: {}'.format( str(e))) # Sleep time.sleep(retry_timeout) if sent_chunk_count >= chunk_count: return True, transaction_id
def associate_app_to_product(product_name, app_id, assignment_groups, deployment_type, optional_id=0): """ Creates a new product for specified application component and assignment groups :param product_name: product name :param app_id: application id to be associated with new product :param assignment_groups: Assignment groups :param deployment_type: Deployment Type(Alpha, Beta, Prod) :param optional_id: optional id :return: True/False to indicate Success/Failure and Product ID """ api_url = get_create_product_url() headers = RequestHeader().header generate_input_value = GenerateInputValueData() maintain_general_input = { 'LocationGroupId': generate_input_value.location_group_id, 'InsertOnly': generate_input_value.insert_only } product_steps = [] product_step = ProductStepData(3, 0, app_id, False) product_step1 = { 'StepType': product_step.step_type, 'SequenceNumber': product_step.sequence_number, 'ApplicationID': product_step.application_id, 'Persist': product_step.persist } product_steps.append(product_step1) smart_group_array = [] for assignment in assignment_groups: smart_group_array.append({'SmartGroupID': assignment}) product_payload_data = ProductPayloadData(product_name, product_steps, deployment_type, app_id, smart_group_array) if optional_id == 0: product = { 'Name': product_payload_data.product_name, 'Description': product_payload_data.description, 'PauseResume': False, 'Platform': PLATFORM, 'ProductType': PRODUCT_TYPE, 'Steps': product_payload_data.product_steps, 'SmartGroups': product_payload_data.smart_group_array } else: product = { 'ProductID': optional_id, 'Name': product_payload_data.product_name, 'Description': product_payload_data.description, 'PauseResume': False, 'Platform': PLATFORM, 'ProductType': PRODUCT_TYPE, 'Steps': product_payload_data.product_steps, 'SmartGroups': product_payload_data.smart_group_array } api_body = { 'MaintainGeneralInput': maintain_general_input, 'Product': product } try: payload = json.dumps(api_body) response = requests.post(api_url, headers=headers, data=payload) if not response.ok: log.error(f'{response.status_code}, {response.reason}, {response.content}') # HTTP return False else: response_data = json.loads(response.content) log.info('Product associated with Application : {}'.format(str(response_data))) return True except Exception as e: log.error('Product creation failed: {}'.format(str(e))) return False
def create_app(transaction_data): """ Creates a new application based on the transaction data model :param transaction_data: Transaction data model :return: Returns True/False indicating Success/Failure and Application ID(0 in case of failure) """ api_url = get_create_internal_app_from_blob_url() headers = RequestHeader().header api_body = { 'TransactionId': str(transaction_data.transaction_id), 'Description': transaction_data.description, 'BlobId': transaction_data.blob_id, 'PushMode': transaction_data.push_mode, 'ApplicationName': transaction_data.application_name, 'FileName': transaction_data.file_name, 'DeviceType': transaction_data.device_type, 'EnableProvisioning': transaction_data.enable_provisioning, 'UploadViaLink': transaction_data.upload_via_link, 'LocationGroupId': config.TENANT_GROUP_ID, 'SupportedModels': transaction_data.supported_models, 'BundleId': None, 'ActualFileVersion': None, 'AppVersion': transaction_data.app_version, 'SupportedProcessorArchitecture': None, 'MsiDeploymentParamModel': { 'RetryCount': None, 'InstallTimeoutInMinutes': None, 'CommandLineArguments': None, 'RetryIntervalInMinutes': None }, 'DeploymentOptions': None, 'IsDependencyFile': False, 'FilesOptions': None, 'CarryOverAssignments': transaction_data.carry_over_assignments } payload = json.dumps(api_body) try: response = requests.post(api_url, headers=headers, data=payload) if not response.ok: log.debug( f'{response.status_code}, {response.reason}, {response.content}' ) # HTTP return False, 0, 0, '' else: response_data = json.loads(response.content) app_version = response_data['AppVersion'] bundle_id = response_data['BundleId'] log.debug('Application saved with Application ID {id}'.format( id=response_data['Id']['Value'])) return True, response_data['Id']['Value'], app_version, bundle_id except Exception as e: log.error('Application creation failed for transactionId: {}'.format( transaction_data.transaction_id, str(e)))