azure_clients.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import json
  2. from azure.identity import ClientSecretCredential
  3. from azure.common.credentials import ServicePrincipalCredentials
  4. from azure.mgmt.compute import ComputeManagementClient
  5. from azure.mgmt.network import NetworkManagementClient
  6. from azure.mgmt.storage import StorageManagementClient
  7. from azure.mgmt.privatedns import PrivateDnsManagementClient
  8. from azure.mgmt.dns import DnsManagementClient
  9. from azure.storage.blob import BlobServiceClient
  10. from azure.storage.queue import QueueServiceClient
  11. from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient
  12. from azure.mgmt.resource import ResourceManagementClient
  13. ############ Resource Management Client ########
  14. def get_resourcegroup_client(parameters):
  15. tenant_id = parameters.get('azure_tenant_id')
  16. client_id = parameters.get('azure_client_id')
  17. secret = parameters.get('azure_client_secret')
  18. subscription_id = parameters.get('azure_subscription_id')
  19. token_credential = ClientSecretCredential(
  20. tenant_id, client_id, secret)
  21. resourcegroup_client = ResourceManagementClient(token_credential, subscription_id)
  22. return resourcegroup_client
  23. ########### Resource Management Client End #####
  24. def get_compute_client(parameters):
  25. tenant_id = parameters.get('azure_tenant_id')
  26. client_id = parameters.get('azure_client_id')
  27. secret = parameters.get('azure_client_secret')
  28. subscription_id = parameters.get('azure_subscription_id')
  29. token_credential = ClientSecretCredential(
  30. tenant_id, client_id, secret)
  31. compute_client = ComputeManagementClient(token_credential,
  32. subscription_id)
  33. return compute_client
  34. def get_network_client(parameters):
  35. tenant_id = parameters.get('azure_tenant_id')
  36. client_id = parameters.get('azure_client_id')
  37. secret = parameters.get('azure_client_secret')
  38. subscription_id = parameters.get('azure_subscription_id')
  39. token_credential = ClientSecretCredential(
  40. tenant_id, client_id, secret)
  41. network_client = NetworkManagementClient(token_credential,
  42. subscription_id)
  43. return network_client
  44. def get_dns_client(parameters):
  45. tenant_id = parameters.get('azure_tenant_id')
  46. client_id = parameters.get('azure_client_id')
  47. secret = parameters.get('azure_client_secret')
  48. subscription_id = parameters.get('azure_subscription_id')
  49. token_credential = ClientSecretCredential(
  50. tenant_id, client_id, secret)
  51. dns_client = PrivateDnsManagementClient(token_credential,
  52. subscription_id)
  53. return dns_client
  54. def get_dns_ops_client(parameters):
  55. tenant_id = parameters.get('azure_tenant_id')
  56. client_id = parameters.get('azure_client_id')
  57. secret = parameters.get('azure_client_secret')
  58. subscription_id = parameters.get('azure_subscription_id')
  59. token_credential = ClientSecretCredential(
  60. tenant_id, client_id, secret)
  61. dns_ops_client = DnsManagementClient(token_credential,
  62. subscription_id)
  63. return dns_ops_client
  64. def get_blob_service_client(parameters):
  65. tenant_id = parameters.get('azure_tenant_id')
  66. client_id = parameters.get('azure_client_id')
  67. secret = parameters.get('azure_client_secret')
  68. account_name = parameters.get('storage_account_name')
  69. token_credential = ClientSecretCredential(
  70. tenant_id, client_id, secret)
  71. blob_service_client = BlobServiceClient(
  72. account_url="https://%s.blob.core.windows.net" % account_name,
  73. credential=token_credential)
  74. return blob_service_client
  75. def get_queue_service_client(parameters):
  76. tenant_id = parameters.get('azure_tenant_id')
  77. client_id = parameters.get('azure_client_id')
  78. secret = parameters.get('azure_client_secret')
  79. account_name = parameters.get('storage_account_name')
  80. token_credential = ClientSecretCredential(
  81. tenant_id, client_id, secret)
  82. queue_service_client = QueueServiceClient(
  83. account_url="https://%s.queue.core.windows.net" % account_name,
  84. credential=token_credential)
  85. return queue_service_client
  86. def get_datalake_client(parameters):
  87. tenant_id = parameters.get('azure_tenant_id')
  88. client_id = parameters.get('azure_client_id')
  89. secret = parameters.get('azure_client_secret')
  90. subscription_id = parameters.get('azure_subscription_id')
  91. credentials = ServicePrincipalCredentials(
  92. client_id=client_id,
  93. secret=secret,
  94. tenant=tenant_id)
  95. datalake_client = DataLakeStoreAccountManagementClient(credentials,
  96. subscription_id)
  97. return datalake_client
  98. def get_storage_client(parameters):
  99. tenant_id = parameters.get('azure_tenant_id')
  100. client_id = parameters.get('azure_client_id')
  101. secret = parameters.get('azure_client_secret')
  102. subscription_id = parameters.get('azure_subscription_id')
  103. token_credential = ClientSecretCredential(
  104. tenant_id, client_id, secret)
  105. storage_client = StorageManagementClient(token_credential,
  106. subscription_id)
  107. return storage_client