Beispiel #1
0
    def get_instances(self, filters=None, orderBy=None):
        """
        Get GCP instances
        Only sorting by "name" or "creationTimestamp desc" is supported
        Specifying both a list filter and sort order is not currently supported
        """
        if filters is not None and orderBy is not None:
            raise FatalError(("Specifying both a list filter and"
                              "sort order is not currently supported"),
                             ValueError)

        instances = []
        requests = {}
        responses = {}

        def callback(request_id, response, exception):
            if exception is not None:
                # Handle some GCP errors with problematic zones
                if isinstance(exception, HttpError):
                    # pylint: disable=protected-access
                    reason = exception._get_reason()
                    if reason.startswith("Invalid value for field 'zone'"):
                        WarningError("GCP", exception)
                        return
                FatalError("GCP", exception)
            if 'items' in response:
                instances.extend(response['items'])
                if 'nextPageToken' in response:
                    retry_zones.append(request_id)
                    responses[request_id] = response

        retry_zones = self.get_zones()
        # To support pagination on batch HTTP requests
        # we save the request & response of each zone
        while retry_zones:
            batch = self._compute.new_batch_http_request()
            for zone in retry_zones:
                if zone not in requests:
                    # Uncomment maxResults=1 to test pagination
                    requests[zone] = self._compute.instances().list(
                        project=self._project,
                        zone=zone,
                        filter=filters,
                        orderBy=orderBy)  # , maxResults=1)
                batch.add(requests[zone], callback=callback, request_id=zone)
            retry_zones.clear()
            try:
                batch.execute()
            except (GoogleAuthError, GoogleError) as exc:
                FatalError("GCP", exc)
            for zone in retry_zones:
                requests[zone] = self._compute.instances().list_next(
                    requests[zone], responses[zone])
        self._cache = instances
        return instances
Beispiel #2
0
 def __init__(self, project=None):
     try:
         self._client = resource_manager.Client()
     except (GoogleAuthError, GoogleAPIError) as exc:
         FatalError("GCP", exc)
     try:
         self._compute = build('compute', 'v1')
     except (GoogleAuthError, GoogleError) as exc:
         FatalError("GCP", exc)
     self._project = project or get_project()
     self._cache = None
 def get_instance_type(self, flavor_id):
     """
     Return instance type
     """
     try:
         return self._client.get_flavor_by_id(flavor_id).name
     except OpenStackCloudException as exc:
         raise FatalError("Openstack", exc)
Beispiel #4
0
 def get_projects(self):
     """
     Returns a list of projects
     """
     try:
         return [_.project_id for _ in self._client.list_projects()]
     except (GoogleAuthError, GoogleAPIError) as exc:
         FatalError("GCP", exc)
Beispiel #5
0
 def __init__(self, api_version=None):
     credentials, subscription_id = get_credentials()
     try:
         self._client = ComputeManagementClient(
             credential=credentials,
             subscription_id=subscription_id,
             api_version=api_version)
     except (AzureError, CloudError, RequestException) as exc:
         FatalError("Azure", exc)
     self._cache = None
 def __init__(self, cloud=None, insecure=False):
     if insecure:
         # https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
         import logging
         logging.captureWarnings(True)
     try:
         self._client = openstack.connect(cloud=cloud, insecure=insecure)
     except OpenStackCloudException as exc:
         raise FatalError("Openstack", exc)
     self._cache = None
Beispiel #7
0
def get_project():
    """
    Get the project from the GCP credentials JSON file
    """
    try:
        with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS']) as file:
            data = json.loads(file.read())
        return data['project_id']
    except (KeyError, OSError) as exc:
        FatalError("GCP", exc)
Beispiel #8
0
def get_credentials():
    """
    Get credentials for Azure
    """
    try:
        subscription_id = os.getenv('AZURE_SUBSCRIPTION_ID',
                                    os.getenv('ARM_SUBSCRIPTION_ID'))
        credentials = DefaultAzureCredential()
        return credentials, subscription_id
    except (KeyError, AzureError, CloudError, RequestException) as exc:
        FatalError("Azure", exc)
    return None
 def get_instances(self, filters=None):
     """
     Get Openstack instances
     """
     filters = filters or {}
     try:
         # https://developer.openstack.org/api-ref/compute/#list-servers
         instances = list(self._client.list_servers(filters=filters))
     except OpenStackCloudException as exc:
         raise FatalError("Openstack", exc)
     self._get_instance_types(instances)
     self._cache = instances
     return instances
Beispiel #10
0
 def get_instance(self, instance_id):
     """
     Return specific instance
     """
     if self._cache is None:
         try:
             return self._client.get_server_by_id(instance_id)
         except OpenStackCloudException as exc:
             raise FatalError("Openstack", exc)
     else:
         for instance in self._cache:
             if instance['id'] == instance_id:
                 return instance
     return None
Beispiel #11
0
 def _get_instance_view(self, instance):
     """
     Get instance view for more information
     """
     resource_group = re.search(r"/resourceGroups/([^/]+)/",
                                instance.id).group(1)
     # https://github.com/Azure/azure-sdk-for-python/issues/573
     try:
         instance_view = self._client.virtual_machines.instance_view(
             resource_group, instance.name)
     except (AzureError, CloudError, RequestException) as exc:
         FatalError("Azure", exc)
     instance.instance_view = instance_view
     return instance.as_dict()
Beispiel #12
0
 def callback(request_id, response, exception):
     if exception is not None:
         # Handle some GCP errors with problematic zones
         if isinstance(exception, HttpError):
             # pylint: disable=protected-access
             reason = exception._get_reason()
             if reason.startswith("Invalid value for field 'zone'"):
                 WarningError("GCP", exception)
                 return
         FatalError("GCP", exception)
     if 'items' in response:
         instances.extend(response['items'])
         if 'nextPageToken' in response:
             retry_zones.append(request_id)
             responses[request_id] = response
Beispiel #13
0
 def get_instances(self, filters=None):
     """
     Get Azure Compute instances
     """
     try:
         instances = self._client.virtual_machines.list_all()
     except (AzureError, CloudError, RequestException) as exc:
         FatalError("Azure", exc)
     # https://github.com/Azure/azure-sdk-for-python/issues/573
     instances = self._get_instance_views(instances)
     if filters is not None:
         instances = filter(filters.search, instances)
     instances = list(instances)
     self._cache = instances
     return instances
Beispiel #14
0
 def get_zones(self, project=None, filters="status: UP"):
     """
     Returns a list of available zones
     """
     project = project or self._project
     items = []
     request = self._compute.zones().list(project=project, filter=filters)
     while request is not None:
         try:
             response = request.execute()
         except (GoogleAuthError, GoogleError) as exc:
             FatalError("GCP", exc)
         if 'items' in response:
             items.extend(_['name'] for _ in response['items'])
         request = self._compute.zones().list_next(request, response)
     return items
Beispiel #15
0
 def get_instance(self, instance_id, name=None, zone=None):
     """
     Get specific instance
     """
     if self._cache is None:
         if name is None or zone is None:
             self.get_instances()
         else:
             try:
                 request = self._compute.instances().get(
                     project=self._project, zone=zone, instance=name)
                 return request.execute()
             except (GoogleAuthError, GoogleError) as exc:
                 FatalError("GCP", exc)
     for instance in self._cache:
         if instance['id'] == instance_id:
             return instance
     return None
Beispiel #16
0
 def get_instances(self, filters=None, jmespath_filter=None):
     """
     Get EC2 instances
     """
     filters = filters or []
     instances = []
     try:
         pages = self._client.get_paginator('describe_instances').paginate(
             Filters=filters)
     except (BotoCoreError, ClientError) as exc:
         raise FatalError("AWS", exc)
     if jmespath_filter is not None:
         pages = pages.search(jmespath_filter)
     for page in pages:
         for item in page['Reservations']:
             instances.extend(item['Instances'])
     self._cache = instances
     return instances
Beispiel #17
0
 def get_instance(self, instance_id, name=None, resource_group=None):
     """
     Return specific instance
     """
     if self._cache is None:
         if name is None or resource_group is None:
             self.get_instances()
         else:
             try:
                 return self._client.virtual_machines.get(
                     resource_group_name=resource_group,
                     vm_name=name,
                     expand="instanceView").as_dict()
             except (AzureError, CloudError, RequestException) as exc:
                 FatalError("Azure", exc)
     for instance in self._cache:
         if instance['vm_id'] == instance_id:
             return instance
     return None
Beispiel #18
0
 def __init__(self):
     try:
         self._client = client('ec2')
     except (BotoCoreError, ClientError) as exc:
         raise FatalError("AWS", exc)
     self._cache = None