Class: Google::Cloud::Storage::PolicyV1

Inherits:
Policy
  • Object
show all
Defined in:
lib/google/cloud/storage/policy.rb

Overview

A subclass of Policy that supports access to #roles and related helpers. Attempts to call #bindings and #version= will raise a runtime error. To update the Policy version and add bindings with a newer syntax, use PolicyV3 instead by calling Bucket#policy with requested_policy_version: 3. To obtain instances of this class, call Bucket#policy without the requested_policy_version keyword argument.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new
bucket = storage.bucket "my-bucket"

bucket.policy do |p|
  p.version # the value is 1
  p.remove "roles/storage.admin", "user:[email protected]"
  p.add "roles/storage.admin", "user:[email protected]"
  p.roles["roles/storage.objectViewer"] = ["allUsers"]
end

Instance Attribute Summary collapse

Attributes inherited from Policy

#etag, #version

Instance Method Summary collapse

Instance Attribute Details

#rolesHash

Returns the version 1 bindings (no conditions) as a hash that associates roles with arrays of members. See Understanding Roles for a listing of primitive and curated roles. See Buckets: setIamPolicy for a listing of values and patterns for members.

Returns:

  • (Hash)

    the current value of roles



112
113
114
# File 'lib/google/cloud/storage/policy.rb', line 112

def roles
  @roles
end

Instance Method Details

#add(role_name, member) ⇒ Object

Convenience method for adding a member to a binding on this policy. See Understanding Roles for a listing of primitive and curated roles. See Buckets: setIamPolicy for a listing of values and patterns for members.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.policy do |p|
  p.add "roles/storage.admin", "user:[email protected]"
end

Parameters:

  • role_name (String)

    A Cloud IAM role, such as "roles/storage.admin".

  • member (String)

    A Cloud IAM identity, such as "user:[email protected]".



146
147
148
# File 'lib/google/cloud/storage/policy.rb', line 146

def add role_name, member
  role(role_name) << member
end

#deep_dupPolicy

Deprecated.

Because the latest policy is now always retrieved by Bucket#policy.

Returns a deep copy of the policy.

Returns:



212
213
214
215
216
217
218
219
220
# File 'lib/google/cloud/storage/policy.rb', line 212

def deep_dup
  warn "DEPRECATED: Storage::PolicyV1#deep_dup"
  dup.tap do |p|
    roles_dup = p.roles.transform_values do |v|
      v.dup rescue value
    end
    p.instance_variable_set :@roles, roles_dup
  end
end

#remove(role_name, member) ⇒ Object

Convenience method for removing a member from a binding on this policy. See Understanding Roles for a listing of primitive and curated roles. See Buckets: setIamPolicy for a listing of values and patterns for members.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.policy do |p|
  p.remove "roles/storage.admin", "user:[email protected]"
end

Parameters:

  • role_name (String)

    A Cloud IAM role, such as "roles/storage.admin".

  • member (String)

    A Cloud IAM identity, such as "user:[email protected]".



174
175
176
# File 'lib/google/cloud/storage/policy.rb', line 174

def remove role_name, member
  role(role_name).delete member
end

#role(role_name) ⇒ Array<String>

Convenience method returning the array of members bound to a role in this policy, or an empty array if no value is present for the role in #roles. See Understanding Roles for a listing of primitive and curated roles. See Buckets: setIamPolicy for a listing of values and patterns for members.

Examples:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.policy do |p|
  p.role("roles/storage.admin") << "user:[email protected]"
end

Returns:

  • (Array<String>)

    The members strings, or an empty array.



200
201
202
# File 'lib/google/cloud/storage/policy.rb', line 200

def role role_name
  roles[role_name] ||= []
end