1# -*- coding: utf-8 -*- #
2# Copyright 2017 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""services disable command."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.api_lib.services import services_util
23from googlecloudsdk.api_lib.services import serviceusage
24from googlecloudsdk.calliope import base
25from googlecloudsdk.command_lib.services import arg_parsers
26from googlecloudsdk.command_lib.services import common_flags
27from googlecloudsdk.core import log
28from googlecloudsdk.core import properties
29from googlecloudsdk.core.console import console_io
30
31
32OP_BASE_CMD = 'gcloud beta services operations '
33OP_WAIT_CMD = OP_BASE_CMD + 'wait {0}'
34
35
36class Disable(base.SilentCommand):
37  """Disable a service for consumption for a project.
38
39     This command disables one or more previously-enabled services for
40     consumption.
41
42     To see a list of the enabled services for a project, run:
43
44       $ {parent_command} list
45
46     More information on listing services can be found at:
47     https://cloud.google.com/service-usage/docs/list-services and on
48     disabling a service at:
49     https://cloud.google.com/service-usage/docs/enable-disable
50
51     ## EXAMPLES
52     To disable a service called `my-consumed-service` for the active
53     project, run:
54
55       $ {command} my-consumed-service
56
57     To run the same command asynchronously (non-blocking), run:
58
59       $ {command} my-consumed-service --async
60  """
61
62  @staticmethod
63  def Args(parser):
64    """Args is called by calliope to gather arguments for this command.
65
66    Args:
67      parser: An argparse parser that you can use to add arguments that go
68          on the command line after this command. Positional arguments are
69          allowed.
70    """
71    common_flags.consumer_service_flag(suffix='to disable').AddToParser(parser)
72    base.ASYNC_FLAG.AddToParser(parser)
73    parser.add_argument(
74        '--force',
75        action='store_true',
76        help='If specified, the disable call will proceed even if there are '
77        'enabled services which depend on the service to be disabled. '
78        'Forcing the call means that the services which depend on the service '
79        'to be disabled will also be disabled.')
80
81  def Run(self, args):
82    """Run 'services disable'.
83
84    Args:
85      args: argparse.Namespace, The arguments that this command was invoked
86          with.
87
88    Returns:
89      Nothing.
90    """
91    project = properties.VALUES.core.project.Get(required=True)
92    for service_name in args.service:
93      service_name = arg_parsers.GetServiceNameFromArg(service_name)
94
95      protected_msg = serviceusage.GetProtectedServiceWarning(service_name)
96      if protected_msg:
97        if args.IsSpecified('quiet'):
98          raise console_io.RequiredPromptError()
99        do_disable = console_io.PromptContinue(protected_msg, default=False,
100                                               throw_if_unattended=True)
101        if not do_disable:
102          continue
103
104      op = serviceusage.DisableApiCall(project, service_name, args.force)
105      if op.done:
106        continue
107      if args.async_:
108        cmd = OP_WAIT_CMD.format(op.name)
109        log.status.Print('Asynchronous operation is in progress... '
110                         'Use the following command to wait for its '
111                         'completion:\n {0}'.format(cmd))
112        continue
113      op = services_util.WaitOperation(op.name, serviceusage.GetOperation)
114      services_util.PrintOperation(op)
115