Class: Update

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, settings, dry_run = nil) ⇒ Update

Returns a new instance of Update.



9
10
11
12
13
# File 'lib/update.rb', line 9

def initialize(env, settings, dry_run = nil)
    @env = env || 'noenv'
    @settings = settings
    @dry_run = dry_run
end

Instance Attribute Details

#dry_runObject

Returns the value of attribute dry_run.



3
4
5
# File 'lib/update.rb', line 3

def dry_run
  @dry_run
end

#envObject

Returns the value of attribute env.



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

def env
  @env
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/update.rb', line 5

def settings
  @settings
end

#svcObject

Returns the value of attribute svc.



6
7
8
# File 'lib/update.rb', line 6

def svc
  @svc
end

#tableObject

Returns the value of attribute table.



7
8
9
# File 'lib/update.rb', line 7

def table
  @table
end

Instance Method Details

#create_table_if_not_existsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/update.rb', line 35

def create_table_if_not_exists
  azure_table_service = Azure::TableService.new
  begin
    azure_table_service.create_table(@table)
  rescue
    puts $!
    puts "table : #{@table}"
    puts
  end
  azure_table_service
end

#echo(key) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/update.rb', line 94

def echo(key)
    entity = get(key)
    if !entity.nil?
        puts "echo: "
        p entity.properties
    end
end

#get(key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/update.rb', line 81

def get(key)

  begin
    result = @svc.get_entity(@table, @env, key)
  rescue
    puts $!
    puts "key : #{key}"
    puts
  end

  result
end

#updateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/update.rb', line 15

def update
    if @env == 'noenv'
        puts 'Environment name required to update settings.'
        return false
    end

    # error thrown by azure gem if these are bad
    Azure.config. = ENV['StorageAccount']
    Azure.config.storage_access_key = ENV['StorageAccountKey']
    @table = ENV['ConfigSettingsTable']

    puts
    puts 'Updating config table...'
    puts

    @svc = create_table_if_not_exists

    upsert_all(@settings)
end

#upsert(key, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/update.rb', line 54

def upsert(key, value)

  # check if setting exists
  result = get(key)

  entity = { 
              "setting" => value, 
              :PartitionKey => @env,
              :RowKey => key
            }

  if (result.nil?)
    @svc.insert_entity(@table, entity) if @dry_run.nil?
    puts ">>>>> inserted entity key: #{key} value: #{value}"
  else
        # don't reinsert same value
        if (result.properties['setting'] != value)
            @svc.delete_entity(@table, @env, key) if @dry_run.nil?
            @svc.insert_entity(@table, entity) if @dry_run.nil?
            puts ">>>>> Updated entity - key: #{key} value: #{value}"
        else
            puts "Same value: #{value} found for key: #{key}" if @dry_run.nil?
        end
  end

end

#upsert_all(settings) ⇒ Object



47
48
49
50
51
52
# File 'lib/update.rb', line 47

def upsert_all(settings)    
    settings.map {|k,v|
        upsert(k, v)
        #echo(k)
    }
end