Created
March 9, 2020 21:21
-
-
Save clcollins/7582ca2a0185442dc2aa702d04f21a4c to your computer and use it in GitHub Desktop.
Identify accounts without legalEntities that are claimed and have a claimLink, but do not have a corresponding account claim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from subprocess import Popen, PIPE | |
import json | |
def get_data_from_hive(): | |
# Get a list of all accounts | |
acct_cmd = [ | |
"oc", | |
"get", | |
"accounts", | |
"-n", | |
"aws-account-operator", | |
"-o=json" | |
] | |
acct_data = Popen( | |
acct_cmd, | |
stdout=PIPE | |
).communicate()[0].decode('UTF-8') | |
# Get a list of all account claims | |
claim_cmd = [ | |
"oc", | |
"get", | |
"accountclaims", | |
"--all-namespaces", | |
"-o=json"] | |
acct_claim_data = Popen( | |
claim_cmd, | |
stdout=PIPE | |
).communicate()[0].decode('UTF-8') | |
return json.loads(acct_data), json.loads(acct_claim_data) | |
def claimed(account): | |
# Checks if the account is listed as claimed | |
if account.get('status', None) is not None: | |
return account['status'].get('claimed', False) | |
return False | |
def has_claim_link(account): | |
# Checks if the account has a claimLink | |
if account.get('spec', None) is not None: | |
return False if account['spec'].get('claimLink', None) is None else True | |
return False | |
def has_legal_entity(account): | |
# Checks if the account has a legalIdentity | |
if account.get('spec', None) is not None: | |
return False if account['spec'].get('legalEntity', None) is None else True | |
return False | |
def strip_owned_accounts(account_list, account_links): | |
# Accept a list of all accounts, and return a list of | |
# accounts that are claimed, have a claimLink, but do | |
# not have a legalEntity, and do not have a | |
# corresponding accountClaim | |
accounts = account_list['items'] | |
return [ i for i in accounts if | |
( | |
claimed(i) and | |
has_claim_link(i) and not | |
has_legal_entity(i) and | |
i['metadata']['name'] not in account_links) | |
] | |
# Get the account and account claim data | |
account_list, account_claim_list = get_data_from_hive() | |
# Make a list of all the account claim names | |
account_links = [i['spec'].get('accountLink', None) for i in account_claim_list['items']] | |
# Strip all the valid accounts and return un-owned accounts | |
unowned_accounts = strip_owned_accounts(account_list, account_links) | |
for i in unowned_accounts: | |
print(i['metadata']['name']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment