1# -*- coding: utf-8 -*- # 2# Copyright 2019 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"""Export autoscaling policy command.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21import sys 22 23from googlecloudsdk.api_lib.dataproc import dataproc as dp 24from googlecloudsdk.calliope import base 25from googlecloudsdk.command_lib.dataproc import flags 26from googlecloudsdk.command_lib.export import util as export_util 27from googlecloudsdk.core.util import files 28 29 30class Export(base.Command): 31 """Export an autoscaling policy. 32 33 Exporting an autoscaling policy is similar to describing one, except that 34 export omits output only fields, such as the policy id and resource name. This 35 is to allow piping the output of export directly into import, which requires 36 that output only fields are omitted. 37 38 ## EXAMPLES 39 40 The following command saves the contents of autoscaling policy 41 `example-autoscaling-policy` to a file so that it can be imported later: 42 43 $ {command} example-autoscaling-policy --destination=saved-policy.yaml 44 """ 45 46 @classmethod 47 def Args(cls, parser): 48 dataproc = dp.Dataproc(cls.ReleaseTrack()) 49 flags.AddAutoscalingPolicyResourceArg(parser, 'export', 50 dataproc.api_version) 51 export_util.AddExportFlags(parser) 52 53 def Run(self, args): 54 dataproc = dp.Dataproc(self.ReleaseTrack()) 55 messages = dataproc.messages 56 57 policy_ref = args.CONCEPTS.autoscaling_policy.Parse() 58 59 request = messages.DataprocProjectsRegionsAutoscalingPoliciesGetRequest( 60 name=policy_ref.RelativeName()) 61 policy = dataproc.client.projects_regions_autoscalingPolicies.Get(request) 62 63 # Filter out OUTPUT_ONLY fields and resource identifying fields. Note this 64 # needs to be kept in sync with v1/v1beta2 autoscaling_policies.proto. 65 policy.id = None 66 policy.name = None 67 68 if args.destination: 69 with files.FileWriter(args.destination) as stream: 70 export_util.Export(message=policy, stream=stream) 71 else: 72 # Print to stdout 73 export_util.Export(message=policy, stream=sys.stdout) 74