# Cloud Spanner API - Class Google::Cloud::Spanner::Policy (v2.36.0)

2.36.0 (latest) 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.0 2.24.0 2.23.0 2.22.0 2.21.0 2.20.0 2.19.1 2.18.1 2.17.0 2.16.1 2.15.0 2.14.0 2.13.0 2.12.1 2.11.0 Reference documentation and code samples for the Cloud Spanner API class Google::Cloud::Spanner::Policy.

## Policy

Represents a Cloud IAM Policy for the Spanner service.

A common pattern for updating a resource's metadata, such as its Policy,
is to read the current data from the service, update the data locally,
and then send the modified data for writing. This pattern may result in
a conflict if two or more processes attempt the sequence simultaneously.
IAM solves this problem with the [#etag](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Policy#Google__Cloud__Spanner__Policy_etag_instance_ "Google::Cloud::Spanner::Policy#etag (method)")
property, which is used to verify whether the policy has changed since
the last request. When you make a request to with an `etag` value, Cloud
IAM compares the `etag` value in the request with the existing `etag`
value associated with the policy. It writes the policy only if the
`etag` values match.

When you update a policy, first read the policy (and its current `etag`)
from the service, then modify the policy locally, and then write the
modified policy to the service. See
[Instance#policy](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance#Google__Cloud__Spanner__Instance_policy_instance_ "Google::Cloud::Spanner::Instance#policy (method)") and
[Instance#policy=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Instance#Google__Cloud__Spanner__Instance_policy__instance_ "Google::Cloud::Spanner::Instance#policy= (method)") and
[Database#policy](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database#Google__Cloud__Spanner__Database_policy_instance_ "Google::Cloud::Spanner::Database#policy (method)") and
[Database#policy=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Database#Google__Cloud__Spanner__Database_policy__instance_ "Google::Cloud::Spanner::Database#policy= (method)").

## Inherits

- Object

## Example

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"

policy = instance.policy do |p|
  p.remove "roles/owner", "user:owner@example.com"
  p.add "roles/owner", "user:newowner@example.com"
  p.roles["roles/viewer"] = ["allUsers"]
end
```

## Methods

### #add

    def add(role_name, member)

Convenience method for adding a member to a binding on this policy.
See [Understanding
Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
listing of primitive and curated roles.
See [Binding](https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Binding)
for a listing of values and patterns for members.
**Parameters**

- **role_name** (String) --- A Cloud IAM role, such as `"roles/spanner.admin"`.
- **member** (String) --- A Cloud IAM identity, such as `"user:owner@example.com"`.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"

policy = instance.policy do |p|
  p.add "roles/owner", "user:newowner@example.com"
end
```

### #etag

    def etag() -> String

Used to verify whether the policy has changed since
the last request. The policy will be written only if the `etag` values
match.
**Returns**

- (String) --- the current value of etag

### #etag=

    def etag=(value) -> String

Used to verify whether the policy has changed since
the last request. The policy will be written only if the `etag` values
match.
**Parameter**

- **value** (String) --- the newly set value
**Returns**

- (String) --- the newly set value

### #remove

    def remove(role_name, member)

Convenience method for removing a member from a binding on this
policy. See [Understanding
Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
listing of primitive and curated roles. See
[Binding](https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Binding)
for a listing of values and patterns for members.
**Parameters**

- **role_name** (String) --- A Cloud IAM role, such as `"roles/spanner.admin"`.
- **member** (String) --- A Cloud IAM identity, such as `"user:owner@example.com"`.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"

policy = instance.policy do |p|
  p.remove "roles/owner", "user:owner@example.com"
end
```

### #role

    def 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](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-spanner/latest/Google-Cloud-Spanner-Policy#Google__Cloud__Spanner__Policy_roles_instance_ "Google::Cloud::Spanner::Policy#roles (method)"). See [Understanding
Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
listing of primitive and curated roles. See
[Binding](https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Binding)
for a listing of values and patterns for members.
**Returns**

- (Array\<String\>) --- The members strings, or an empty array.
**Example**

```ruby
require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new
instance = spanner.instance "my-instance"

policy = instance.policy do |p|
  p.role("roles/viewer") << "user:viewer@example.com"
end
```

### #roles

    def roles() -> Hash{String => Array<String>}

The bindings that associate
roles with an array of members. See [Understanding
Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
listing of primitive and curated roles.
See [Binding](https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Binding)
for a listing of values and patterns for members.
**Returns**

- (Hash{String =\> Array\<String\>}) --- the current value of roles

### #roles=

    def roles=(value) -> Hash{String => Array<String>}

The bindings that associate
roles with an array of members. See [Understanding
Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
listing of primitive and curated roles.
See [Binding](https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Binding)
for a listing of values and patterns for members.
**Parameter**

- **value** (Hash{String =\> Array\<String\>}) --- the newly set value
**Returns**

- (Hash{String =\> Array\<String\>}) --- the newly set value