Class: HolePunch::Definition

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Definition

Returns a new instance of Definition.



84
85
86
87
88
# File 'lib/holepunch/definition.rb', line 84

def initialize(env = nil)
  @env = env
  @groups = {}
  @services = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



69
70
71
# File 'lib/holepunch/definition.rb', line 69

def env
  @env
end

#groupsObject (readonly)

Returns the value of attribute groups.



70
71
72
# File 'lib/holepunch/definition.rb', line 70

def groups
  @groups
end

#servicesObject (readonly)

Returns the value of attribute services.



71
72
73
# File 'lib/holepunch/definition.rb', line 71

def services
  @services
end

Class Method Details

.build(file, env) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/holepunch/definition.rb', line 74

def build(file, env)
  filename = Pathname.new(file).expand_path
  unless filename.file?
    raise SecurityGroupsFileNotFoundError, "#{filename} not found"
  end

  DSL.evaluate(file, env)
end

Instance Method Details

#add_group(group) ⇒ Object

Raises:

  • (DuplicateGroupError)


90
91
92
93
# File 'lib/holepunch/definition.rb', line 90

def add_group(group)
  raise DuplicateGroupError, "another group already exists with id #{id}" if groups.include?(group.id)
  groups[group.id] = group
end

#validate!Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/holepunch/definition.rb', line 95

def validate!
  # verify group references are defined
  groups.each do |id, group|
    group.ingresses.each do |ingress|
      ingress.sources.each do |source|
        next if HolePunch.cidr?(source)
        unless groups.include?(source)
          raise GroupError, "group '#{source}' referenced by group '#{id}' does not exist"
        end
      end
    end
  end

  # verify service group references are defined
  services.each do |name, service|
    service.groups.each do |group|
      unless groups.include?(group)
        raise GroupDoesNotExistError, "group '#{group}' referenced by service '#{name}' does not exist"
      end
    end
  end
end