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 all certificates and generates a new server SSL certificate."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.sql import api_util
22from googlecloudsdk.api_lib.sql import operations
23from googlecloudsdk.api_lib.sql import validate
24from googlecloudsdk.calliope import base
25from googlecloudsdk.command_lib.sql import flags
26from googlecloudsdk.core import log
27from googlecloudsdk.core import properties
28from googlecloudsdk.core.console import console_io
29
30
31@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA,
32                    base.ReleaseTrack.ALPHA)
33class ResetSslConfig(base.Command):
34  """Deletes all client certificates and generates a new server certificate."""
35
36  @staticmethod
37  def Args(parser):
38    """Args is called by calliope to gather arguments for this command.
39
40    Args:
41      parser: An argparse parser that you can use to add arguments that go
42          on the command line after this command. Positional arguments are
43          allowed.
44    """
45    base.ASYNC_FLAG.AddToParser(parser)
46    parser.add_argument(
47        'instance',
48        completer=flags.InstanceCompleter,
49        help='Cloud SQL instance ID.')
50
51  def Run(self, args):
52    """Deletes all certificates and generates a new server SSL certificate.
53
54    Args:
55      args: argparse.Namespace, The arguments that this command was invoked
56          with.
57
58    Returns:
59      A dict object representing the operations resource describing the
60      resetSslConfig operation if the reset was successful.
61    """
62    client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
63    sql_client = client.sql_client
64    sql_messages = client.sql_messages
65
66    validate.ValidateInstanceName(args.instance)
67    instance_ref = client.resource_parser.Parse(
68        args.instance,
69        params={'project': properties.VALUES.core.project.GetOrFail},
70        collection='sql.instances')
71
72    console_io.PromptContinue(
73        message='Resetting your SSL configuration will delete all client '
74        'certificates and generate a new server certificate.',
75        default=True,
76        cancel_on_no=True)
77
78    result_operation = sql_client.instances.ResetSslConfig(
79        sql_messages.SqlInstancesResetSslConfigRequest(
80            project=instance_ref.project, instance=instance_ref.instance))
81
82    operation_ref = client.resource_parser.Create(
83        'sql.operations',
84        operation=result_operation.name,
85        project=instance_ref.project)
86
87    if args.async_:
88      return sql_client.operations.Get(
89          sql_messages.SqlOperationsGetRequest(
90              project=operation_ref.project, operation=operation_ref.operation))
91
92    operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
93                                                  'Resetting SSL config')
94
95    log.status.write(
96        'Reset SSL config for [{resource}].\n'.format(resource=instance_ref))
97