1# -*- coding: utf-8 -*- #
2# Copyright 2018 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"""List all server CA certs for a Cloud SQL instance."""
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 validate
23from googlecloudsdk.api_lib.sql.ssl import server_ca_certs
24from googlecloudsdk.calliope import base
25from googlecloudsdk.command_lib.sql import flags
26from googlecloudsdk.core import properties
27
28
29class _BaseList(object):
30  """Base class for sql ssl server_ca_certs list."""
31
32  @staticmethod
33  def Args(parser):
34    flags.AddInstance(parser)
35    parser.display_info.AddFormat(flags.SERVER_CA_CERTS_FORMAT)
36
37  def Run(self, args):
38    """List all server CA certs for a Cloud SQL instance.
39
40    Args:
41      args: argparse.Namespace, The arguments that this command was invoked
42          with.
43
44    Returns:
45      A dict object that has the list of sslCerts resources if the api request
46      was successful.
47    """
48    client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
49    sql_client = client.sql_client
50    sql_messages = client.sql_messages
51
52    validate.ValidateInstanceName(args.instance)
53    instance_ref = client.resource_parser.Parse(
54        args.instance,
55        params={'project': properties.VALUES.core.project.GetOrFail},
56        collection='sql.instances')
57
58    result = server_ca_certs.ListServerCas(sql_client, sql_messages,
59                                           instance_ref)
60    return iter(result.certs)
61
62
63@base.ReleaseTracks(base.ReleaseTrack.BETA, base.ReleaseTrack.ALPHA)
64class List(_BaseList, base.ListCommand):
65  """List all server CA certs for a Cloud SQL instance."""
66  pass
67