1# -*- coding: utf-8 -*- #
2# Copyright 2019 Google Inc. 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"""Command to wait for Cloud Life Sciences operation to complete."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.api_lib.lifesciences import lifesciences_client
22from googlecloudsdk.api_lib.util import waiter
23from googlecloudsdk.calliope import base
24from googlecloudsdk.calliope.concepts import concepts
25from googlecloudsdk.command_lib.lifesciences import operation_poller
26from googlecloudsdk.command_lib.util.apis import yaml_data
27from googlecloudsdk.command_lib.util.concepts import concept_parsers
28
29
30class Wait(base.SilentCommand):
31  r"""Wait for Cloud Life Sciences operation to complete.
32
33  ## EXAMPLES
34  To wait for the completion of the operation called `my-operation`, run:
35
36    $ {command} my-operation
37  """
38
39  WAIT_CEILING_MS = 60 * 20 * 1000
40
41  @staticmethod
42  def Args(parser):
43    operation_spec = concepts.ResourceSpec.FromYaml(
44        yaml_data.ResourceYAMLData.FromPath('lifesciences.operation')
45        .GetData())
46    concept_parsers.ConceptParser.ForResource(
47        'operation', operation_spec, 'The Cloud Life Sciences operation to wait for.',
48        required=True).AddToParser(parser)
49
50  def Run(self, args):
51    client = lifesciences_client.LifeSciencesClient()
52    operation_ref = args.CONCEPTS.operation.Parse()
53
54    req = client.messages.LifesciencesProjectsLocationsOperationsGetRequest(
55        name=operation_ref.RelativeName())
56
57    operation = client.client.projects_locations_operations.Get(req)
58
59    waiter.WaitFor(
60        operation_poller.OperationPoller(),
61        operation.name,
62        'Waiting for [{}] to complete.'.format(operation.name),
63        wait_ceiling_ms=self.WAIT_CEILING_MS)
64