我在Python脚本中使用boto3,我使用describe_db_cluster_parameters来获取TLS ParameterName及其ParameterValue。我使用下面的代码:
import boto3
import json
client = boto3.client('docdb')
response = client.describe_db_cluster_parameters(
DBClusterParameterGroupName='testaudit',
Filters=[
{
'Name': 'ParameterName',
'Values': [
'tls',
]
}
]
)
parameter = response.get('Parameters', {})
这是我输出到json格式时的示例响应。虽然顺序可以根据参数而改变。
[
{
"ApplyMethod": "pending-reboot",
"Description": "Enables auditing on cluster.",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "enabled,disabled",
"Source": "system",
"ParameterValue": "disabled",
"ParameterName": "audit_logs",
"ApplyType": "dynamic"
},
{
"ApplyMethod": "pending-reboot",
"Description": "Operations longer than profiler_threshold_ms will be logged",
"DataType": "integer",
"IsModifiable": true,
"AllowedValues": "50-2147483646",
"Source": "system",
"ParameterValue": "100",
"ParameterName": "profiler_threshold_ms",
"ApplyType": "dynamic"
},
{
"ApplyMethod": "pending-reboot",
"Description": "Config to enable/disable TLS",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "disabled,enabled",
"Source": "user",
"ParameterValue": "disabled",
"ParameterName": "tls",
"ApplyType": "static"
},
{
"ApplyMethod": "pending-reboot",
"Description": "Enables TTL Monitoring",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "disabled,enabled",
"Source": "system",
"ParameterValue": "enabled",
"ParameterName": "ttl_monitor",
"ApplyType": "dynamic"
}
]
如果我只需要获取ParameterName等于"tls"的字典因为我还需要得到它的ParameterValue,我该怎么做呢?我是否需要使用for循环或迭代来检查每个字典中的每个ParameterName ?谢谢!
简单地遍历列表并检查ParameterName
是否等于tls
:
resp = [
{
"ApplyMethod": "pending-reboot",
"Description": "Enables auditing on cluster.",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "enabled,disabled",
"Source": "system",
"ParameterValue": "disabled",
"ParameterName": "audit_logs",
"ApplyType": "dynamic"
},
{
"ApplyMethod": "pending-reboot",
"Description": "Operations longer than profiler_threshold_ms will be logged",
"DataType": "integer",
"IsModifiable": true,
"AllowedValues": "50-2147483646",
"Source": "system",
"ParameterValue": "100",
"ParameterName": "profiler_threshold_ms",
"ApplyType": "dynamic" },
{
"ApplyMethod": "pending-reboot",
"Description": "Config to enable/disable TLS",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "disabled,enabled",
"Source": "user",
"ParameterValue": "disabled",
"ParameterName": "tls",
"ApplyType": "static" },
{
"ApplyMethod": "pending-reboot",
"Description": "Enables TTL Monitoring",
"DataType": "string",
"IsModifiable": true,
"AllowedValues": "disabled,enabled",
"Source": "system",
"ParameterValue": "enabled",
"ParameterName": "ttl_monitor",
"ApplyType": "dynamic"
}
]
result = dict()
for elt in resp:
param_name = elt.get("ParameterName", None)
if param_name == "tls":
result = elt
break
print(result)
输出:
{'ApplyMethod': 'pending-reboot', 'Description': 'Config to enable/disable TLS', 'DataType': 'string', 'IsModifiable': True, 'AllowedValues': 'disabled,enabled', 'Source': 'user', 'ParameterValue': 'disabled', 'ParameterName': 'tls', 'ApplyType': 'static'}
您可以使用这种方法:
a = {'a':'tls','b':'not', 'c':'tls', 'd':'not'}
b = {x[0]:x[1] for x in a.items() if x[1]=='tls'}
print(b)
# {'a': 'tls', 'c': 'tls'}
在你的情况下你可以使用:
[x for x in resp if x['ParameterName']=='tls']
# keep in mind this will break when the first record does not have the key
如果他们的键丢失,这将不会破坏:
[x for x in resp if x.get('ParameterName', None)=='tls']
[{'ApplyMethod': 'pending-reboot',
'Description': 'Config to enable/disable TLS',
'DataType': 'string',
'IsModifiable': True,
'AllowedValues': 'disabled,enabled',
'Source': 'user',
'ParameterValue': 'disabled',
'ParameterName': 'tls',
'ApplyType': 'static'}]