def fetch_sync_by_id(self, instance_id): self.at_api_client = api.init_api_connection() query = Query(self.model_class.__name__) query.WHERE('id', query.Equals, instance_id) ticket = self.at_api_client.query(query).fetch_one() instance, _ = self.update_or_create_instance(ticket) return instance
def get(self, results): """ Fetch records from the API. ATWS automatically makes multiple separate queries if the request is over 500 records. """ sync_job_qset = models.SyncJob.objects.filter( entity_name=self.model_class.__name__) query = Query(self.model_class.__name__) if sync_job_qset.exists() and self.last_updated_field \ and not self.full: last_sync_job_time = sync_job_qset.last().start_time query.WHERE(self.last_updated_field, query.GreaterThanorEquals, last_sync_job_time) else: query.WHERE('id', query.GreaterThanorEquals, 0) # Apply extra conditions if they exist, else nothing happens self._get_query_conditions(query) try: logger.info('Fetching {} records.'.format(self.model_class)) for record in self.at_api_client.query(query): self.persist_record(record, results) except AutotaskAPIException as e: logger.error('Failed to fetch {} object. {}'.format( self.model_class, e)) return results
def fetch_object(entity, object_id, at): """ Fetch the give entity from the Autotask. """ query = Query(entity) query.WHERE('id', query.Equals, object_id) return at.query(query).fetch_one()
def update_assigned_resource(at_object, resource, role): entity = at_object.__class__.__name__ query = Query(entity) query.WHERE('id', query.Equals, at_object.id) at = init_api_connection() t = at.query(query).fetch_one() t.AssignedResourceID = resource.id if resource else None t.AssignedResourceRoleID = role.id if role else None # Fetch one executes the update and returns the created object. return at.update([t]).fetch_one()
def build_base_query(self, sync_job_qset): query = Query(self.model_class.__name__) if sync_job_qset.exists() and self.last_updated_field \ and not self.full: last_sync_job_time = sync_job_qset.last().start_time query.WHERE(self.last_updated_field, query.GreaterThanorEquals, last_sync_job_time) else: query.WHERE('id', query.GreaterThanorEquals, 0) return query
def build_base_query(self, sync_job_qset): query = Query(self.model_class.__bases__[0].__name__) # Since the job is created before it begins, make sure to exclude # itself, and at least one other sync job exists. if sync_job_qset.count() > 1 and self.last_updated_field \ and not self.full: last_sync_job_time = sync_job_qset.exclude( id=sync_job_qset.last().id).last().start_time query.WHERE(self.last_updated_field, query.GreaterThanorEquals, last_sync_job_time) else: query.WHERE('id', query.GreaterThanorEquals, 0) return query
def update_ticket(ticket, status): # We need to query for the object first, then alter it and execute it. # https://atws.readthedocs.io/usage.html#querying-for-entities # This is because we can not create a valid (enough) object to update # to autotask unless we sync EVERY non-readonly field. If you submit the # object with no values supplied for the readonly fields, autotask will # null them out. query = Query('Ticket') query.WHERE('id', query.Equals, ticket.id) at = init_api_connection() t = at.query(query).fetch_one() t.Status = status.id # Fetch one executes the update and returns the created object. return at.update([t]).fetch_one()