Best Python code snippet using localstack_python
c7n-s3-public-block.py
Source:c7n-s3-public-block.py  
...20            return 021    except ClientError as e:22        logging.error(e)23        return 124def put_public_access_block(bucket_name):25    """Put public access block on s3 bucket_name"""26    s3 = boto3.client('s3')27    try:28        s3.put_public_access_block(29            Bucket=bucket_name,30            PublicAccessBlockConfiguration={31            'BlockPublicAcls': False,32            'IgnorePublicAcls': True,33            'BlockPublicPolicy': False,34            'RestrictPublicBuckets': False35        }36    )37    except ClientError as e:38        logging.info(e)39        return None40def c7n_update_public_access_block(event_list):41    """Set up logging"""42    logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(asctime)s: %(message)s')43    """Parsing event"""44    s3_bucket_name = event_list["resources"][0]['Name']45    """Get public access block"""46    if get_public_access_block(s3_bucket_name) != 0:47        logging.info("No public block found: %s", s3_bucket_name)48        """Put public access block"""49        try:50            put_public_access_block(s3_bucket_name)51            return 052        except ClientError as e:53            logging.error(e)54            return -155def lambda_handler(event, context):...test_s3control.py
Source:test_s3control.py  
...14        "IgnorePublicAcls": True,15        "BlockPublicPolicy": True,16        "RestrictPublicBuckets": True,17    }18    put_response = s3control_client.put_public_access_block(19        AccountId=TEST_AWS_ACCOUNT_ID, PublicAccessBlockConfiguration=access_block_config20    )21    assert put_response["ResponseMetadata"]["HTTPStatusCode"] == 20122    get_response = s3control_client.get_public_access_block(AccountId=TEST_AWS_ACCOUNT_ID)23    assert access_block_config == get_response["PublicAccessBlockConfiguration"]24    s3control_client.delete_public_access_block(AccountId=TEST_AWS_ACCOUNT_ID)25def test_public_access_block_validations():26    with pytest.raises(ClientError) as error:27        s3control_client.get_public_access_block(AccountId="111111111111")28    assert error.value.response["Error"]["Code"] == "AccessDenied"29    with pytest.raises(ClientError) as error:30        s3control_client.put_public_access_block(31            AccountId="111111111111",32            PublicAccessBlockConfiguration={"BlockPublicAcls": True},33        )34    assert error.value.response["Error"]["Code"] == "AccessDenied"35    with pytest.raises(ClientError) as error:36        s3control_client.put_public_access_block(37            AccountId=TEST_AWS_ACCOUNT_ID, PublicAccessBlockConfiguration={}38        )...svcs3control.py
Source:svcs3control.py  
...16            return response['PublicAccessBlockConfiguration']17        except botocore.exceptions.ClientError as e:18            raise RdqError(self._utils.fail(e, op, 'AccountId', accountId))19    # PREVIEW20    def put_public_access_block(self, accountId, cfg):21        op = 'put_public_access_block'22        accountId = self._profile.accountId23        args = {24            'AccountId': accountId,25            'PublicAccessBlockConfiguration': cfg26        }27        if self._utils.preview(op, args): return28        try:29            self._client.put_public_access_block(30                PublicAccessBlockConfiguration=cfg,31                AccountId=accountId32            )33        except botocore.exceptions.ClientError as e:34            raise RdqError(self._utils.fail(e, op, 'AccountId', accountId))35    # PREVIEW36    def declarePublicAccessBlock(self, targetAccountId=None, requiredState=True):37        accountId = targetAccountId if targetAccountId else self._profile.accountId38        keys = ['BlockPublicAcls', 'IgnorePublicAcls', 'BlockPublicPolicy', 'RestrictPublicBuckets']39        exMap = self.get_public_access_block(accountId)40        delta = False41        for key in keys:42            if key in exMap:43                exValue = exMap[key]44                if exValue != requiredState:45                    delta = True46            else:47                delta = True48        if not delta: return False49        cfg = {}50        for key in keys:51            cfg[key] = requiredState52        self.put_public_access_block(accountId, cfg)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
