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 Url maps command.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import unicode_literals 20 21import sys 22from googlecloudsdk.api_lib.compute import base_classes 23from googlecloudsdk.calliope import base 24from googlecloudsdk.command_lib.compute import flags as compute_flags 25from googlecloudsdk.command_lib.compute import scope as compute_scope 26from googlecloudsdk.command_lib.compute.url_maps import flags 27from googlecloudsdk.command_lib.compute.url_maps import url_maps_utils 28from googlecloudsdk.command_lib.export import util as export_util 29from googlecloudsdk.core.util import files 30 31 32def _DetailedHelp(): 33 return { 34 'brief': 35 'Export a URL map.', 36 'DESCRIPTION': 37 """\ 38 Exports a URL map's configuration to a file. 39 This configuration can be imported at a later time. 40 """, 41 'EXAMPLES': 42 """\ 43 A URL map can be exported by running: 44 45 $ {command} NAME --destination=<path-to-file> 46 """ 47 } 48 49 50def _GetApiVersion(release_track): 51 """Returns the API version based on the release track.""" 52 if release_track == base.ReleaseTrack.ALPHA: 53 return 'alpha' 54 elif release_track == base.ReleaseTrack.BETA: 55 return 'beta' 56 return 'v1' 57 58 59def _GetSchemaPath(release_track, for_help=False): 60 """Returns the resource schema path.""" 61 return export_util.GetSchemaPath( 62 'compute', _GetApiVersion(release_track), 'UrlMap', for_help=for_help) 63 64 65def _Run(args, holder, url_map_arg, release_track): 66 """Issues requests necessary to export URL maps.""" 67 client = holder.client 68 69 url_map_ref = url_map_arg.ResolveAsResource( 70 args, 71 holder.resources, 72 default_scope=compute_scope.ScopeEnum.GLOBAL, 73 scope_lister=compute_flags.GetDefaultScopeLister(client)) 74 75 url_map = url_maps_utils.SendGetRequest(client, url_map_ref) 76 77 if args.destination: 78 with files.FileWriter(args.destination) as stream: 79 export_util.Export( 80 message=url_map, 81 stream=stream, 82 schema_path=_GetSchemaPath(release_track)) 83 else: 84 export_util.Export( 85 message=url_map, 86 stream=sys.stdout, 87 schema_path=_GetSchemaPath(release_track)) 88 89 90@base.ReleaseTracks(base.ReleaseTrack.GA, base.ReleaseTrack.BETA, 91 base.ReleaseTrack.ALPHA) 92class Export(base.Command): 93 """Export a URL map.""" 94 95 _include_l7_internal_load_balancing = True 96 97 detailed_help = _DetailedHelp() 98 URL_MAP_ARG = None 99 100 @classmethod 101 def Args(cls, parser): 102 cls.URL_MAP_ARG = flags.UrlMapArgument( 103 include_l7_internal_load_balancing=cls 104 ._include_l7_internal_load_balancing) 105 cls.URL_MAP_ARG.AddArgument(parser, operation_type='export') 106 export_util.AddExportFlags( 107 parser, _GetSchemaPath(cls.ReleaseTrack(), for_help=True)) 108 109 def Run(self, args): 110 holder = base_classes.ComputeApiHolder(self.ReleaseTrack()) 111 return _Run(args, holder, self.URL_MAP_ARG, self.ReleaseTrack()) 112