Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/cleanup/janitor/utils.py: 70.59%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2025-04-14 16:27 +0000

1# Copyright (C) 2024 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16import typing 

17 

18import boto3 

19import botocore 

20 

21if typing.TYPE_CHECKING: 

22 from mypy_boto3_s3.client import S3Client 

23 

24from buildgrid.server.cleanup.janitor.config import S3Config 

25 

26 

27def get_s3_client(config: S3Config) -> "S3Client": 

28 try: 

29 return boto3.client( 

30 "s3", 

31 endpoint_url=config.endpoint, 

32 aws_access_key_id=config.access_key, 

33 aws_secret_access_key=config.secret_key, 

34 ) 

35 except Exception as e: 

36 raise ValueError("Failed to create an S3 client, check the S3 configuration options.") from e 

37 

38 

39def check_bucket_versioning(s3_client: "S3Client", bucket: str) -> bool: 

40 try: 

41 response = s3_client.get_bucket_versioning(Bucket=bucket) 

42 return bool(response.get("Status")) 

43 except botocore.exceptions.ClientError: 

44 return False