0

I am using boto3 within a Python script and I'm using the describe_db_cluster_parameters to get the TLS ParameterName and its ParameterValue. I'm using this code below:

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', {})

This is the sample response when I output it to json format. Though the sequence could be changed depending on the parameter.

[
  {
    "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"   
   } 
  ]

If I need to get only the dictionary with the ParameterName equal to "tls" since I need to also get its ParameterValue, how would I do that? Do I need to use for loop or iterate to check each ParameterName per dictionary? Thanks!

jrust
  • 23
  • 1
  • 3
  • Does this answer your question? [Python list of dictionaries search](https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search) – Håken Lid Feb 19 '21 at 14:23

2 Answers2

0

Simply iterate over the list and check if the ParameterName equals 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)
    

Output:

{'ApplyMethod': 'pending-reboot', 'Description': 'Config to enable/disable TLS', 'DataType': 'string', 'IsModifiable': True, 'AllowedValues': 'disabled,enabled', 'Source': 'user', 'ParameterValue': 'disabled', 'ParameterName': 'tls', 'ApplyType': 'static'}
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0

You can use this approach:

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'}

In your case you can use:

[x for x in resp if x['ParameterName']=='tls'] 
# keep in mind this will break when the first record does not have the key

This will not break if they key is missing:

[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'}]
Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55