-1

I'm currently working on a script for deleting aws resources using aws cli and python. As a part of my script, I have to delete the rules of a security group. The approach that I have taken is I execute the describe-security-groups command and I am able to store the following value in a variable:

[{u'IpProtocol':u'-1',u'PrefixListIds':[],u'IpRanges':[{u'CidrIp':u'0.0.0.0/0'}],u'UserIdGroupPairs':[],u'Ipv6Ranges':[]}]

However, for passing this value to the revoke-security-group-egress command, I need it in the following form:

[{"IpProtocol":"-1","PrefixListIds":[],"IpRanges":[{"CidrIp":"0.0.0.0/0"}],"UserIdGroupPairs":[],"Ipv6Ranges":[]}]

I'm looking for a way which could be used for other lists with different structures as well.

Or is there another way to delete all the rules of a security group using aws cli and python?

--UPDATE--

I found a way to come close to what I wanted after reading the answer here

Arya Shah
  • 1
  • 2

1 Answers1

0

This may be overkill, but you can do it like this:

import ast

# if your data comes in as an actual list, convert it to a string
data = "[{u'IpProtocol':u'-1',u'PrefixListIds':[],u'IpRanges':[{u'CidrIp':u'0.0.0.0/0'}],u'UserIdGroupPairs':[],u'Ipv6Ranges':[]}]"

AST = ast.parse(data, mode='eval')

for node in ast.walk(AST):
    if isinstance(node, ast.Str):
        node.s = str(node.s) # replace `unicode` with `str`

res = ast.literal_eval(AST)

res == [{'IpProtocol': '-1', 'Ipv6Ranges': [], 'IpRanges': [{'CidrIp': '0.0.0.0/0'}], 'UserIdGroupPairs': [], 'PrefixListIds': []}]

Docs on the ast module. This also works in Python 3.

If you know exactly the structure of your data (what values can your dictionaries have, how many dictionaries there are), you can loop through every single key and value of your dictionary (and every single dictionary of the list) and change everything of type unicode to str. That may be a lot more cumbersome, but may be faster.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I found something here https://stackoverflow.com/questions/1254454/fastest-way-to-convert-a-dicts-keys-values-from-unicode-to-str and then just used the replace function in python. Thanks for the effort! :) – Arya Shah Mar 11 '19 at 11:21