def encode(self, entity: Entity, property_names: Optional[List[str]]): entity_dict = { "entity": { "key": { "partitionId": { "projectId": entity.key.project if entity and entity.key else None, }, "path": entity.key.path if entity and entity.key else None, }, "properties": [], }, "URLSafeKey": entity._serialized_key if hasattr(entity, "_serialized_key") else None } for prop_name in entity.keys(): value_type, value = self._property_encode(entity.get(prop_name)) entity_dict['entity']['properties'].append( { "property_name": prop_name, "value_type": value_type, "value": value, "index": prop_name in property_names if property_names else None, }, ) return entity_dict
def setData(client, key, propname, data, method): # save data to datastore try: with client.transaction(): task = client.get(key) if not task: task = Entity(key) task.update({propname: data}) else: # overwrite data for put if method == "PUT": task[propname] = data # append data for post elif method == "POST": if propname in task: if isinstance(task[propname], list): task[propname].append(data) else: task[propname] = [task[propname], data] else: task[propname] = [data] elif method == "DELETE": del task[propname] client.put(task) except: return abort(400) return ""
def put_user(cls, user): key = CLIENT().key(cls.KIND, user.username) ent = Entity(key) user_dct = dataclasses.asdict(user) user_dct.pop('password') user_dct['encrypted_password'] = crypto.PASSWORD.encrypt( user.password.encode('utf-8')) ent.update(user_dct) CLIENT().put(ent)
def _update_user_entity(entity: Entity, platform: str, os_version: str, app_version: str, device_type: str, lang: str) -> None: entity.update({ "platform": platform, "os_version": os_version, "app_version": app_version, "device_type": device_type, "lang": lang, "last_status_requested": datetime.utcnow(), }) datastore_client.put(entity)
async def put( self, key: Key, data: Dict[str, Any], exclude_from_indexes: Iterable[str] = (), **kwargs: Any, ) -> None: entity = Entity(key, exclude_from_indexes=exclude_from_indexes) entity.update(data) loop = asyncio.get_running_loop() await loop.run_in_executor(self.executor, partial(self.client.put, entity))
def put(self, model, overwrite=True): if not overwrite: raise NotImplementedError("Unique key checking not supported") storage_model = StorageModel(model_type=type(model)) serialized_item = storage_model.serialize(model) key_value = getattr(model, storage_model.key_field) key = self.client.key(storage_model.table_name, key_value) entity = Entity(key=key, exclude_from_indexes=tuple(serialized_item.keys())) entity.update(serialized_item) self.client.put(entity)
def handleRest(urlarr, token, data, method): print('handled by REST') # unpack property id and name propname, *prop_id = urlarr prop_id = int(prop_id[0]) if prop_id else None # we don't allow puts or deletes on the whole collection if not prop_id and re.match(r"^DELETE|PUT$", method): return abort(405) # don't allow posts to an individual item if prop_id and method == "POST": return abort(409) # set up the client kind = GROUPNAME client = Client() # create the key if prop_id: # create a full key key = client.key(kind, token["email"], propname, prop_id) else: # create a partial key key = client.key(kind, token["email"], propname) # if GET return the data if method == "GET": if key.is_partial: ancestor = client.key(kind, token['email']) return get_rest_query(client, ancestor, propname) return get_rest(client, key) # if POST create new entry or PUT overwrite if re.match(r"^POST|PUT$", method): entity = Entity(key=key) entity.update(data) client.put(entity) # return 200 OK with entity id in the payload return json.dumps({}) # if DELETE remove entry if method == "DELETE": client.delete(key) return json.dumps({}) return abort(400)
def set_key(self, entity_name: str, key_name: str, **properties: Any) -> None: key = self.client.key(entity_name, key_name) with self.client.transaction(): entity = self.client.get(key) if entity is None: entity = Entity(key=key) entity.update(properties) if self._batched_update_entities is not None: self._batched_update_entities.append(entity) else: self.client.put(entity)
def __to_entity(self) -> Optional[Entity]: entity = Entity(self.key, self._exclude_from_index) for name, field in self._fields.items(): value = field._db_repr(self._data.get(name)) if value is not None: entity[name] = value return None if len(entity) == 0 else entity
def handlerUsers(token, userdata, method): if not isAuthorized(token, "admin"): return abort(404) # Instantiates a client client = Client() # The kind for the new entity kind = GROUPNAME # The Cloud Datastore key for the new entity key = client.key(kind, "users") if method == "GET": try: task = client.get(key) # no users saved if not task: return json.dumps({}) return json.dumps(task) except Exception as e: return abort(400) elif method == "POST" or method == "PUT": try: with client.transaction(): task = client.get(key) if not task: task = Entity(key) task.update(userdata) client.put(task) except: return abort(400) elif method == "DELETE": try: with client.transaction(): task = client.get(key) if not task: return abort(400) else: for user in userdata: del task[user] client.put(task) except Exception as e: print(e) return abort(400) else: return abort(400) return ""
def put(self, model: ModelTypes, overwrite: bool = True) -> bool: if not overwrite: raise NotImplementedError("Unique key checking not supported") storage_model = StorageModel(model_type=type(model)) serialized_item = storage_model.serialize(model) key_value = getattr(model, storage_model.key_field) key = self.client.key(storage_model.table_name, key_value) exclude_from_indexes = tuple( field for field in serialized_item.keys() if field not in storage_model.index_fields) entity = Entity(key=key, exclude_from_indexes=exclude_from_indexes) entity.update(serialized_item) self.client.put(entity) return True
def entity_to_node_req(entity: datastore.Entity) -> NodeReq: return NodeReq( operation_id=entity.key.name, cluster_id=entity["cluster_id"], status=entity["status"], node_class=entity["node_class"], sequence=entity["sequence"], job_id=entity.get("job_id"), instance_name=entity["instance_name"], )
def put_dog(id: hug.types.number, name: hug.types.text, age: hug.types.number): client = Client() key = client.key("Dog", id) dog_key = key dog_entity = Entity(dog_key) dog_entity["name"] = name dog_entity["age"] = age client.put(dog_entity) return {"id": dog_entity.key.id}
def put_token(cls, token, request): key = CLIENT().key(cls.KIND, token['access_token']) token_obj = OAuth2Token(token_type=token['token_type'], access_token=token['access_token'], scope=token['scope'], expires_at=token['expires_in'] + int(time.time()), user_id=request.user.username) ent = Entity(key) ent.update(dataclasses.asdict(token_obj)) CLIENT().put(ent) refresh_token = token['refresh_token'] refresh_obj = OAuth2RefreshToken(refresh_token, token_obj.access_token) key = CLIENT().key(cls.REFRESH_KIND, refresh_token) ent = Entity(key) ent.update(dataclasses.asdict(refresh_obj)) CLIENT().put(ent)
def from_entity(self, entity: datastore.Entity) -> Document: data = dict(entity.items()) if self.pk_field not in data: data[self.pk_field] = entity.id return self.doc_klass.Meta.from_dict(data=data)
def _update_registration(entity: Entity, status: str): entity.update({"status": status}) datastore_client.put(entity)
def test_audit_log_loop_event(self): """Test receiving an audit loop event.""" url = '/push_notification?secret={}'.format('my-test-secret-key') request = webapp2.Request.blank(url) request.method = 'POST' request.headers['content-type'] = 'application/json' data_files = common.read_data_files([ 'tests/data/audit-log-loop-message.json', 'tests/data/compute.v1.json', 'tests/data/audit-log-loop-compute-operation.json', 'tests/data/audit-log-resource-get.json', 'tests/data/monitoring.v3.json', 'tests/data/dns.v1.json', 'tests/data/dns-zone-response.json', 'tests/data/instance-creation-dns-pending-operation.json', 'tests/data/instance-creation-dns-done-operation.json', 'tests/data/instance-creation-dns-record-set-response.json' ]) data = base64.encodestring(data_files['audit-log-loop-message.json']) post = { 'message': { 'data': data, 'attributes': { 'compute.googleapis.com/resource_id': '18082097775580039429', 'compute.googleapis.com/resource_name': 'dns-sync-test', 'compute.googleapis.com/resource_type': 'instance', 'compute.googleapis.com/resource_zone': 'us-central1-a', 'logging.googleapis.com/timestamp': '2016-04-03T23: 06: 31.17867Z' }, 'message_id': '29119446125187' }, 'subscription': 'projects/project-1/subscriptions/gae-push' } request.body = json.dumps(post) success = {'status': '200'} compute_mock_http = common.LoggingHttpMockSequence([ (success, '{"access_token":"token","expires_in":3600}'), (success, data_files['compute.v1.json']), (success, data_files['audit-log-loop-compute-operation.json']), (success, data_files['audit-log-resource-get.json']), # stop instance (success, data_files['audit-log-loop-compute-operation.json']) ]) api.Clients.compute.http = compute_mock_http api.Clients.compute.cache_discovery = False metrics_mock_http = common.LoggingHttpMockSequence([ (success, '{"access_token":"token","expires_in":3600}'), (success, data_files['monitoring.v3.json']), # timeseries.write (success, '{}') ]) api.Clients.metrics.http = metrics_mock_http api.Clients.metrics.cache_discovery = False mock_dns = mock.MagicMock() mock_dns.changes().get().execute.return_value = {'status': 'done'} api.Clients.dns = mock_dns mock_datastore = mock.Mock(spec=Client) mock_datastore.project = 'project-1' now = audit_log.utcnow() last_call_time = now - datetime.timedelta(0, 30) entity = Entity() entity.update({ 'running': True, 'last_call': 'start', 'last_call_time': last_call_time, 'last_call_event_received': False }) mock_datastore.get.side_effect = [common.config_entity(), entity] api.CLIENTS.datastore = mock_datastore dns_sync_app = main.DnsSyncApplication() auth.AdminRequestHandler.SKIP_AUTHENTICATION = True # Get a response for that request. response = request.get_response(dns_sync_app) self.assertEquals(response.status_int, 200)
def put_auth_code(cls, code): code_key = CLIENT().key(cls.KIND, code.code) ent = Entity(code_key) ent.update(dataclasses.asdict(code)) CLIENT().put(ent)
def entity_asdict(entity: Entity) -> Dict[str, Any]: return { k: entity_asdict(v) if isinstance(v, Entity) else v for k, v in entity.items() }