Skip to content

Commit 727bc4f

Browse files
authored
cleanup(storage): remove unused code in emulator (#7048)
Since we are no longer sending gRPC requests for Bucket metadata operations, we do not need to emulate them either. This is needed because the v2/ protos do not expose these RPCs (yet).
1 parent 00e3a86 commit 727bc4f

5 files changed

Lines changed: 70 additions & 292 deletions

File tree

google/cloud/storage/emulator/database.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,9 @@ def insert_test_bucket(self, context):
100100
"GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME", "bucket"
101101
)
102102
if self.buckets.get(bucket_name) is None:
103-
if context is not None:
104-
request = storage_pb2.InsertBucketRequest(bucket={"name": bucket_name})
105-
else:
106-
request = utils.common.FakeRequest(
107-
args={}, data=json.dumps({"name": bucket_name})
108-
)
103+
request = utils.common.FakeRequest(
104+
args={}, data=json.dumps({"name": bucket_name})
105+
)
109106
bucket_test, _ = gcs.bucket.Bucket.init(request, context)
110107
self.insert_bucket(request, bucket_test, context)
111108
bucket_test.metadata.metageneration = 4

google/cloud/storage/emulator/emulator.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -363,32 +363,21 @@ def bucket_default_object_acl_delete(bucket_name, entity):
363363
@retry_test(method="storage.notifications.list")
364364
def bucket_notification_list(bucket_name):
365365
bucket = db.get_bucket(flask.request, bucket_name, None)
366-
response = {"kind": "storage#notifications", "items": []}
367-
for notification in bucket.notifications.values():
368-
response["items"].append(
369-
json_format.MessageToDict(notification, preserving_proto_field_name=True)
370-
)
371-
return response
366+
return bucket.list_notifications(None)
372367

373368

374369
@gcs.route("/b/<bucket_name>/notificationConfigs", methods=["POST"])
375370
@retry_test(method="storage.notifications.insert")
376371
def bucket_notification_insert(bucket_name):
377372
bucket = db.get_bucket(flask.request, bucket_name, None)
378-
notification = bucket.insert_notification(flask.request, None)
379-
response = json_format.MessageToDict(notification, preserving_proto_field_name=True)
380-
response["kind"] = "storage#notification"
381-
return response
373+
return bucket.insert_notification(flask.request, None)
382374

383375

384376
@gcs.route("/b/<bucket_name>/notificationConfigs/<notification_id>")
385377
@retry_test(method="storage.notifications.get")
386378
def bucket_notification_get(bucket_name, notification_id):
387379
bucket = db.get_bucket(flask.request, bucket_name, None)
388-
notification = bucket.get_notification(notification_id, None)
389-
response = json_format.MessageToDict(notification, preserving_proto_field_name=True)
390-
response["kind"] = "storage#notification"
391-
return response
380+
return bucket.get_notification(notification_id, None)
392381

393382

394383
@gcs.route("/b/<bucket_name>/notificationConfigs/<notification_id>", methods=["DELETE"])

google/cloud/storage/emulator/gcs/bucket.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,24 @@ def delete_default_object_acl(self, entity, context):
460460
# === NOTIFICATIONS === #
461461

462462
def insert_notification(self, request, context):
463-
notification = None
464-
if context is not None:
465-
notification = request.notification
466-
else:
467-
notification = json_format.ParseDict(
468-
json.loads(request.data), resources_pb2.Notification()
469-
)
470-
notification.id = "notification-%d" % random.getrandbits(16)
471-
self.notifications[notification.id] = notification
463+
notification = {
464+
"kind": "storage#notification",
465+
"id": "notification-%d" % random.getrandbits(16),
466+
}
467+
data = json.loads(request.data)
468+
for required_key in {"topic", "payload_format"}:
469+
value = data.pop(required_key, None)
470+
if value is not None:
471+
notification[required_key] = value
472+
else:
473+
utils.error.invalid(
474+
"Missing field in notification %s" % required_key, context
475+
)
476+
for key in {"event_types", "custom_attributes", "object_name_prefix"}:
477+
value = data.pop(key, None)
478+
if value is not None:
479+
notification[key] = value
480+
self.notifications[notification["id"]] = notification
472481
return notification
473482

474483
def get_notification(self, notification_id, context):
@@ -477,6 +486,12 @@ def get_notification(self, notification_id, context):
477486
def delete_notification(self, notification_id, context):
478487
del self.notifications[notification_id]
479488

489+
def list_notifications(self, context):
490+
response = {"kind": "storage#notifications", "items": []}
491+
for notification in self.notifications.values():
492+
response["items"].append(notification)
493+
return response
494+
480495
# === RESPONSE === #
481496

482497
def rest(self):

google/cloud/storage/emulator/grpc_server.py

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -30,136 +30,6 @@
3030

3131
class StorageServicer(storage_pb2_grpc.StorageServicer):
3232

33-
# === BUCKET ===#
34-
35-
def ListBuckets(self, request, context):
36-
db.insert_test_bucket(context)
37-
result = resources_pb2.ListBucketsResponse(next_page_token="", items=[])
38-
for bucket in db.list_bucket(request, request.project, context):
39-
result.items.append(bucket.metadata)
40-
return result
41-
42-
def InsertBucket(self, request, context):
43-
db.insert_test_bucket(context)
44-
bucket, projection = gcs_type.bucket.Bucket.init(request, context)
45-
db.insert_bucket(request, bucket, context)
46-
return bucket.metadata
47-
48-
def GetBucket(self, request, context):
49-
bucket_name = request.bucket
50-
bucket = db.get_bucket(request, bucket_name, context)
51-
return bucket.metadata
52-
53-
def UpdateBucket(self, request, context):
54-
bucket_name = request.bucket
55-
bucket = db.get_bucket(request, bucket_name, context)
56-
bucket.update(request, context)
57-
return bucket.metadata
58-
59-
def DeleteBucket(self, request, context):
60-
bucket_name = request.bucket
61-
db.delete_bucket(request, bucket_name, context)
62-
return Empty()
63-
64-
def ListBucketAccessControls(self, request, context):
65-
bucket_name = request.bucket
66-
bucket = db.get_bucket(request, bucket_name, context)
67-
result = resources_pb2.ListBucketAccessControlsResponse(
68-
items=bucket.metadata.acl
69-
)
70-
return result
71-
72-
def InsertBucketAccessControl(self, request, context):
73-
bucket_name = request.bucket
74-
bucket = db.get_bucket(request, bucket_name, context)
75-
return bucket.insert_acl(request, context)
76-
77-
def GetBucketAccessControl(self, request, context):
78-
bucket_name = request.bucket
79-
bucket = db.get_bucket(request, bucket_name, context)
80-
return bucket.get_acl(request.entity, context)
81-
82-
def UpdateBucketAccessControl(self, request, context):
83-
bucket_name = request.bucket
84-
bucket = db.get_bucket(request, bucket_name, context)
85-
return bucket.update_acl(request, request.entity, context)
86-
87-
def DeleteBucketAccessControl(self, request, context):
88-
bucket_name = request.bucket
89-
bucket = db.get_bucket(request, bucket_name, context)
90-
bucket.delete_acl(request.entity, context)
91-
return Empty()
92-
93-
def ListDefaultObjectAccessControls(self, request, context):
94-
bucket_name = request.bucket
95-
bucket = db.get_bucket(request, bucket_name, context)
96-
result = resources_pb2.ListObjectAccessControlsResponse(
97-
items=bucket.metadata.default_object_acl
98-
)
99-
return result
100-
101-
def InsertDefaultObjectAccessControl(self, request, context):
102-
bucket_name = request.bucket
103-
bucket = db.get_bucket(request, bucket_name, context)
104-
return bucket.insert_default_object_acl(request, context)
105-
106-
def GetDefaultObjectAccessControl(self, request, context):
107-
bucket_name = request.bucket
108-
bucket = db.get_bucket(request, bucket_name, context)
109-
return bucket.get_default_object_acl(request.entity, context)
110-
111-
def UpdateDefaultObjectAccessControl(self, request, context):
112-
bucket_name = request.bucket
113-
bucket = db.get_bucket(request, bucket_name, context)
114-
return bucket.update_default_object_acl(request, request.entity, context)
115-
116-
def DeleteDefaultObjectAccessControl(self, request, context):
117-
bucket_name = request.bucket
118-
bucket = db.get_bucket(request, bucket_name, context)
119-
bucket.delete_default_object_acl(request.entity, context)
120-
return Empty()
121-
122-
def InsertNotification(self, request, context):
123-
bucket_name = request.bucket
124-
bucket = db.get_bucket(request, bucket_name, context)
125-
return bucket.insert_notification(request, context)
126-
127-
def ListNotifications(self, request, context):
128-
bucket_name = request.bucket
129-
bucket = db.get_bucket(request, bucket_name, context)
130-
result = resources_pb2.ListNotificationsResponse(
131-
items=bucket.notifications.values()
132-
)
133-
return result
134-
135-
def GetNotification(self, request, context):
136-
bucket_name = request.bucket
137-
bucket = db.get_bucket(request, bucket_name, context)
138-
notification_id = request.notification
139-
return bucket.get_notification(notification_id, context)
140-
141-
def DeleteNotification(self, request, context):
142-
bucket_name = request.bucket
143-
bucket = db.get_bucket(request, bucket_name, context)
144-
notification_id = request.notification
145-
bucket.delete_notification(notification_id, context)
146-
return Empty()
147-
148-
def GetBucketIamPolicy(self, request, context):
149-
bucket_name = request.iam_request.resource
150-
bucket = db.get_bucket(request, bucket_name, context)
151-
return bucket.get_iam_policy(request, context)
152-
153-
def SetBucketIamPolicy(self, request, context):
154-
bucket_name = request.iam_request.resource
155-
bucket = db.get_bucket(request, bucket_name, context)
156-
return bucket.set_iam_policy(request, context)
157-
158-
def TestBucketIamPermissions(self, request, context):
159-
return iam_policy_pb2.TestIamPermissionsResponse(
160-
permissions=request.iam_request.permissions
161-
)
162-
16333
# === OBJECT === #
16434

16535
def handle_insert_object_streaming_rpc(self, request_iterator, context):

0 commit comments

Comments
 (0)