Class: Cumulus::S3::BucketConfig

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

Overview

Public: An object representing configuration for an S3 bucket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, json = nil) ⇒ BucketConfig

Public: Constructor

name - the name of the bucket json - a hash containing the JSON configuration for the bucket



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/s3/models/BucketConfig.rb', line 81

def initialize(name, json = nil)
  @name = name
  if json
    @region = json["region"]
    @tags = json["tags"] || {}
    if json["permissions"]["cors"]
      @cors = Loader.cors_policy(
        json["permissions"]["cors"]["template"],
        json["permissions"]["cors"]["vars"] || {}
      )
    end
    if json["permissions"]["policy"]
      @policy = Loader.bucket_policy(
        json["permissions"]["policy"]["template"],
        json["permissions"]["policy"]["vars"] || {}
      )
    end
    if json["permissions"]["grants"]
      @grants = Hash[json["permissions"]["grants"].map do |g|
        [g["name"], GrantConfig.new(g)]
      end]
    end
    if json["default_encryption"]
      @default_encryption = DefaultEncryptionConfig.new(json["default_encryption"])
    end
    @website = if json["website"] then WebsiteConfig.new(json["website"]) end
    @logging = if json["logging"] then LoggingConfig.new(json["logging"]) end
    @notifications = Hash[(json["notifications"] || []).map { |n| [n["name"], NotificationConfig.new(n)] }]
    @lifecycle = Hash[(json["lifecycle"] || []).map { |l| [l["name"], LifecycleConfig.new(l)] }]
    @versioning = json["versioning"] || false
    @replication = if json["replication"] then ReplicationConfig.new(json["replication"]) end
  end
end

Instance Attribute Details

#corsObject (readonly)

Returns the value of attribute cors.



63
64
65
# File 'lib/s3/models/BucketConfig.rb', line 63

def cors
  @cors
end

#default_encryptionObject (readonly)

Returns the value of attribute default_encryption.



75
76
77
# File 'lib/s3/models/BucketConfig.rb', line 75

def default_encryption
  @default_encryption
end

#grantsObject (readonly)

Returns the value of attribute grants.



64
65
66
# File 'lib/s3/models/BucketConfig.rb', line 64

def grants
  @grants
end

#lifecycleObject (readonly)

Returns the value of attribute lifecycle.



65
66
67
# File 'lib/s3/models/BucketConfig.rb', line 65

def lifecycle
  @lifecycle
end

#loggingObject (readonly)

Returns the value of attribute logging.



66
67
68
# File 'lib/s3/models/BucketConfig.rb', line 66

def logging
  @logging
end

#nameObject (readonly)

Returns the value of attribute name.



67
68
69
# File 'lib/s3/models/BucketConfig.rb', line 67

def name
  @name
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



68
69
70
# File 'lib/s3/models/BucketConfig.rb', line 68

def notifications
  @notifications
end

#policyObject (readonly)

Returns the value of attribute policy.



69
70
71
# File 'lib/s3/models/BucketConfig.rb', line 69

def policy
  @policy
end

#regionObject (readonly)

Returns the value of attribute region.



70
71
72
# File 'lib/s3/models/BucketConfig.rb', line 70

def region
  @region
end

#replicationObject (readonly)

Returns the value of attribute replication.



71
72
73
# File 'lib/s3/models/BucketConfig.rb', line 71

def replication
  @replication
end

#tagsObject (readonly)

Returns the value of attribute tags.



72
73
74
# File 'lib/s3/models/BucketConfig.rb', line 72

def tags
  @tags
end

#versioningObject (readonly)

Returns the value of attribute versioning.



73
74
75
# File 'lib/s3/models/BucketConfig.rb', line 73

def versioning
  @versioning
end

#websiteObject (readonly)

Returns the value of attribute website.



74
75
76
# File 'lib/s3/models/BucketConfig.rb', line 74

def website
  @website
end

Instance Method Details

#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 BucketDiffs that were found



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/s3/models/BucketConfig.rb', line 196

def diff(aws)
  diffs = []

  if @tags != Hash[aws.tagging.safe_tags.map { |t| [t.key, t.value] }]
    diffs << BucketDiff.new(BucketChange::TAGS, aws, self)
  end
  if @policy != aws.policy.policy_string and !(@policy.nil? and aws.policy.policy_string == "")
    diffs << BucketDiff.new(BucketChange::POLICY, aws, self)
  end
  if @cors != aws.cors.rules and !(@cors.nil? and aws.cors.rules == [])
    diffs << BucketDiff.new(BucketChange::CORS, aws, self)
  end
  if @website != aws.website.to_cumulus
    diffs << BucketDiff.new(BucketChange::WEBSITE, aws, self)
  end
  if @logging != aws.logging.to_cumulus
    diffs << BucketDiff.new(BucketChange::LOGGING, aws, self)
  end
  if @versioning != aws.versioning.enabled
    diffs << BucketDiff.new(BucketChange::VERSIONING, aws, self)
  end

  grants_diffs = diff_grants(@grants, aws.acl.to_cumulus)
  if !grants_diffs.empty?
    diffs << BucketDiff.grant_changes(grants_diffs, self)
  end

  notification_diffs = diff_notifications(@notifications, aws.notification.to_cumulus)
  if !notification_diffs.empty?
    diffs << BucketDiff.notification_changes(notification_diffs, self)
  end

  lifecycle_diffs = diff_lifecycle(@lifecycle, aws.lifecycle.to_cumulus)
  if !lifecycle_diffs.empty?
    diffs << BucketDiff.lifecycle_changes(lifecycle_diffs, self)
  end

  aws_replication = aws.replication
  if aws_replication then aws_replication = aws_replication.to_cumulus end
  replication_diffs = diff_replication(@replication, aws_replication)
  if !replication_diffs.empty?
    diffs << BucketDiff.replication_changes(replication_diffs, self)
  end

  aws_default_encryption = aws.default_encryption
  if aws_default_encryption then aws_default_encryption = aws_default_encryption.to_cumulus end
  default_encryption_diffs = diff_encryption(@default_encryption, aws_default_encryption)
  if !default_encryption_diffs.empty?
    diffs << BucketDiff.default_encryption_changes(default_encryption_diffs, self)
  end

  diffs
end

#populate!(aws, cors, policies) ⇒ Object

Public: Populate this BucketConfig from the values in an AWS bucket.

aws - the aws resource cors - a hash of the names of cors policies to the string value of those policies policies - a hash of the names of policies to the string value of those policies

Returns the key names of the new policy or cors policy so they can be written to file immediately



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/s3/models/BucketConfig.rb', line 123

def populate!(aws, cors, policies)
  @region = aws.location
  @grants = aws.acl.to_cumulus
  @website = aws.website.to_cumulus
  @logging = aws.logging.to_cumulus
  @notifications = aws.notification.to_cumulus
  @lifecycle = aws.lifecycle.to_cumulus
  @versioning = aws.versioning.enabled
  @replication = aws.replication.to_cumulus rescue nil
  @tags = Hash[aws.tagging.safe_tags.map { |t| [t.key, t.value] }]
  default_encryption = aws.default_encryption
  if default_encryption
    @default_encryption = default_encryption.to_cumulus
  end

  policy = aws.policy.policy_string
  if policy and policy != ""
    policy = JSON.pretty_generate(JSON.parse(policy))
    if policies.has_value? policy
      @policy_name = policies.key(policy)
    else
      @policy_name = "#{@name}-policy"
      policies[@policy_name] = policy
      @new_policy_key = @policy_name
    end
  end

  cors_string = JSON.pretty_generate(aws.cors.rules.map(&:to_h))
  if cors_string and !aws.cors.rules.empty?
    if cors.has_value? cors_string
      @cors_name = cors.key(cors_string)
    else
      @cors_name = "#{@name}-cors"
      cors[@cors_name] = cors_string
      @new_cors_key = @cors_name
    end
  end

  return @new_policy_key, @new_cors_key
end

#pretty_jsonObject

Public: Produce a pretty JSON version of this BucketConfig.

Returns the pretty JSON string.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/s3/models/BucketConfig.rb', line 167

def pretty_json
  JSON.pretty_generate({
    region: @region,
    permissions: {
      policy: if @policy_name then {
        template: @policy_name,
      } end,
      cors: if @cors_name then {
        template: @cors_name,
      } end,
      grants: @grants.values.map(&:to_h)
    }.reject { |k, v| v.nil? },
    website: if @website then @website.to_h end,
    logging: if @logging then @logging.to_h end,
    notifications: if !@notifications.empty? then @notifications.values.map(&:to_h) end,
    lifecycle: if !@lifecycle.empty? then @lifecycle.values.map(&:to_h) end,
    versioning: @versioning,
    replication: if @replication then @replication.to_h end,
    default_encryption: if @default_encryption then @default_encryption end,
    tags: @tags,
  }.reject { |k, v| v.nil? })
end