From 6bebc8e15be61aa6157842e2884623e49eb75f37 Mon Sep 17 00:00:00 2001 From: jamiehannaford Date: Tue, 20 Aug 2013 15:29:03 +0200 Subject: [PATCH] Adding models, collections and making a start on request classes --- lib/fog/rackspace/auto_scale.rb | 46 ++++++++- lib/fog/rackspace/models/auto_scale/config.rb | 17 ++++ lib/fog/rackspace/models/auto_scale/group.rb | 73 ++++++++++++++ .../models/auto_scale/group_config.rb | 34 +++++++ lib/fog/rackspace/models/auto_scale/groups.rb | 25 +++++ .../models/auto_scale/launch_config.rb | 28 ++++++ .../rackspace/models/auto_scale/policies.rb | 25 +++++ lib/fog/rackspace/models/auto_scale/policy.rb | 98 +++++++++++++++++++ .../rackspace/models/auto_scale/webhook.rb | 34 +++++++ .../requests/auto_scale/create_group.rb | 84 ++++------------ 10 files changed, 395 insertions(+), 69 deletions(-) create mode 100644 lib/fog/rackspace/models/auto_scale/config.rb create mode 100644 lib/fog/rackspace/models/auto_scale/group.rb create mode 100644 lib/fog/rackspace/models/auto_scale/group_config.rb create mode 100644 lib/fog/rackspace/models/auto_scale/groups.rb create mode 100644 lib/fog/rackspace/models/auto_scale/launch_config.rb create mode 100644 lib/fog/rackspace/models/auto_scale/policies.rb create mode 100644 lib/fog/rackspace/models/auto_scale/policy.rb create mode 100644 lib/fog/rackspace/models/auto_scale/webhook.rb diff --git a/lib/fog/rackspace/auto_scale.rb b/lib/fog/rackspace/auto_scale.rb index 0cc913856..7efd83c2d 100644 --- a/lib/fog/rackspace/auto_scale.rb +++ b/lib/fog/rackspace/auto_scale.rb @@ -8,6 +8,22 @@ module Fog class ServiceError < Fog::Rackspace::Errors::ServiceError; end class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end + class MissingArgumentException < InvalidStateException + def initialize(resource, argument) + @resource = resource + @argument = argument + end + def to_s + "This #{resource} resource requires the #{argument} argument" + end + end + + class InvalidImageStateException < InvalidStateException + def to_s + "Image should have transitioned to '#{desired_state}' not '#{current_state}'" + end + end + class BadRequest < Fog::Rackspace::Errors::BadRequest attr_reader :validation_errors @@ -36,16 +52,40 @@ module Fog recognizes :rackspace_auto_scale_url model_path 'fog/rackspace/models/auto_scale' + model :group + collection :groups + model :policy + collection :policies + model :group_config + model :launch_config + model :webhook request_path 'fog/rackspace/requests/auto_scale' request :list_groups - request :get_group request :create_group + request :get_group request :delete_group request :get_group_state + request :pause_group_state + request :resume_group_state - request :update_config - request :get_config + request :get_group_config + request :update_group_config + request :get_launch_config + request :replace_launch_config + + request :get_policies + request :create_policy + request :get_policy + request :update_policy + request :delete_policy + request :execute_policy + + request :execute_anonymous_webhook + + request :get_webhook + request :update_webhook + request :delete_webhook class Mock < Fog::Rackspace::Service def initialize(options) diff --git a/lib/fog/rackspace/models/auto_scale/config.rb b/lib/fog/rackspace/models/auto_scale/config.rb new file mode 100644 index 000000000..fc6f5f15b --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/config.rb @@ -0,0 +1,17 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class Config < Fog::Model + + def pause + end + + def resume + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/group.rb b/lib/fog/rackspace/models/auto_scale/group.rb new file mode 100644 index 000000000..4f987da1d --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/group.rb @@ -0,0 +1,73 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class Group < Fog::Model + + identity :id + + attribute :links + + attribute :group_config + + attribute :launch_config + + attribute :policies + + def initialize(attributes={}) + @service = attributes[:service] + super + end + + def create(options) + requires :launch_config, :group_config, :policies + + data = service.create_group(launch_config, group_config, policies) + merge_attributes(data.body['group']) + true + end + + def destroy + requires :identity + service.delete_server(identity) + true + end + + def group_config + @group_config ||= begin + Fog::Rackspace::AutoScale::GroupConfig.new({ + :service => service, + :group => self + }) + end + end + + def launch_config + @launch_config ||= begin + Fog::Rackspace::AutoScale::LaunchConfig.new({ + :service => service, + :group => self + }) + end + end + + def policies + @policies ||= begin + Fog::Rackspace::Autoscale::Policies.new({ + :service => service, + :group => self + }) + end + end + + def state + requires :identity + data = service.get_group_state(identity) + merge_attributes(data.body['group']) + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/group_config.rb b/lib/fog/rackspace/models/auto_scale/group_config.rb new file mode 100644 index 000000000..12a194f5d --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/group_config.rb @@ -0,0 +1,34 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class GroupConfig < Fog::AutoScale::Rackspace::Config + + identity :id + + attribute :name + attribute :cooldown + attribute :min_entities + attribute :max_entities + attribute :metadata + + def update + requires :identity + options = { + 'name' => name, + 'cooldown' => cooldown, + 'min_entities' => min_entities, + 'max_entities' => max_entities, + 'metadata' => metadata + } + + data = service.update_group_config(identity, options) + merge_attributes(data.body) + true + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/groups.rb b/lib/fog/rackspace/models/auto_scale/groups.rb new file mode 100644 index 000000000..a7ea34a29 --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/groups.rb @@ -0,0 +1,25 @@ +require 'fog/core/collection' +require 'fog/rackspace/models/auto_scale/group' + +module Fog + module Rackspace + class AutoScale + class Groups < Fog::Collection + + model Fog::Rackspace::AutoScale::Group + + def all + data = service.list_groups.body['groups'] + load(data) + end + + def get(group_id) + data = service.get_group(group_id).body['group'] + new(data) + rescue Fog::Rackspace::AutoScale::NotFound + nil + end + end + end + end +end diff --git a/lib/fog/rackspace/models/auto_scale/launch_config.rb b/lib/fog/rackspace/models/auto_scale/launch_config.rb new file mode 100644 index 000000000..55bcb00c9 --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/launch_config.rb @@ -0,0 +1,28 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class LaunchConfig < Fog::AutoScale::Rackspace::Config + + identity :id + + attribute :type + attribute :args + + def update + requires :identity + options = { + 'type' => type, + 'args' => args + } + + data = service.update_launch_config(identity, options) + merge_attributes(data.body['launchConfiguration']) + true + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/policies.rb b/lib/fog/rackspace/models/auto_scale/policies.rb new file mode 100644 index 000000000..ddea70e64 --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/policies.rb @@ -0,0 +1,25 @@ +require 'fog/core/collection' +require 'fog/rackspace/models/auto_scale/policy' + +module Fog + module Rackspace + class AutoScale + class Policies < Fog::Collection + + model Fog::Rackspace::AutoScale::Policy + + def all + data = service.list_policies.body['policies'] + load(data) + end + + def get(policy_id) + data = service.get_policy(policy_id).body['policy'] + new(data) + rescue Fog::Rackspace::AutoScale::NotFound + nil + end + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/policy.rb b/lib/fog/rackspace/models/auto_scale/policy.rb new file mode 100644 index 000000000..dd45bb0e1 --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/policy.rb @@ -0,0 +1,98 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class Policy < Fog::Model + + identity :id + + attribute :group_id + + attribute :links + attribute :name + + # integer + attribute :change + attribute :changePercent + + # integer + attribute :cooldown + + # webhook|schedule|cloud_monitoring + attribute :type + + # hash depending on the type chosen + # - "cron": "23 * * * *" + # - "at": "2013-06-05T03:12Z" + # - "check": { + # "label": "Website check 1", + # "type": "remote.http", + # "details": { + # "url": "http://www.example.com", + # "method": "GET" + # }, + # "monitoring_zones_poll": [ + # "mzA" + # ], + # "timeout": 30, + # "period": 100, + # "target_alias": "default" + # }, + # "alarm_criteria": { + # "criteria": "if (metric[\"duration\"] >= 2) { return new AlarmStatus(OK); } return new AlarmStatus(CRITICAL);" + # } + attribute :args + + attribute :desiredCapacity + + def check_options(options) + if options[:type] == 'schedule' + args = options['args'] + raise MissingArgumentException(self.name, "cron OR at") if args['cron'].nil? && args['at'].nil? + end + true + end + + def create(options) + requires :name, :type, :cooldown + + check_options + + data = service.create_policy(group_id, options) + merge_attributes(data.body['group']) + true + end + + def update + requires :identity + + options = { + 'name' => name, + 'change' => change, + 'changePercent' => changePercent, + 'cooldown' => cooldown, + 'type' => type, + 'args' => args, + 'desiredCapacity' => desiredCapacity + } + + data = service.update_policy(identity, options) + merge_attributes(data.body) + true + end + + def destroy + requires :identity + service.delete_policy(identity) + end + + def execute + requires :identity + service.execute_policy(identity) + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/models/auto_scale/webhook.rb b/lib/fog/rackspace/models/auto_scale/webhook.rb new file mode 100644 index 000000000..5b91892df --- /dev/null +++ b/lib/fog/rackspace/models/auto_scale/webhook.rb @@ -0,0 +1,34 @@ +require 'fog/core/model' + +module Fog + module Rackspace + class AutoScale + class Webhook < Fog::Model + + identity :id + + attribute :name + attribute :metadata + attribute :links + + def update + required :identity + + options = { + 'name' => name, + 'metadata' => metadata + } + + data = service.update_webhook(identity, options) + merge_attribute(data.body) + end + + def destroy + required :identity + service.delete_webhook(identity) + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/rackspace/requests/auto_scale/create_group.rb b/lib/fog/rackspace/requests/auto_scale/create_group.rb index aad0c1017..54fb1a9dd 100644 --- a/lib/fog/rackspace/requests/auto_scale/create_group.rb +++ b/lib/fog/rackspace/requests/auto_scale/create_group.rb @@ -3,76 +3,28 @@ module Fog class AutoScale class Real - def create_group + def create_group(launch_config, group_config, policies) + + body['launchConfiguration'] = { + 'args' => launch_config.args, + 'type' => launch_config.type + } + + body['groupConfiguration'] = { + 'name' => group_config.name, + 'cooldown' => group_config.cooldown, + 'maxEntities' => group_config.max_entities, + 'minEntities' => group_config.min_entities, + 'metadata' => group_config.metadata + } + + body['scalingPolicies'] = policies.collect { |p| p.to_a } + request( :expects => [201], :method => 'POST', :path => 'groups', - :body => <<-JSON - { - \"groupConfiguration\": { - \"name\": \"workers2\", - \"cooldown\": 60, - \"minEntities\": 5, - \"maxEntities\": 25, - \"metadata\": { - \"firstkey\": \"this is a string\", - \"secondkey\": \"1\" - } - }, - \"launchConfiguration\": { - \"type\": \"launch_server\", - \"args\": { - \"server\": { - \"flavorRef\": 3, - \"name\": \"webhead\", - \"imageRef\": \"0d589460-f177-4b0f-81c1-8ab8903ac7d8\", - \"OS-DCF:diskConfig\": \"AUTO\", - \"metadata\": { - \"mykey\": \"myvalue\" - }, - \"personality\": [ - { - \"path\": \"/root/.ssh/authorized_keys\", - \"contents\": \"ssh-rsa AAAAB3Nza...LiPk== user@example.net\" - } - ], - \"networks\": [ - { - \"uuid\": \"11111111-1111-1111-1111-111111111111\" - } - ] - }, - \"loadBalancers\": [ - { - \"loadBalancerId\": 2200, - \"port\": 8081 - } - ] - } - }, - \"scalingPolicies\": [ - { - \"name\": \"scale up by 10\", - \"change\": 10, - \"cooldown\": 5, - \"type\": \"webhook\" - }, - { - \"name\": \"scale down by 5.5 percent\", - \"changePercent\": -5.5, - \"cooldown\": 6, - \"type\": \"webhook\" - }, - { - \"name\": \"set number of servers to 10\", - \"desiredCapacity\": 10, - \"cooldown\": 3, - \"type\": \"webhook\" - } - ] - } - JSON + :body => Fog::JSON.encode(body) ) end end