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.new
  else
    @sdb = AWS::SimpleDB.new(:region => region)
  end
end

Instance Method Details

#create(domain) ⇒ Object



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

def create(domain)
  AWS::SimpleDB.consistent_reads do
    @sdb.domains.create(domain)
  end
end

#destroy(domain) ⇒ Object



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

def destroy(domain)
  AWS::SimpleDB.consistent_reads do
    @sdb.domains[domain].delete
  end
end

#destroy_item(domain, item_name) ⇒ Object



51
52
53
54
55
# File 'lib/opendelivery/domain.rb', line 51

def destroy_item(domain, item_name)
  AWS::SimpleDB.consistent_reads do
    @sdb.domains[domain].items[item_name].delete
  end
end

#get_encrypted_property(domain, item_name, key, index = 0, name = nil) ⇒ Object



65
66
67
68
# File 'lib/opendelivery/domain.rb', line 65

def get_encrypted_property(domain, item_name, key, index=0, name=nil)
  value = get_property(domain, item_name, key, index, name)
  decrypted_value = EncryptoSigno.decrypt(@key_path, value.chomp)
end

#get_property(domain, item_name, key, index = 0, name = nil) ⇒ Object

Look for AWS::Some:Type|MyItemName or just AWS::Some::Type.



72
73
74
75
76
77
78
79
80
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
114
115
116
117
118
119
120
121
# File 'lib/opendelivery/domain.rb', line 72

def get_property(domain, item_name, key, index=0, name=nil)
  property_value = nil
  attribute      = nil

  AWS::SimpleDB.consistent_reads do
    item = @sdb.domains[domain].items[item_name]
    if !item.nil?
      item.attributes.each do |att|
        col_name = nil

        att_array = att.name.split('|')
        col_title = att_array.first

        if att_array.length > 1
          col_name = att_array[1]
        end

        if col_title == key
          # Found a column with first portion that matches our search 'key'
          # Now, determine if we need to match the "|name" name criteria.

          if name.nil?
            # Not given a name to search for, just return the first one
            # we have found.
            attribute = att
            break
          else

            # Give a 'name' search criteria, so match it against this column
            if name == col_name
              # 'name' criteria matches "|name" value, found a match
              attribute = att
              break
            else
              # 'name' criteria did not match, keep searching
            end

          end
        end
      end
      if !attribute.nil?
        value = attribute.values[index]
        if !value.nil?
          property_value = value.chomp
        end
      end
    end
  end
  return property_value
end

#load_domain(domain, json_file) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/opendelivery/domain.rb', line 137

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|
      AWS::SimpleDB.consistent_reads do
        @sdb.domains[domain].items[item].attributes.set(key => [value])
      end
    end
  end
end

#load_stack_properties(domain, stack) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/opendelivery/domain.rb', line 57

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

#set_encrypted_property(domain, item_name, key, value, name = nil) ⇒ Object



123
124
125
126
# File 'lib/opendelivery/domain.rb', line 123

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

#set_property(domain, item_name, key, value, name = nil) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/opendelivery/domain.rb', line 128

def set_property(domain, item_name, key, value, name=nil)
  if name then key = key + "|" + name end

  AWS::SimpleDB.consistent_reads do
    item = @sdb.domains[domain].items[item_name]
    item.attributes.set(key => [value])
  end
end