def _ComputeInstanceGroupSize(items, client, resources):
    """Add information about Instance Group size."""
    errors = []
    zone_refs = [
        resources.Parse(
            mig['zone'],
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.zones') for mig in items if 'zone' in mig
    ]
    region_refs = [
        resources.Parse(
            mig['region'],
            params={'project': properties.VALUES.core.project.GetOrFail},
            collection='compute.regions') for mig in items if 'region' in mig
    ]

    zonal_instance_groups = []
    for project, zone_refs in GroupByProject(zone_refs).iteritems():
        zonal_instance_groups.extend(
            lister.GetZonalResources(
                service=client.apitools_client.instanceGroups,
                project=project,
                requested_zones=set([zone.zone for zone in zone_refs]),
                filter_expr=None,
                http=client.apitools_client.http,
                batch_url=client.batch_url,
                errors=errors))

    regional_instance_groups = []
    if getattr(client.apitools_client, 'regionInstanceGroups', None):
        for project, region_refs in GroupByProject(region_refs).iteritems():
            regional_instance_groups.extend(
                lister.GetRegionalResources(
                    service=client.apitools_client.regionInstanceGroups,
                    project=project,
                    requested_regions=set(
                        [region.region for region in region_refs]),
                    filter_expr=None,
                    http=client.apitools_client.http,
                    batch_url=client.batch_url,
                    errors=errors))

    instance_groups = zonal_instance_groups + regional_instance_groups
    instance_group_uri_to_size = {
        ig.selfLink: ig.size
        for ig in instance_groups
    }

    if errors:
        utils.RaiseToolException(errors)

    for item in items:
        self_link = item['selfLink']
        gm_self_link = self_link.replace('/instanceGroupManagers/',
                                         '/instanceGroups/')

        item['size'] = str(instance_group_uri_to_size.get(gm_self_link, ''))
        yield item
示例#2
0
 def GetZonalResources(self, **kwargs):
   args = dict(
       service=self.compute.instances,
       project='my-project',
       requested_zones=[],
       filter_expr=None,
       http=self.mock_http,
       batch_url=self.batch_url,
       errors=[])
   args.update(kwargs)
   return lister.GetZonalResources(**args)
示例#3
0
 def GetResources(self, args, errors):
     zone_names = [
         self.CreateGlobalReference(zone, resource_type='zones').Name()
         for zone in args.zones
     ]
     return lister.GetZonalResources(service=self.service,
                                     project=self.project,
                                     requested_zones=zone_names,
                                     filter_expr=self.GetFilterExpr(args),
                                     http=self.http,
                                     batch_url=self.batch_url,
                                     errors=errors)
示例#4
0
    def GetInstances(self):
        """Returns a generator of all instances in the project."""
        compute = self.compute
        errors = []
        instances = lister.GetZonalResources(service=compute.instances,
                                             project=self.project,
                                             requested_zones=None,
                                             filter_expr=None,
                                             http=self.http,
                                             batch_url=self.batch_url,
                                             errors=errors)

        if errors:
            base_classes.RaiseToolException(
                errors, error_message='Could not fetch all instances:')
        return instances
示例#5
0
    def GetRunningInstances(self, client):
        """Returns a generator of all running instances in the project."""
        errors = []
        instances = lister.GetZonalResources(
            service=client.apitools_client.instances,
            project=properties.VALUES.core.project.GetOrFail(),
            requested_zones=None,
            filter_expr='status eq RUNNING',
            http=client.apitools_client.http,
            batch_url=client.batch_url,
            errors=errors)

        if errors:
            utils.RaiseToolException(
                errors, error_message='Could not fetch all instances:')
        return instances
示例#6
0
  def ObtainInstances(cls, names, **kwargs):
    """Returns a list of instances according to the flags."""
    errors = []
    result = lister.GetZonalResources(
        service=kwargs['service'],
        project=kwargs['project'],
        requested_zones=kwargs['zones'],
        filter_expr=kwargs['filters'],
        http=kwargs['http'],
        batch_url=kwargs['batch_url'],
        errors=errors)
    instances = list(result)

    # We filter them according to the names
    filtered_instances = []
    if not names:
      filtered_instances = instances
    else:
      for name in names:
        # First compare by name
        name_match = None
        in_name = None
        in_self_link = None

        for instance in instances:
          if name == instance.name:
            # Exact name match has a priority
            # over loose match on instance name or selfLink
            name_match = instance
            break
          elif name in instance.name:
            in_name = instance
          elif name in instance.selfLink:
            in_self_link = instance

        if name_match:
          filtered_instances.append(name_match)
        elif in_name:
          filtered_instances.append(in_name)
        elif in_self_link:
          filtered_instances.append(in_self_link)

    return filtered_instances
def ComputeInstanceGroupManagerMembership(
    compute,
    resources,
    http,
    batch_url,
    items,
    filter_mode=(InstanceGroupFilteringMode.ALL_GROUPS)):
    """Add information if instance group is managed.

  Args:
    compute: GCE Compute API client,
    resources: resource registry,
    http: http client,
    batch_url: str, batch url
    items: list of instance group messages,
    filter_mode: InstanceGroupFilteringMode, managed/unmanaged filtering options
  Returns:
    list of instance groups with computed dynamic properties
  """
    errors = []
    items = list(items)
    zone_links = set([ig['zone'] for ig in items if 'zone' in ig])

    project_to_zones = {}
    for zone in zone_links:
        zone_ref = resources.Parse(
            zone,
            params={
                'project': properties.VALUES.core.project.GetOrFail,
            },
            collection='compute.zones')
        if zone_ref.project not in project_to_zones:
            project_to_zones[zone_ref.project] = set()
        project_to_zones[zone_ref.project].add(zone_ref.zone)

    zonal_instance_group_managers = []
    for project, zones in project_to_zones.iteritems():
        zonal_instance_group_managers.extend(
            lister.GetZonalResources(service=compute.instanceGroupManagers,
                                     project=project,
                                     requested_zones=zones,
                                     filter_expr=None,
                                     http=http,
                                     batch_url=batch_url,
                                     errors=errors))

    regional_instance_group_managers = []
    if hasattr(compute, 'regionInstanceGroups'):
        # regional instance groups are just in 'alpha' API
        region_links = set([ig['region'] for ig in items if 'region' in ig])
        project_to_regions = {}
        for region in region_links:
            region_ref = resources.Parse(region, collection='compute.regions')
            if region_ref.project not in project_to_regions:
                project_to_regions[region_ref.project] = set()
            project_to_regions[region_ref.project].add(region_ref.region)
        for project, regions in project_to_regions.iteritems():
            regional_instance_group_managers.extend(
                lister.GetRegionalResources(
                    service=compute.regionInstanceGroupManagers,
                    project=project,
                    requested_regions=regions,
                    filter_expr=None,
                    http=http,
                    batch_url=batch_url,
                    errors=errors))

    instance_group_managers = (list(zonal_instance_group_managers) +
                               list(regional_instance_group_managers))
    instance_group_managers_refs = set([
        path_simplifier.ScopedSuffix(igm.selfLink)
        for igm in instance_group_managers
    ])

    if errors:
        utils.RaiseToolException(errors)

    results = []
    for item in items:
        self_link = item['selfLink']
        igm_self_link = self_link.replace('/instanceGroups/',
                                          '/instanceGroupManagers/')
        scoped_suffix = path_simplifier.ScopedSuffix(igm_self_link)
        is_managed = scoped_suffix in instance_group_managers_refs

        if (is_managed and filter_mode
                == InstanceGroupFilteringMode.ONLY_UNMANAGED_GROUPS):
            continue
        elif (not is_managed and filter_mode
              == InstanceGroupFilteringMode.ONLY_MANAGED_GROUPS):
            continue

        item['isManaged'] = ('Yes' if is_managed else 'No')
        if is_managed:
            item['instanceGroupManagerUri'] = igm_self_link
        results.append(item)

    return results
def ComputeInstanceGroupManagerMembership(
    compute,
    project,
    http,
    batch_url,
    items,
    filter_mode=(InstanceGroupFilteringMode.ALL_GROUPS)):
    """Add information if instance group is managed.

  Args:
    compute: GCE Compute API client,
    project: str, project name
    http: http client,
    batch_url: str, batch url
    items: list of instance group messages,
    filter_mode: InstanceGroupFilteringMode, managed/unmanaged filtering options
  Returns:
    list of instance groups with computed dynamic properties
  """
    errors = []
    items = list(items)
    zone_names = set(
        [path_simplifier.Name(ig['zone']) for ig in items if 'zone' in ig])
    region_names = set(
        [path_simplifier.Name(ig['region']) for ig in items if 'region' in ig])

    if zone_names:
        zonal_instance_group_managers = lister.GetZonalResources(
            service=compute.instanceGroupManagers,
            project=project,
            requested_zones=zone_names,
            filter_expr=None,
            http=http,
            batch_url=batch_url,
            errors=errors)
    else:
        zonal_instance_group_managers = []

    if region_names and hasattr(compute, 'regionInstanceGroups'):
        # regional instance groups are just in 'alpha' API
        regional_instance_group_managers = lister.GetRegionalResources(
            service=compute.regionInstanceGroupManagers,
            project=project,
            requested_regions=region_names,
            filter_expr=None,
            http=http,
            batch_url=batch_url,
            errors=errors)
    else:
        regional_instance_group_managers = []

    instance_group_managers = (list(zonal_instance_group_managers) +
                               list(regional_instance_group_managers))
    instance_group_managers_refs = set([
        path_simplifier.ScopedSuffix(igm.selfLink)
        for igm in instance_group_managers
    ])

    if errors:
        utils.RaiseToolException(errors)

    results = []
    for item in items:
        self_link = item['selfLink']
        igm_self_link = self_link.replace('/instanceGroups/',
                                          '/instanceGroupManagers/')
        scoped_suffix = path_simplifier.ScopedSuffix(igm_self_link)
        is_managed = scoped_suffix in instance_group_managers_refs

        if (is_managed and filter_mode
                == InstanceGroupFilteringMode.ONLY_UNMANAGED_GROUPS):
            continue
        elif (not is_managed and filter_mode
              == InstanceGroupFilteringMode.ONLY_MANAGED_GROUPS):
            continue

        item['isManaged'] = ('Yes' if is_managed else 'No')
        if is_managed:
            item['instanceGroupManagerUri'] = igm_self_link
        results.append(item)

    return results