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

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SuperLWRP

#load_prior_resource

Constructor Details

#initialize(name, run_context = nil) ⇒ AWSResource

Returns a new instance of AWSResource.



11
12
13
14
15
16
17
18
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 11

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 Method Details

.attribute(name, aws_id_attribute: false, **validation_opts) ⇒ Object

Add support for aws_id_attribute: true



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

def self.attribute(name, aws_id_attribute: false, **validation_opts)
  @aws_id_attribute = name if aws_id_attribute
  super(name, validation_opts)
end

.aws_id_attributeObject



187
188
189
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 187

def self.aws_id_attribute
  @aws_id_attribute
end

.aws_id_prefixObject



168
169
170
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 168

def self.aws_id_prefix
  @aws_id_prefix
end

.aws_option_handlersObject



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

def self.aws_option_handlers
  @@aws_option_handlers
end

.aws_sdk_classObject



160
161
162
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 160

def self.aws_sdk_class
  @aws_sdk_class
end

.aws_sdk_class_idObject



164
165
166
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 164

def self.aws_sdk_class_id
  @aws_sdk_class_id
end

.aws_sdk_option_nameObject



172
173
174
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 172

def self.aws_sdk_option_name
  @aws_sdk_option_name
end

.aws_sdk_type(sdk_class, option_names: nil, option_name: NOT_PASSED, load_provider: true, id: :name, aws_id_prefix: nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 129

def self.aws_sdk_type(sdk_class,
                      option_names: nil,
                      option_name: NOT_PASSED,
                      load_provider: true,
                      id: :name,
                      aws_id_prefix: nil)
  self.resource_name = self.dsl_name
  @aws_sdk_class = sdk_class
  @aws_sdk_class_id = id
  @aws_id_prefix = aws_id_prefix

  # Go ahead and require the provider since we're here anyway ...
  require "chef/provider/#{resource_name}" if load_provider

  option_name = :"#{resource_name[4..-1]}" if option_name == NOT_PASSED
  @aws_sdk_option_name = option_name

  option_names ||= begin
    option_names = []
    option_names << aws_sdk_option_name
    option_names << :"#{option_name}_#{aws_sdk_class_id}" if aws_sdk_class_id
    option_names
  end
  option_names.each do |option_name|
    aws_option_handlers[option_name] = self
  end

  name = self.name.split('::')[-1]
  eval("Chef::Provisioning::AWSDriver::Resources::#{name} = self", binding, __FILE__, __LINE__)
end

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 98

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
  if required && result.nil?
    raise "#{self}[#{value}] does not exist!"
  end
  result
end

.get_aws_object_id(value, **options) ⇒ Object



120
121
122
123
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 120

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.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 81

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
      if aws_option_handlers[name]
        options[name] = aws_option_handlers[name].get_aws_object_id(value, **handler_options)
      end
    end
  end
  options
end

Instance Method Details

#action(*args) ⇒ Object

Backwards compatibility for action :destroy



21
22
23
24
25
26
27
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 21

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

#action=(value) ⇒ Object



32
33
34
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 32

def action=(value)
  action(value)
end

#aws_objectObject

Get the current AWS object.

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/chef/provisioning/aws_driver/aws_resource.rb', line 64

def aws_object
  raise NotImplementedError, :aws_object
end