Deleting all the CloudWatch rules using boto3 - python

We have ton of cloudwatch rules that we need to get rid of, I was working on a python script, to delete all the CloudWatch rules , but I could only find the delete rule for a specific rule on boto3 website, but I want to delete all the rules we have.
import boto3
client = boto3.client('events')
response = client.delete_rule(
Name='string'
)

You have to do it in two stages.
Get the list of Name of all the rules you have using list_rules
Use iteration to delete all your rules, one by one, using using delete_rule.
client = boto3.client('events')
rule_names = [rule['Name'] for rule in client.list_rules()['Rules']]
for rule_name in rule_names:
response = client.delete_rule(Name=rule_name)
print(response)
Depending on how many rules you actually have, you may need to run list_rules multiple times with NextToken.

As CloudWatch Event Rule usually has at least one target, you need to:
Get list of your rules using list_rules method;
Loop through the list of rules and within each iteration:
Get list of rule's targets' IDs using list_targets_by_rule method;
Remove targets using remove_targets method;
Remove the rule using delete_rule method.
So the resulting script will look like this:
import boto3
client = boto3.client('events')
rules = client.list_rules()['Rules']
for rule in rules:
rule_targets = client.list_targets_by_rule(
Rule=rule['Name']
)['Targets']
target_ids = [target['Id'] for target in rule_targets]
remove_targets_response = client.remove_targets(
Rule=rule['Name'],
Ids=target_ids
)
print(remove_targets_response)
delete_rule_response = client.delete_rule(
Name=rule['Name']
)
print(delete_rule_response)

Related

Python - Append to Docitionary List

I have a dictionary with a list that appears like this:
{'subscription_id_list': ['92878351', '93153640', '93840845', '93840847'.......]
There will be up to 10,000 entries in the list for each call to the API. What I want to do is loop and append the list so when complete, my {'subscription_id_list'} contains everything.
Any thoughts?
It sounds like you have something like
for api_request in some_structure:
response = get_remote_results(api_request)
# response structure is single-key dict,
# key is 'subscription_id_list'; value is list of str of int
# i.e. {'subscription-id-list': ['1234','5678',....]}
and you want some complete dict to contain the list of all values in the various responses. Then use list.extend:
complete_sub_id_list = []
for api_request in some_structure:
response = get_remote_results(api_request)
complete_sub_id_list.extend(response['subscription_id_list'])
As noted in a comment, if you're making API requests to a remote server or resource (or even something dependent on local I/O), you're likely going to want to use asynchronous calls; if you're not already familiar with what that means, perhaps aiohttp and/or asyncio will be quite helpful. See e.g. this Twilio guide for more info. TL;DR you could see 5-30x speedup as opposed to using synchronous calls.

Google Workspace Reports API - Customer Usage Metrics - Get All Gmail Parameters

I am using Google Workspace Reports API to pull customer usage metrics for GMail via Python client. I would like to pull all parameters, but would prefer to not list out each of them individually (ie: service.customerUsageReports().get(date=temp, parameters='gmail:num_1day_active_users,gmail:num_30day_active_users,gmail:num_emails_received').execute())
I have tried parameters='gmail' and parameters='gmail:*' to no avail.
Is there a shorthand for pulling all customer usage metrics related to GMail, or do you have to pass each parameter individually?
I don't think you can. But if you like to have automated and have different parameters in multiple calls, you can do these steps:
Store all possible parameters in an array.
Filter the array using regex.
Append all filtered elements into a string via comma
Pass the string as the parameter
Code:
import re
parameters = [
'accounts:num_30day_logins',
'accounts:num_7day_logins',
'accounts:num_1day_logins',
'accounts:num_disabled_accounts',
'accounts:apps_total_licenses',
'accounts:apps_used_licenses',
'accounts:num_users_2sv_enrolled',
'gmail:num_emails_received',
'gmail:num_inbound_spam_emails',
'gmail:num_inbound_non_spam_emails',
'gmail:num_inbound_delivered_emails',
'gmail:num_emails_sent',
'gmail:num_inbound_rejected_emails',
'gmail:num_inbound_rerouted_emails',
'gplus:num_video_calls',
'gplus:num_video_conferences',
'gplus:num_video_conferences_cfm',
'gplus:total_video_call_minutes',
'docs:num_docs']
separator = ','
r_gmail = re.compile("gmail.*")
r_accounts = re.compile("accounts.*")
gmail_list = list(filter(r_gmail.match, parameters))
accounts_list = list(filter(r_accounts.match, parameters))
gmail_parameters = separator.join(gmail_list)
accounts_parameters = separator.join(accounts_list)
# contains gmail parameters
print(gmail_parameters)
# contains accounts parameters
print(accounts_parameters)
Hopefully you get the idea of the above code. This will be helpful when your script gets large and lots of parameters come and go.
This way, you don't need to maintain numerous long lists of parameters. Just one and you only need to create a regex for it to be filtered the way you like it.
Output:

How to send multiple HTTP requests using Python

I'm trying to create a script that checks all possible top level domain combinations based on a given root domain name.
What I've done is generate a list of all possible TLD combinations and what I want to do is make multiple HTTP requests, analyze its results and determine whether a root domain name has multiple active top level domains or not.
Example:
A client of mine has this active domains:
domain.com
domain.com.ar
domain.ar
I've tried using grequests but get this error:
TypeError: 'AsyncRequest' object is not iterable
Code:
import grequests
responses = grequests.get([url for url in urls])
grequests.map(responses)
As I mentioned, you cannot put code as a parameter. What you want is to add a list and you can using an inline for loop, like this:
[url for url in urls]
It is called list comprehensions and more information about this can be found over here: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
So I just added two brackets:
import grequests
responses = (grequests.get(u) for u in urls)
grequests.map(responses)

List users in a group LDAP python

I'm new to LDAP. So I don't really know all my terms and fully understand all the terms yet. However, I'm working on an existing system and all the set up is done. I'm just adding a method to it.
I'm trying to write a method in Python using LDAP query. I've played around on LDAP Browser and can see that my query is correct. However, I'm not sure how to put it in a python method to return a list. The method needs to return a list of all the users' username. So far I have:
def getUsersInGroup(self, group):
searchQuery= //for privacy Im not going to share this
searchAttribute=["username"]
results = self.ldap.search_s(self.ldap_root, ldap.SCOP_SUBTREE,
searchQuery, searchAttribute)
I'm unsure how to go from here. I don't fully understand what the search_s method returns. I read online that its better to use search_s over search method because the while loop can be avoided. Could you please provide and example of where I can go from here. Thanks.
You need to perform a LDAP search something like:
# Find all Groups user is a member of:
import ldap
l = ldap.initialize("ldap://my_host")
l.simple_bind_s("[my_dn]", "[my_pass]")
myfilter = "(member=(CN=UserName,CN=Users,DC=EXAMPLE,DC=COM))"
# for all groups including Nested Groups (Only Microsoft Active Directory)
# (member:1.2.840.113556.1.4.1941:=CN=UserName,CN=Users,DC=EXAMPLE,DC=COM)
ldap_result = l.search("[BASE_DN]", ldap.SCOPE_SUBTREE, myfilter, None)
res_type, data = l.result(ldap_result, 0)
print(data)
You need to use the full dn of the user.

Google Directory API groups

I am trying to use Google's admin directory API (with Google's python library).
I am able to list the users on the directory just fine, using some code like this:
results = client.users().list(customer='my_customer').execute()
However, those results do not include which groups the users are a member of. Instead, it appears that once I have the list of users, I then have to make a call to get a list of groups:
results = client.groups().list(customer='my_customer').execute()
And then go through each group and call the "members" api to see which users are in a group:
results = client.members().list(groupKey='[group key]').execute()
Which means that I have to make a new request for every group.
It seems to be horribly inefficient. There has to be a better way than this, and I'm just missing it. What is it?
There is no better way than the one you described (yet?): Iterate over groups and get member list of each one.
I agree that is not the answer you want to read, but it is also the way we do maintenance over group members.
The following method should be more efficient, although not 100% great :
For each user, call the groups.list method and pass the userKey parameter to specify that you only want groups who have user X as a member.
I'm not a Python developper, but it should look like this :
results = client.groups().list(customer='my_customer',userKey='user#domain.com').execute()
For each user:
results = client.groups().list(userKey=user,pageToken=None).execute()
where 'user' is the user's primary/alias email address or ID
This will return a page of groups the user is a member of, see:
https://developers.google.com/admin-sdk/directory/v1/reference/groups/list

Categories

Resources