1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Adding models, collections and making a start on request classes

This commit is contained in:
jamiehannaford 2013-08-20 15:29:03 +02:00
parent bf0dac91b0
commit 6bebc8e15b
10 changed files with 395 additions and 69 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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