How to use modify_snapshot_attribute method in localstack

Best Python code snippet using localstack_python

test_elastic_block_store.py

Source:test_elastic_block_store.py Github

copy

Full Screen

...377 "groups": "all",378 }379 # Add 'all' group and confirm380 with pytest.raises(EC2ResponseError) as ex:381 conn.modify_snapshot_attribute(**dict(ADD_GROUP_ARGS, **{"dry_run": True}))382 ex.value.error_code.should.equal("DryRunOperation")383 ex.value.status.should.equal(400)384 ex.value.message.should.equal(385 "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set"386 )387 conn.modify_snapshot_attribute(**ADD_GROUP_ARGS)388 attributes = conn.get_snapshot_attribute(389 snapshot.id, attribute="createVolumePermission"390 )391 attributes.attrs["groups"].should.have.length_of(1)392 attributes.attrs["groups"].should.equal(["all"])393 # Add is idempotent394 conn.modify_snapshot_attribute.when.called_with(**ADD_GROUP_ARGS).should_not.throw(395 EC2ResponseError396 )397 # Remove 'all' group and confirm398 with pytest.raises(EC2ResponseError) as ex:399 conn.modify_snapshot_attribute(**dict(REMOVE_GROUP_ARGS, **{"dry_run": True}))400 ex.value.error_code.should.equal("DryRunOperation")401 ex.value.status.should.equal(400)402 ex.value.message.should.equal(403 "An error occurred (DryRunOperation) when calling the ModifySnapshotAttribute operation: Request would have succeeded, but DryRun flag is set"404 )405 conn.modify_snapshot_attribute(**REMOVE_GROUP_ARGS)406 attributes = conn.get_snapshot_attribute(407 snapshot.id, attribute="createVolumePermission"408 )409 attributes.attrs.should.have.length_of(0)410 # Remove is idempotent411 conn.modify_snapshot_attribute.when.called_with(412 **REMOVE_GROUP_ARGS413 ).should_not.throw(EC2ResponseError)414 # Error: Add with group != 'all'415 with pytest.raises(EC2ResponseError) as cm:416 conn.modify_snapshot_attribute(417 snapshot.id,418 attribute="createVolumePermission",419 operation="add",420 groups="everyone",421 )422 cm.value.code.should.equal("InvalidAMIAttributeItemValue")423 cm.value.status.should.equal(400)424 cm.value.request_id.should_not.be.none425 # Error: Add with invalid snapshot ID426 with pytest.raises(EC2ResponseError) as cm:427 conn.modify_snapshot_attribute(428 "snapshot-abcd1234",429 attribute="createVolumePermission",430 operation="add",431 groups="all",432 )433 cm.value.code.should.equal("InvalidSnapshot.NotFound")434 cm.value.status.should.equal(400)435 cm.value.request_id.should_not.be.none436 # Error: Remove with invalid snapshot ID437 with pytest.raises(EC2ResponseError) as cm:438 conn.modify_snapshot_attribute(439 "snapshot-abcd1234",440 attribute="createVolumePermission",441 operation="remove",442 groups="all",443 )444 cm.value.code.should.equal("InvalidSnapshot.NotFound")445 cm.value.status.should.equal(400)446 cm.value.request_id.should_not.be.none447@mock_ec2448def test_modify_snapshot_attribute():449 import copy450 ec2_client = boto3.client("ec2", region_name="us-east-1")451 response = ec2_client.create_volume(Size=80, AvailabilityZone="us-east-1a")452 volume = boto3.resource("ec2", region_name="us-east-1").Volume(response["VolumeId"])453 snapshot = volume.create_snapshot()454 # Baseline455 attributes = ec2_client.describe_snapshot_attribute(456 SnapshotId=snapshot.id, Attribute="createVolumePermission"457 )458 assert not attributes[459 "CreateVolumePermissions"460 ], "Snapshot should have no permissions."461 ADD_GROUP_ARGS = {462 "SnapshotId": snapshot.id,463 "Attribute": "createVolumePermission",464 "OperationType": "add",465 "GroupNames": ["all"],466 }467 REMOVE_GROUP_ARGS = {468 "SnapshotId": snapshot.id,469 "Attribute": "createVolumePermission",470 "OperationType": "remove",471 "GroupNames": ["all"],472 }473 # Add 'all' group and confirm474 with pytest.raises(ClientError) as cm:475 ec2_client.modify_snapshot_attribute(**dict(ADD_GROUP_ARGS, **{"DryRun": True}))476 cm.value.response["Error"]["Code"].should.equal("DryRunOperation")477 cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none478 cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)479 ec2_client.modify_snapshot_attribute(**ADD_GROUP_ARGS)480 attributes = ec2_client.describe_snapshot_attribute(481 SnapshotId=snapshot.id, Attribute="createVolumePermission"482 )483 assert attributes["CreateVolumePermissions"] == [484 {"Group": "all"}485 ], "This snapshot should have public group permissions."486 # Add is idempotent487 ec2_client.modify_snapshot_attribute.when.called_with(488 **ADD_GROUP_ARGS489 ).should_not.throw(ClientError)490 assert attributes["CreateVolumePermissions"] == [491 {"Group": "all"}492 ], "This snapshot should have public group permissions."493 # Remove 'all' group and confirm494 with pytest.raises(ClientError) as ex:495 ec2_client.modify_snapshot_attribute(496 **dict(REMOVE_GROUP_ARGS, **{"DryRun": True})497 )498 cm.value.response["Error"]["Code"].should.equal("DryRunOperation")499 cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none500 cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)501 ec2_client.modify_snapshot_attribute(**REMOVE_GROUP_ARGS)502 attributes = ec2_client.describe_snapshot_attribute(503 SnapshotId=snapshot.id, Attribute="createVolumePermission"504 )505 assert not attributes[506 "CreateVolumePermissions"507 ], "This snapshot should have no permissions."508 # Remove is idempotent509 ec2_client.modify_snapshot_attribute.when.called_with(510 **REMOVE_GROUP_ARGS511 ).should_not.throw(ClientError)512 assert not attributes[513 "CreateVolumePermissions"514 ], "This snapshot should have no permissions."515 # Error: Add with group != 'all'516 with pytest.raises(ClientError) as cm:517 ec2_client.modify_snapshot_attribute(518 SnapshotId=snapshot.id,519 Attribute="createVolumePermission",520 OperationType="add",521 GroupNames=["everyone"],522 )523 cm.value.response["Error"]["Code"].should.equal("InvalidAMIAttributeItemValue")524 cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none525 cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)526 # Error: Add with invalid snapshot ID527 with pytest.raises(ClientError) as cm:528 ec2_client.modify_snapshot_attribute(529 SnapshotId="snapshot-abcd1234",530 Attribute="createVolumePermission",531 OperationType="add",532 GroupNames=["all"],533 )534 cm.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound")535 cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none536 cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)537 # Error: Remove with invalid snapshot ID538 with pytest.raises(ClientError) as cm:539 ec2_client.modify_snapshot_attribute(540 SnapshotId="snapshot-abcd1234",541 Attribute="createVolumePermission",542 OperationType="remove",543 GroupNames=["all"],544 )545 cm.value.response["Error"]["Code"].should.equal("InvalidSnapshot.NotFound")546 cm.value.response["ResponseMetadata"]["RequestId"].should_not.be.none547 cm.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)548 # Test adding user id549 ec2_client.modify_snapshot_attribute(550 SnapshotId=snapshot.id,551 Attribute="createVolumePermission",552 OperationType="add",553 UserIds=["1234567891"],554 )555 attributes = ec2_client.describe_snapshot_attribute(556 SnapshotId=snapshot.id, Attribute="createVolumePermission"557 )558 assert len(attributes["CreateVolumePermissions"]) == 1559 # Test adding user id again along with additional.560 ec2_client.modify_snapshot_attribute(561 SnapshotId=snapshot.id,562 Attribute="createVolumePermission",563 OperationType="add",564 UserIds=["1234567891", "2345678912"],565 )566 attributes = ec2_client.describe_snapshot_attribute(567 SnapshotId=snapshot.id, Attribute="createVolumePermission"568 )569 assert len(attributes["CreateVolumePermissions"]) == 2570 # Test removing both user IDs.571 ec2_client.modify_snapshot_attribute(572 SnapshotId=snapshot.id,573 Attribute="createVolumePermission",574 OperationType="remove",575 UserIds=["1234567891", "2345678912"],576 )577 attributes = ec2_client.describe_snapshot_attribute(578 SnapshotId=snapshot.id, Attribute="createVolumePermission"579 )580 assert len(attributes["CreateVolumePermissions"]) == 0581 # Idempotency when removing users.582 ec2_client.modify_snapshot_attribute(583 SnapshotId=snapshot.id,584 Attribute="createVolumePermission",585 OperationType="remove",586 UserIds=["1234567891"],587 )588 attributes = ec2_client.describe_snapshot_attribute(589 SnapshotId=snapshot.id, Attribute="createVolumePermission"590 )591 assert len(attributes["CreateVolumePermissions"]) == 0592@mock_ec2_deprecated593def test_create_volume_from_snapshot():594 conn = boto.ec2.connect_to_region("us-east-1")595 volume = conn.create_volume(80, "us-east-1a")596 snapshot = volume.create_snapshot("a test snapshot")...

Full Screen

Full Screen

ebs_snapshots.py

Source:ebs_snapshots.py Github

copy

Full Screen

...53 try:54 if accounts_to_add:55 logger.debug(f"Sharing the snapshot {self.name} with the accounts {', '.join(accounts_to_add)}")56 if "all" in accounts_to_add:57 self.client.modify_snapshot_attribute(58 Attribute="createVolumePermission",59 SnapshotId=self.name,60 GroupNames=["all"],61 OperationType="add",62 )63 else:64 self.client.modify_snapshot_attribute(65 Attribute="createVolumePermission",66 SnapshotId=self.name,67 UserIds=accounts_to_add,68 OperationType="add",69 )70 if accounts_to_remove:71 logger.debug(f"Removing access to the snapshot {self.name} from the accounts {', '.join(accounts_to_add)}")72 if "all" in accounts_to_remove:73 self.client.modify_snapshot_attribute(74 Attribute="createVolumePermission",75 SnapshotId=self.name,76 GroupNames=["all"],77 OperationType="remove",78 )79 else:80 self.client.modify_snapshot_attribute(81 Attribute="createVolumePermission",82 SnapshotId=self.name,83 UserIds=accounts_to_remove,84 OperationType="remove",85 )86 shared_with_accounts = self._get_shared_with_accounts().shared_with_accounts87 success = True88 except botocore.exceptions.ClientError:89 success = False90 response_message = ResponseGetSharingApi(shared_with_accounts=shared_with_accounts, success=success,91 evil_principal="", victim_resource_arn=self.arn,92 resource_name=self.name, resource_type=self.resource_type,93 service=self.service,94 original_policy=self.original_shared_with_accounts,...

Full Screen

Full Screen

ebs_snapshot_remover.py

Source:ebs_snapshot_remover.py Github

copy

Full Screen

...18 awsid = UserId['UserId']19 if awsid in SHARED_ACCOUNTS:20 if awsid == 'aws-marketplace':21 awsid = marketplace_id22 remove = ec2.modify_snapshot_attribute(SnapshotId=['SnapshotId'],Attribute='createVolumePermission',OperationType='remove',UserIds=[awsid])23 print("Removed " str(awsid) + " share from: " + permissions['SnapshotId'])24 else:25 remove = ec2.modify_snapshot_attribute(SnapshotId=['SnapshotId'],Attribute='createVolumePermission',OperationType='remove',UserIds=[awsid])26 print("Removed " str(awsid) + " share from: " + permissions['SnapshotId'])27 except Exception as error:...

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