Class: Chef::ResourceBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, name: nil, created_at: nil, params: nil, run_context: nil, cookbook_name: nil, recipe_name: nil, enclosing_provider: nil) ⇒ ResourceBuilder

FIXME (ruby-2.1 syntax): most of these are mandatory



34
35
36
37
38
39
40
41
42
43
# File 'lib/chef/resource_builder.rb', line 34

def initialize(type:nil, name:nil, created_at: nil, params: nil, run_context: nil, cookbook_name: nil, recipe_name: nil, enclosing_provider: nil)
  @type               = type
  @name               = name
  @created_at         = created_at
  @params             = params
  @run_context        = run_context
  @cookbook_name      = cookbook_name
  @recipe_name        = recipe_name
  @enclosing_provider = enclosing_provider
end

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



28
29
30
# File 'lib/chef/resource_builder.rb', line 28

def cookbook_name
  @cookbook_name
end

#created_atObject (readonly)

Returns the value of attribute created_at.



25
26
27
# File 'lib/chef/resource_builder.rb', line 25

def created_at
  @created_at
end

#enclosing_providerObject (readonly)

Returns the value of attribute enclosing_provider.



30
31
32
# File 'lib/chef/resource_builder.rb', line 30

def enclosing_provider
  @enclosing_provider
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/chef/resource_builder.rb', line 24

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



26
27
28
# File 'lib/chef/resource_builder.rb', line 26

def params
  @params
end

#recipe_nameObject (readonly)

Returns the value of attribute recipe_name.



29
30
31
# File 'lib/chef/resource_builder.rb', line 29

def recipe_name
  @recipe_name
end

#resourceObject (readonly)

Returns the value of attribute resource.



31
32
33
# File 'lib/chef/resource_builder.rb', line 31

def resource
  @resource
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



27
28
29
# File 'lib/chef/resource_builder.rb', line 27

def run_context
  @run_context
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/chef/resource_builder.rb', line 23

def type
  @type
end

Instance Method Details

#build(&block) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/resource_builder.rb', line 45

def build(&block)
  raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil?

  @resource = resource_class.new(name, run_context)
  if resource.resource_name.nil?
    raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`!  Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?"
  end
  resource.source_line = created_at
  resource.declared_type = type

  # If we have a resource like this one, we want to steal its state
  # This behavior is very counter-intuitive and should be removed.
  # See CHEF-3694, https://tickets.opscode.com/browse/CHEF-3694
  # Moved to this location to resolve CHEF-5052, https://tickets.opscode.com/browse/CHEF-5052
  if prior_resource
    resource.load_from(prior_resource)
  end

  resource.cookbook_name = cookbook_name
  resource.recipe_name = recipe_name
  # Determine whether this resource is being created in the context of an enclosing Provider
  resource.enclosing_provider = enclosing_provider

  # XXX: this is required for definition params inside of the scope of a
  # subresource to work correctly.
  resource.params = params

  # Evaluate resource attribute DSL
  if block_given?
    resource.resource_initializing = true
    begin
      resource.instance_eval(&block)
    ensure
      resource.resource_initializing = false
    end
  end

  # emit a cloned resource warning if it is warranted
  if prior_resource
    if is_trivial_resource?(prior_resource) && identicalish_resources?(prior_resource, resource)
      emit_harmless_cloning_debug
    else
      emit_cloned_resource_warning
    end
  end

  # Run optional resource hook
  resource.after_created

  resource
end