Class: Google::Cloud::Storage::Policy::Condition

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

Overview

Condition

Value object accepting an attribute-based logic expression based on a subset of the Common Expression Language (CEL).

Examples:

require "google/cloud/storage"

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

policy = bucket.policy requested_policy_version: 3
policy.bindings.each do |binding|
  puts binding.condition.title if binding.condition
end

Updating a Policy from version 1 to version 3 by adding a condition:

require "google/cloud/storage"

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

bucket.uniform_bucket_level_access = true

bucket.policy requested_policy_version: 3 do |p|
  p.version # the value is 1
  p.version = 3 # Must be explicitly set to opt-in to support for conditions.

  expr = "resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"
  p.bindings.insert({
                      role: "roles/storage.admin",
                      members: ["user:[email protected]"],
                      condition: {
                        title: "my-condition",
                        description: "description of condition",
                        expression: expr
                      }
                    })
end

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, expression:, description: nil) ⇒ Condition

Creates a Condition object.

Parameters:

  • title (String)

    Used to identify the condition. Required.

  • description (String) (defaults to: nil)

    Used to document the condition. Optional.

  • expression (String)

    Defines an attribute-based logic expression using a subset of the Common Expression Language (CEL). The condition expression can contain multiple statements, each uses one attributes, and statements are combined using logic operators, following CEL language specification. Required.



88
89
90
91
92
# File 'lib/google/cloud/storage/policy/condition.rb', line 88

def initialize title:, expression:, description: nil
  @title = String title
  @description = String description
  @expression = String expression
end

Instance Attribute Details

#descriptionString

Used to document the condition. Optional.

Returns:

  • (String)

    the current value of description



72
73
74
# File 'lib/google/cloud/storage/policy/condition.rb', line 72

def description
  @description
end

#expressionString

Defines an attribute-based logic expression using a subset of the Common Expression Language (CEL). The condition expression can contain multiple statements, each uses one attributes, and statements are combined using logic operators, following CEL language specification. Required.

Returns:

  • (String)

    the current value of expression



72
73
74
# File 'lib/google/cloud/storage/policy/condition.rb', line 72

def expression
  @expression
end

#titleString

Used to identify the condition. Required.

Returns:

  • (String)

    the current value of title



72
73
74
# File 'lib/google/cloud/storage/policy/condition.rb', line 72

def title
  @title
end

Instance Method Details

#to_gapiObject



127
128
129
130
131
132
133
# File 'lib/google/cloud/storage/policy/condition.rb', line 127

def to_gapi
  {
    title: @title,
    description: @description,
    expression: @expression
  }.delete_if { |_, v| v.nil? }
end