1# -*- coding: utf-8 -*- #
2# Copyright 2018 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"""bigtable app profiles create command."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21import textwrap
22
23from apitools.base.py.exceptions import HttpError
24from googlecloudsdk.api_lib.bigtable import app_profiles
25from googlecloudsdk.api_lib.bigtable import util
26from googlecloudsdk.calliope import base
27from googlecloudsdk.command_lib.bigtable import arguments
28from googlecloudsdk.core import log
29
30
31class CreateAppProfile(base.CreateCommand):
32  """Create a new Bigtable app profile."""
33
34  detailed_help = {
35      'EXAMPLES':
36          textwrap.dedent("""\
37          To create an app profile with a multi-cluster routing policy, run:
38
39            $ {command} my-app-profile-id --instance=my-instance-id --route-any
40
41          To create an app profile with a single-cluster routing policy which
42          routes all requests to `my-cluster-id`, run:
43
44            $ {command} my-single-cluster-app-profile --instance=my-instance-id --route-to=my-cluster-id
45
46          To create an app profile with a friendly description, run:
47
48            $ {command} my-app-profile-id --instance=my-instance-id --route-any --description="Routes requests for my use case"
49
50          """),
51  }
52
53  @staticmethod
54  def Args(parser):
55    arguments.AddAppProfileResourceArg(parser, 'to create')
56    (arguments.ArgAdder(parser).AddDescription(
57        'app profile',
58        required=False).AddForce('create').AddAppProfileRouting())
59
60  def Run(self, args):
61    """This is what gets called when the user runs this command.
62
63    Args:
64      args: an argparse namespace. All the arguments that were provided to this
65        command invocation.
66
67    Raises:
68      exceptions.ConflictingArgumentsException: If the user provides
69        --transactional-writes and --route-any.
70
71    Returns:
72      Created resource.
73    """
74    app_profile_ref = args.CONCEPTS.app_profile.Parse()
75    try:
76      result = app_profiles.Create(
77          app_profile_ref,
78          cluster=args.route_to,
79          description=args.description,
80          multi_cluster=args.route_any,
81          transactional_writes=args.transactional_writes,
82          force=args.force)
83    except HttpError as e:
84      util.FormatErrorMessages(e)
85    else:
86      log.CreatedResource(app_profile_ref.Name(), kind='app profile')
87      return result
88