1# -*- coding: utf-8 -*- #
2# Copyright 2015 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"""Delete cluster command."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.api_lib.dataproc import dataproc as dp
23from googlecloudsdk.api_lib.dataproc import util
24from googlecloudsdk.calliope import base
25from googlecloudsdk.command_lib.dataproc import flags
26from googlecloudsdk.core import log
27from googlecloudsdk.core.console import console_io
28
29
30class Delete(base.DeleteCommand):
31  """Delete a cluster."""
32
33  detailed_help = {
34      'EXAMPLES': """\
35          To delete a cluster, run:
36
37            $ {command} my_cluster --region=us-central1
38          """,
39  }
40
41  @classmethod
42  def Args(cls, parser):
43    base.ASYNC_FLAG.AddToParser(parser)
44    flags.AddTimeoutFlag(parser)
45    dataproc = dp.Dataproc(cls.ReleaseTrack())
46    flags.AddClusterResourceArg(parser, 'delete', dataproc.api_version)
47
48  def Run(self, args):
49    dataproc = dp.Dataproc(self.ReleaseTrack())
50
51    cluster_ref = args.CONCEPTS.cluster.Parse()
52
53    request = dataproc.messages.DataprocProjectsRegionsClustersDeleteRequest(
54        clusterName=cluster_ref.clusterName,
55        region=cluster_ref.region,
56        projectId=cluster_ref.projectId,
57        requestId=util.GetUniqueId())
58
59    console_io.PromptContinue(
60        message="The cluster '{0}' and all attached disks will be "
61        'deleted.'.format(cluster_ref.clusterName),
62        cancel_on_no=True,
63        cancel_string='Deletion aborted by user.')
64
65    operation = dataproc.client.projects_regions_clusters.Delete(request)
66
67    if args.async_:
68      log.status.write('Deleting [{0}] with operation [{1}].'.format(
69          cluster_ref, operation.name))
70      return operation
71
72    operation = util.WaitForOperation(
73        dataproc,
74        operation,
75        message='Waiting for cluster deletion operation',
76        timeout_s=args.timeout)
77    log.DeletedResource(cluster_ref)
78
79    return operation
80