1# -*- coding: utf-8 -*- #
2# Copyright 2015 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"""Creates or updates a Google Cloud Function."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.calliope import base
22from googlecloudsdk.command_lib.functions import flags
23from googlecloudsdk.command_lib.functions.v1.deploy import command as command_v1
24from googlecloudsdk.command_lib.functions.v1.deploy import labels_util
25from googlecloudsdk.command_lib.functions.v2.deploy import command as command_v2
26from googlecloudsdk.command_lib.functions.v2.deploy import env_vars_util
27from googlecloudsdk.command_lib.util.args import labels_util as args_labels_util
28
29
30@base.ReleaseTracks(base.ReleaseTrack.GA)
31class Deploy(base.Command):
32  """Create or update a Google Cloud Function."""
33
34  @staticmethod
35  def Args(parser):
36    """Register flags for this command."""
37    # Add a positional "resource argument" for the name of the function
38    flags.AddFunctionResourceArg(parser, 'to deploy')
39
40    # Add args for function properties
41    flags.AddAllowUnauthenticatedFlag(parser)
42    flags.AddFunctionMemoryFlag(parser)
43    flags.AddFunctionRetryFlag(parser)
44    flags.AddFunctionTimeoutFlag(parser)
45    flags.AddMaxInstancesFlag(parser)
46    flags.AddRuntimeFlag(parser)
47    flags.AddServiceAccountFlag(parser)
48    args_labels_util.AddUpdateLabelsFlags(
49        parser,
50        extra_update_message=labels_util.NO_LABELS_STARTING_WITH_DEPLOY_MESSAGE,
51        extra_remove_message=labels_util.NO_LABELS_STARTING_WITH_DEPLOY_MESSAGE)
52
53    # Add args for specifying the function source code
54    flags.AddSourceFlag(parser)
55    flags.AddStageBucketFlag(parser)
56    flags.AddEntryPointFlag(parser)
57
58    # Add args for specifying the function trigger
59    flags.AddTriggerFlagGroup(parser)
60
61    # Add args for specifying environment variables
62    env_vars_util.AddUpdateEnvVarsFlags(parser)
63
64    # Add flags for specifying build environment variables
65    env_vars_util.AddBuildEnvVarsFlags(parser)
66
67    # Add args for specifying ignore files to upload source
68    flags.AddIgnoreFileFlag(parser)
69
70    # Add flags for network settings
71    flags.AddVPCConnectorMutexGroup(parser)
72    flags.AddEgressSettingsFlag(parser)
73    flags.AddIngressSettingsFlag(parser)
74
75  def Run(self, args):
76    return command_v1.Run(args, track=self.ReleaseTrack())
77
78
79@base.ReleaseTracks(base.ReleaseTrack.BETA)
80class DeployBeta(base.Command):
81  """Create or update a Google Cloud Function."""
82
83  @staticmethod
84  def Args(parser):
85    """Register flags for this command."""
86    Deploy.Args(parser)
87
88    # Add additional args for this release track
89    flags.AddBuildWorkerPoolMutexGroup(parser)
90    flags.AddSecurityLevelFlag(parser)
91
92  def Run(self, args):
93    return command_v1.Run(
94        args,
95        track=self.ReleaseTrack(),
96        enable_build_worker_pool=True,
97        enable_security_level=True)
98
99
100@base.ReleaseTracks(base.ReleaseTrack.ALPHA)
101class DeployAlpha(base.Command):
102  """Create or update a Google Cloud Function."""
103
104  @staticmethod
105  def Args(parser):
106    """Register flags for this command."""
107    Deploy.Args(parser)
108
109    # Add additional args for this release track
110    flags.AddBuildWorkerPoolMutexGroup(parser)
111    flags.AddSecurityLevelFlag(parser)
112
113    # Add additional flags for GCFv2
114    flags.AddRunServiceAccountFlag(parser)
115    flags.AddTriggerEventFiltersFlag(parser)
116    flags.AddTriggerLocationFlag(parser)
117    flags.AddTriggerServiceAccountFlag(parser)
118    flags.AddV2Flag(parser)
119
120  def Run(self, args):
121    if flags.ShouldUseV2(args):
122      return command_v2.Run(args, self.ReleaseTrack())
123    else:
124      return command_v1.Run(
125          args,
126          track=self.ReleaseTrack(),
127          enable_build_worker_pool=True,
128          enable_security_level=True)
129
130
131DETAILED_HELP = {
132    'EXAMPLES':
133        """\
134        To deploy a function that is triggered by write events on the document
135        ``/messages/{pushId}'', run:
136
137          $ {command} my_function --runtime=python37 --trigger-event=providers/cloud.firestore/eventTypes/document.write --trigger-resource=projects/project_id/databases/(default)/documents/messages/{pushId}
138
139        See https://cloud.google.com/functions/docs/calling for more details
140        of using other types of resource as triggers.
141        """
142}
143
144Deploy.detailed_help = DETAILED_HELP
145DeployBeta.detailed_help = DETAILED_HELP
146DeployAlpha.detailed_help = DETAILED_HELP
147