Class: Conjur::DSL::Runner

Inherits:
Object
  • Object
show all
Includes:
IdentifierManipulation
Defined in:
lib/conjur/dsl/runner.rb

Overview

Entry point for the Conjur DSL.

Methods are available in two categories: name scoping and asset building.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IdentifierManipulation

#conjur_account, #full_resource_id, #get_kind_and_id_from_args

Constructor Details

#initialize(script, filename = nil) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/conjur/dsl/runner.rb', line 13

def initialize(script, filename = nil)
  @context = {
    "env" => Conjur.env,
    "stack" => Conjur.stack,
    "account" => Conjur.,
    "api_keys" => {}
  }
  @script = script
  @filename = filename
  @api = nil
  @scopes = Array.new
  @owners = Array.new
  @objects = Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (protected)



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/conjur/dsl/runner.rb', line 119

def method_missing(sym, *args, &block)
  if create_compatible_args?(args) && api.respond_to?(sym)
    id = args[0]
    id = qualify_id(id) unless sym == :user
    find_or_create sym, id, args[1] || {}, &block
  elsif current_object && current_object.respond_to?(sym)
    current_object.send(sym, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def context
  @context
end

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def filename
  @filename
end

#scriptObject (readonly)

Returns the value of attribute script.



11
12
13
# File 'lib/conjur/dsl/runner.rb', line 11

def script
  @script
end

Instance Method Details

#apiObject



34
35
36
# File 'lib/conjur/dsl/runner.rb', line 34

def api
  @api ||= connect
end

#api_keysObject



42
43
44
# File 'lib/conjur/dsl/runner.rb', line 42

def api_keys
  @context["api_keys"]
end

#assetsObject

Provides a hash to export various application specific asset ids (or anything else you want)



30
31
32
# File 'lib/conjur/dsl/runner.rb', line 30

def assets
  @context['assets'] ||= {}
end

#create_variable(id = nil, options = {}, &block) ⇒ Object



94
95
96
97
98
# File 'lib/conjur/dsl/runner.rb', line 94

def create_variable id = nil, options = {}, &block
  options[:id] = id if id
  mime_type = options.delete(:mime_type)
  kind = options.delete(:kind)
end

#current_objectObject



46
47
48
# File 'lib/conjur/dsl/runner.rb', line 46

def current_object
  !@objects.empty? ? @objects.last : nil
end

#current_scopeObject



50
51
52
# File 'lib/conjur/dsl/runner.rb', line 50

def current_scope
  !@scopes.empty? ? @scopes.join('/') : nil
end

#executeObject



78
79
80
81
82
# File 'lib/conjur/dsl/runner.rb', line 78

def execute
  args = [ script ]
  args << filename if filename
  instance_eval(*args)
end

#namespace(ns = nil, &block) ⇒ Object Also known as: model



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/conjur/dsl/runner.rb', line 62

def namespace ns = nil, &block
  if block_given?
    ns ||= context["namespace"]
    if ns.nil?
      require 'conjur/api/variables'
      ns = context["namespace"] = api.create_variable("text/plain", "namespace").id
    end
    do_scope ns, &block
    context
  else
    @scopes[0]
  end
end

#ownsObject



100
101
102
103
104
105
106
107
# File 'lib/conjur/dsl/runner.rb', line 100

def owns
  @owners.push current_object
  begin
    yield
  ensure
    @owners.pop
  end
end

#resource(kind, id, options = {}, &block) ⇒ Object



84
85
86
87
# File 'lib/conjur/dsl/runner.rb', line 84

def resource kind, id, options = {}, &block
  id = full_resource_id([kind, qualify_id(id) ].join(':'))
  find_or_create :resource, id, options, &block
end

#role(kind, id, options = {}, &block) ⇒ Object



89
90
91
92
# File 'lib/conjur/dsl/runner.rb', line 89

def role kind, id, options = {}, &block
  id = full_resource_id([ kind, qualify_id(id) ].join(':'))
  find_or_create :role, id, options, &block
end

#scope(name = nil, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/conjur/dsl/runner.rb', line 54

def scope name = nil, &block
  if name != nil
    do_scope name, &block
  else
    current_scope
  end
end