Best Python code snippet using localstack_python
svccwl.py
Source:svccwl.py  
...76        except botocore.exceptions.ClientError as e:77            if self._utils.is_resource_not_found(e): return78            raise RdqError(self._utils.fail(e, op, 'LogGroupName', logGroupName))79    #PREVIEW80    def associate_kms_key(self, logGroupName, kmsArn):81        op = 'associate_kms_key'82        args = {83            'LogGroupName': logGroupName,84            'KmsKeyId' : kmsArn85        }86        if self._utils.preview(op, args): return87        try:88            self._client.associate_kms_key(89                logGroupName=logGroupName,90                kmsKeyId=kmsArn91            )92        except botocore.exceptions.ClientError as e:93            if self._utils.is_resource_not_found(e): return94            raise RdqError(self._utils.fail(e, op, 'LogGroupName', logGroupName, 'KmsKeyId', kmsArn))95    #PREVIEW96    def disassociate_kms_key(self, logGroupName):97        op = 'disassociate_kms_key'98        args = {99            'LogGroupName': logGroupName100        }101        if self._utils.preview(op, args): return102        try:103            self._client.disassociate_kms_key(104                logGroupName=logGroupName105            )106        except botocore.exceptions.ClientError as e:107            if self._utils.is_resource_not_found(e): return108            raise RdqError(self._utils.fail(e, op, 'LogGroupName', logGroupName))109    def getLogGroupDescriptor(self, logGroupName :str) -> LogGroupDescriptor:110        exBase = self.describe_log_group(logGroupName)111        if not exBase: return None112        optTagDict = self.list_log_group_tags(logGroupName)113        exTags = Tags(optTagDict, logGroupName)114        return LogGroupDescriptor(exBase, exTags)115    #PREVIEW116    def associateKmsKeyWithLogGroup(self, logGroupName :str, cmkArn :str):117        self.associate_kms_key(logGroupName, cmkArn)118    #PREVIEW119    def disassociateKmsKeyWithLogGroup(self, logGroupName :str):120        self.associate_kms_key(logGroupName)121    #PREVIEW122    def putTags(self, logGroupName: str, tags :Tags):...log_management.py
Source:log_management.py  
...45    for log_group in log_groups:46        # If a KMS key alias is supplied, try to encrypt all log groups with that KMS key47        if os.environ.get("KMS_KEY_ALIAS", "None") != "None":48            try:49                cw.associate_kms_key(50                    logGroupName=log_group['logGroupName'],51                    kmsKeyId=key_id52                )53            except Exception as e:54                print(f"Ran into error when encrypting log group {log_group['logGroupName']} in {cw.meta.region_name}")55                print(traceback.format_exc())56        # If a KMS key is not supplied AND we want to allow KMS disassociation from Cloudwatch, try to disassociate KMS keys from Cloudwatch57        # This will remove KMS associations for ALL Cloudwatch log groups in the specified account and region(s)58        if os.environ.get("KMS_KEY_ALIAS", "None") == "None" and os.environ.get("ALLOW_KMS_DISASSOCIATE", "False") == "True":59            try:60                cw.disassociate_kms_key(61                    logGroupName=log_group['logGroupName'],62                )63            except Exception as e:64                print(f"Ran into error when removing encryption for log group {log_group['logGroupName']} in {cw.meta.region_name}")65                print(traceback.format_exc())66        if os.environ.get("RETENTION_IN_DAYS", "None") != "None":67            try:68                cw.put_retention_policy(69                   logGroupName=log_group['logGroupName'],70                   retentionInDays=int(os.environ['RETENTION_IN_DAYS'])71                )72            except Exception as e:73                print(f"Ran into error when attaching retention policy to log group {log_group['logGroupName']} in {cw.meta.region_name}")74                print(traceback.format_exc())...fix.py
Source:fix.py  
2import boto33def associateCMKWithCWLG(cwlgName, cmkArn):4    try:5        cwl_client = boto3.client('logs')6        response = cwl_client.associate_kms_key(logGroupName=cwlgName, kmsKeyId=cmkArn)7        return True8    except botocore.exceptions.ClientError as e:9        print("Failed to associate_kms_key")10        print(e)11        print("logGroupName="+cwlgName)12        print("kmsKeyId="+cmkArn)13        return False14def cwlgPutRetentionPolicy(cwlgName, retentionInDays):15    try:16        cwl_client = boto3.client('logs')17        response = cwl_client.put_retention_policy(logGroupName=cwlgName, retentionInDays=retentionInDays)18        return True19    except botocore.exceptions.ClientError as e:20        print("Failed to put_retention_policy")...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!!
