Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions storage/cloud-client/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ To run this sample:



Bucket Policy Only
Uniform Bucket Level Access
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. image:: https://gstatic.com/cloudssh/images/open-btn.png
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/cloud-client/bucket_policy_only.py,storage/cloud-client/README.rst
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=storage/cloud-client/uniform_bucket_level_access.py,storage/cloud-client/README.rst



Expand All @@ -325,20 +325,20 @@ To run this sample:

.. code-block:: bash

$ python bucket_policy_only.py
$ python uniform_bucket_level_access.py

usage: bucket_policy_only.py [-h]
{enable-bucket-policy-only,disable-bucket-policy-only,get-bucket-policy-only}
usage: uniform_bucket_level_access.py [-h]
{enable-uniform-bucket-level-access,disable-uniform-bucket-level-access,get-uniform-bucket-level-access}
...

positional arguments:
{enable-bucket-policy-only,disable-bucket-policy-only,get-bucket-policy-only}
enable-bucket-policy-only
Enable Bucket Policy Only for a bucket
disable-bucket-policy-only
Disable Bucket Policy Only for a bucket
get-bucket-policy-only
Get Bucket Policy Only for a bucket
{enable-uniform-bucket-level-access,disable-uniform-bucket-level-access,get-uniform-bucket-level-access}
enable-uniform-bucket-level-access
Enable uniform bucket-level access for a bucket
disable-uniform-bucket-level-access
Disable uniform bucket-level access for a bucket
get-uniform-bucket-level-access
Get uniform bucket-level access for a bucket

optional arguments:
-h, --help show this help message and exit
Expand Down Expand Up @@ -437,4 +437,4 @@ to `browse the source`_ and `report issues`_.
https://github.com/GoogleCloudPlatform/google-cloud-python/issues


.. _Google Cloud SDK: https://cloud.google.com/sdk/
.. _Google Cloud SDK: https://cloud.google.com/sdk/
4 changes: 2 additions & 2 deletions storage/cloud-client/README.rst.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ samples:
- name: Bucket Lock
file: bucket_lock.py
show_help: true
- name: Bucket Policy Only
file: bucket_policy_only.py
- name: Uniform bucket-level access
file: uniform_bucket_level_access.py
show_help: true
- name: Notification Polling
file: notification_polling.py
Expand Down
96 changes: 0 additions & 96 deletions storage/cloud-client/bucket_policy_only.py

This file was deleted.

2 changes: 1 addition & 1 deletion storage/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-pubsub==1.0.0
google-cloud-storage==1.19.1
google-cloud-storage==1.22.0
102 changes: 102 additions & 0 deletions storage/cloud-client/uniform_bucket_level_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python

# Copyright 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from google.cloud import storage


def enable_uniform_bucket_level_access(bucket_name):
"""Enable uniform bucket-level access for a bucket"""
# [START storage_enable_uniform_bucket_level_access]
# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)

bucket.iam_configuration.uniform_bucket_level_access_enabled = True
bucket.patch()

print('Uniform bucket-level access was enabled for {}.'.format(
bucket.name))
# [END storage_enable_uniform_bucket_level_access]


def disable_uniform_bucket_level_access(bucket_name):
"""Disable uniform bucket-level access for a bucket"""
# [START storage_uniform_bucket_level_access]
# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)

bucket.iam_configuration.uniform_bucket_level_access_enabled = False
bucket.patch()

print('Uniform bucket-level access was disabled for {}.'.format(
bucket.name))


def get_uniform_bucket_level_access(bucket_name):
"""Get uniform bucket-level access for a bucket"""
# [START storage_get_uniform_bucket_level_access]
# bucket_name = "my-bucket"

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
iam_configuration = bucket.iam_configuration

if iam_configuration.uniform_bucket_level_access_enabled:
print('Uniform bucket-level access is enabled for {}.'.format(
bucket.name))
print('Bucket will be locked on {}.'.format(
iam_configuration.uniform_bucket_level_locked_time))
else:
print('Uniform bucket-level access is disabled for {}.'.format(
bucket.name))
# [END storage_get_uniform_bucket_level_access]


if __name__ == '__main__':

parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')

enable_uniform_bucket_level_access_parser = subparsers.add_parser(
'enable-uniform-bucket-level-access',
help=enable_uniform_bucket_level_access.__doc__)
enable_uniform_bucket_level_access_parser.add_argument('bucket_name')

disable_uniform_bucket_level_access_parser = subparsers.add_parser(
'disable-uniform-bucket-level-access',
help=disable_uniform_bucket_level_access.__doc__)
disable_uniform_bucket_level_access_parser.add_argument('bucket_name')

get_uniform_bucket_level_access_parser = subparsers.add_parser(
'get-uniform-bucket-level-access',
help=get_uniform_bucket_level_access.__doc__)
get_uniform_bucket_level_access_parser.add_argument('bucket_name')

args = parser.parse_args()

if args.command == 'enable-uniform-bucket-level-access':
enable_uniform_bucket_level_access(args.bucket_name)
elif args.command == 'disable-uniform-bucket-level-access':
disable_uniform_bucket_level_access(args.bucket_name)
elif args.command == 'get-uniform-bucket-level-access':
get_uniform_bucket_level_access(args.bucket_name)
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,37 @@

import pytest

import bucket_policy_only
import uniform_bucket_level_access


@pytest.fixture()
def bucket():
"""Creates a test bucket and deletes it upon completion."""
client = storage.Client()
bucket_name = 'bucket-policy-only-' + str(int(time.time()))
bucket_name = 'uniform-bucket-level-access-' + str(int(time.time()))
bucket = client.create_bucket(bucket_name)
yield bucket
time.sleep(3)
bucket.delete(force=True)


def test_get_bucket_policy_only(bucket, capsys):
bucket_policy_only.get_bucket_policy_only(bucket.name)
def test_get_uniform_bucket_level_access(bucket, capsys):
uniform_bucket_level_access.get_uniform_bucket_level_access(bucket.name)
out, _ = capsys.readouterr()
assert 'Bucket Policy Only is disabled for {}.'.format(
assert 'Uniform bucket-level access is disabled for {}.'.format(
bucket.name) in out


def test_enable_bucket_policy_only(bucket, capsys):
bucket_policy_only.enable_bucket_policy_only(bucket.name)
def test_enable_uniform_bucket_level_access(bucket, capsys):
uniform_bucket_level_access.enable_uniform_bucket_level_access(bucket.name)
out, _ = capsys.readouterr()
assert 'Bucket Policy Only was enabled for {}.'.format(
assert 'Uniform bucket-level access was enabled for {}.'.format(
bucket.name) in out


def test_disable_bucket_policy_only(bucket, capsys):
bucket_policy_only.disable_bucket_policy_only(bucket.name)
def test_disable_uniform_bucket_level_access(bucket, capsys):
uniform_bucket_level_access.disable_uniform_bucket_level_access(
bucket.name)
out, _ = capsys.readouterr()
assert 'Bucket Policy Only was disabled for {}.'.format(
assert 'Uniform bucket-level access was disabled for {}.'.format(
bucket.name) in out