Esempio n. 1
0
    def CreateAutoscalerResource(self, igm_ref, args):
        if _IsZonalGroup(igm_ref):
            scope_type = 'zone'
            location = managed_instance_groups_utils.CreateZoneRef(
                self.resources, igm_ref)
            zones, regions = [location], None
        else:
            scope_type = 'region'
            location = managed_instance_groups_utils.CreateRegionRef(
                self.resources, igm_ref)
            zones, regions = None, [location]

        autoscaler = managed_instance_groups_utils.AutoscalerForMig(
            mig_name=igm_ref.Name(),
            autoscalers=managed_instance_groups_utils.AutoscalersForLocations(
                regions=regions,
                zones=zones,
                compute=self.compute,
                http=self.http,
                batch_url=self.batch_url),
            location=location,
            scope_type=scope_type)
        autoscaler_name = getattr(autoscaler, 'name', None)
        new_one = autoscaler_name is None
        autoscaler_name = autoscaler_name or args.name

        if _IsZonalGroup(igm_ref):
            autoscaler_resource = managed_instance_groups_utils.BuildAutoscaler(
                args, self.messages, igm_ref, autoscaler_name)
        else:
            autoscaler_resource = managed_instance_groups_utils.BuildAutoscaler(
                args, self.messages, igm_ref, autoscaler_name)

        return autoscaler_resource, new_one
Esempio n. 2
0
    def _GenerateAutoscalerDeleteRequests(self, holder, project, mig_requests):
        """Generates Delete requestes for autoscalers attached to instance groups.

    Args:
      holder: ComputeApiHolder, object encapsulating compute api.
      project: str, project this request should apply to.
      mig_requests: Messages which will be sent to delete instance group
        managers.

    Returns:
      Messages, which will be sent to delete autoscalers.
    """
        mig_requests = zip(*mig_requests)[2] if mig_requests else []
        zone_migs = [(request.instanceGroupManager, 'zone',
                      managed_instance_groups_utils.CreateZoneRef(
                          holder.resources, request))
                     for request in mig_requests
                     if hasattr(request, 'zone') and request.zone is not None]
        region_migs = [
            (request.instanceGroupManager, 'region',
             managed_instance_groups_utils.CreateRegionRef(
                 holder.resources, request)) for request in mig_requests
            if hasattr(request, 'region') and request.region is not None
        ]

        zones = zip(*zone_migs)[2] if zone_migs else []
        regions = zip(*region_migs)[2] if region_migs else []

        client = holder.client.apitools_client
        messages = client.MESSAGES_MODULE
        autoscalers_to_delete = managed_instance_groups_utils.AutoscalersForMigs(
            migs=zone_migs + region_migs,
            autoscalers=managed_instance_groups_utils.AutoscalersForLocations(
                zones=zones,
                regions=regions,
                compute=client,
                http=client.http,
                batch_url=holder.client.batch_url))
        requests = []
        for autoscaler in autoscalers_to_delete:
            if autoscaler.zone:
                service = client.autoscalers
                request = messages.ComputeAutoscalersDeleteRequest(
                    zone=path_simplifier.Name(autoscaler.zone))
            else:
                service = client.regionAutoscalers
                request = messages.ComputeRegionAutoscalersDeleteRequest(
                    region=path_simplifier.Name(autoscaler.region))

            request.autoscaler = autoscaler.name
            request.project = project
            requests.append((service, 'Delete', request))
        return requests
    def GetAutoscalerResource(self, client, resources, igm_ref, args):
        if _IsZonalGroup(igm_ref):
            scope_type = 'zone'
            location = managed_instance_groups_utils.CreateZoneRef(
                resources, igm_ref)
            zones, regions = [location], None
        else:
            scope_type = 'region'
            location = managed_instance_groups_utils.CreateRegionRef(
                resources, igm_ref)
            zones, regions = None, [location]

        autoscaler = managed_instance_groups_utils.AutoscalerForMig(
            mig_name=args.name,
            autoscalers=managed_instance_groups_utils.AutoscalersForLocations(
                regions=regions, zones=zones, client=client),
            location=location,
            scope_type=scope_type)
        if autoscaler is None:
            raise managed_instance_groups_utils.ResourceNotFoundException(
                'The managed instance group is not autoscaled.')
        return autoscaler