add_azure_account.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import numpy as np
  2. import boto3
  3. import requests
  4. import sys
  5. import json
  6. import time
  7. """
  8. To run this you use python add_azure_account.py <CloudCheckrApiKey> <NameOfCloudCheckrAccount> <AzureDirectoryId> <AzureApplicationId> <AzureApplictionSecretKey> <AzureSubscriptionId>
  9. To run this are the following input parameters cloudcheckr-admin-api-key unique-account-name-in-cloudcheckr azure-active-directory-id azure-application-id azure-application-secret azure-subscription-id
  10. The CloudCheckr admin api key is a 64 character string.
  11. The CloudCheckr Account name is the name of the new account in CloudCheckr.
  12. The azure-active-directory-id is the GUID directory id. This will generally be the same for all subscriptions that have the same parent. (Parent being their associated CSP or EA account that contains cost data)
  13. The azure-application-id is the GUID id of the application that was created previously. It can be re-used for multiple subscriptions, but have to give the application permissions in each subscription.
  14. The azure-application-secret is the secret key that was created previously. This is shown only once when generating the key. It can last 1 year, 2 years, or forever.
  15. The azure-subscription-id is the GUID that corresponds to the id of the subscription. This subscription will be different for every account that is added to CloudCheckr.
  16. """
  17. def create_azure_account(env, admin_api_key, account_name, azure_ad_id, azure_app_id, azure_api_access_key, azure_subscription_id):
  18. """
  19. Creates an Azure Account in CloudCheckr. It will populate it with azure subscription credentials that were provided.
  20. """
  21. api_url = env + "/api/account.json/add_azure_inventory_account"
  22. add_azure_account_info = json.dumps({"account_name": account_name, "azure_ad_id": azure_ad_id, "azure_app_id": azure_app_id, "azure_api_access_key": azure_api_access_key, "azure_subscription_id": azure_subscription_id})
  23. r7 = requests.post(api_url, headers = {"Content-Type": "application/json", "access_key": admin_api_key}, data = add_azure_account_info)
  24. print(r7.json())
  25. def main():
  26. try:
  27. admin_api_key = str(sys.argv[1])
  28. except IndexError:
  29. print("Must include an admin api key in the command line")
  30. return
  31. try:
  32. account_name = str(sys.argv[2])
  33. except IndexError:
  34. print("Must include a cloudcheckr account name")
  35. return
  36. try:
  37. azure_ad_id = str(sys.argv[3])
  38. except IndexError:
  39. print("Must include an Azure Directory Id")
  40. return
  41. try:
  42. azure_app_id = str(sys.argv[4])
  43. except IndexError:
  44. print("Must include an Azure Application Id")
  45. return
  46. try:
  47. azure_api_access_key = str(sys.argv[5])
  48. except IndexError:
  49. print("Must include an Azure Api Access Key")
  50. return
  51. try:
  52. azure_subscription_id = str(sys.argv[6])
  53. except IndexError:
  54. print("Must include an Azure Subscription Id")
  55. return
  56. # can change this it eu.cloudcheckr.com or au.cloudcheckr.com for different environments
  57. env = "https://api.cloudcheckr.com"
  58. create_azure_account(env, admin_api_key, account_name, azure_ad_id, azure_app_id, azure_api_access_key, azure_subscription_id)
  59. if __name__ == "__main__":
  60. main()