From 875d14d7afd44661898e808d255017c6db36e44f Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 1 Apr 2014 07:27:02 -0700 Subject: [PATCH 1/9] Add abstract orchestration modeling --- lib/fog/orchestration.rb | 6 +- lib/fog/orchestration/models/event.rb | 26 ++++++ lib/fog/orchestration/models/events.rb | 19 ++++ lib/fog/orchestration/models/output.rb | 21 +++++ lib/fog/orchestration/models/outputs.rb | 20 +++++ lib/fog/orchestration/models/resource.rb | 26 ++++++ lib/fog/orchestration/models/resources.rb | 24 +++++ lib/fog/orchestration/models/stack.rb | 103 ++++++++++++++++++++++ lib/fog/orchestration/models/stacks.rb | 19 ++++ 9 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 lib/fog/orchestration/models/event.rb create mode 100644 lib/fog/orchestration/models/events.rb create mode 100644 lib/fog/orchestration/models/output.rb create mode 100644 lib/fog/orchestration/models/outputs.rb create mode 100644 lib/fog/orchestration/models/resource.rb create mode 100644 lib/fog/orchestration/models/resources.rb create mode 100644 lib/fog/orchestration/models/stack.rb create mode 100644 lib/fog/orchestration/models/stacks.rb diff --git a/lib/fog/orchestration.rb b/lib/fog/orchestration.rb index bd641ba..06961cc 100644 --- a/lib/fog/orchestration.rb +++ b/lib/fog/orchestration.rb @@ -10,7 +10,11 @@ def self.new(attributes) provider = attributes.delete(:provider).to_s.downcase.to_sym if self.providers.include?(provider) - require "fog/#{provider}/network" + begin + require "fog/#{provider}/network" + rescue LoadError + # is there a reason for this being automatic? + end return Fog::Orchestration.const_get(Fog.providers[provider]).new(attributes) end diff --git a/lib/fog/orchestration/models/event.rb b/lib/fog/orchestration/models/event.rb new file mode 100644 index 0000000..64d6326 --- /dev/null +++ b/lib/fog/orchestration/models/event.rb @@ -0,0 +1,26 @@ +require 'fog/core/model' + +module Fog + module Orchestration + + class Event < Fog::Model + + def self.inherited(klass) + klass.class_eval do + identity :id + + attribute :id + attribute :event_time + attribute :links + attribute :logical_resource_id + attribute :physical_resource_id + attribute :resource_name + attribute :resource_status + attribute :resource_status_reason + end + end + + end + + end +end diff --git a/lib/fog/orchestration/models/events.rb b/lib/fog/orchestration/models/events.rb new file mode 100644 index 0000000..92ee5f1 --- /dev/null +++ b/lib/fog/orchestration/models/events.rb @@ -0,0 +1,19 @@ +require 'fog/core/collection' + +module Fog + module Orchestration + + class Events < Fog::Collection + + def all(stack) + raise NotImplemented + end + + def find_by_id(id) + self.find {|event| event.id == id} + end + alias_method :get, :find_by_id + end + + end +end diff --git a/lib/fog/orchestration/models/output.rb b/lib/fog/orchestration/models/output.rb new file mode 100644 index 0000000..fc143a8 --- /dev/null +++ b/lib/fog/orchestration/models/output.rb @@ -0,0 +1,21 @@ +require 'fog/core/model' + +module Fog + module Orchestration + + class Output < Fog::Model + + def self.inherited(klass) + klass.class_eval do + identity :key + + attribute :key + attribute :value + attribute :description + end + end + + end + + end +end diff --git a/lib/fog/orchestration/models/outputs.rb b/lib/fog/orchestration/models/outputs.rb new file mode 100644 index 0000000..0adc55f --- /dev/null +++ b/lib/fog/orchestration/models/outputs.rb @@ -0,0 +1,20 @@ +require 'fog/core/collection' + +module Fog + module Orchestration + + class Outputs < Fog::Collection + + def all(stack) + raise NotImplemented + end + + def find_by_key(key) + self.find {|output| output.key == key} + end + alias_method :find_by_id, :find_by_key + alias_method :get, :find_by_key + end + + end +end diff --git a/lib/fog/orchestration/models/resource.rb b/lib/fog/orchestration/models/resource.rb new file mode 100644 index 0000000..c17bfce --- /dev/null +++ b/lib/fog/orchestration/models/resource.rb @@ -0,0 +1,26 @@ +require 'fog/core/model' + +module Fog + module Orchestration + + class Resource < Fog::Model + + def self.inherited(klass) + klass.class_eval do + identity :physical_resource_id + + attribute :resource_name + attribute :links + attribute :logical_resource_id + attribute :physical_resource_id + attribute :resource_type + attribute :resource_status + attribute :resource_status_reason + attribute :updated_time + end + end + + end + + end +end diff --git a/lib/fog/orchestration/models/resources.rb b/lib/fog/orchestration/models/resources.rb new file mode 100644 index 0000000..8e5c2ab --- /dev/null +++ b/lib/fog/orchestration/models/resources.rb @@ -0,0 +1,24 @@ +require 'fog/core/collection' + +module Fog + module Orchestration + + class Resources < Fog::Collection + + def all(stack) + raise NotImplemented + end + + def find_by_physical_id(id) + self.find {|resource| resource.physical_resource_id == id} + end + alias_method :find_by_id, :find_by_physical_id + alias_method :get, :find_by_id + + def find_by_logical_id(id) + self.find {|resource| resource.logical_resource_id == id} + end + end + + end +end diff --git a/lib/fog/orchestration/models/stack.rb b/lib/fog/orchestration/models/stack.rb new file mode 100644 index 0000000..0d5d7bc --- /dev/null +++ b/lib/fog/orchestration/models/stack.rb @@ -0,0 +1,103 @@ +require 'fog/core/model' + +module Fog + module Orchestration + + class Stack < Fog::Model + + class << self + + def resources(model_klass=nil) + if(model_klass) + @resources_model = model_klass + end + @resources_model + end + + def events(model_klass=nil) + if(model_klass) + @events_model = model_klass + end + @events_model + end + + def outputs(model_klass=nil) + if(model_klass) + @outputs_model = model_klass + end + @outputs_model + end + + def inherited(klass) + klass.class_eval do + identity :id + + attribute :stack_name + attribute :stack_status + attribute :stack_status_reason + attribute :creation_time + attribute :updated_time + attribute :id + + attribute :template_url + attribute :template + attribute :parameters + attribute :timeout_in_minutes + attribute :disable_rollback + attribute :capabilities + attribute :notification_topics + attribute :template_description + end + end + + end + + def save + requires :stack_name + identity ? update : create + end + + def create + raise NotImlemented + end + + def update + raise NotImlemented + end + + def destroy + raise NotImlemented + end + + def resources + if(self.class.resources) + self.class.resources.new(:service => service).all(self) + else + raise NotImplemented + end + end + + def events + if(self.class.events) + self.class.events.new(:service => service).all(self) + else + raise NotImplemented + end + end + + def outputs + if(self.class.outputs) + self.class.outputs.new(:service => service).all(self) + else + raise NotImplemented + end + end + + def validate + requires :template + raise NotImplemented + end + + end + end +end diff --git a/lib/fog/orchestration/models/stacks.rb b/lib/fog/orchestration/models/stacks.rb new file mode 100644 index 0000000..1f20d2a --- /dev/null +++ b/lib/fog/orchestration/models/stacks.rb @@ -0,0 +1,19 @@ +require 'fog/core/collection' + +module Fog + module Orchestration + + class Stacks < Fog::Collection + + def all + raise NotImplemented + end + + def find_by_id(id) + self.find {|stack| stack.id == id} + end + alias_method :get, :find_by_id + end + + end +end From 4a2e5cb2cbc9ce6f2d029aaff1bc12a4e9e0a769 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 3 Jun 2014 11:38:28 -0700 Subject: [PATCH 2/9] Remove #find_by_id methods and aliases. Update with #get where required. --- lib/fog/orchestration/models/events.rb | 4 ++-- lib/fog/orchestration/models/outputs.rb | 5 ++--- lib/fog/orchestration/models/resources.rb | 3 +-- lib/fog/orchestration/models/stacks.rb | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/fog/orchestration/models/events.rb b/lib/fog/orchestration/models/events.rb index 92ee5f1..13989e6 100644 --- a/lib/fog/orchestration/models/events.rb +++ b/lib/fog/orchestration/models/events.rb @@ -9,10 +9,10 @@ def all(stack) raise NotImplemented end - def find_by_id(id) + def get(id) self.find {|event| event.id == id} end - alias_method :get, :find_by_id + end end diff --git a/lib/fog/orchestration/models/outputs.rb b/lib/fog/orchestration/models/outputs.rb index 0adc55f..2ae6ee3 100644 --- a/lib/fog/orchestration/models/outputs.rb +++ b/lib/fog/orchestration/models/outputs.rb @@ -9,11 +9,10 @@ def all(stack) raise NotImplemented end - def find_by_key(key) + def get(key) self.find {|output| output.key == key} end - alias_method :find_by_id, :find_by_key - alias_method :get, :find_by_key + end end diff --git a/lib/fog/orchestration/models/resources.rb b/lib/fog/orchestration/models/resources.rb index 8e5c2ab..9a113ed 100644 --- a/lib/fog/orchestration/models/resources.rb +++ b/lib/fog/orchestration/models/resources.rb @@ -12,8 +12,7 @@ def all(stack) def find_by_physical_id(id) self.find {|resource| resource.physical_resource_id == id} end - alias_method :find_by_id, :find_by_physical_id - alias_method :get, :find_by_id + alias_method :get, :find_by_physical_id def find_by_logical_id(id) self.find {|resource| resource.logical_resource_id == id} diff --git a/lib/fog/orchestration/models/stacks.rb b/lib/fog/orchestration/models/stacks.rb index 1f20d2a..35fdb8f 100644 --- a/lib/fog/orchestration/models/stacks.rb +++ b/lib/fog/orchestration/models/stacks.rb @@ -9,10 +9,10 @@ def all raise NotImplemented end - def find_by_id(id) + def get(id) self.find {|stack| stack.id == id} end - alias_method :get, :find_by_id + end end From 3c070a9b63c0dbc874ecb6a4680671cfab09c1fa Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 14 Jul 2014 16:01:48 -0700 Subject: [PATCH 3/9] Fix exception name --- lib/fog/orchestration/models/events.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/orchestration/models/events.rb b/lib/fog/orchestration/models/events.rb index 13989e6..1ae24ce 100644 --- a/lib/fog/orchestration/models/events.rb +++ b/lib/fog/orchestration/models/events.rb @@ -6,7 +6,7 @@ module Orchestration class Events < Fog::Collection def all(stack) - raise NotImplemented + raise NotImplementedError end def get(id) From ab33ff92cd4682db775819bb2e3216e885f1da48 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 14 Jul 2014 16:01:53 -0700 Subject: [PATCH 4/9] Search by name or id --- lib/fog/orchestration/models/stacks.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fog/orchestration/models/stacks.rb b/lib/fog/orchestration/models/stacks.rb index 35fdb8f..7d469db 100644 --- a/lib/fog/orchestration/models/stacks.rb +++ b/lib/fog/orchestration/models/stacks.rb @@ -9,8 +9,11 @@ def all raise NotImplemented end - def get(id) - self.find {|stack| stack.id == id} + def get(name_or_id) + self.find do |stack| + stack.id == name_or_id || + stack.stack_name == name_or_id + end end end From f5abfc24de6c3097ca0e6822a97227933aa4e315 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 25 Jul 2014 14:49:24 -0700 Subject: [PATCH 5/9] Load orchestration specific errors --- lib/fog/orchestration.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/fog/orchestration.rb b/lib/fog/orchestration.rb index 06961cc..83bc384 100644 --- a/lib/fog/orchestration.rb +++ b/lib/fog/orchestration.rb @@ -1,3 +1,5 @@ +require 'fog/orchestration/error' + module Fog module Orchestration From ad8e6c278e1b8c5abb6a256a75a176261e035723 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 25 Jul 2014 15:04:15 -0700 Subject: [PATCH 6/9] Add customized errors --- lib/fog/orchestration/error.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib/fog/orchestration/error.rb diff --git a/lib/fog/orchestration/error.rb b/lib/fog/orchestration/error.rb new file mode 100644 index 0000000..00b0c36 --- /dev/null +++ b/lib/fog/orchestration/error.rb @@ -0,0 +1,16 @@ +require 'fog/core/errors' + +module Fog + module Errors + + # Orchestration related errors + class OrchestrationError < Error + + # Invalid template error + class InvalidTemplate < OrchestrationError + end + + end + + end +end From f667f9b80d819ba28828d68a00f64c6983ccd702 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 25 Jul 2014 15:04:27 -0700 Subject: [PATCH 7/9] Fill in method documentation for orchestration model abstracts --- lib/fog/orchestration/models/event.rb | 5 ++- lib/fog/orchestration/models/events.rb | 15 +++++++-- lib/fog/orchestration/models/output.rb | 5 ++- lib/fog/orchestration/models/outputs.rb | 15 +++++++-- lib/fog/orchestration/models/resource.rb | 5 ++- lib/fog/orchestration/models/resources.rb | 19 ++++++++++-- lib/fog/orchestration/models/stack.rb | 37 +++++++++++++++++++++-- lib/fog/orchestration/models/stacks.rb | 7 ++++- 8 files changed, 96 insertions(+), 12 deletions(-) diff --git a/lib/fog/orchestration/models/event.rb b/lib/fog/orchestration/models/event.rb index 64d6326..e700190 100644 --- a/lib/fog/orchestration/models/event.rb +++ b/lib/fog/orchestration/models/event.rb @@ -2,9 +2,12 @@ module Fog module Orchestration - + # Stack event class Event < Fog::Model + # Load common attributes into subclass + # + # @param klass [Class] def self.inherited(klass) klass.class_eval do identity :id diff --git a/lib/fog/orchestration/models/events.rb b/lib/fog/orchestration/models/events.rb index 1ae24ce..8c6dbcf 100644 --- a/lib/fog/orchestration/models/events.rb +++ b/lib/fog/orchestration/models/events.rb @@ -2,13 +2,24 @@ module Fog module Orchestration - + # Stack events class Events < Fog::Collection - def all(stack) + # @return [Fog::Orchestration::Stack] + attr_accessor :stack + + # Load all events for stack + # + # @param stack [Fog::Orchestration::Stack] + # @return [self] + def all(stack=nil) raise NotImplementedError end + # Fetch event by ID + # + # @param id [String] + # @return [Fog::Orchestration::Event] def get(id) self.find {|event| event.id == id} end diff --git a/lib/fog/orchestration/models/output.rb b/lib/fog/orchestration/models/output.rb index fc143a8..a651677 100644 --- a/lib/fog/orchestration/models/output.rb +++ b/lib/fog/orchestration/models/output.rb @@ -2,9 +2,12 @@ module Fog module Orchestration - + # Stack output class Output < Fog::Model + # Load common attributes into subclass + # + # @param klass [Class] def self.inherited(klass) klass.class_eval do identity :key diff --git a/lib/fog/orchestration/models/outputs.rb b/lib/fog/orchestration/models/outputs.rb index 2ae6ee3..ccebdd2 100644 --- a/lib/fog/orchestration/models/outputs.rb +++ b/lib/fog/orchestration/models/outputs.rb @@ -2,13 +2,24 @@ module Fog module Orchestration - + # Stack outputs class Outputs < Fog::Collection - def all(stack) + # @return [Fog::Orchestration::Stack] + attr_accessor :stack + + # Load all outputs for stack + # + # @param stack [Fog::Orchestration::Stack] + # @return [self] + def all(stack=nil) raise NotImplemented end + # Fetch output by key + # + # @param key [String] + # @return [Fog::Orchestration::Output] def get(key) self.find {|output| output.key == key} end diff --git a/lib/fog/orchestration/models/resource.rb b/lib/fog/orchestration/models/resource.rb index c17bfce..0dfa78f 100644 --- a/lib/fog/orchestration/models/resource.rb +++ b/lib/fog/orchestration/models/resource.rb @@ -2,9 +2,12 @@ module Fog module Orchestration - + # Stack resource class Resource < Fog::Model + # Load common attributes into subclass + # + # @param klass [Class] def self.inherited(klass) klass.class_eval do identity :physical_resource_id diff --git a/lib/fog/orchestration/models/resources.rb b/lib/fog/orchestration/models/resources.rb index 9a113ed..aaecdaf 100644 --- a/lib/fog/orchestration/models/resources.rb +++ b/lib/fog/orchestration/models/resources.rb @@ -2,18 +2,33 @@ module Fog module Orchestration - + # Stack resources class Resources < Fog::Collection - def all(stack) + # @return [Fog::Orchestration::Stack] + attr_accessor :stack + + # Load all resources for stack + # + # @param stack [Fog::Orchestration::Stack] + # @return [self] + def all(stack=nil) raise NotImplemented end + # Fetch resource by physical ID + # + # @param id [String] + # @return [Fog::Orchestration::Resource] def find_by_physical_id(id) self.find {|resource| resource.physical_resource_id == id} end alias_method :get, :find_by_physical_id + # Fetch resource by logical ID + # + # @param id [String] + # @return [Fog::Orchestration::Resource] def find_by_logical_id(id) self.find {|resource| resource.logical_resource_id == id} end diff --git a/lib/fog/orchestration/models/stack.rb b/lib/fog/orchestration/models/stack.rb index 0d5d7bc..5e1a0f2 100644 --- a/lib/fog/orchestration/models/stack.rb +++ b/lib/fog/orchestration/models/stack.rb @@ -2,11 +2,15 @@ module Fog module Orchestration - + # Stack model class Stack < Fog::Model class << self + # Register resources collection class + # + # @param model_klass [Class] + # @return [Class] def resources(model_klass=nil) if(model_klass) @resources_model = model_klass @@ -14,6 +18,10 @@ def resources(model_klass=nil) @resources_model end + # Register events collection class + # + # @param model_klass [Class] + # @return [Class] def events(model_klass=nil) if(model_klass) @events_model = model_klass @@ -21,6 +29,10 @@ def events(model_klass=nil) @events_model end + # Register outputs collection class + # + # @param model_klass [Class] + # @return [Class] def outputs(model_klass=nil) if(model_klass) @outputs_model = model_klass @@ -28,6 +40,9 @@ def outputs(model_klass=nil) @outputs_model end + # Load common attributes into subclass + # + # @param klass [Class] def inherited(klass) klass.class_eval do identity :id @@ -52,23 +67,36 @@ def inherited(klass) end + # Save the stack + # + # @return [self] def save requires :stack_name identity ? update : create end + # Create the stack + # + # @return [self] def create raise NotImlemented end + # Update the stack + # + # @return [self] def update raise NotImlemented end + # Destroy the stack + # + # @return [self] def destroy raise NotImlemented end + # @return [Fog::Orchestration::Resources] def resources if(self.class.resources) self.class.resources.new(:service => service).all(self) @@ -77,6 +105,7 @@ def resources end end + # @return [Fog::Orchestration::Events] def events if(self.class.events) self.class.events.new(:service => service).all(self) @@ -85,6 +114,7 @@ def events end end + # @return [Fog::Orchestration::Outputs] def outputs if(self.class.outputs) self.class.outputs.new(:service => service).all(self) @@ -93,8 +123,11 @@ def outputs end end + # Validate the stack template + # + # @return [TrueClass] + # @raises [Fog::Errors::OrchestationError::InvalidTemplate] def validate - requires :template raise NotImplemented end diff --git a/lib/fog/orchestration/models/stacks.rb b/lib/fog/orchestration/models/stacks.rb index 7d469db..0b21e5a 100644 --- a/lib/fog/orchestration/models/stacks.rb +++ b/lib/fog/orchestration/models/stacks.rb @@ -2,13 +2,18 @@ module Fog module Orchestration - + # All stacks class Stacks < Fog::Collection + # @return [self] def all raise NotImplemented end + # Fetch stack by name or ID + # + # @param name_or_id [String] + # @return [Fog::Orchestration::Stack] def get(name_or_id) self.find do |stack| stack.id == name_or_id || From 8a73bfad9345cb5b122bcddafa6953314da33730 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Sun, 27 Jul 2014 07:18:58 -0700 Subject: [PATCH 8/9] Add note on events abstract about collection sorting --- lib/fog/orchestration/models/events.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/fog/orchestration/models/events.rb b/lib/fog/orchestration/models/events.rb index 8c6dbcf..1153e0e 100644 --- a/lib/fog/orchestration/models/events.rb +++ b/lib/fog/orchestration/models/events.rb @@ -12,6 +12,8 @@ class Events < Fog::Collection # # @param stack [Fog::Orchestration::Stack] # @return [self] + # @note events should be ordered by timestamp + # in ascending order def all(stack=nil) raise NotImplementedError end From bf00b969b7f5fabd5283515665c25774db19a1c8 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 6 Aug 2014 08:47:03 -0700 Subject: [PATCH 9/9] Define required methods --- lib/fog/orchestration.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/fog/orchestration.rb b/lib/fog/orchestration.rb index 83bc384..7c470cf 100644 --- a/lib/fog/orchestration.rb +++ b/lib/fog/orchestration.rb @@ -27,5 +27,18 @@ def self.providers Fog.services[:orchestration] end + def self.included(klass) + klass.class_eval do + include Fog::Orchestration::InstanceMethods + end + end + + module InstanceMethods + # @return [Fog::Orchestration::Stacks] + def stacks + raise NotImplementedError + end + end + end end