1# -*- coding: utf-8 -*- # 2# Copyright 2013 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"""Deletes a Cloud SQL instance.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21from apitools.base.py import exceptions 22 23from googlecloudsdk.api_lib.sql import api_util 24from googlecloudsdk.api_lib.sql import operations 25from googlecloudsdk.api_lib.sql import validate 26from googlecloudsdk.calliope import base 27from googlecloudsdk.command_lib.sql import flags 28from googlecloudsdk.core import log 29from googlecloudsdk.core import properties 30from googlecloudsdk.core.console import console_io 31import six 32 33 34@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA, 35 base.ReleaseTrack.ALPHA) 36class Delete(base.Command): 37 """Deletes a Cloud SQL instance.""" 38 39 @staticmethod 40 def Args(parser): 41 """Args is called by calliope to gather arguments for this command. 42 43 Args: 44 parser: An argparse parser that you can use to add arguments that go 45 on the command line after this command. Positional arguments are 46 allowed. 47 """ 48 base.ASYNC_FLAG.AddToParser(parser) 49 parser.add_argument( 50 'instance', 51 completer=flags.InstanceCompleter, 52 help='Cloud SQL instance ID.') 53 54 def Run(self, args): 55 """Deletes a Cloud SQL instance. 56 57 Args: 58 args: argparse.Namespace, The arguments that this command was invoked 59 with. 60 61 Returns: 62 A dict object representing the operations resource describing the delete 63 operation if the delete was successful. 64 """ 65 client = api_util.SqlClient(api_util.API_VERSION_DEFAULT) 66 sql_client = client.sql_client 67 sql_messages = client.sql_messages 68 operation_ref = None 69 70 validate.ValidateInstanceName(args.instance) 71 instance_ref = client.resource_parser.Parse( 72 args.instance, 73 params={'project': properties.VALUES.core.project.GetOrFail}, 74 collection='sql.instances') 75 76 if not console_io.PromptContinue( 77 'All of the instance data will be lost when the instance is deleted.'): 78 return None 79 try: 80 result = sql_client.instances.Delete( 81 sql_messages.SqlInstancesDeleteRequest( 82 instance=instance_ref.instance, project=instance_ref.project)) 83 84 operation_ref = client.resource_parser.Create( 85 'sql.operations', operation=result.name, project=instance_ref.project) 86 87 if args.async_: 88 return sql_client.operations.Get( 89 sql_messages.SqlOperationsGetRequest( 90 project=operation_ref.project, 91 operation=operation_ref.operation)) 92 93 operations.OperationsV1Beta4.WaitForOperation( 94 sql_client, operation_ref, 'Deleting Cloud SQL instance') 95 96 log.DeletedResource(instance_ref) 97 98 except exceptions.HttpError: 99 log.debug('operation : %s', six.text_type(operation_ref)) 100 raise 101