Class: Jackal::CfnTools::AmiManager
- Inherits:
-
Jackal::Cfn::Resource
- Object
- Jackal::Callback
- Jackal::Cfn::Resource
- Jackal::CfnTools::AmiManager
- Defined in:
- lib/jackal-cfn/resource/ami_manager.rb
Overview
Manage AMI Resources
Expected resource:
{
"Type": "Custom::AmiManager",
"Properties": {
"Parameters": {
"InstanceId": "",
"AmiId": "",
"Action": "",
"Region": ""
}
}
}
Required configuration:
{
"config": {
"ami": {
"credentials": {
"compute": {
FOG_CREDENTIALS
}
}
}
}
}
Constant Summary collapse
- PHYSICAL_ID_JOINER =
'__-__'
Constants inherited from Jackal::Cfn::Resource
Jackal::Cfn::Resource::VALID_RESOURCE_STATUS
Instance Method Summary collapse
-
#ami_response_update(response, parameters) ⇒ Hash
Update the physical resource id to include the ami id.
-
#compute_api(region) ⇒ Fog::Compute
Build new compute api connection.
-
#destroy_ami(response, payload, parameters) ⇒ TrueClass
Destroy the AMI referenced by the resource.
-
#execute(message) ⇒ Object
Process message, send value back to CFN.
Methods inherited from Jackal::Cfn::Resource
#build_response, inherited, #physical_resource_id, #respond_to_stack, #setup, #unpack, #valid?
Methods included from Jackal::Cfn::Utils::Http
Methods included from Jackal::Cfn::Utils
#snakecase, #transform_parameters
Instance Method Details
#ami_response_update(response, parameters) ⇒ Hash
Update the physical resource id to include the ami id
66 67 68 69 70 71 72 |
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 66 def ami_response_update(response, parameters) response['PhysicalResourceId'] = [ response['PhysicalResourceId'], parameters[:ami_id] ].join(PHYSICAL_ID_JOINER) response end |
#compute_api(region) ⇒ Fog::Compute
Build new compute api connection
99 100 101 102 103 104 105 106 107 |
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 99 def compute_api(region) Fog::Compute.new( {:provider => :aws}.merge( config.fetch(:ami, :credentials, :compute).merge( :region => region ) ) ) end |
#destroy_ami(response, payload, parameters) ⇒ TrueClass
Destroy the AMI referenced by the resource
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 80 def destroy_ami(response, payload, parameters) ami_id = payload[:physical_resource_id].split(PHYSICAL_ID_JOINER).last begin compute_api(parameters[:region]).deregister_image(ami_id) rescue Fog::Compute::AWS::Error => e warn "Non-fatal error encountered on AMI removal: #{e.class}: #{e}" response['Reason'] = e. rescue => e error "Failed to remove AMI: #{e.class}: #{e}" response['Status'] = 'FAILED' response['Reason'] = e. end true end |
#execute(message) ⇒ Object
Process message, send value back to CFN
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jackal-cfn/resource/ami_manager.rb', line 39 def execute() failure_wrap do |payload| cfn_resource = rekey_hash(payload.get(:data, :cfn_resource)) properties = rekey_hash(cfn_resource[:resource_properties]) parameters = rekey_hash(properties[:parameters]) cfn_response = build_response(cfn_resource) case cfn_resource[:request_type] when :create ami_response_update(cfn_response, parameters) when :delete destroy_ami(cfn_response, cfn_resource, parameters) when :update else error "Unknown request type received: #{cfn_resource[:request_type].inspect}" response['Status'] = 'FAILED' response['Reason'] = 'Unknown request type received' end respond_to_stack(cfn_response, cfn_resource[:response_url]) completed(payload, ) end end |