1- from localstack .aws .api import CommonServiceException , RequestContext
1+ from localstack .aws .api import RequestContext
22from localstack .aws .api .s3control import (
33 AccountId ,
44 ListTagsForResourceResult ,
99 TagResourceResult ,
1010 UntagResourceResult ,
1111)
12- from localstack .aws .forwarder import NotImplementedAvoidFallbackError
13- from localstack .services .s3 .models import s3_stores
14- from localstack .services .s3control .validation import validate_tags
12+ from localstack .services .s3 .models import S3Store , s3_stores
13+ from localstack .services .s3control .validation import validate_arn_for_tagging , validate_tags
1514from localstack .state import StateVisitor
16- from localstack .utils .tagging import TaggingService
17-
18-
19- class NoSuchResource (CommonServiceException ):
20- def __init__ (self , message = None ):
21- super ().__init__ ("NoSuchResource" , status_code = 404 , message = message )
2215
2316
2417class S3ControlProvider (S3ControlApi ):
@@ -33,26 +26,26 @@ def accept_state_visitor(self, visitor: StateVisitor):
3326 """
3427
3528 @staticmethod
36- def _get_tagging_service_for_bucket (
37- resource_arn : S3ResourceArn ,
38- partition : str ,
39- region : str ,
40- account_id : str ,
41- ) -> TaggingService :
42- s3_prefix = f"arn:{ partition } :s3:::"
43- if not resource_arn .startswith (s3_prefix ):
44- # Moto does not support Tagging operations for S3 Control, so we should not forward those operations back
45- # to it
46- raise NotImplementedAvoidFallbackError (
47- "LocalStack only support Bucket tagging operations for S3Control"
48- )
29+ def get_s3_store (account_id : str , region : str ) -> S3Store :
30+ return s3_stores [account_id ][region ]
4931
50- store = s3_stores [account_id ][region ]
51- bucket_name = resource_arn .removeprefix (s3_prefix )
52- if bucket_name not in store .global_bucket_map :
53- raise NoSuchResource ("The specified resource doesn't exist." )
32+ def _tag_bucket_resource (
33+ self , resource_arn : str , partition : str , region : str , account_id : str , tags : TagList
34+ ) -> None :
35+ tagging_service = self .get_s3_store (account_id , region ).TAGS
36+ tagging_service .tag_resource (resource_arn , tags )
5437
55- return store .TAGS
38+ def _untag_bucket_resource (
39+ self , resource_arn : str , partition : str , region : str , account_id : str , tag_keys : TagKeyList
40+ ) -> None :
41+ tagging_service = self .get_s3_store (account_id , region ).TAGS
42+ tagging_service .untag_resource (resource_arn , tag_keys )
43+
44+ def _list_bucket_tags (
45+ self , resource_arn : str , partition : str , region : str , account_id : str
46+ ) -> TagList :
47+ tagging_service = self .get_s3_store (account_id , region ).TAGS
48+ return tagging_service .list_tags_for_resource (resource_arn )["Tags" ]
5649
5750 def tag_resource (
5851 self ,
@@ -62,17 +55,11 @@ def tag_resource(
6255 tags : TagList ,
6356 ** kwargs ,
6457 ) -> TagResourceResult :
65- # currently S3Control only supports tagging buckets
66- tagging_service = self ._get_tagging_service_for_bucket (
67- resource_arn = resource_arn ,
68- partition = context .partition ,
69- region = context .region ,
70- account_id = account_id ,
71- )
72-
73- validate_tags (tags = tags )
74- tagging_service .tag_resource (resource_arn , tags )
58+ # Currently S3Control only supports tagging buckets
59+ validate_arn_for_tagging (resource_arn , context .partition , account_id , context .region )
60+ validate_tags (tags )
7561
62+ self ._tag_bucket_resource (resource_arn , context .partition , context .region , account_id , tags )
7663 return TagResourceResult ()
7764
7865 def untag_resource (
@@ -83,28 +70,19 @@ def untag_resource(
8370 tag_keys : TagKeyList ,
8471 ** kwargs ,
8572 ) -> UntagResourceResult :
86- # currently S3Control only supports tagging buckets
87- tagging_service = self ._get_tagging_service_for_bucket (
88- resource_arn = resource_arn ,
89- partition = context .partition ,
90- region = context .region ,
91- account_id = account_id ,
92- )
93-
94- tagging_service .untag_resource (resource_arn , tag_keys )
73+ # Currently S3Control only supports tagging buckets
74+ validate_arn_for_tagging (resource_arn , context .partition , account_id , context .region )
9575
76+ self ._untag_bucket_resource (
77+ resource_arn , context .partition , context .region , account_id , tag_keys
78+ )
9679 return TagResourceResult ()
9780
9881 def list_tags_for_resource (
9982 self , context : RequestContext , account_id : AccountId , resource_arn : S3ResourceArn , ** kwargs
10083 ) -> ListTagsForResourceResult :
101- # currently S3Control only supports tagging buckets
102- tagging_service = self ._get_tagging_service_for_bucket (
103- resource_arn = resource_arn ,
104- partition = context .partition ,
105- region = context .region ,
106- account_id = account_id ,
107- )
84+ # Currently S3Control only supports tagging buckets
85+ validate_arn_for_tagging (resource_arn , context .partition , account_id , context .region )
10886
109- tags = tagging_service . list_tags_for_resource (resource_arn )
110- return ListTagsForResourceResult (Tags = tags [ "Tags" ] )
87+ tags = self . _list_bucket_tags (resource_arn , context . partition , context . region , account_id )
88+ return ListTagsForResourceResult (Tags = tags )
0 commit comments