1# -*- coding: utf-8 -*- #
2# Copyright 2016 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"""Flags and helpers for the compute disks commands."""
17
18from __future__ import absolute_import
19from __future__ import division
20from __future__ import unicode_literals
21
22from googlecloudsdk.command_lib.compute import completers as compute_completers
23from googlecloudsdk.command_lib.compute import flags as compute_flags
24
25_DETAILED_SOURCE_SNAPSHOT_HELP = """\
26      Source snapshot used to create the disks. It is safe to
27      delete a snapshot after a disk has been created from the
28      snapshot. In such cases, the disks will no longer reference
29      the deleted snapshot. To get a list of snapshots in your
30      current project, run `gcloud compute snapshots list`. A
31      snapshot from an existing disk can be created using the
32      `gcloud compute disks snapshot` command. This flag is mutually
33      exclusive with *--image*.
34
35      When using this option, the size of the disks must be at least
36      as large as the snapshot size. Use *--size* to adjust the
37      size of the disks.
38"""
39
40_DETAILED_SOURCE_INSTANT_SNAPSHOT_HELP = """\
41      Source instant snapshot used to create the disks.
42"""
43
44_SOURCE_DISK_DETAILED_HELP = """\
45      Source disk used to create the disk(s). It is safe to
46      delete a source disk after a disk has been created from the
47      source disk. To get a list of disks in your current project,
48      run `gcloud compute disks list`. This flag is mutually
49      exclusive with *--image* and *--source-snapshot*.
50
51      When using this option, the size of the disks must be at least
52      as large as the source disk size. Use *--size* to adjust the
53      size of the disks.
54
55      The source disk must be in the same zone/region as the disk to be created.
56"""
57
58_SOURCE_DISK_ZONE_EXPLANATION = """\
59      Zone of the source disk, this argument is not required if target disk is in same zone of source disk.
60"""
61
62_SOURCE_DISK_REGION_EXPLANATION = """\
63      Region of the source disk, this argument is not required if target disk is in same region of source disk.
64"""
65
66DEFAULT_LIST_FORMAT = """\
67    table(
68      name,
69      zone.basename(),
70      sizeGb,
71      type.basename(),
72      status
73    )"""
74
75
76MULTISCOPE_LIST_FORMAT = """
77    table(
78      name,
79      location(),
80      location_scope(),
81      sizeGb,
82      type.basename(),
83      status
84      )"""
85
86
87class SnapshotsCompleter(compute_completers.ListCommandCompleter):
88
89  def __init__(self, **kwargs):
90    super(SnapshotsCompleter, self).__init__(
91        collection='compute.snapshots',
92        list_command='compute snapshots list --uri',
93        **kwargs)
94
95
96def MakeDiskArgZonal(plural):
97  return compute_flags.ResourceArgument(
98      resource_name='disk',
99      completer=compute_completers.DisksCompleter,
100      plural=plural,
101      name='DISK_NAME',
102      zonal_collection='compute.disks',
103      zone_explanation=compute_flags.ZONE_PROPERTY_EXPLANATION)
104
105
106def MakeDiskArg(plural):
107  return compute_flags.ResourceArgument(
108      resource_name='disk',
109      completer=compute_completers.DisksCompleter,
110      plural=plural,
111      name='DISK_NAME',
112      zonal_collection='compute.disks',
113      regional_collection='compute.regionDisks',
114      zone_explanation=compute_flags.ZONE_PROPERTY_EXPLANATION,
115      region_explanation=compute_flags.REGION_PROPERTY_EXPLANATION)
116
117
118def AddMultiWriterFlag(parser):
119  return parser.add_argument(
120      '--multi-writer',
121      action='store_true',
122      help="""
123      Create the disk in multi-writer mode so that it can be attached
124      with read-write access to multiple VMs. Can only be used with
125      zonal SSD persistent disks. Disks in multi-writer mode do not support
126      resize and snapshot operations.
127      """)
128
129
130def AddProvisionedIopsFlag(parser, arg_parsers, constants):
131  return parser.add_argument(
132      '--provisioned-iops',
133      type=arg_parsers.BoundedInt(constants.MIN_PROVISIONED_IOPS,
134                                  constants.MAX_PROVISIONED_IOPS),
135      help=(
136          'Provisioned IOPS of pd-extreme disk to create. Only for use with '
137          'disks of type pd-extreme. If specified, the value must be in the '
138          'range between {min} and {max}. If not specified, the default value '
139          'is {default}.').format(
140              min=constants.MIN_PROVISIONED_IOPS,
141              max=constants.MAX_PROVISIONED_IOPS,
142              default=constants.DEFAULT_PROVISIONED_IOPS))
143
144
145SOURCE_SNAPSHOT_ARG = compute_flags.ResourceArgument(
146    resource_name='snapshot',
147    completer=SnapshotsCompleter,
148    name='--source-snapshot',
149    plural=False,
150    required=False,
151    global_collection='compute.snapshots',
152    short_help='Source snapshot used to create the disks.',
153    detailed_help=_DETAILED_SOURCE_SNAPSHOT_HELP,)
154
155SOURCE_INSTANT_SNAPSHOT_ARG = compute_flags.ResourceArgument(
156    resource_name='source instant snapshot',
157    completer=compute_completers.InstantSnapshotsCompleter,
158    name='--source-instant-snapshot',
159    zonal_collection='compute.zoneInstantSnapshots',
160    regional_collection='compute.regionInstantSnapshots',
161    plural=False,
162    required=False,
163    short_help='Source instant snapshot used to create the disks.',
164    detailed_help=_DETAILED_SOURCE_INSTANT_SNAPSHOT_HELP,
165    use_existing_default_scope=True)
166
167SOURCE_DISK_ARG = compute_flags.ResourceArgument(
168    resource_name='source disk',
169    name='--source-disk',
170    completer=compute_completers.DisksCompleter,
171    short_help='Source disk used to create the disks. Source disk must be in'
172    ' the same zone/region as the disk to be created.',
173    detailed_help=_SOURCE_DISK_DETAILED_HELP,
174    zonal_collection='compute.disks',
175    regional_collection='compute.regionDisks',
176    required=False,
177    zone_help_text=_SOURCE_DISK_ZONE_EXPLANATION,
178    region_help_text=_SOURCE_DISK_REGION_EXPLANATION)
179