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"""ai-platform models list command."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.ml_engine import models
22from googlecloudsdk.calliope import base
23from googlecloudsdk.command_lib.ml_engine import endpoint_util
24from googlecloudsdk.command_lib.ml_engine import flags
25from googlecloudsdk.command_lib.ml_engine import models_util
26from googlecloudsdk.command_lib.ml_engine import region_util
27from googlecloudsdk.core import resources
28
29
30_COLLECTION = 'ml.models'
31_DEFAULT_FORMAT = """
32        table(
33            name.basename(),
34            defaultVersion.name.basename()
35        )
36    """
37
38
39def _GetUri(model):
40  ref = resources.REGISTRY.ParseRelativeName(
41      model.name, models_util.MODELS_COLLECTION)
42  return ref.SelfLink()
43
44
45def _AddListArgs(parser):
46  parser.display_info.AddFormat(_DEFAULT_FORMAT)
47  parser.display_info.AddUriFunc(_GetUri)
48  flags.GetRegionArg(include_global=True).AddToParser(parser)
49
50
51def _Run(args):
52  region = region_util.GetRegion(args)
53  with endpoint_util.MlEndpointOverrides(region=region):
54    return models_util.List(models.ModelsClient())
55
56
57@base.ReleaseTracks(base.ReleaseTrack.GA)
58class List(base.ListCommand):
59  """List existing AI Platform models."""
60
61  @staticmethod
62  def Args(parser):
63    _AddListArgs(parser)
64
65  def Run(self, args):
66    return _Run(args)
67
68
69@base.ReleaseTracks(base.ReleaseTrack.BETA, base.ReleaseTrack.ALPHA)
70class ListBeta(base.ListCommand):
71  """List existing AI Platform models."""
72
73  @staticmethod
74  def Args(parser):
75    _AddListArgs(parser)
76
77  def Run(self, args):
78    return _Run(args)
79