1# -*- coding: utf-8 -*- #
2# Copyright 2020 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"""Describe a secret's metadata."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.secrets import api as secrets_api
22from googlecloudsdk.calliope import base
23from googlecloudsdk.calliope import exceptions
24from googlecloudsdk.command_lib.secrets import args as secrets_args
25
26
27@base.ReleaseTracks(base.ReleaseTrack.BETA, base.ReleaseTrack.GA)
28class Get(base.DescribeCommand):
29  r"""Describe a secret's replication.
30
31  Describe a secret's replication
32
33  ## EXAMPLES
34
35  To describe the replication of a secret named 'my-secret', run:
36
37    $ {command} my-secret
38  """
39
40  SECRET_MISSING_MESSAGE = (
41      'Cannot get replication for secret [{secret}] because it does not exist. '
42      'Please use the create command to create a new secret.')
43
44  @staticmethod
45  def Args(parser):
46    secrets_args.AddSecret(
47        parser, purpose='to describe', positional=True, required=True)
48
49  def Run(self, args):
50    secret_ref = args.CONCEPTS.secret.Parse()
51    secret = secrets_api.Secrets().GetOrNone(secret_ref)
52
53    # Secret does not exist
54    if secret is None:
55      raise exceptions.InvalidArgumentException(
56          'secret',
57          self.SECRET_MISSING_MESSAGE.format(secret=secret_ref.Name()))
58    return secret.replication
59