Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # requirements: pip install azure msrestazure
  4.  
  5. import os
  6.  
  7. '''
  8. AZURE_PUBLIC_CLOUD
  9. AZURE_CHINA_CLOUD
  10. AZURE_US_GOV_CLOUD
  11. AZURE_GERMAN_CLOUD
  12. '''
  13. from msrestazure.azure_cloud import AZURE_GERMAN_CLOUD as cloud_environment
  14.  
  15. from msrestazure.azure_active_directory import UserPassCredentials
  16. from msrestazure.azure_active_directory import ServicePrincipalCredentials
  17.  
  18. from azure.mgmt.resource import ResourceManagementClient
  19. from azure.mgmt.compute import ComputeManagementClient
  20.  
  21.  
  22. # Prefered way for Azure connection is the use of ServicePrincipalCredentials
  23. # in environment variables. When they are not existing, then we ask for
  24. # username and password
  25. if os.environ.has_key("AZURE_CLIENT_ID"):
  26. credentials = ServicePrincipalCredentials(
  27. client_id=os.environ['AZURE_CLIENT_ID'],
  28. secret=os.environ['AZURE_CLIENT_SECRET'],
  29. tenant=os.environ['AZURE_TENANT_ID'],
  30. cloud_environment=cloud_environment
  31. )
  32. else:
  33. credentials = UserPassCredentials(
  34. username=os.environ.get('AZURE_USER', raw_input('Username: ')),
  35. password=os.environ.get('AZURE_PASSWORD', raw_input('Password: ')), ,
  36. tenant=os.environ.get('AZURE_TENANT_ID', raw_input('Tenant (AD Identifier): ')), ,
  37. cloud_environment=cloud_environment
  38. )
  39.  
  40. subscription_id = os.environ.get('AZURE_SUBSCRIPTION', raw_input('Subscription: '))
  41.  
  42. client = ResourceManagementClient(
  43. credentials,
  44. subscription_id,
  45. base_url=cloud_environment.endpoints.resource_manager
  46. )
  47.  
  48. compute_client = ComputeManagementClient(
  49. credentials,
  50. subscription_id,
  51. base_url=cloud_environment.endpoints.resource_manager
  52. )
  53.  
  54. try:
  55. # List VMs in subscription
  56. print('\nList VMs in subscription')
  57. for vm in compute_client.virtual_machines.list_all():
  58. print("\tVM: {}".format(vm.name))
  59.  
  60. pass
  61. except Exception as e:
  62. print e
  63. raise
  64. finally:
  65. print ('done')
  66. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement