diff --git a/deletion/README.md b/deletion/README.md new file mode 100644 index 0000000..1d76a38 --- /dev/null +++ b/deletion/README.md @@ -0,0 +1,15 @@ +## Script is used to delete the resources based on the Resource log book excel sheet +------------------------------------------------------------------------------------------- +### To Run the Script you must have google project service principal credentials with google sheet API , to generate Credential follow this doc. + + +Identity Console - https://developers.google.com/identity/protocols/oauth2/service-account#python + +Developer console - https://console.cloud.google.com/projectselector2/apis/dashboard?pli=1&supportedpurview=project + +### sample credential.json file is attached in the repo + +To use Script +1. Generate service principal credential +2. Change google sheet key & enter your key in the script (line 15) +3. Run the script diff --git a/deletion/deletion.py b/deletion/deletion.py new file mode 100644 index 0000000..a4a74bd --- /dev/null +++ b/deletion/deletion.py @@ -0,0 +1,53 @@ +from __future__ import print_function +from importlib.resources import Resource +import pygsheets +from google.oauth2 import service_account +import json +from datetime import * +import datetime +import os + +# opening & authnetication of google sheet +with open('python-delete-42f8aae80dcc.json') as source: + info = json.load(source) +credentials = service_account.Credentials.from_service_account_info(info) +client = pygsheets.authorize(service_account_file='python-delete-42f8aae80dcc.json') +sheet = client.open_by_key('1oeOxeUvPftRFdVDVVfgW1ABD7Xp-I95OPG0J-1iUXTU') +wks = sheet.worksheet_by_title('test') +all_values = wks.get_all_values() + +#date +date=datetime.datetime.today().strftime('%m/%d/%Y') + +#getting required columns +fifth_column = wks.get_col(5) +fifth_list = [i for i in fifth_column if i] +fifth_list.remove('Required till (mm/dd/yyyy)') + +first_column = wks.get_col(1) +first_list = [i for i in first_column if i] +first_list.remove('Resource Details') + +second_column = wks.get_col(2) +second_list = [i for i in second_column if i] +second_list.remove('Azure Resource Group') + +#function performing deletion on the basis of rg_name +def delete(rg_name): + exit_status=os.system(f' az group delete -n {rg_name} -y') + if(exit_status==0): + print("Deleted RG"+" --> "+ rg_name) + else: + print("Command fail to execute with exit status -> %d" % exit_status) + + +# vmname= corresponding VM name +# vm_date= corresponding required till vm date +for (vm_name,rg_name,vm_date) in zip(first_list,second_list,fifth_list): + if(date>vm_date): + delete(rg_name) + else: + print("Resource group --->"+ rg_name + " is in required limits ") + + + diff --git a/deletion/python-delete-42f8aae80dcc.json b/deletion/python-delete-42f8aae80dcc.json new file mode 100644 index 0000000..db88857 --- /dev/null +++ b/deletion/python-delete-42f8aae80dcc.json @@ -0,0 +1,13 @@ +{ + "type": "service_account", + "project_id": "python-delete", + "private_key_id": "42f8aae80dccea6bd2d541aac22f598049c54238", + "private_key": "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAoIBAQDBFWN6JvG/igpU\nsZmlYIuOYlaL6TsVJEAdn1g3ghg7G85g1aGKliO3oeq1S4t9fhAoDZXQghRhXJnV\nYSUSwSXmZXLqwejZYDUO7gBilYjd4YuuGopUOlDjiE0QPmQP1Hgf1hfv3vCLlzC1\nf/6POIB5dm63asUwSFDKx13QVsMNI2UkCN0+ZTQEOvMTZs74TkkIcK5yjbHemP3W\nw32xZMLcgRmRQXyolvWQxYDV2b5uaZTpBscJdHY2mDXoDOXAZv2s0pqs4w1GMkQJ\niHiwNSSmt+L3jVtzcKIprwUeyPMGcip9vGbroE9nDLlzPprX8UItIezubTJ8kGir\n1iNT5CqzAgMBAAECggEARNijh8KrIGjQFjLdXiJ2oGZgBbM75eXvFqDii6hPi4P2\nqoCCATBXu/p3/fzZ1/1DNYTijl5TdgPWtkbVOUK1iNlTiHFr3VZ4lMI4UO6QxszN\nSV8dQ7snbHoFQG1+sEZKLs/f6R/NFXjrXUwuMFMtPCIGiFABz515cVR1FvxYwC3n\nKP9JCLfe+hikYsUOlQbqHeSfxWx8iTJ7MhIaJaeOgtY931/s9+fsbCzl45mV3pbL\nii92pWjmbm5DbisaV+Rjd/ETjtzP99LEoCg8CA5bLSNtHuaVVTFGSeXThkCtS5OI\nJAIs6HKhwEGybUmY+PvOHQzuJruULDSpaouoDmlyQQKBgQD4/x9JkDQajFNOVoh8\nqU5xGpMpn/NsykGdTJvmuN0Hr2cD9P8IfsENYPmax7UGXJCQck4eVpqz+JF8Cvcm\ny6Qfz+SBIFnxrHjaXzHKmrIuyn/1wUU45fjFO/Amav8iylM7QMz5E/+qazNbOH1e\nvwkCPuJC1wLjpo51GyNRsO2p3QKBgQDGg6tqwH+3jTSyX1bmqY9ov1d0D43kFgRr\nnsep/duO5CHgcDM+h2H/dFFEFrzvi3FQho0uCQHEyo0ls48fl2/r1pHzVotmOFMs\nopKrGd4SZJp/jCEtBzeVd2KGOkWGIaTlsK8XIuqW7fbJQCT9UWIrJnsjQlcrm0dj\nqsKQ2NiFzwKBgQCBTXMA75qvIqnsJ3GvUviCMau0VPTDTPDv76WSFfPqFsMnQeXi\nknhMK0zTfNvb1tvXtxQsGwsxZGaghM0930h2oK3oBFXQ86C6ve6MaPMY612x0FDT\nMFGUkwCt4qhld63rOkZ3HlSJOiwsNdSdDBVwjhlPXnqIn6ig1ZvswzVYHQKBgB2x\nllN6heAj3A0OW8ZW+J+Xd3Jh3TyP2otqjb0svsnJ8BNNVSz2PT9j28AOkqZWOlXs\nOnP3Xu9byO8pw6/bDB+/0ccPW/ynUjGsA7paPTCNIdsQT+oXWUYfPHqNftbIc4zz\n98zDKUn4QGDsGlxV7xQ7RlII21cne+85nK7j8MdbAoGBAJhxZrNbqC2hOBARrCas\nVK6j8qQEasE8U8DeUw2XYCxGfW8bm2tb76tBqcBvFNJApqq3h4yjcGaW+TluuMCW\nDi1DRbBL4KsUFv62FzixmzzE7ObCrojyvgAi7OGG7kAi1rvh38jYW2g+8VRUvQAn\nv2biW9ZIEutIK6LjLhxgFPks\n-----END PRIVATE KEY-----\n", + "client_email": "python-delete@python-delete.iam.gserviceaccount.com", + "client_id": "108802611706879214102", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/python-delete%40python-delete.iam.gserviceaccount.com" + } + \ No newline at end of file