Class: OpenDelivery::Domain

Inherits:
Object
  • Object
show all
Defined in:
lib/opendelivery/domain.rb

Instance Method Summary collapse

Constructor Details

#initialize(region = nil, key_path = nil) ⇒ Domain

Returns a new instance of Domain.



29
30
31
32
33
34
35
36
37
# File 'lib/opendelivery/domain.rb', line 29

def initialize(region=nil, key_path=nil)
  @key_path = File.read(key_path) unless key_path.nil?

  if region.nil?
    @sdb = Aws::SimpleDB::Client.new
  else
    @sdb = Aws::SimpleDB::Client.new(:region => region)
  end
end

Instance Method Details

#create(domain) ⇒ Object



39
40
41
# File 'lib/opendelivery/domain.rb', line 39

def create(domain)
  @sdb.create_domain(domain_name: domain)
end

#destroy(domain) ⇒ Object



43
44
45
# File 'lib/opendelivery/domain.rb', line 43

def destroy(domain)
  @sdb.delete_domain(domain_name: domain)
end

#destroy_item(domain, item_name) ⇒ Object



47
48
49
50
# File 'lib/opendelivery/domain.rb', line 47

def destroy_item(domain, item_name)
  @sdb.delete_attributes(domain_name: domain,
                         item_name: item_name)
end

#get_encrypted_property(domain, item_name, key) ⇒ Object



61
62
63
64
# File 'lib/opendelivery/domain.rb', line 61

def get_encrypted_property(domain, item_name, key)
  value = get_property(domain, item_name, key)
  EncryptoSigno.decrypt(@key_path, value.chomp)
end

#get_item_attributes_json(domain, item_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/opendelivery/domain.rb', line 79

def get_item_attributes_json(domain, item_name)
  get_attributes_response = @sdb.get_attributes(domain_name: domain,
                                                item_name: item_name,
                                                consistent_read: true)

  if get_attributes_response.attributes.empty?
    nil
  else
    JSON.generate(get_attributes_response.attributes.map { |attribute|
      { name: attribute.name, value: attribute.value }
    })
  end
end

#get_property(domain, item_name, key) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opendelivery/domain.rb', line 66

def get_property(domain, item_name, key)
  get_attributes_response = @sdb.get_attributes(domain_name: domain,
                                                item_name: item_name,
                                                attribute_names: [key],
                                                consistent_read: true)

  if get_attributes_response.attributes.empty?
    nil
  else
    get_attributes_response.attributes.first.value.chomp
  end
end

#load_domain(domain, json_file) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/opendelivery/domain.rb', line 110

def load_domain(domain, json_file)
  json = File.read(json_file)
  obj = JSON.parse(json)

  obj.each do |item, attributes|
    attributes.each do |key,value|
      set_property(domain, item, key, value)
    end
  end
end

#load_stack_properties(domain, stack) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/opendelivery/domain.rb', line 52

def load_stack_properties(domain, stack)
  stack.resources.each do |resource|
    set_property(domain,
                 stack.name,
                 resource.resource_type,
                 resource.physical_resource_id)
  end
end

#set_encrypted_property(domain, item_name, key, value) ⇒ Object



93
94
95
96
# File 'lib/opendelivery/domain.rb', line 93

def set_encrypted_property(domain, item_name, key, value)
  encrypted_value = EncryptoSigno.encrypt(@key_path, value.chomp)
  set_property(domain, item_name, key, encrypted_value)
end

#set_property(domain, item_name, key, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/opendelivery/domain.rb', line 98

def set_property(domain, item_name, key, value)
  @sdb.put_attributes(domain_name: domain,
                      item_name: item_name,
                      attributes: [
                        {
                          name: key,
                          value: value,
                          replace: true,
                        }
                      ])
end