azure_rm_2.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. def __init__(self, args):
  2. self._args = args
  3. self._cloud_environment = None
  4. self._compute_client = None
  5. self._resource_client = None
  6. self._network_client = None
  7. self.debug = False
  8. if args.debug:
  9. self.debug = True
  10. self.credentials = self._get_credentials(args)
  11. if not self.credentials:
  12. self.fail("Failed to get credentials. Either pass as parameters, set environment variables, "
  13. "or define a profile in ~/.azure/credentials.")
  14. # if cloud_environment specified, look up/build Cloud object
  15. raw_cloud_env = self.credentials.get('cloud_environment')
  16. if not raw_cloud_env:
  17. self._cloud_environment = azure_cloud.AZURE_PUBLIC_CLOUD # SDK default
  18. else:
  19. # try to look up "well-known" values via the name attribute on azure_cloud members
  20. all_clouds = [x[1] for x in inspect.getmembers(azure_cloud) if isinstance(x[1], azure_cloud.Cloud)]
  21. matched_clouds = [x for x in all_clouds if x.name == raw_cloud_env]
  22. if len(matched_clouds) == 1:
  23. self._cloud_environment = matched_clouds[0]
  24. elif len(matched_clouds) > 1:
  25. self.fail("Azure SDK failure: more than one cloud matched for cloud_environment name '{0}'".format(raw_cloud_env))
  26. else:
  27. if not urlparse.urlparse(raw_cloud_env).scheme:
  28. self.fail("cloud_environment must be an endpoint discovery URL or one of {0}".format([x.name for x in all_clouds]))
  29. try:
  30. self._cloud_environment = azure_cloud.get_cloud_from_metadata_endpoint(raw_cloud_env)
  31. except Exception as e:
  32. self.fail("cloud_environment {0} could not be resolved: {1}".format(raw_cloud_env, e.message))
  33. if self.credentials.get('subscription_id', None) is None:
  34. self.fail("Credentials did not include a subscription_id value.")
  35. self.log("setting subscription_id")
  36. self.subscription_id = self.credentials['subscription_id']
  37. if self.credentials.get('client_id') is not None and \
  38. self.credentials.get('secret') is not None and \
  39. self.credentials.get('tenant') is not None:
  40. self.azure_credentials = ServicePrincipalCredentials(client_id=self.credentials['client_id'],
  41. secret=self.credentials['secret'],
  42. tenant=self.credentials['tenant'],
  43. cloud_environment=self._cloud_environment)
  44. elif self.credentials.get('ad_user') is not None and self.credentials.get('password') is not None:
  45. tenant = self.credentials.get('tenant')
  46. if not tenant:
  47. tenant = 'common'
  48. self.azure_credentials = UserPassCredentials(self.credentials['ad_user'],
  49. self.credentials['password'],
  50. tenant=tenant,
  51. cloud_environment=self._cloud_environment)
  52. else:
  53. self.fail("Failed to authenticate with provided credentials. Some attributes were missing. "
  54. "Credentials must include client_id, secret and tenant or ad_user and password.")