Class: Chef::Provisioning::AWSDriver::AWSResource

Inherits:
SuperLWRP
  • Object
show all
Defined in:
lib/chef/provisioning/aws_driver/aws_resource.rb

Constant Summary collapse

NOT_PASSED =

This supports the introduction of the NOT_PASSED chef constant in chef 12.5 TODO remove when we make 12.5 and higher a dependency

Object.new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SuperLWRP

#_pv_is, lazy

Constructor Details

#initialize(name, run_context = nil) ⇒ AWSResource

Returns a new instance of AWSResource.



15
16
17
18
19
20
21
22
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 15

def initialize(name, run_context = nil)
  name = name.public_send(self.class.aws_sdk_class_id) if name.is_a?(self.class.aws_sdk_class)
  super
  if run_context
    driver run_context.chef_provisioning.current_driver
    chef_server run_context.cheffish.current_chef_server
  end
end

Class Attribute Details

.aws_id_attributeObject (readonly)

Returns the value of attribute aws_id_attribute.



204
205
206
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 204

def aws_id_attribute
  @aws_id_attribute
end

.aws_id_prefixObject (readonly)

Returns the value of attribute aws_id_prefix.



185
186
187
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 185

def aws_id_prefix
  @aws_id_prefix
end

.aws_sdk_classObject (readonly)

Returns the value of attribute aws_sdk_class.



177
178
179
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 177

def aws_sdk_class
  @aws_sdk_class
end

.aws_sdk_class_idObject (readonly)

Returns the value of attribute aws_sdk_class_id.



181
182
183
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 181

def aws_sdk_class_id
  @aws_sdk_class_id
end

.aws_sdk_option_nameObject (readonly)

Returns the value of attribute aws_sdk_option_name.



189
190
191
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 189

def aws_sdk_option_name
  @aws_sdk_option_name
end

Class Method Details

.get_aws_object(value, resource: nil, run_context: nil, driver: nil, managed_entry_store: nil, required: true) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 112

def self.get_aws_object(value, resource: nil, run_context: nil, driver: nil, managed_entry_store: nil, required: true)
  return nil if value.nil?

  if resource
    run_context         ||= resource.run_context
    driver              ||= resource.driver
    managed_entry_store ||= resource.managed_entry_store
  end
  if value.is_a?(self)
    resource = value
  else
    resource = new(value, run_context)
    resource.driver driver if driver
    resource.managed_entry_store managed_entry_store if managed_entry_store
  end

  result = resource.aws_object
  raise "#{self}[#{value}] does not exist!" if required && result.nil?
  result
end

.get_aws_object_id(value, **options) ⇒ Object



133
134
135
136
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 133

def self.get_aws_object_id(value, **options)
  aws_object = get_aws_object(value, **options)
  aws_object.public_send(aws_sdk_class_id) if aws_object
end

.lookup_options(options = {}, **handler_options) ⇒ Object

Look up an AWS options list, translating standard names using the appropriate classes.

For example, ‘load_balancer_options` is passed into `lookup_options`, and if it looks like this: `{ subnets: `[ ’subnet1’, ‘subnet2’ ] }‘, then `AWSResource.lookup_options` will translate each ID with `AwsSubnet.get_aws_object(’subnet1’)‘, which supports Chef names (`mysubnet`) as well as AWS subnet Ids (`subnet-1234abcd`) or AWS objects (`::Aws::EC2::Subnet`).

Keys that represent non-AWS-objects (such as ‘timeout`) are left alone.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 94

def self.lookup_options(options = {}, **handler_options)
  options = options.dup
  options.each do |name, value|
    if name.to_s.end_with?("s")
      handler_name = :"#{name[0..-2]}"
      if aws_option_handlers[handler_name]
        options[name] = [options[name]].flatten.map { |value| aws_option_handlers[handler_name].get_aws_object_id(value, **handler_options) }
      end
    else
      handler_name = name.to_sym
      if aws_option_handlers[handler_name]
        options[handler_name] = aws_option_handlers[handler_name].get_aws_object_id(value, **handler_options)
      end
    end
  end
  options
end

Instance Method Details

#action(*args) ⇒ Object

Backwards compatibility for action :destroy



25
26
27
28
29
30
31
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 25

def action(*args)
  if args == [:delete]
    super(:destroy)
  else
    super
  end
end

#action=(value) ⇒ Object



36
37
38
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 36

def action=(value)
  action(value)
end

#aws_objectObject

Get the current AWS object. This should return the aws_object even if it has a status like ‘deleted’ or ‘inactive’. If there is an aws_object, we return it. Callers will need to check the status if they care.

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 70

def aws_object
  raise NotImplementedError, :aws_object
end

#aws_object_idObject



74
75
76
77
78
79
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 74

def aws_object_id
  @aws_object_id ||= begin
    o = aws_object
    o.public_send(self.class.aws_sdk_class_id) if o
  end
end