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
16"""Client for interaction with Api CRUD on API Gateway API."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from apitools.base.py import exceptions as apitools_exceptions
23
24from googlecloudsdk.api_lib.api_gateway import base
25from googlecloudsdk.command_lib.api_gateway import common_flags
26
27
28class ApiClient(base.BaseClient):
29  """Client for Api objects on Cloud API Gateway API."""
30
31  def __init__(self, client=None):
32    base.BaseClient.__init__(self,
33                             client=client,
34                             message_base='ApigatewayProjectsLocationsApis',
35                             service_name='projects_locations_apis')
36    self.DefineGet()
37    self.DefineList('apis')
38    self.DefineUpdate('apigatewayApi')
39    self.DefineDelete()
40    self.DefineIamPolicyFunctions()
41
42  def DoesExist(self, api_ref):
43    """Checks if an Api object exists.
44
45    Args:
46      api_ref: Resource, a resource reference for the api
47
48    Returns:
49      Boolean, indicating whether or not exists
50    """
51    try:
52      self.Get(api_ref)
53    except apitools_exceptions.HttpNotFoundError:
54      return False
55
56    return True
57
58  def Create(self, api_ref, managed_service=None, labels=None,
59             display_name=None):
60    """Creates a new Api object.
61
62    Args:
63      api_ref: Resource, a resource reference for the api
64      managed_service: Optional string, reference name for OP service
65      labels: Optional cloud labels
66      display_name: Optional display name
67
68    Returns:
69      Long running operation response object.
70    """
71    labels = common_flags.ProcessLabelsFlag(
72        labels,
73        self.messages.ApigatewayApi.LabelsValue)
74    api = self.messages.ApigatewayApi(
75        name=api_ref.RelativeName(),
76        managedService=managed_service,
77        labels=labels,
78        displayName=display_name)
79
80    req = self.create_request(
81        apiId=api_ref.Name(),
82        apigatewayApi=api,
83        parent=api_ref.Parent().RelativeName())
84
85    return self.service.Create(req)
86