Class: Ec2::ProfileDsl

Inherits:
Object
  • Object
show all
Includes:
Helper, Logger
Defined in:
lib/ec2/profile_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#error

Methods included from Logger

#debug?, #logger, logger, #stderr, stderr

Constructor Details

#initialize(file, api:) ⇒ ProfileDsl

Returns a new instance of ProfileDsl.



15
16
17
18
19
20
21
22
# File 'lib/ec2/profile_dsl.rb', line 15

def initialize(file, api:)
  @file = file
  @profiles = {}
  @api = api
  @templates = {}
  @availability_zones = ["a", "b"]
  @required = Set.new
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



13
14
15
# File 'lib/ec2/profile_dsl.rb', line 13

def api
  @api
end

Instance Method Details

#availability_zones(*zones) ⇒ Object



69
70
71
# File 'lib/ec2/profile_dsl.rb', line 69

def availability_zones(*zones)
  @availability_zones = zones
end

#base_profile(name) ⇒ Object



61
62
63
# File 'lib/ec2/profile_dsl.rb', line 61

def base_profile(name)
  @base_profile = name
end

#base_subnet(name) ⇒ Object



65
66
67
# File 'lib/ec2/profile_dsl.rb', line 65

def base_subnet(name)
  @base_subnet = name
end

#import(path) ⇒ Object



88
89
90
91
92
93
# File 'lib/ec2/profile_dsl.rb', line 88

def import(path)
  abs_path = File.realpath path
  return if @required.include? abs_path
  instance_eval((File.read abs_path), abs_path)
  @required << abs_path
end

#mprofile(name, template: nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ec2/profile_dsl.rb', line 39

def mprofile(name, template: nil, &block)
  data = @templates.fetch template if template
  @availability_zones.each do |az|
    profile = Profile.new(data: data, api: api)
    profile.extends(@base_profile) if @base_profile
    profile.subnet("#{@base_subnet}-#{az}")
    profile.create &block
    profile_name = "#{name}-#{az}"
    profile.data.freeze
    @profiles[profile_name] = profile.data
  end
end

#profile(name, template: nil, &block) ⇒ Object



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

def profile(name, template: nil, &block)
  data = @templates.fetch template if template
  profile = Profile.new(data: data, api: api)
  profile.extends(@base_profile) if @base_profile
  profile.create &block
  profile.data.freeze
  @profiles[name] = profile.data
end

#renderObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/ec2/profile_dsl.rb', line 73

def render
  if not File.readable? @file
    logger.info "#{@file} not readable"
    return
  end
  instance_eval(File.read(@file), @file)
  YAML.dump @profiles
rescue NoMethodError => e
  error "invalid option used in profiles config: #{e.name}"
end

#template(name, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/ec2/profile_dsl.rb', line 31

def template(name, &block)
  profile = Profile.new(api: api)
  profile.extends(@base_profile) if @base_profile
  profile.create(transform: false, &block)
  @templates[name] = profile.data
  @templates[name].freeze
end

#use(*templates) ⇒ Object



24
25
26
27
28
29
# File 'lib/ec2/profile_dsl.rb', line 24

def use(*templates)
  templates.each do |t|
    t = t.to_s
    mprofile(t, template: t){}
  end
end

#vpc_id(vpc_id) ⇒ Object



84
85
86
# File 'lib/ec2/profile_dsl.rb', line 84

def vpc_id(vpc_id)
  @vpc_id = vpc_id
end