How to use delete_policy_version method in localstack

Best Python code snippet using localstack_python

manage_policy.py

Source:manage_policy.py Github

copy

Full Screen

...48 print("Existing policy versions: {}".format(versions))49 print("Default policy version: {}".format(default_version))50 except ClientError as e:51 print(e)52def delete_policy_version(account, policy_name, version):53 policy_arn = 'arn:aws:iam::' + account + ':policy/' + policy_name54 try: 55 response = iam.delete_policy_version(56 PolicyArn=policy_arn,57 VersionId=version58 )59 if response['ResponseMetadata']['HTTPStatusCode'] == 200:60 print("Successfully deleted IAM Policy version: {}".format(version))61 except ClientError as e:62 print(e)63def update_policy(account, policy_name, policy):64 # Get existing IAM policy65 policy_arn = 'arn:aws:iam::' + account + ':policy/' + policy_name66 try: 67 # Create the new version of the policy and set it as the default version68 response = iam.create_policy_version(PolicyArn=policy_arn,69 PolicyDocument=json.dumps(policy),70 SetAsDefault=True)71 if response['ResponseMetadata']['HTTPStatusCode'] == 200:72 print("Successfully updated IAM Policy: {}".format(policy_arn))73 except ClientError as e:74 print(e)75def yes_or_no(question):76 while "The answer is invalid":77 reply = str(raw_input(question+' (y/n): ')).lower().strip()78 if reply[:1] == 'y':79 return True80 if reply[:1] == 'n':81 sys.exit(0)82def main(_argv):83 parser = argparse.ArgumentParser(84 description='Alert Logic IAM Policy Management'85 )86 parser.add_argument(87 '--policy-name',88 help='IAM policy name to create/update',89 dest='policy_name',90 type=str,91 required=True92 )93 parser.add_argument(94 '--account',95 help='AWS account number',96 dest='account',97 type=str98 )99 parser.add_argument(100 '--create',101 help='Create IAM policy',102 action='store_true',103 dest='create'104 )105 parser.add_argument(106 '--update',107 help='Update existing IAM policy',108 action="store_true",109 dest='update'110 )111 parser.add_argument(112 '--delete-version',113 help='IAM policy version to delete',114 dest='delete_version',115 type=str116 )117 parser.add_argument(118 '--list-versions',119 help='List existing IAM policy versions',120 action='store_true',121 dest='list_versions'122 )123 parser.add_argument(124 '--minimal',125 help='Apply minimal IAM policy',126 action="store_true",127 dest='minimal'128 )129 args = parser.parse_args()130 minimal = False131 132 policy_name = args.policy_name133 if args.account:134 account = args.account135 if args.create:136 if args.minimal:137 minimal = True138 print("Minimal permission option allows you to maintain full control over the changes in your deployment, and requires you to perform any necessary actions manually.")139 yes_or_no("Continue applying minimal permissions policy?")140 policy = get_policy(True)141 else:142 print("Full permission option allows Alert Logic to make all the necessary changes to your AWS account")143 yes_or_no("Continue applying full permissions policy?")144 policy = get_policy(False)145 yes_or_no("Are you sure you want to create IAM Policy {}?".format(policy_name))146 create_policy(policy_name, policy)147 if args.update:148 if args.minimal:149 minimal = True150 print("Minimal permission option allows you to maintain full control over the changes in your deployment, and requires you to perform any necessary actions manually.")151 yes_or_no("Continue applying minimal permissions policy?")152 policy = get_policy(True)153 else:154 print("Full permission option allows Alert Logic to make all the necessary changes to your AWS account")155 yes_or_no("Continue applying full permissions policy?")156 policy = get_policy(False)157 yes_or_no("Are you sure you want to update IAM Policy {}?".format(policy_name))158 update_policy(account, policy_name, policy)159 if args.list_versions:160 list_policy_versions(account, policy_name)161 if args.delete_version:162 yes_or_no("Are you sure you want to delete IAM Policy {} version {}?".format(policy_name, args.delete_version))163 delete_policy_version(account, policy_name, args.delete_version)164if __name__ == '__main__':...

Full Screen

Full Screen

iot_policy.py

Source:iot_policy.py Github

copy

Full Screen

...65 if not version["isDefaultVersion"]:66 version_id = version["versionId"]67 logger.info("Deleting policy version: %s.%s", policy_name,68 version_id)69 response = iot.delete_policy_version(policyName=policy_name,70 policyVersionId=version_id)71 logger.debug("response=%s", response)72 logger.info("Deleting policy %s", policy_name)73 response = iot.delete_policy(policyName=policy_name)74 logger.debug("response=%s".response)75 return {"PhysicalResourceId": physical_id}76def delete_oldest_version(policy_name, policy_versions):77 non_default_versions = [78 ver for ver in policy_versions if not ver["isDefaultVersion"]79 ]80 oldest_version = min(non_default_versions,81 default=None,82 key=lambda version: version["versionId"])83 if oldest_version:84 iot.delete_policy_version(policyName=policy_name,85 policyVersionId=oldest_version["versionId"])86 logger.info("Deleted policy version policy_name=%s, versionId=%s",87 policy_name, oldest_version["versionId"])88def physical_id_from_policy_name(policy_name: str) -> str:89 return f"CustomIotPolicy{policy_name}"90def create_policy(policy_name: str, policy_document: Any):91 response = iot.create_policy(policyName=policy_name,92 policyDocument=policy_document)...

Full Screen

Full Screen

iot_policy_event_handler.py

Source:iot_policy_event_handler.py Github

copy

Full Screen

...45 for version in response.get('policyVersions'):46 if not version['isDefaultVersion']:47 version_id = version['versionId']48 print(f'Deleting policy version: {policy_name}.{version_id}')49 response = client.delete_policy_version(policyName=policy_name, policyVersionId=version_id)50 print(f'{response=}')51 print(f'Deleting policy {policy_name}')52 response = client.delete_policy(policyName=policy_name)53 print(f'{response=}')54 return {'PhysicalResourceId': physical_id}55def delete_oldest_version(client, policy_name, policy_versions):56 non_default_versions = [ver for ver in policy_versions if not ver['isDefaultVersion']]57 oldest_version = min(non_default_versions, default=None, key=lambda version: version['versionId'])58 if oldest_version:59 client.delete_policy_version(policyName=policy_name, policyVersionId=oldest_version['versionId'])60 print(f'Deleted policy version {policy_name=}, versionId={oldest_version["versionId"]}')61 62def physical_id_from_policy_name(policy_name: str) -> str:63 return f'CustomIotPolicy{policy_name}'64 65def create_policy(policy_name: str, policy_document: Any):66 response = client.create_policy(policyName=policy_name, policyDocument=policy_document)67 print(f'Created policy {response=}')...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful