1 # -*- coding: utf-8 -*-
2 # Copyright 2012 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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 """Implementation of versioning configuration command for buckets."""
17 from __future__
import absolute_import
19 from gslib
.command
import Command
20 from gslib
.command_argument
import CommandArgument
21 from gslib
.cs_api_map
import ApiSelector
22 from gslib
.exception
import CommandException
23 from gslib
.help_provider
import CreateHelpText
24 from gslib
.third_party
.storage_apitools
import storage_v1_messages
as apitools_messages
25 from gslib
.util
import NO_MAX
29 gsutil versioning set [on|off] bucket_url...
33 gsutil versioning get bucket_url...
36 _SYNOPSIS
= _SET_SYNOPSIS
+ _GET_SYNOPSIS
.lstrip('\n')
38 _SET_DESCRIPTION
= """
40 The "set" sub-command requires an additional sub-command, either "on" or
41 "off", which, respectively, will enable or disable versioning for the
46 _GET_DESCRIPTION
= """
48 The "get" sub-command gets the versioning configuration for a
49 bucket and displays whether or not it is enabled.
53 The Versioning Configuration feature enables you to configure a Google Cloud
54 Storage bucket to keep old versions of objects.
56 The gsutil versioning command has two sub-commands:
57 """ + _SET_DESCRIPTION
+ _GET_DESCRIPTION
59 _DETAILED_HELP_TEXT
= CreateHelpText(_SYNOPSIS
, _DESCRIPTION
)
61 _get_help_text
= CreateHelpText(_GET_SYNOPSIS
, _GET_DESCRIPTION
)
62 _set_help_text
= CreateHelpText(_SET_SYNOPSIS
, _SET_DESCRIPTION
)
65 class VersioningCommand(Command
):
66 """Implementation of gsutil versioning command."""
68 # Command specification. See base class for documentation.
69 command_spec
= Command
.CreateCommandSpec(
71 command_name_aliases
=['setversioning', 'getversioning'],
72 usage_synopsis
=_SYNOPSIS
,
75 supported_sub_args
='',
77 provider_url_ok
=False,
79 gs_api_support
=[ApiSelector
.XML
, ApiSelector
.JSON
],
80 gs_default_api
=ApiSelector
.JSON
,
83 CommandArgument('mode', choices
=['on', 'off']),
84 CommandArgument
.MakeZeroOrMoreCloudBucketURLsArgument()
87 CommandArgument
.MakeZeroOrMoreCloudBucketURLsArgument()
91 # Help specification. See help_provider.py for documentation.
92 help_spec
= Command
.HelpSpec(
93 help_name
='versioning',
94 help_name_aliases
=['getversioning', 'setversioning'],
95 help_type
='command_help',
96 help_one_line_summary
=(
97 'Enable or suspend versioning for one or more buckets'),
98 help_text
=_DETAILED_HELP_TEXT
,
99 subcommand_help_text
={'get': _get_help_text
, 'set': _set_help_text
},
102 def _CalculateUrlsStartArg(self
):
104 self
.RaiseWrongNumberOfArgumentsException()
105 if self
.args
[0].lower() == 'set':
110 def _SetVersioning(self
):
111 """Gets versioning configuration for a bucket."""
112 versioning_arg
= self
.args
[0].lower()
113 if versioning_arg
not in ('on', 'off'):
114 raise CommandException('Argument to "%s set" must be either [on|off]'
115 % (self
.command_name
))
116 url_args
= self
.args
[1:]
118 self
.RaiseWrongNumberOfArgumentsException()
120 # Iterate over URLs, expanding wildcards and set the versioning
121 # configuration on each.
123 for url_str
in url_args
:
124 bucket_iter
= self
.GetBucketUrlIterFromArg(url_str
, bucket_fields
=['id'])
125 for blr
in bucket_iter
:
126 url
= blr
.storage_url
128 bucket_metadata
= apitools_messages
.Bucket(
129 versioning
=apitools_messages
.Bucket
.VersioningValue())
130 if versioning_arg
== 'on':
131 self
.logger
.info('Enabling versioning for %s...', url
)
132 bucket_metadata
.versioning
.enabled
= True
134 self
.logger
.info('Suspending versioning for %s...', url
)
135 bucket_metadata
.versioning
.enabled
= False
136 self
.gsutil_api
.PatchBucket(url
.bucket_name
, bucket_metadata
,
137 provider
=url
.scheme
, fields
=['id'])
139 raise CommandException('No URLs matched')
141 def _GetVersioning(self
):
142 """Gets versioning configuration for one or more buckets."""
145 # Iterate over URLs, expanding wildcards and getting the versioning
146 # configuration on each.
148 for url_str
in url_args
:
149 bucket_iter
= self
.GetBucketUrlIterFromArg(url_str
,
150 bucket_fields
=['versioning'])
151 for blr
in bucket_iter
:
153 if blr
.root_object
.versioning
and blr
.root_object
.versioning
.enabled
:
154 print '%s: Enabled' % blr
.url_string
.rstrip('/')
156 print '%s: Suspended' % blr
.url_string
.rstrip('/')
158 raise CommandException('No URLs matched')
160 def RunCommand(self
):
161 """Command entry point for the versioning command."""
162 action_subcommand
= self
.args
.pop(0)
163 if action_subcommand
== 'get':
164 func
= self
._GetVersioning
165 elif action_subcommand
== 'set':
166 func
= self
._SetVersioning
168 raise CommandException((
169 'Invalid subcommand "%s" for the %s command.\n'
170 'See "gsutil help %s".') % (
171 action_subcommand
, self
.command_name
, self
.command_name
))