def deleteStaleResultCount(runtime, resultType, serviceType, thresholdInHours): """Issue an API call to delete the matching results.""" content = { 'filter': [{ 'negation': True, 'condition': { 'attribute': 'time_started', 'operator': 'lastnumhours', 'value': thresholdInHours } }] } apiResponse = getApiResult(runtime, 'job/{}/{}/filter'.format( resultType, serviceType), 'delete', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) runtime.logger.report('Delete completed: {queryResults!r}', queryResults=queryResults) else: raise EnvironmentError( 'Unexpected response from API. Code: {}. Payload: {}'.format( responseCode, responseAsJson)) ## end deleteStaleResultCount return
def getStaleResultCount(runtime, resultType, serviceType, thresholdInHours): """Issue an API call to get the object count.""" resultCount = 0 content = { 'count': True, 'filter': [{ 'negation': True, 'condition': { 'attribute': 'time_started', 'operator': 'lastnumhours', 'value': thresholdInHours } }] } apiResponse = getApiResult(runtime, 'job/{}/{}/filter'.format( resultType, serviceType), 'get', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) resultCount = queryResults.get('Result Count') runtime.logger.report( 'Stale {resultType!r} results found: {resultCount!r}', resultType=resultType, resultCount=resultCount) else: raise EnvironmentError( 'Unexpected response from API. Code: {}. Payload: {}'.format( responseCode, responseAsJson)) ## end getStaleResultCount return resultCount
def updateObject(runtime, objectType, objectId, objectData): content = {'content' : objectData} apiResponse = utilities.getApiResult(runtime, 'archive/{}/{}'.format(objectType, objectId), 'put', customPayload=content) runtime.logger.report('model response: {apiResponse!r}', apiResponse=apiResponse) responseAsJson = json.loads(apiResponse.text) if str(apiResponse.status_code) != '200': raise EnvironmentError('Unable to add {}. Response: {}'.format(objectType, responseAsJson)) ## end updateObject return
def createTimestamp(runtime): """Create a new archived snapshot timestamp.""" content = {'content' : {'source': runtime.jobName}} apiResponse = utilities.getApiResult(runtime, 'archive/modelSnapshotTimestamp', 'post', customPayload=content) runtime.logger.report('modelSnapshotTimestamp response: {apiResponse!r}', apiResponse=apiResponse) responseAsJson = json.loads(apiResponse.text) timestampId = responseAsJson.get('object_id') if str(apiResponse.status_code) != '200' or timestampId is None: raise EnvironmentError('Unable to create an archived snapshot timestamp. Response: {}'.format(responseAsJson)) ## end createTimestamp return timestampId
def getStaleObjectCount(runtime, objectType, thresholdInHours): """Issue an API call to get the object count.""" resultCount = 0 content = { 'count': True, 'objects': [{ 'class_name': objectType, 'attributes': ['time_gathered', 'caption'], 'minimum': '1', 'maximum': '', 'filter': [{ 'negation': True, 'condition': { 'attribute': 'time_gathered', 'operator': 'lastnumhours', 'value': thresholdInHours } }] }], 'links': [] } apiResponse = getApiResult(runtime, 'query/this', 'get', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) resultCount = queryResults.get('Result Count') runtime.logger.report( 'Stale {objectType!r} objects found: {resultCount!r}', objectType=objectType, resultCount=resultCount) else: raise EnvironmentError( 'Unexpected response from API. Code: {}. Payload: {}'.format( responseCode, responseAsJson)) ## end getStaleObjectCount return resultCount
def getEmptyGroupsCount(runtime, objectType): """Issue an API call to get the object count.""" resultCount = 0 content = { "count": True, "objects": [ { "label": "TARGET", "class_name": objectType, "attributes": ["caption"], "filter": [], "minimum": "1", "maximum": "", "linchpin": True }, { "label": "CHILD", "class_name": "BaseObject", "attributes": ["caption"], "filter": [], "minimum": "0", "maximum": "0" } ], "links": [ { "label": "TARGET_TO_CHILD", "class_name": "Contain", "first_id": "TARGET", "second_id": "CHILD" } ] } apiResponse = getApiResult(runtime, 'query/this', 'get', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) resultCount = queryResults.get('Result Count') runtime.logger.report('Stale {objectType!r} objects found: {resultCount!r}', objectType=objectType, resultCount=resultCount) else: raise EnvironmentError('Unexpected response from API. Code: {}. Payload: {}'.format(responseCode, responseAsJson)) ## end getEmptyGroupsCount return resultCount
def getArchivedModels(runtime, archivedModels): """Gather all the currently archived model entries.""" apiResponse = utilities.getApiResult(runtime, 'archive/model', 'get') runtime.logger.report('model response: {apiResponse!r}', apiResponse=apiResponse) responseAsJson = json.loads(apiResponse.text) ## Sample entry in the list of models: ## { ## "object_id": "d2c002c6da8f4a4089b8662a41694230", ## "object_type": "BusinessApplication", ## "caption": "IT Discovery Machine" ## }, for entry in responseAsJson.get('Models'): modelId = entry.get('object_id') modelName = entry.get('caption') if modelId is not None: archivedModels[modelId] = modelName ## end getArchivedModels return
def deleteEmptyGroups(runtime, objectType): """Issue an API call to delete the matching results.""" content = { "objects": [ { "label": "TARGET", "class_name": objectType, "attributes": ["caption"], "filter": [], "minimum": "1", "maximum": "", "linchpin": True }, { "label": "CHILD", "class_name": "BaseObject", "attributes": ["caption"], "filter": [], "minimum": "0", "maximum": "0" } ], "links": [ { "label": "TARGET_TO_CHILD", "class_name": "Contain", "first_id": "TARGET", "second_id": "CHILD" } ] } apiResponse = getApiResult(runtime, 'query/this', 'delete', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) runtime.logger.report('Delete completed: {queryResults!r}', queryResults=queryResults) else: raise EnvironmentError('Unexpected response from API. Code: {}. Payload: {}'.format(responseCode, responseAsJson)) ## end deleteEmptyGroups return
def deleteStaleObjects(runtime, objectType, thresholdInHours): """Issue an API call to delete the matching results.""" content = { 'objects': [{ 'class_name': objectType, 'attributes': ['time_gathered', 'caption'], 'minimum': '1', 'maximum': '', 'filter': [{ 'negation': True, 'condition': { 'attribute': 'time_gathered', 'operator': 'lastnumhours', 'value': thresholdInHours } }] }], 'links': [] } apiResponse = getApiResult(runtime, 'query/this', 'delete', customPayload={'content': content}) (responseCode, responseAsJson) = parseResponse(apiResponse) if responseCode is not None and str(responseCode) == '200': queryResults = json.loads(apiResponse.text) runtime.logger.report('Delete completed: {queryResults!r}', queryResults=queryResults) else: raise EnvironmentError( 'Unexpected response from API. Code: {}. Payload: {}'.format( responseCode, responseAsJson)) ## end deleteStaleObjects return
def getArchivedModelDetails(runtime, modelId): """Gather details on the provided archived model.""" apiResponse = utilities.getApiResult(runtime, 'archive/model/{}'.format(modelId), 'get') runtime.logger.report('model response: {apiResponse!r}', apiResponse=apiResponse) responseAsJson = json.loads(apiResponse.text) ## Sample response: ## { ## "d2c002c6da8f4a4089b8662a41694230": { ## "object_id": "d2c002c6da8f4a4089b8662a41694230", ## "object_type": "BusinessApplication", ## "time_stamp": "2018-11-04 11:29:06", ## "caption": "IT Discovery Machine", ## "data" : {...} ## "meta_data" : {...} ## } ## } thisModel = responseAsJson.get(modelId) ## Remove informational attributes that we won't want to update thisModel.pop('time_stamp', None) thisModel.pop('object_type', None) thisModel.pop('caption', None) ## end getArchivedModelDetails return thisModel