Best Python code snippet using localstack_python
test_list_secrets.py
Source:test_list_secrets.py
...9 return boto3.client("secretsmanager", region_name="us-west-2")10@mock_secretsmanager11def test_empty():12 conn = boto_client()13 secrets = conn.list_secrets()14 assert secrets["SecretList"] == []15@mock_secretsmanager16def test_list_secrets():17 conn = boto_client()18 conn.create_secret(Name="test-secret", SecretString="foosecret")19 conn.create_secret(20 Name="test-secret-2",21 SecretString="barsecret",22 Tags=[{"Key": "a", "Value": "1"}],23 )24 secrets = conn.list_secrets()25 assert secrets["SecretList"][0]["ARN"] is not None26 assert secrets["SecretList"][0]["Name"] == "test-secret"27 assert secrets["SecretList"][0]["SecretVersionsToStages"] is not None28 assert secrets["SecretList"][1]["ARN"] is not None29 assert secrets["SecretList"][1]["Name"] == "test-secret-2"30 assert secrets["SecretList"][1]["Tags"] == [{"Key": "a", "Value": "1"}]31 assert secrets["SecretList"][1]["SecretVersionsToStages"] is not None32@mock_secretsmanager33def test_with_name_filter():34 conn = boto_client()35 conn.create_secret(Name="foo", SecretString="secret")36 conn.create_secret(Name="bar", SecretString="secret")37 secrets = conn.list_secrets(Filters=[{"Key": "name", "Values": ["foo"]}])38 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))39 assert secret_names == ["foo"]40@mock_secretsmanager41def test_with_tag_key_filter():42 conn = boto_client()43 conn.create_secret(44 Name="foo", SecretString="secret", Tags=[{"Key": "baz", "Value": "1"}]45 )46 conn.create_secret(Name="bar", SecretString="secret")47 secrets = conn.list_secrets(Filters=[{"Key": "tag-key", "Values": ["baz"]}])48 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))49 assert secret_names == ["foo"]50@mock_secretsmanager51def test_with_tag_value_filter():52 conn = boto_client()53 conn.create_secret(54 Name="foo", SecretString="secret", Tags=[{"Key": "1", "Value": "baz"}]55 )56 conn.create_secret(Name="bar", SecretString="secret")57 secrets = conn.list_secrets(Filters=[{"Key": "tag-value", "Values": ["baz"]}])58 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))59 assert secret_names == ["foo"]60@mock_secretsmanager61def test_with_description_filter():62 conn = boto_client()63 conn.create_secret(Name="foo", SecretString="secret", Description="baz qux")64 conn.create_secret(Name="bar", SecretString="secret")65 secrets = conn.list_secrets(Filters=[{"Key": "description", "Values": ["baz"]}])66 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))67 assert secret_names == ["foo"]68@mock_secretsmanager69def test_with_all_filter():70 # The 'all' filter will match a secret that contains ANY field with the criteria. In other words an implicit OR.71 conn = boto_client()72 conn.create_secret(Name="foo", SecretString="secret")73 conn.create_secret(Name="bar", SecretString="secret", Description="foo")74 conn.create_secret(75 Name="baz", SecretString="secret", Tags=[{"Key": "foo", "Value": "1"}]76 )77 conn.create_secret(78 Name="qux", SecretString="secret", Tags=[{"Key": "1", "Value": "foo"}]79 )80 conn.create_secret(81 Name="multi", SecretString="secret", Tags=[{"Key": "foo", "Value": "foo"}]82 )83 conn.create_secret(Name="none", SecretString="secret")84 secrets = conn.list_secrets(Filters=[{"Key": "all", "Values": ["foo"]}])85 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))86 assert sorted(secret_names) == ["bar", "baz", "foo", "multi", "qux"]87@mock_secretsmanager88def test_with_no_filter_key():89 conn = boto_client()90 with pytest.raises(ClientError) as ire:91 conn.list_secrets(Filters=[{"Values": ["foo"]}])92 ire.value.response["Error"]["Code"].should.equal("InvalidParameterException")93 ire.value.response["Error"]["Message"].should.equal("Invalid filter key")94@mock_secretsmanager95def test_with_no_filter_values():96 conn = boto_client()97 conn.create_secret(Name="foo", SecretString="secret", Description="hello")98 with pytest.raises(ClientError) as ire:99 conn.list_secrets(Filters=[{"Key": "description"}])100 ire.value.response["Error"]["Code"].should.equal("InvalidParameterException")101 ire.value.response["Error"]["Message"].should.equal(102 "Invalid filter values for key: description"103 )104@mock_secretsmanager105def test_with_invalid_filter_key():106 conn = boto_client()107 with pytest.raises(ClientError) as ire:108 conn.list_secrets(Filters=[{"Key": "invalid", "Values": ["foo"]}])109 ire.value.response["Error"]["Code"].should.equal("ValidationException")110 ire.value.response["Error"]["Message"].should.equal(111 "1 validation error detected: Value 'invalid' at 'filters.1.member.key' failed to satisfy constraint: Member "112 "must satisfy enum value set: [all, name, tag-key, description, tag-value]"113 )114@mock_secretsmanager115def test_with_duplicate_filter_keys():116 # Multiple filters with the same key combine with an implicit AND operator117 conn = boto_client()118 conn.create_secret(Name="foo", SecretString="secret", Description="one two")119 conn.create_secret(Name="bar", SecretString="secret", Description="one")120 conn.create_secret(Name="baz", SecretString="secret", Description="two")121 conn.create_secret(Name="qux", SecretString="secret", Description="unrelated")122 secrets = conn.list_secrets(123 Filters=[124 {"Key": "description", "Values": ["one"]},125 {"Key": "description", "Values": ["two"]},126 ]127 )128 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))129 assert secret_names == ["foo"]130@mock_secretsmanager131def test_with_multiple_filters():132 # Multiple filters combine with an implicit AND operator133 conn = boto_client()134 conn.create_secret(135 Name="foo", SecretString="secret", Tags=[{"Key": "right", "Value": "right"}]136 )137 conn.create_secret(138 Name="bar", SecretString="secret", Tags=[{"Key": "right", "Value": "wrong"}]139 )140 conn.create_secret(141 Name="baz", SecretString="secret", Tags=[{"Key": "wrong", "Value": "right"}]142 )143 conn.create_secret(144 Name="qux", SecretString="secret", Tags=[{"Key": "wrong", "Value": "wrong"}]145 )146 secrets = conn.list_secrets(147 Filters=[148 {"Key": "tag-key", "Values": ["right"]},149 {"Key": "tag-value", "Values": ["right"]},150 ]151 )152 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))153 assert secret_names == ["foo"]154@mock_secretsmanager155def test_with_filter_with_multiple_values():156 conn = boto_client()157 conn.create_secret(Name="foo", SecretString="secret")158 conn.create_secret(Name="bar", SecretString="secret")159 conn.create_secret(Name="baz", SecretString="secret")160 secrets = conn.list_secrets(Filters=[{"Key": "name", "Values": ["foo", "bar"]}])161 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))162 assert secret_names == ["foo", "bar"]163@mock_secretsmanager164def test_with_filter_with_value_with_multiple_words():165 conn = boto_client()166 conn.create_secret(Name="foo", SecretString="secret", Description="one two")167 conn.create_secret(Name="bar", SecretString="secret", Description="one and two")168 conn.create_secret(Name="baz", SecretString="secret", Description="one")169 conn.create_secret(Name="qux", SecretString="secret", Description="two")170 conn.create_secret(Name="none", SecretString="secret", Description="unrelated")171 secrets = conn.list_secrets(Filters=[{"Key": "description", "Values": ["one two"]}])172 secret_names = list(map(lambda s: s["Name"], secrets["SecretList"]))...
create_secrets.py
Source:create_secrets.py
...13secret_prefix = 'ansible-'14def is_match(secret):15 return secret['Name'].startswith(secret_prefix)16def get_secrets_dict():17 secret_ids = list_secrets(is_match)18 secrets = get_secret_dict(secret_ids)19 return secrets20def is_json(str):21 chars = {22 34: True,23 91: True,24 123: True,25 }26 return chars.get(ord(str[0]), False)27def get_secret_dict(secret_ids):28 client = boto3.client('secretsmanager')29 secrets = {}30 for id in secret_ids:31 result = client.get_secret_value(SecretId=id)32 name = result['Name'][len(secret_prefix):]33 value = result['SecretString']34 if is_json(value):35 secrets[name] = json.loads(value)36 else:37 secrets[name] = value38 return secrets39def list_secrets(matcher):40 client = boto3.client('secretsmanager')41 token = ''42 secret_ids = []43 while token is not None:44 if len(token) > 0:45 result = client.list_secrets(NextToken=token)46 else:47 result = client.list_secrets()48 secret_ids.extend(49 get_secret_ids(result['SecretList'], matcher)50 )51 token = result.get('NextToken', None)52 return secret_ids53def get_secret_ids(secrets, matcher):54 secret_ids = []55 for s in secrets:56 if matcher(s):57 secret_ids.append(s['ARN'])58 return secret_ids59if __name__ == '__main__':60 print('creating secrets.yaml')61 create_secrets_yaml('/tmp/ansible/secrets.yaml')
handler.py
Source:handler.py
...10 'max_attempts': 2,11 }12 )13 )14 list_secrets = client.list_secrets()15 return {16 'statusCode': 200,17 'body': json.dumps('Hello from Lambda!'),18 'api_response': json.dumps(list_secrets, default=str)...
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!!