This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from rapid7/task/issue-99-support-extending-t…
…he-convection-dsl Fix #99 - Support extending the convection resource DSL
- Loading branch information
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'forwardable' | ||
require_relative './resource' | ||
require_relative '../mixin/conditional' | ||
|
||
module Convection | ||
module Model | ||
class Template | ||
# A collection of different {Convection::Model::Template::Resource}s. | ||
class ResourceCollection | ||
extend Forwardable | ||
include DSL::Helpers | ||
include DSL::Template::Resource | ||
include Mixin::Conditional | ||
|
||
attr_reader :name | ||
attr_reader :parent | ||
attr_reader :template | ||
|
||
def_delegator :@template, :stack | ||
|
||
class << self | ||
def attach_to_dsl(dsl_name) | ||
DSL::Template::Resource.attach_resource_collection(dsl_name, self) | ||
end | ||
end | ||
|
||
def initialize(name, parent, &definition) | ||
@definition = definition | ||
@name = name | ||
@parent = parent | ||
@template = parent.template | ||
end | ||
|
||
# @note This method is in place to be overriden by subclasses. | ||
def execute | ||
end | ||
|
||
def run_definition | ||
instance_exec(&@definition) if @definition | ||
end | ||
|
||
def resources | ||
@resources ||= Convection::Model::Collection.new | ||
end | ||
end | ||
end | ||
end | ||
end |
63 changes: 63 additions & 0 deletions
63
spec/convection/model/template/resource_collection_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'spec_helper' | ||
|
||
class Convection::Model::Template | ||
describe ResourceCollection do | ||
class WebService < Convection::Model::Template::ResourceCollection | ||
attach_to_dsl(:web_service) | ||
|
||
attribute :use_elastic_load_balancing | ||
|
||
def execute | ||
web_service = self # Expose this instance to nested template methods. | ||
|
||
ec2_instance "#{name}WebService" | ||
|
||
elb "#{name}LoadBalancer" do | ||
tag 'Description', "Load balancer for the #{web_service.name} web service." | ||
end if use_elastic_load_balancing | ||
end | ||
end | ||
|
||
let(:use_elb_value) { nil } | ||
let(:template) do | ||
outer_scope = self | ||
Convection.template do | ||
description 'ResourceCollection Test Template' | ||
|
||
# A lone resource for testing merging of resources and nested resources. | ||
ec2_instance 'LoneResource1' | ||
|
||
web_service 'ExampleDotOrg' do | ||
use_elastic_load_balancing outer_scope.use_elb_value | ||
end | ||
end | ||
end | ||
|
||
subject do | ||
template_json | ||
.fetch('Resources') | ||
end | ||
|
||
context 'when the use_elastic_load_balancing attribute is set' do | ||
let(:use_elb_value) { true } | ||
|
||
it { is_expected.to have_key('LoneResource1') } | ||
it { is_expected.to have_key('ExampleDotOrgWebService') } | ||
it { is_expected.to have_key('ExampleDotOrgLoadBalancer') } | ||
end | ||
|
||
context 'when the use_elastic_load_balancing attribute is not set' do | ||
let(:use_elb_value) { false } | ||
|
||
it { is_expected.to have_key('LoneResource1') } | ||
it { is_expected.to have_key('ExampleDotOrgWebService') } | ||
it { is_expected.to_not have_key('ExampleDotOrgLoadBalancer') } | ||
end | ||
|
||
private | ||
|
||
def template_json | ||
JSON.parse(template.to_json) | ||
end | ||
end | ||
end |