示例#1
0
 def delete(self, record: Record):
     """
     Deletes the record from the Custodian
     :param record:
     :return:
             """
     self.client.execute(command=Command(name=self._get_record_command_name(
         record.obj, record.get_pk()),
                                         method=COMMAND_METHOD.DELETE))
     setattr(record, record.obj.key, None)
示例#2
0
 def get(self, obj_name: str, record_id: str, **kwargs):
     """
     Retrieves an existing record from Custodian
     :param obj_name:
     :param record_id:
     """
     data, ok = self.client.execute(
         command=Command(name=self._get_record_command_name(obj_name, record_id), method=COMMAND_METHOD.GET),
         params=kwargs
     )
     return Record(obj_name, **data) if ok else None
示例#3
0
 def get_all(self):
     """
     Retrieves a list of existing objects from Custodian
     :return:
     """
     data, ok = self.client.execute(command=Command(
         name=self._get_object_command_name(''), method=COMMAND_METHOD.GET))
     if ok and data:
         return [
             ObjectFactory.factory(object_data, self)
             for object_data in data
         ]
     else:
         return []
示例#4
0
 def update(self, record: Record, **kwargs):
     """
     Updates an existing record in the Custodian
     """
     data, ok = self.client.execute(command=Command(
         name=self._get_record_command_name(record.obj, record.get_pk()),
         method=COMMAND_METHOD.PATCH),
                                    data=record.serialize(),
                                    params=kwargs)
     if ok:
         return Record(obj=record.obj, **data)
     else:
         if data.get('code') == 'cas_failed':
             raise CasFailureException(data.get('Msg', ''))
         else:
             raise RecordUpdateException(data.get('Msg', ''))
示例#5
0
 def bulk_update(self, *records: Record):
     """
     :return:
     """
     self._check_records_have_same_object(*records)
     obj = records[0].obj
     data, ok = self.client.execute(
         command=Command(name=self._get_record_command_name(obj),
                         method=COMMAND_METHOD.PATCH),
         data=[record.serialize() for record in records])
     if ok:
         for i in range(0, len(data)):
             records[i].__init__(obj, **data[i])
         return list(records)
     else:
         raise ObjectUpdateException(data.get('Msg'))
示例#6
0
 def update(self, obj: Object) -> Object:
     """
     Updates existing object in Custodian
     :param obj:
     :return:
     """
     self._pre_process_reverse_relations(obj)
     data, ok = self.client.execute(command=Command(
         name=self._get_object_command_name(obj.name),
         method=COMMAND_METHOD.POST),
                                    data=obj.serialize())
     if ok:
         self._cache.flush()
         return obj
     else:
         raise ObjectUpdateException(data.get('Msg'))
示例#7
0
    def delete(self, obj: Object) -> Object:
        """
        Deletes existing object from Custodian
        :param obj:
        :return:
        """

        data, ok = self.client.execute(
            command=Command(name=self._get_object_command_name(obj.name),
                            method=COMMAND_METHOD.DELETE))
        if ok:
            if self._use_cache:
                self._cache.flush()
            return obj
        else:
            raise ObjectDeletionException(data.get('msg'))
示例#8
0
    def _query(self, obj_name: str, query_string: str, **kwargs):
        """
        Performs an Custodian API call and returns a list of records
        :param obj_name:
        :param query_string:
        """
        if kwargs.get("omit_outers", None) is False:
            del kwargs['omit_outers']
        data, _ = self.client.execute(
            command=Command(name=self._get_record_command_name(obj_name), method=COMMAND_METHOD.GET),
            params={'q': query_string, **kwargs}
        )

        records = []
        for record_data in data:
            records.append(Record(obj_name, **record_data))
        return records
示例#9
0
 def partial_update(self, obj: Object, pk, values, **kwargs):
     """
     Performs partial update of existing record
     """
     self._validation_class.validate_partial(obj, values)
     data, ok = self.client.execute(
         command=Command(name=self._get_record_command_name(obj, pk),
                         method=COMMAND_METHOD.PATCH),
         data=self._serialization_class.serialize(obj, values),
         params=kwargs)
     if ok:
         return Record(obj=obj, **data)
     else:
         if data.get('code') == 'cas_failed':
             raise CasFailureException(data.get('Msg', ''))
         else:
             raise RecordUpdateException(data.get('Msg', ''))
示例#10
0
 def create(self, record: Record, **kwargs) -> Record:
     """
     Creates a new record in the Custodian
     :param record:
     :return:
     """
     data, ok = self.client.execute(command=Command(
         name=self._get_record_command_name(record.obj),
         method=COMMAND_METHOD.POST),
                                    data=record.serialize(),
                                    params=kwargs)
     if ok:
         return Record(obj=record.obj, **data)
     elif data.get('Msg', '').find('duplicate') != -1:
         raise RecordAlreadyExistsException
     else:
         raise CommandExecutionFailureException(data.get('Msg'))
示例#11
0
 def get(self, object_name):
     """
     Retrieves existing object from Custodian by name
     :param object_name:
     """
     if not self._use_cache or self._cache.get(object_name) is None:
         data, ok = self.client.execute(command=Command(
             name=self._get_object_command_name(object_name),
             method=COMMAND_METHOD.GET))
         obj = ObjectFactory.factory(data,
                                     objects_manager=self) if ok else None
         if ok and self._use_cache:
             self._cache.set(object_name, obj)
     if self._use_cache:
         return self._cache.get(object_name)
     else:
         return obj
示例#12
0
 def partial_update(self, obj_name: str, pk, values, **kwargs):
     """
     Performs partial update of existing record
     :param obj_name:        
     """
     data, ok = self.client.execute(
         command=Command(name=self._get_record_command_name(obj_name, pk),
                         method=COMMAND_METHOD.PATCH),
         data=values,
         params=kwargs
     )
     if ok:
         return Record(obj_name, **data)
     else:
         if data.get('code') == 'cas_failed':
             raise CasFailureException(data.get('Msg', ''))
         else:
             raise RecordUpdateException(data.get('Msg', ''))
示例#13
0
 def bulk_create(self, *records: Record):
     """
     Creates new records in the Custodian
     :param records:
     :return:
     """
     self._check_records_have_same_object(*records)
     obj = records[0].obj
     data, ok = self.client.execute(
         command=Command(name=self._get_record_command_name(obj),
                         method=COMMAND_METHOD.POST),
         data=[record.serialize() for record in records])
     records = []
     if ok:
         for i in range(0, len(data)):
             records.append(Record(obj, **data[i]))
         return list(records)
     else:
         raise CommandExecutionFailureException(data.get('Msg'))
示例#14
0
    def update(self, *records: Record, **kwargs):
        """
        Updates new records in the Custodian.
        :param records:
        """
        self._check_records_have_same_object(*records)
        obj = records[0].obj
        data, ok = self.client.execute(
            command=Command(name=self._get_record_command_name(obj), method=COMMAND_METHOD.PATCH),
            data=[record.data for record in records], params=kwargs
        )

        if ok:
            for i, d in enumerate(data):
                print(d)
                records[i].__init__(obj, **d)
            if len(records) == 1:
                return records[0]
            return list(records)
        
        else:
            raise ObjectUpdateException(data.get('Msg'))
示例#15
0
 def bulk_delete(self, *records: Record):
     """
     Deletes records from the Custodian
     :return:
     """
     if records:
         self._check_records_have_same_object(*records)
         obj = records[0].obj
         data, ok = self.client.execute(command=Command(
             name=self._get_record_command_name(obj),
             method=COMMAND_METHOD.DELETE),
                                        data=[{
                                            obj.key: record.get_pk()
                                        } for record in records])
         if ok:
             for record in records:
                 record.id = None
             return list(records)
         else:
             raise ObjectDeletionException(data.get('Msg'))
     else:
         return []
示例#16
0
    def create(self, obj: Object) -> Object:
        """
        Creates new object in Custodian
        :param obj:
        :return:
        """
        # omit if this object is already pending
        if obj.name in self._pending_objects:
            return

        safe_obj, fields_to_add_later = self._split_object_by_phases(obj)
        # mark current object as pending
        self._pending_objects.append(safe_obj.name)
        # create safe object
        data, ok = self.client.execute(command=Command(
            name=self._base_command_name, method=COMMAND_METHOD.POST),
                                       data=safe_obj.serialize())
        if ok:
            for field in fields_to_add_later:
                # process referenced fields` objects
                if not self.get(field.obj.name):
                    self.create(field.obj)
                else:
                    self.update(field.obj)

            # unmark current object as pending
            self._pending_objects.remove(obj.name)
            if fields_to_add_later:
                # now update object with the rest of fields
                obj = self.update(obj)
            self._post_process_reverse_relations(obj)

            self._cache.flush()
            # retrieve final object version
            return self.get(obj.name)
        else:
            raise ObjectCreateException(data.get('msg'))
示例#17
0
    def create(self, *records: Record, **kwargs):
        """
        Creates new records in the Custodian.
        :param records:
        """
        requests_data = self._get_request_data(*records)
        self._check_records_have_same_object(*records)
        obj = records[0].obj
        data, ok = self.client.execute(
            command=Command(name=self._get_record_command_name(obj), method=COMMAND_METHOD.POST),
            data=requests_data, params=kwargs
        )
        records = []
        if ok:
            if isinstance(data, dict):
                return Record(obj, **data)

            elif isinstance(data, list):
                return [Record(obj, **d) for d in data]

        elif data.get('Msg', '').find('duplicate') != -1:
            raise RecordAlreadyExistsException
        else:
            raise CommandExecutionFailureException(data.get('Msg'))