def _SetAutoscalerFromFile(self, autoscaling_file, autoscalers_client,
                               igm_ref, existing_autoscaler_name):
        new_autoscaler = json.loads(files.ReadFileContents(autoscaling_file))
        if new_autoscaler is None:
            if existing_autoscaler_name is None:
                log.info(
                    'Configuration specifies no autoscaling and there is no '
                    'autoscaling configured. Nothing to do.')
                return
            else:
                console_io.PromptContinue(
                    message=_DELETE_AUTOSCALER_PROMPT,
                    cancel_on_no=True,
                    cancel_string=_DELETION_CANCEL_STRING)
                return autoscalers_client.Delete(igm_ref,
                                                 existing_autoscaler_name)

        new_autoscaler = encoding.DictToMessage(
            new_autoscaler, autoscalers_client.message_type)
        if existing_autoscaler_name is None:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                new_autoscaler, igm_ref)
            return autoscalers_client.Insert(igm_ref, new_autoscaler)

        if (getattr(new_autoscaler, 'name', None) and
                getattr(new_autoscaler, 'name') != existing_autoscaler_name):
            console_io.PromptContinue(message=_REPLACE_AUTOSCALER_PROMPT,
                                      cancel_on_no=True,
                                      cancel_string=_DELETION_CANCEL_STRING)
            autoscalers_client.Delete(igm_ref, existing_autoscaler_name)
            return autoscalers_client.Insert(igm_ref, new_autoscaler)

        new_autoscaler.name = existing_autoscaler_name
        return autoscalers_client.Update(igm_ref, new_autoscaler)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        managed_instance_groups_utils.ValidateAutoscalerArgs(args)
        managed_instance_groups_utils.ValidateStackdriverMetricsFlags(args)
        managed_instance_groups_utils.ValidateConflictsWithAutoscalingFile(
            args, (managed_instance_groups_utils.
                   ARGS_CONFLICTING_WITH_AUTOSCALING_FILE_ALPHA))
        igm_ref = self.CreateGroupReference(client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client, holder.resources, igm_ref, args)

        managed_instance_groups_utils.ValidateGeneratedAutoscalerIsValid(
            args, autoscaler_resource)

        if args.IsSpecified('autoscaling_file'):
            if is_new:
                existing_autoscaler_name = None
            else:
                existing_autoscaler_name = autoscaler_resource.name
            return self._SetAutoscalerFromFile(args.autoscaling_file, client,
                                               igm_ref,
                                               existing_autoscaler_name)

        if is_new:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource, igm_ref)
            return self._InsertAutoscaler(client, igm_ref, autoscaler_resource)
        return self._UpdateAutoscaler(client, igm_ref, autoscaler_resource)
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = instance_groups_flags.CreateGroupReference(
            client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        # Require confirmation if autoscaling a GKE node group.
        self._PromptToAutoscaleGKENodeGroup(args)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client, holder.resources, igm_ref, args)

        autoscalers_client = autoscalers_api.GetClient(client, igm_ref)
        if is_new:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource, igm_ref)
            return autoscalers_client.Insert(igm_ref, autoscaler_resource)
        return autoscalers_client.Update(igm_ref, autoscaler_resource)
Exemple #4
0
    def CreateRequests(self, args):
        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = self.CreateGroupReference(args)
        service = self.GetAutoscalerServiceForGroup(igm_ref)

        managed_instance_groups_utils.AssertInstanceGroupManagerExists(
            igm_ref, self.project, self.compute, self.http, self.batch_url)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            igm_ref, args)

        if is_new:
            method = 'Insert'
            request = service.GetRequestType(method)(project=self.project)
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource)
            request.autoscaler = autoscaler_resource
        else:
            method = 'Update'
            request = service.GetRequestType(method)(project=self.project)
            request.autoscaler = autoscaler_resource.name
            request.autoscalerResource = autoscaler_resource

        self.ScopeRequest(request, igm_ref)
        return ((service, method, request), )
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = self.CreateGroupReference(client, holder.resources, args)
        service = self.GetAutoscalerServiceForGroup(client, igm_ref)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client, holder.resources, igm_ref, args)

        if is_new:
            method = 'Insert'
            request = service.GetRequestType(method)(project=igm_ref.project)
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource)
            request.autoscaler = autoscaler_resource
        else:
            method = 'Update'
            request = service.GetRequestType(method)(project=igm_ref.project)
            request.autoscaler = autoscaler_resource.name
            request.autoscalerResource = autoscaler_resource

        self.ScopeRequest(request, igm_ref)
        return client.MakeRequests([(service, method, request)])
    def _SetAutoscalerFromFile(self, autoscaling_file, client, igm_ref,
                               existing_autoscaler_name):
        with open(autoscaling_file) as f:
            new_autoscaler = json.load(f)
        if new_autoscaler is None:
            if existing_autoscaler_name is None:
                log.info(
                    'Configuration specifies no autoscaling and there is no '
                    'autoscaling configured. Nothing to do.')
                return
            else:
                return self._PromptToDeleteAutoscaler(
                    client,
                    igm_ref,
                    existing_autoscaler_name,
                    prompt_message=(
                        'Configuation specifies no autoscaling configuration. '
                        'Continuing will delete the existing autoscaler '
                        'configuration. Do you want to continue?'))

        if igm_ref.Collection() == 'compute.instanceGroupManagers':
            new_autoscaler = encoding.DictToMessage(new_autoscaler,
                                                    client.messages.Autoscaler)
        else:
            new_autoscaler = encoding.DictToMessage(
                new_autoscaler, client.messages.RegionAutoscaler)
        if existing_autoscaler_name is None:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                new_autoscaler, igm_ref)
            return self._InsertAutoscaler(client, igm_ref, new_autoscaler)

        if (getattr(new_autoscaler, 'name', None) and
                getattr(new_autoscaler, 'name') != existing_autoscaler_name):
            self._PromptToDeleteAutoscaler(
                client,
                igm_ref,
                existing_autoscaler_name,
                prompt_message=
                ('Configuation specifies autoscaling configuration with a '
                 'different name than existing. Continuing will delete '
                 'existing autoscaler and create new one with a different name. '
                 'Do you want to continue?'))
            return self._InsertAutoscaler(client, igm_ref, new_autoscaler)

        new_autoscaler.name = existing_autoscaler_name
        return self._UpdateAutoscaler(client, igm_ref, new_autoscaler)
    def CreateRequests(self, args):
        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = self.CreateZonalReference(
            args.name, args.zone, resource_type='instanceGroupManagers')
        # We need the zone name, which might have been passed after prompting.
        # In that case, we get it from the reference.
        zone = args.zone or igm_ref.zone

        managed_instance_groups_utils.AssertInstanceGroupManagerExists(
            igm_ref, self.project, self.messages, self.compute, self.http,
            self.batch_url)

        autoscaler = managed_instance_groups_utils.AutoscalerForMig(
            mig_name=args.name,
            autoscalers=managed_instance_groups_utils.AutoscalersForZones(
                zones=[zone],
                project=self.project,
                compute=self.compute,
                http=self.http,
                batch_url=self.batch_url),
            zone=zone,
            project=self.project)
        autoscaler_name = getattr(autoscaler, 'name', None)
        as_ref = self.CreateZonalReference(autoscaler_name or args.name, zone)
        autoscaler_resource = managed_instance_groups_utils.BuildAutoscaler(
            args, self.messages, as_ref, igm_ref)

        if autoscaler_name is None:
            self._method = 'Insert'
            request = self.messages.ComputeAutoscalersInsertRequest(
                project=self.project)
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource)
            request.autoscaler = autoscaler_resource
        else:
            self._method = 'Update'
            request = self.messages.ComputeAutoscalersUpdateRequest(
                project=self.project)
            request.autoscaler = as_ref.Name()
            request.autoscalerResource = autoscaler_resource

        request.zone = as_ref.zone
        return (request, )
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        managed_instance_groups_utils.ValidateAutoscalerArgs(args)

        igm_ref = self.CreateGroupReference(client, holder.resources, args)

        # Assert that Instance Group Manager exists.
        managed_instance_groups_utils.GetInstanceGroupManagerOrThrow(
            igm_ref, client)

        autoscaler_resource, is_new = self.CreateAutoscalerResource(
            client, holder.resources, igm_ref, args)

        if is_new:
            managed_instance_groups_utils.AdjustAutoscalerNameForCreation(
                autoscaler_resource, igm_ref)
            return self._InsertAutoscaler(client, igm_ref, autoscaler_resource)
        return self._UpdateAutoscaler(client, igm_ref, autoscaler_resource)