Sha256: 93faf2fb999113f4b6d80bd37557995e1d2e13a99c1aee0698b1a244d4591ec1

Contents?: true

Size: 1.85 KB

Versions: 27

Compression:

Stored size: 1.85 KB

Contents

module Awspec::Type
  class S3Bucket < Base
    aws_resource Aws::S3::Bucket

    def initialize(id)
      super
      @resource_via_client = find_bucket(id)
      @id = id if @resource_via_client
    end

    def has_object?(key)
      res = s3_client.head_object({
                                    bucket: @id,
                                    key: key.sub(%r(\A/), '')
                                  })
      res
    rescue
      false
    end

    def has_acl_grant?(grantee:, permission:)
      @acl = find_bucket_acl(@id)
      @acl.grants.find do |grant|
        grant.permission == permission &&
          (grant.grantee.display_name == grantee || grant.grantee.uri == grantee || grant.grantee.id == grantee)
      end
    end

    def acl_owner
      @acl = find_bucket_acl(@id)
      @acl.owner.display_name
    end

    def acl_grants_count
      @acl = find_bucket_acl(@id)
      @acl.grants.count
    end

    def has_cors_rule?(allowed_headers: [],
                       allowed_methods:,
                       allowed_origins:,
                       expose_headers: [],
                       max_age_seconds: nil)
      cors_rules.any? do |rule|
        (rule.allowed_headers - allowed_headers).empty? &&
          (rule.allowed_methods - allowed_methods).empty? &&
          (rule.allowed_origins - allowed_origins).empty? &&
          (rule.expose_headers - expose_headers).empty? &&
          rule.max_age_seconds == max_age_seconds
      end
    end

    def cors_rules_count
      cors_rules.count
    end

    def has_policy?(policy)
      bp = find_bucket_policy(@id)
      # newlines, spaces, etc.. are removed from policy which is returned from API
      bp ? (bp.policy.read == policy.gsub(/(\n|\r|\r\n|\s|\t)/, '')) : false
    end

    private

    def cors_rules
      cors = find_bucket_cors(@id)
      cors ? cors.cors_rules : []
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
awspec-0.41.0 lib/awspec/type/s3_bucket.rb
awspec-0.40.0 lib/awspec/type/s3_bucket.rb
awspec-0.39.0 lib/awspec/type/s3_bucket.rb
awspec-0.38.0 lib/awspec/type/s3_bucket.rb
awspec-0.37.8 lib/awspec/type/s3_bucket.rb
awspec-0.37.7 lib/awspec/type/s3_bucket.rb
awspec-0.37.6 lib/awspec/type/s3_bucket.rb
awspec-0.37.5 lib/awspec/type/s3_bucket.rb
awspec-0.37.4 lib/awspec/type/s3_bucket.rb
awspec-0.37.3 lib/awspec/type/s3_bucket.rb
awspec-0.37.2 lib/awspec/type/s3_bucket.rb
awspec-0.37.1 lib/awspec/type/s3_bucket.rb
awspec-0.37.0 lib/awspec/type/s3_bucket.rb
awspec-0.36.1 lib/awspec/type/s3_bucket.rb
awspec-0.36.0 lib/awspec/type/s3_bucket.rb
awspec-0.35.0 lib/awspec/type/s3_bucket.rb
awspec-0.34.0 lib/awspec/type/s3_bucket.rb
awspec-0.33.0 lib/awspec/type/s3_bucket.rb
awspec-0.32.0 lib/awspec/type/s3_bucket.rb
awspec-0.31.0 lib/awspec/type/s3_bucket.rb