Class: Cumulus::S3::GrantConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/s3/models/GrantConfig.rb

Constant Summary collapse

@@all_permissions =
["list", "update", "view-permissions", "edit-permissions"].sort

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json = nil) ⇒ GrantConfig

Public: Constructor

json - a hash representing the JSON configuration. Expects to be passed

an object from the "grants" array of S3 bucket configuration.


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/s3/models/GrantConfig.rb', line 58

def initialize(json = nil)
  if json
    @name = json["name"]
    @email = json["email"]
    @id = json["id"]
    @permissions = json["permissions"].sort
    if @permissions.include?("all")
      @permissions = @@all_permissions
    end
  end
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/s3/models/GrantConfig.rb', line 8

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/s3/models/GrantConfig.rb', line 9

def name
  @name
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



10
11
12
# File 'lib/s3/models/GrantConfig.rb', line 10

def permissions
  @permissions
end

Class Method Details

.to_aws_permission(cumulus_permission) ⇒ Object

Public: A static method that will produce the AWS version of the permission.

cumulus_permission - the string permission to convert

Returns the converted permission string



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/s3/models/GrantConfig.rb', line 41

def self.to_aws_permission(cumulus_permission)
  case cumulus_permission
  when "update"
    "WRITE"
  when "list"
    "READ"
  when "edit-permissions"
    "WRITE_ACP"
  when "view-permissions"
    "READ_ACP"
  end
end

.to_cumulus_permission(aws_permission) ⇒ Object

Public: A static method that will produce the Cumulus version of the permission so that the names we use in Cumulus are a little closer to the names in the AWS console.

aws_permission - the string permission to convert

Returns an array of the Cumulus version of the permission



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/s3/models/GrantConfig.rb', line 20

def self.to_cumulus_permission(aws_permission)
  case aws_permission
  when "FULL_CONTROL"
    @@all_permissions
  when "WRITE"
    ["update"]
  when "READ"
    ["list"]
  when "WRITE_ACP"
    ["edit-permissions"]
  when "READ_ACP"
    ["view-permissions"]
  end
end

Instance Method Details

#!=(other) ⇒ Object

Public: Check if this GrantConfig is not equal to the other object

other - the other object to check

Returns whether this GrantConfig is not equal to ‘other`



184
185
186
# File 'lib/s3/models/GrantConfig.rb', line 184

def !=(other)
  !(self == other)
end

#==(other) ⇒ Object

Public: Check GrantConfig equality with other objects.

other - the other object to check

Returns whether this GrantConfig is equal to ‘other`



168
169
170
171
172
173
174
175
176
177
# File 'lib/s3/models/GrantConfig.rb', line 168

def ==(other)
  if !other.is_a? GrantConfig or
      @name != other.name or
      @email != other.email or
      @permissions.sort != other.permissions.sort
    false
  else
    true
  end
end

#add_permissions!(permissions) ⇒ Object

Public: Add permissions to the permissions of this Grant.

permissions - an Array of the permissions to add



159
160
161
# File 'lib/s3/models/GrantConfig.rb', line 159

def add_permissions!(permissions)
  @permissions = (@permissions + permissions).uniq.sort
end

#diff(aws) ⇒ Object

Public: Produce an array of differences between this local configuration and the configuration in AWS

aws - the AWS resource

Returns an array of the GrantDiffs that were found



146
147
148
149
150
151
152
153
154
# File 'lib/s3/models/GrantConfig.rb', line 146

def diff(aws)
  diffs = []

  if @permissions != aws.permissions
    diffs << GrantDiff.new(GrantChange::PERMISSIONS, aws, self)
  end

  diffs
end

#populate!(aws) ⇒ Object

Public: Populate this GrantConfig from the avlues in an Aws::S3::Types::Grant

aws - the aws object to populate from



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/s3/models/GrantConfig.rb', line 74

def populate!(aws)
  @name = if aws.grantee.type == "CanonicalUser"
    aws.grantee.display_name
  else
    case aws.grantee.uri
    when "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
      "AuthenticatedUsers"
    when "http://acs.amazonaws.com/groups/global/AllUsers"
      "Everyone"
    when "http://acs.amazonaws.com/groups/s3/LogDelivery"
      "LogDelivery"
    end
  end
  @email = aws.grantee.email_address
  @permissions = GrantConfig.to_cumulus_permission(aws.permission)
  @id = aws.grantee.id
end

#to_awsObject

Public: Produce an AWS compatible array of hashes representing this GrantConfig.

Returns the array of hashes.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/s3/models/GrantConfig.rb', line 96

def to_aws
  @permissions.map do |permission|
    if @name == "AuthenticatedUsers"
      type = "Group"
      uri = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
    elsif @name == "Everyone"
      type = "Group"
      uri = "http://acs.amazonaws.com/groups/global/AllUsers"
    elsif @name == "LogDelivery"
      type = "Group"
      uri = "http://acs.amazonaws.com/groups/s3/LogDelivery"
    elsif @email
      type = "AmazonCustomerByEmail"
    else
      type = "CanonicalUser"
      display_name = @name
    end

    {
      grantee: {
        display_name: if !@email then @name end,
        email_address: if @email then @email end,
        id: if !@email then @id end,
        type: type,
        uri: uri,
      }.reject { |k, v| v.nil? },
      permission: GrantConfig.to_aws_permission(permission)
    }
  end
end

#to_hObject

Public: Converts this GrantConfig to a hash that matches Cumulus configuration.

Returns the hash



131
132
133
134
135
136
137
138
# File 'lib/s3/models/GrantConfig.rb', line 131

def to_h
  {
    name: @name,
    id: @id,
    email: @email,
    permissions: if @permissions.sort == @@all_permissions then ["all"] else @permissions.sort end,
  }.reject { |k, v| v.nil? }
end