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 ssl policies command."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import unicode_literals
19
20import sys
21from googlecloudsdk.api_lib.compute import base_classes
22from googlecloudsdk.api_lib.compute.ssl_policies import ssl_policies_utils
23from googlecloudsdk.calliope import base
24from googlecloudsdk.command_lib.compute import flags as compute_flags
25from googlecloudsdk.command_lib.compute.ssl_policies import flags
26from googlecloudsdk.command_lib.export import util as export_util
27from googlecloudsdk.core.util import files
28
29
30@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
31class Export(base.Command):
32  """Export an SSL policy.
33
34  Exports an SSL policy's configuration to a file.
35  This configuration can be imported at a later time.
36  """
37
38  SSL_POLICY_ARG = None
39
40  @classmethod
41  def GetApiVersion(cls):
42    """Returns the API version based on the release track."""
43    if cls.ReleaseTrack() == base.ReleaseTrack.ALPHA:
44      return 'alpha'
45    elif cls.ReleaseTrack() == base.ReleaseTrack.BETA:
46      return 'beta'
47    return 'v1'
48
49  @classmethod
50  def GetSchemaPath(cls, for_help=False):
51    """Returns the resource schema path."""
52    return export_util.GetSchemaPath(
53        'compute', cls.GetApiVersion(), 'SslPolicy', for_help=for_help)
54
55  @classmethod
56  def Args(cls, parser):
57    cls.SSL_POLICY_ARG = flags.GetSslPolicyArgument()
58    cls.SSL_POLICY_ARG.AddArgument(parser, operation_type='export')
59    export_util.AddExportFlags(parser, cls.GetSchemaPath(for_help=True))
60
61  def Run(self, args):
62    holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
63    helper = ssl_policies_utils.SslPolicyHelper(holder)
64    client = holder.client
65
66    ssl_policy_ref = self.SSL_POLICY_ARG.ResolveAsResource(
67        args,
68        holder.resources,
69        scope_lister=compute_flags.GetDefaultScopeLister(client))
70
71    ssl_policy = helper.Describe(ssl_policy_ref)
72
73    if args.destination:
74      with files.FileWriter(args.destination) as stream:
75        export_util.Export(
76            message=ssl_policy, stream=stream, schema_path=self.GetSchemaPath())
77    else:
78      export_util.Export(
79          message=ssl_policy,
80          stream=sys.stdout,
81          schema_path=self.GetSchemaPath())
82