Class: AwsXRegionSync::AwsSync

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_xregion_sync/aws_sync.rb

Direct Known Subclasses

Ec2AmiSync, RdsAutomatedSnapshotSync

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sync_name, config) ⇒ AwsSync

Returns a new instance of AwsSync.



6
7
8
9
# File 'lib/aws_xregion_sync/aws_sync.rb', line 6

def initialize sync_name, config
  @sync_name = sync_name
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/aws_xregion_sync/aws_sync.rb', line 4

def config
  @config
end

#sync_nameObject (readonly)

Returns the value of attribute sync_name.



4
5
6
# File 'lib/aws_xregion_sync/aws_sync.rb', line 4

def sync_name
  @sync_name
end

Instance Method Details

#aws_configObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aws_xregion_sync/aws_sync.rb', line 36

def aws_config
  global = config['aws_client_config']
  # We want to make sure if we create a new config hash that we're
  # storing it so any changes made by a caller to the returned hash
  # are stored off
  unless global 
    global = {}
    config['aws_client_config'] = global
  end

  global
end

#create_sync_tag(region, source_identifier, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/aws_xregion_sync/aws_sync.rb', line 49

def create_sync_tag region, source_identifier, options = {}
  options = {timestamp: Time.now}.merge options

  timestamp = options[:timestamp].utc
  key = "#{sync_tag_indicator}"
  key += "-#{options[:sync_subtype]}" if options[:sync_subtype]
  key += "-#{region}"
  {key: key, value: build_sync_tag_value(source_identifier, timestamp)}
end

#discover_aws_account_id(raise_error_if_not_found = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aws_xregion_sync/aws_sync.rb', line 76

def  raise_error_if_not_found = true
  return @aws_account_id if defined?(@aws_account_id)

   = aws_config['aws_account_id']
  unless 
     = 
  end
  raise AwsXRegionSyncConfigError, "The #{self.sync_name} configuration must provide an 'aws_account_id' option to use for manipulating snapshot tags, unable to retrieve id automatically." if (.nil? || .length == 0) && raise_error_if_not_found

  @aws_account_id = 
end

#parse_config_filters(config_filters) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aws_xregion_sync/aws_sync.rb', line 24

def parse_config_filters config_filters
  # Splits all the given filters on '=' so they can be utilized in a standard describe filter clause
  filters = []
  config_filters.each do |cf|
    split = cf.to_s.split("=")
    raise AwsXRegionSyncConfigError, "The #{sync_name} configuration 'filters' value '#{cf}' must be of the form filter-field=filter-value." unless split.size == 2

    filters << split
  end
  filters
end

#parse_sync_tag_value(value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aws_xregion_sync/aws_sync.rb', line 59

def parse_sync_tag_value value
  # Sync Timestamp (YYYYMMDDHHmm) to second / resource identifier
  # AWS only allows 10 tags per resource so we're trying to limit the # of tags we consume by combining the timestamp and resource identifier into a single tag
  values = {}
  if value && value.length > 0
    split = value.split(" / ")
    values[:timestamp] = Time.strptime(split[0], timestamp_format).utc
    values[:resource_identifier] = split[1]
  end
  
  values
end

#sync_tag_indicatorObject



72
73
74
# File 'lib/aws_xregion_sync/aws_sync.rb', line 72

def sync_tag_indicator
  "Sync"
end

#validate_configObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/aws_xregion_sync/aws_sync.rb', line 11

def validate_config
  # We could validate if these are actual valid region names via the AWS client, but for now we'll assume we're actually getting something valid
  # as long as the option is there - the sync will blow up and give the reason anyway.
  raise AwsXRegionSyncConfigError, "The #{sync_name} configuration must have a valid 'source_region' value." unless config['source_region'] && config['source_region'].length > 0
  raise AwsXRegionSyncConfigError, "The #{sync_name} configuration must have a valid 'destination_region' value." unless config['destination_region'] && config['destination_region'].length > 0

  if config['filters']
    raise AwsXRegionSyncConfigError, "The #{sync_name} configuration 'filters' value must be an Enumerable object." unless config['filters'] && config['filters'].is_a?(Enumerable)
    parse_config_filters config['filters']
  end
  self
end