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 Cloud DNS 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 import completers 25from googlecloudsdk.command_lib.util.concepts import concept_parsers 26from googlecloudsdk.core import properties 27 28 29class PolicyCompleter(completers.ListCommandCompleter): 30 31 def __init__(self, api_version, **kwargs): 32 super(PolicyCompleter, self).__init__( 33 collection='dns.policies', 34 api_version=api_version, 35 list_command='alpha dns policies list --format=value(name)', 36 parse_output=True, 37 **kwargs) 38 39 40def PolicyAttributeConfig(api_version): 41 return concepts.ResourceParameterAttributeConfig( 42 name='policy', 43 completer=PolicyCompleter(api_version=api_version), 44 help_text='The Cloud DNS policy name {resource}.') 45 46 47def ResponsePolicyAttributeConfig(): 48 return concepts.ResourceParameterAttributeConfig( 49 name='response-policy', 50 help_text='The Cloud DNS response policy name {resource}.') 51 52 53def ResponsePolicyRuleAttributeConfig(): 54 return concepts.ResourceParameterAttributeConfig( 55 name='response-policy-rule', 56 help_text='The Cloud DNS response policy rule name {resource}.') 57 58 59def ProjectAttributeConfig(): 60 return concepts.ResourceParameterAttributeConfig( 61 name='project', 62 help_text='The Cloud project for the {resource}.', 63 fallthroughs=[deps.PropertyFallthrough(properties.VALUES.core.project)]) 64 65 66def GetPolicyResourceSpec(api_version): 67 return concepts.ResourceSpec( 68 'dns.policies', 69 api_version=api_version, 70 resource_name='policy', 71 policy=PolicyAttributeConfig(api_version=api_version), 72 project=ProjectAttributeConfig()) 73 74 75def GetResponsePolicyResourceSpec(api_version): 76 return concepts.ResourceSpec( 77 'dns.responsePolicies', 78 api_version=api_version, 79 resource_name='response_policy', 80 responsePolicy=ResponsePolicyAttributeConfig(), 81 project=ProjectAttributeConfig()) 82 83 84def GetResponsePolicyRuleSpec(api_version): 85 return concepts.ResourceSpec( 86 'dns.responsePolicyRules', 87 api_version=api_version, 88 resource_name='response_policy_rule', 89 responsePolicy=ResponsePolicyAttributeConfig(), 90 responsePolicyRule=ResponsePolicyRuleAttributeConfig(), 91 project=ProjectAttributeConfig()) 92 93 94def AddPolicyResourceArg(parser, 95 verb, 96 api_version, 97 positional=True, 98 required=True): 99 """Add a resource argument for a Cloud DNS Policy. 100 101 Args: 102 parser: the parser for the command. 103 verb: str, the verb to describe the resource, such as 'to update'. 104 api_version: str, the version of the API to use. 105 positional: bool, if True, means that the policy name is a positional rather 106 than a flag. 107 required: bool, if True, means that the arg will be required. 108 """ 109 if positional: 110 name = 'policy' 111 else: 112 name = '--policy' 113 concept_parsers.ConceptParser.ForResource( 114 name, 115 GetPolicyResourceSpec(api_version), 116 'The policy {}.'.format(verb), 117 required=required).AddToParser(parser) 118 119 120def AddResponsePolicyResourceArg(parser, 121 verb, 122 api_version, 123 positional=True, 124 required=True): 125 """Add a resource argument for a Cloud DNS Response Policy. 126 127 Args: 128 parser: the parser for the command. 129 verb: str, the verb to describe the resource, such as 'to update'. 130 api_version: str, the version of the API to use. 131 positional: bool, if True, means that the policy name is a positional rather 132 than a flag. 133 required: bool, if True, means that the arg will be required. 134 """ 135 if positional: 136 name = 'response_policies' 137 else: 138 name = '--response_policies' 139 concept_parsers.ConceptParser.ForResource( 140 name, 141 GetResponsePolicyResourceSpec(api_version), 142 'The response policy {}.'.format(verb), 143 required=required).AddToParser(parser) 144 145 146def AddResponsePolicyRuleArg(parser, 147 verb, 148 api_version, 149 positional=True, 150 required=True): 151 """Add a resource argument for a Cloud DNS Policy Rule. 152 153 Args: 154 parser: the parser for the command. 155 verb: str, the verb to describe the resource, such as 'to update'. 156 api_version: str, the version of the API to use. 157 positional: bool, if True, means that the policy name is a positional rather 158 than a flag. 159 required: bool, if True, means that the arg will be required. 160 """ 161 if positional: 162 name = 'response_policy_rule' 163 else: 164 name = '--response_policy_rule' 165 concept_parsers.ConceptParser.ForResource( 166 name, 167 GetResponsePolicyRuleSpec(api_version), 168 'The response policy rule {}.'.format(verb), 169 flag_name_overrides={'response-policy-rule': 'response_policy_rule'}, 170 required=required).AddToParser(parser) 171