Class: Cloudkeeper::Aws::ProtoHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkeeper/aws/proto_helper.rb

Overview

Module refining basic GRPC structs with additional methods

used for conversion from one format to another

Constant Summary collapse

APPLIANCE_PREFIX =
'cloudkeeper_appliance_'.freeze
IMAGE_PREFIX =
'cloudkeeper_image_'.freeze
NAME_TAG_KEY =
'Name'.freeze
EXTRA_APPLIANCE_TAGS =
%i[description title].freeze

Class Method Summary collapse

Class Method Details

.appliance_from_tags(tags) ⇒ Object



39
40
41
42
43
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 39

def appliance_from_tags(tags)
  appliance = appliance_prepare_values(prepare_tags(tags, APPLIANCE_PREFIX))
  appliance[:image] = image_from_tags(tags)
  CloudkeeperGrpc::Appliance.new(appliance)
end

.appliance_prepare_values(appliance) ⇒ Object



45
46
47
48
49
50
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 45

def appliance_prepare_values(appliance)
  appliance[:ram] = appliance[:ram].to_i
  appliance[:core] = appliance[:core].to_i
  appliance[:expiration_date] = appliance[:expiration_date].to_i
  appliance
end

.appliance_to_tags(appliance) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 28

def appliance_to_tags(appliance)
  appliance_hash = appliance.to_hash
  shorten_extra_tags!(appliance_hash)
  image = appliance_hash.delete(:image)
  tags = appliance_hash.map { |k, v| { key: "#{APPLIANCE_PREFIX}#{k}", value: v.to_s } }
  tags += image_to_tags(image) if image
  tags << { key: Cloudkeeper::Aws::FilterHelper::TAG_CLOUDKEEPER_IDENTIFIER,
            value: Cloudkeeper::Aws::Settings['identifier'] }
  tags << { key: NAME_TAG_KEY, value: appliance_hash[:title] }
end

.filter_tags(tags, prefix) ⇒ Object



12
13
14
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 12

def filter_tags(tags, prefix)
  tags.select { |tag| tag[:key].include?(prefix) }
end

.image_from_tags(tags) ⇒ Object



56
57
58
59
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 56

def image_from_tags(tags)
  image = image_prepare_values(prepare_tags(tags, IMAGE_PREFIX))
  CloudkeeperGrpc::Image.new(image)
end

.image_prepare_values(image) ⇒ Object



61
62
63
64
65
66
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 61

def image_prepare_values(image)
  image[:size] = image[:size].to_i
  image[:mode] = image[:mode].upcase.to_sym
  image[:format] = image[:format].upcase.to_sym
  image
end

.image_to_tags(image) ⇒ Object



52
53
54
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 52

def image_to_tags(image)
  image.to_hash.map { |k, v| { key: "#{IMAGE_PREFIX}#{k}", value: v.to_s } }
end

.prepare_tags(tags, prefix) ⇒ Object



20
21
22
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 20

def prepare_tags(tags, prefix)
  remove_prefix(filter_tags(tags, prefix), prefix)
end

.remove_prefix(tags, prefix) ⇒ Object



16
17
18
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 16

def remove_prefix(tags, prefix)
  tags.map { |tag| { tag[:key].sub(prefix, '').to_sym => tag[:value] } }.reduce(&:merge)
end

.shorten_extra_tags!(appliance_hash) ⇒ Object



24
25
26
# File 'lib/cloudkeeper/aws/proto_helper.rb', line 24

def shorten_extra_tags!(appliance_hash)
  EXTRA_APPLIANCE_TAGS.each { |key| appliance_hash[key] = appliance_hash[key][0..254] }
end