1# -*- coding: utf-8 -*- #
2# Copyright 2017 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"""Shared resource flags for kms related compute commands."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.calliope.concepts import concepts
23from googlecloudsdk.calliope.concepts import deps
24from googlecloudsdk.command_lib.util.concepts import concept_parsers
25from googlecloudsdk.core import properties
26
27
28def KeyAttributeConfig():
29  return concepts.ResourceParameterAttributeConfig(
30      name='kms-key',
31      help_text='The KMS key of the {resource}.')
32
33
34def KeyringAttributeConfig():
35  return concepts.ResourceParameterAttributeConfig(
36      name='kms-keyring',
37      help_text='The KMS keyring of the {resource}.')
38
39
40def LocationAttributeConfig(region_fallthrough=False):
41  fallthroughs = []
42  if region_fallthrough:
43    fallthroughs.append(deps.ArgFallthrough('--region'))
44  return concepts.ResourceParameterAttributeConfig(
45      name='kms-location',
46      help_text='The Cloud location for the {resource}.',
47      fallthroughs=fallthroughs)
48
49
50def ProjectAttributeConfig():
51  return concepts.ResourceParameterAttributeConfig(
52      name='kms-project',
53      help_text='The Cloud project for the {resource}.',
54      fallthroughs=[deps.PropertyFallthrough(properties.VALUES.core.project)])
55
56
57def GetKmsKeyResourceSpec(region_fallthrough=False):
58  return concepts.ResourceSpec(
59      'cloudkms.projects.locations.keyRings.cryptoKeys',
60      resource_name='key',
61      cryptoKeysId=KeyAttributeConfig(),
62      keyRingsId=KeyringAttributeConfig(),
63      locationsId=LocationAttributeConfig(
64          region_fallthrough=region_fallthrough),
65      projectsId=ProjectAttributeConfig(),
66      disable_auto_completers=False)
67
68
69def AddKmsKeyResourceArg(parser, resource, region_fallthrough=False,
70                         boot_disk_prefix=False):
71  """Add a resource argument for a KMS key.
72
73  Args:
74    parser: the parser for the command.
75    resource: str, the name of the resource that the cryptokey will be used to
76      protect.
77    region_fallthrough: bool, True if the command has a region flag that should
78      be used as a fallthrough for the kms location.
79    boot_disk_prefix: If the key flags have the 'boot-disk' prefix.
80  """
81  flag_name_overrides = None
82  if boot_disk_prefix:
83    kms_flags = ['kms-key', 'kms-keyring', 'kms-location', 'kms-project']
84    flag_name_overrides = dict(
85        [(flag, '--boot-disk-' + flag) for flag in kms_flags])
86  concept_parsers.ConceptParser.ForResource(
87      '--kms-key',
88      GetKmsKeyResourceSpec(region_fallthrough=region_fallthrough),
89      'The Cloud KMS (Key Management Service) cryptokey that will be used to '
90      'protect the {}.'.format(resource),
91      flag_name_overrides=flag_name_overrides).AddToParser(parser)
92