def getItemByUniqueAttributes(self, domainName, itemName, itemIdentifier1=None, itemIdentifier2=None, derivedFromItemId=None): if domainName is None or not len(domainName): raise InvalidRequest("domainName must be provided") if itemName is None or not len(itemName): raise InvalidRequest("itemName must be provided") domainName = Encoder.encode(domainName) itemName = Encoder.encode(itemName) url = '%s/items/uniqueAttributes/%s/%s' % (self.getContextRoot(), domainName, itemName) if itemIdentifier1 is not None: itemIdentifier1 = Encoder.encode(str(itemIdentifier1)) url = self._appendUrlParameter(url, "itemIdentifier1", itemIdentifier1) if itemIdentifier2 is not None: itemIdentifier2 = Encoder.encode(str(itemIdentifier2)) url = self._appendUrlParameter(url, "itemIdentifier2", itemIdentifier2) if derivedFromItemId is not None: url = self._appendUrlParameter(url, "derivedFromItemId", derivedFromItemId) responseData = self.sendRequest(url=url, method="GET") return Item(responseData)
def updateLogEntry(self, logId, text=None, effectiveFromDateTime=None, effectiveToDateTime=None, logTopicName=None): if logId is None or not len(logId): raise InvalidRequest('Log id must be provided.') url = '%s/logs/%s/update' % (self.getContextRoot(), logId) if text is not None: text = Encoder.encode(text) url = self._appendUrlParameter(url, 'text', text) if effectiveFromDateTime is not None: effectiveFromDateTime = Encoder.encode(effectiveFromDateTime) url = self._appendUrlParameter(url, 'effectiveFromDateTime', effectiveFromDateTime) if effectiveToDateTime is not None: effectiveToDateTime = Encoder.encode(effectiveToDateTime) url = self._appendUrlParameter(url, 'effectiveToDateTime', effectiveToDateTime) if logTopicName is not None: logTopicName = Encoder.encode(logTopicName) url = self._appendUrlParameter(url, 'logTopicName', logTopicName) responseDict = self.sendSessionRequest(url=url, method='PUT') return Log(responseDict)
def __finalizeAddItemRelationship(self, url, relationshipDetails, description): if relationshipDetails is not None: relationshipDetails = Encoder.encode(relationshipDetails) url = self._appendUrlParameter(url, "relationshipDetails", relationshipDetails) if description is not None: description = Encoder.encode(description) url = self._appendUrlParameter(url, "description", description) responseData = self.sendSessionRequest(url=url, method='POST') return ItemElementRelationship(responseData)
def addItemElement(self, itemElementName, parentItemId, containedItemId=-1, description=None, ownerUserId=None, ownerGroupId=None, isRequired=-1, isGroupWriteable=None): if parentItemId is not None: parentItemId = str(parentItemId) if parentItemId is None or not len(parentItemId): raise InvalidRequest("parentItemId must be provided") if itemElementName is None or not len(itemElementName): raise InvalidRequest("itemElementName must be provided") itemElementName = Encoder.encode(itemElementName) url = '%s/itemElements/add/%s/%s' % (self.getContextRoot(), itemElementName, parentItemId) url = self.__appendOptionalItemElementParametersToUrl( url, containedItemId, description, ownerUserId, ownerGroupId, isRequired, isGroupWriteable) responseData = self.sendSessionRequest(url=url, method='POST') return ItemElement(responseData)
def __finalizeAddLogEntryToItem(self, url, logEntry, attachment=None): if logEntry is None or not len(logEntry): raise InvalidRequest('Log entry must be provided.') url += '?logEntry=%s' % Encoder.encode(logEntry) if attachment is not None and len(attachment) > 0: fileName, data = self._generateFileData(attachment) url += '&attachmentName=%s' % Encoder.encode(fileName) responseDict = self.sendSessionRequest(url=url, method='POST', data=data) else: responseDict = self.sendSessionRequest(url=url, method='POST') return Log(responseDict)
def updateItemElement(self, itemElementId, containedItemId=-1, isRequired=-1, name=None, description=None, ownerUserId=None, ownerGroupId=None, isGroupWriteable=None): if itemElementId is not None: itemElementId = str(itemElementId) if itemElementId is None or not len(itemElementId): raise InvalidRequest("itemElementId must be provided") url = '%s/itemElements/update/%s' % (self.getContextRoot(), itemElementId) if name is not None: name = Encoder.encode(name) url = self._appendUrlParameter(url, "name", name) url = self.__appendOptionalItemElementParametersToUrl( url, containedItemId, description, ownerUserId, ownerGroupId, isRequired, isGroupWriteable) responseData = self.sendSessionRequest(url=url, method='PUT') return ItemElement(responseData)
def addLogEntryToItemWithQrId(self, qrId, logEntry, attachment): if qrId is None or not len(qrId): raise InvalidRequest("QrId must be provided") url = '%s/items/%s/addLogEntry' % (self.getContextRoot(), qrId) if logEntry is None or not len(logEntry): raise InvalidRequest('Log entry must be provided.') url += '?logEntry=%s' % Encoder.encode(logEntry) if attachment is not None and len(attachment) > 0: fileName, data = self._generateFileData(attachment) url += '&attachmentName=%s' % Encoder.encode(fileName) responseDict = self.sendSessionRequest(url=url, method='POST', data=data) else: responseDict = self.sendSessionRequest(url=url, method='POST') return Log(responseDict)
def writeFile(self, path, content): try: fileName = os.path.basename(path) parentDirectory = os.path.dirname(path) encodedFileContent = Encoder.encode(content) url = '%s/files/%s?parentDirectory=%s&encodedFileContent=%s' % (self.getContextRoot(), fileName, parentDirectory, encodedFileContent) responseDict = self.sendSessionRequest(url=url, method='POST') return responseDict except CdbException, ex: raise
def addImagePropertyToItemWithId(self, itemId, imageFile): url = '%s/items/%s/addImageProperty' % (self.getContextRoot(), itemId) fileName, data = self._generateFileData(imageFile) fileName = Encoder.encode(fileName) url = self._appendUrlParameter(url, 'imageFileName', fileName) response = self.sendSessionRequest(url=url, method='POST', data=data) return PropertyValue(response)
def deletePropertyValuesFromItemWithId(self, itemId, propertyTypeName): if itemId is not None: itemId = str(itemId) if itemId is None or not len(itemId): raise InvalidRequest("itemId must be provided") if propertyTypeName is None or not len(propertyTypeName): raise InvalidRequest("propertyTypeName must be provided") propertyTypeName = Encoder.encode(propertyTypeName) url = '%s/items/%s/deletePropertyValues/%s' url = url % (self.getContextRoot(), itemId, propertyTypeName) responseData = self.sendSessionRequest(url=url, method='DELETE') return self.toCdbObjectList(responseData, PropertyValue)
def getFirstItemRelationshipList(self, itemId, relationshipTypeName): if itemId is None: raise InvalidRequest("item id must be provided") if relationshipTypeName is None or not len(relationshipTypeName): raise InvalidRequest("relationship type name must be provided") relationshipTypeName = Encoder.encode(relationshipTypeName) url = "%s/items/%s/firstItemElementRelationships/%s" % ( self.getContextRoot(), itemId, relationshipTypeName) responseData = self.sendRequest(url=url, method="GET") return self.toCdbObjectList(responseData, ItemElementRelationship)
def addLogAttachment(self, logId, attachment, attachmentDescription=None): if logId is None or not len(logId): raise InvalidRequest('Log id must be provided.') if attachment is None or len(attachment) == 0: raise InvalidRequest('Attachment must be specified.') url = '%s/logs/%s/addAttachment' % (self.getContextRoot(), logId) # Add attachment information fileName, data = self._generateFileData(attachment) url += '?attachmentName=%s' % Encoder.encode(fileName) if attachmentDescription is not None and len( attachmentDescription) > 0: url += '&attachmentDescription=%s' % Encoder.encode( attachmentDescription) responseDict = self.sendSessionRequest(url=url, method='POST', data=data) return LogAttachment(responseDict)
def addPropertyMetadataToPropertyValue(self, propertyValueId, metadataKey=None, metadataValue=None, metadataDict=None): if propertyValueId is not None: propertyValueId = str(propertyValueId) if propertyValueId is None or not len(propertyValueId): raise InvalidRequest("propertyValueId must be provided") url = '%s/property/values/%s/addMetadata' % (self.getContextRoot(), propertyValueId) list = False if metadataKey is None or not len(metadataKey): if metadataDict is None or not len(metadataDict): raise InvalidRequest( "metadataKey and value or metadataDict must be provided") # use metadata dict metadataDict = json.dumps(metadataDict) url += '?metadataDict=%s' % Encoder.encode(metadataDict) list = True else: if metadataValue is None or not len(metadataValue): raise InvalidRequest("metadataValue must be provided") url = '%s/%s' % (url, metadataKey) url += '?metadataValue=%s' % Encoder.encode(metadataValue) response = self.sendSessionRequest(url=url, method='POST') if list: return self.toCdbObjectList(response, PropertyMetadata) else: return PropertyMetadata(response)
def updateInventoryItemStatus(self, itemId, status): if itemId is None: raise InvalidRequest("Item id must be provided") if status is None: raise InvalidRequest("Status must be provided") itemId = str(itemId) status = Encoder.encode(status) url = "%s/items/domain/inventory/status/%s/%s" % ( self.getContextRoot(), itemId, status) response = self.sendSessionRequest(url, method="POST") return PropertyValue(response)
def addItemRelationshipByQrId(self, firstItemQrId, secondItemQrId, relationshipTypeName, relationshipDetails=None, description=None): if firstItemQrId is None: raise InvalidRequest("first item qr id must be provided") if secondItemQrId is None: raise InvalidRequest("second item qr id must be provided") relationshipTypeName = Encoder.encode(relationshipTypeName) url = "%s/items/%s/%s/addItemElementRelationshipByQrId/%s" \ % (self.getContextRoot(), firstItemQrId, secondItemQrId, relationshipTypeName) return self.__finalizeAddItemRelationship(url, relationshipDetails, description)
def __createAddPropertyRequest(self, url, id, propertyTypeName, tag=None, value=None, units=None, description=None, isUserWriteable=None, isDynamic=None, displayValue=None): if propertyTypeName is None or not len(propertyTypeName): raise InvalidRequest("propertyTypeName must be provided") propertyTypeName = Encoder.encode(propertyTypeName) url = url % (self.getContextRoot(), id, propertyTypeName) if tag is not None: tag = Encoder.encode(tag) url = self._appendUrlParameter(url, 'tag', tag) if value is not None: value = Encoder.encode(value) url = self._appendUrlParameter(url, 'value', value) if displayValue is not None: displayValue = Encoder.encode(displayValue) url = self._appendUrlParameter(url, 'displayValue', displayValue) if units is not None: units = Encoder.encode(units) url = self._appendUrlParameter(url, 'units', units) if description is not None: description = Encoder.encode(description) url = self._appendUrlParameter(url, 'description', description) if isUserWriteable is not None: url = self._appendUrlParameter(url, 'isUserWriteable', isUserWriteable) if isDynamic is not None: url = self._appendUrlParameter(url, 'isDynamc', isDynamic) responseDict = self.sendSessionRequest(url=url, method='POST') return PropertyValue(responseDict)
def __appendOptionalItemElementParametersToUrl(self, url, containedItemId=-1, description=None, ownerUserId=None, ownerGroupId=None, isRequired=-1, isGroupWriteable=None): if containedItemId != -1: containedItemId = str(containedItemId) url = self._appendUrlParameter(url, "containedItemId", containedItemId) if description is not None: description = Encoder.encode(description) url = self._appendUrlParameter(url, "description", description) if ownerUserId is not None: ownerUserId = str(ownerUserId) url = self._appendUrlParameter(url, "ownerUserId", ownerUserId) if ownerGroupId is not None: ownerGroupId = str(ownerGroupId) url = self._appendUrlParameter(url, "ownerGroupId", ownerGroupId) if isRequired != -1: isRequired = str(isRequired) url = self._appendUrlParameter(url, "isRequired", isRequired) if isGroupWriteable is not None: isGroupWriteable = str(isGroupWriteable) url = self._appendUrlParameter(url, "isGroupWriteable", isGroupWriteable) return url
def addItem(self, domainName, name, itemProjectName=None, itemIdentifier1=None, itemIdentifier2=None, qrId=None, description=None, ownerUserId=None, ownerGroupId=None, isGroupWriteable=None, entityTypeNames=None, derivedFromItemId=None): if domainName is None or not len(domainName): raise InvalidRequest("domainName must be provided") if name is None or not len(name): raise InvalidRequest("name must be provided") name = Encoder.encode(name) domainName = Encoder.encode(domainName) url = '%s/items/add/%s/domain/%s' % (self.getContextRoot(), name, domainName) if itemIdentifier1 is not None: itemIdentifier1 = Encoder.encode(itemIdentifier1) url = self._appendUrlParameter(url, 'itemIdentifier1', itemIdentifier1) if itemIdentifier2 is not None: itemIdentifier2 = Encoder.encode(itemIdentifier2) url = self._appendUrlParameter(url, 'itemIdentifier2', itemIdentifier2) if qrId is not None: url = self._appendUrlParameter(url, 'qrId', qrId) if description is not None: description = Encoder.encode(description) url = self._appendUrlParameter(url, 'description', description) if ownerUserId is not None: url = self._appendUrlParameter(url, 'ownerUserId', ownerUserId) if ownerGroupId is not None: url = self._appendUrlParameter(url, 'ownerGroupId', ownerGroupId) if isGroupWriteable is not None: url = self._appendUrlParameter(url, 'isGroupWriteable', isGroupWriteable) if entityTypeNames is not None: entityTypeNames = Encoder.encode(str(entityTypeNames)) url = self._appendUrlParameter(url, "entityTypeNames", entityTypeNames) if derivedFromItemId is not None: url = self._appendUrlParameter(url, "derivedFromItemId", derivedFromItemId) if itemProjectName is not None: itemProjectName = Encoder.encode(itemProjectName) url = self._appendUrlParameter(url, 'itemProjectName', itemProjectName) responseData = self.sendSessionRequest(url, method='POST') return Item(responseData)