From ff9fd54c1a09631c2dee0b6acf8031936a0b38c7 Mon Sep 17 00:00:00 2001 From: Jason Hansen & Josh Lane Date: Mon, 21 May 2012 16:53:53 -0700 Subject: [PATCH] [cloudstack|compute] support async jobs --- lib/fog/cloudstack/compute.rb | 2 ++ lib/fog/cloudstack/models/compute/job.rb | 25 +++++++++++++++++ lib/fog/cloudstack/models/compute/jobs.rb | 28 +++++++++++++++++++ lib/fog/cloudstack/models/compute/server.rb | 8 ++++++ .../requests/compute/list_async_jobs.rb | 22 +++++++++++---- .../compute/list_service_offerings.rb | 11 ++++++-- .../compute/reboot_virtual_machine.rb | 11 ++++++++ 7 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 lib/fog/cloudstack/models/compute/job.rb create mode 100644 lib/fog/cloudstack/models/compute/jobs.rb diff --git a/lib/fog/cloudstack/compute.rb b/lib/fog/cloudstack/compute.rb index 01c45ba15..92f0152e7 100644 --- a/lib/fog/cloudstack/compute.rb +++ b/lib/fog/cloudstack/compute.rb @@ -19,6 +19,8 @@ module Fog model_path 'fog/cloudstack/models/compute' model :address + model :job + collection :jobs model :server collection :servers model :image diff --git a/lib/fog/cloudstack/models/compute/job.rb b/lib/fog/cloudstack/models/compute/job.rb new file mode 100644 index 000000000..b9cbf529f --- /dev/null +++ b/lib/fog/cloudstack/models/compute/job.rb @@ -0,0 +1,25 @@ +module Fog + module Compute + class Cloudstack + class Job < Fog::Model + + identity :id, :aliases => 'jobid' + attribute :user_id, :aliases => 'userid' + attribute :account_id, :aliases => 'accountid' + attribute :cmd + attribute :job_status, :aliases => 'jobstatus' + attribute :job_result_type, :aliases => 'jobresulttype' + attribute :job_result_code, :aliases => 'jobresultcode' + attribute :job_proc_status, :aliases => 'jobprocstatus' + + attribute :created_at, :aliases => 'created', :type => :time + attribute :job_result, :aliases => 'jobresult' + + def reload + requires :id + merge_attributes(connection.query_async_job_result('jobid' => self.id)['queryasyncjobresultresponse']) + end + end # Job + end # Cloudstack + end # Compute +end # Fog diff --git a/lib/fog/cloudstack/models/compute/jobs.rb b/lib/fog/cloudstack/models/compute/jobs.rb new file mode 100644 index 000000000..cbbba3242 --- /dev/null +++ b/lib/fog/cloudstack/models/compute/jobs.rb @@ -0,0 +1,28 @@ +require 'fog/core/collection' +require 'fog/cloudstack/models/compute/job' + +module Fog + module Compute + class Cloudstack + + class Jobs < Fog::Collection + + model Fog::Compute::Cloudstack::Job + + def all + data = connection.list_async_jobs["listasyncjobsresponse"]["asyncjobs"] || [] + load(data) + end + + def get(job_id) + if job = connection.query_async_job_result('jobid' => job_id)["listasyncjobsresponse"]["asyncjobs"].first + new(job) + end + rescue Fog::Compute::Cloudstack::BadRequest + nil + end + end + + end + end +end diff --git a/lib/fog/cloudstack/models/compute/server.rb b/lib/fog/cloudstack/models/compute/server.rb index 40d508dfd..25008b1eb 100644 --- a/lib/fog/cloudstack/models/compute/server.rb +++ b/lib/fog/cloudstack/models/compute/server.rb @@ -42,6 +42,14 @@ module Fog state == 'Running' end + def reboot + requires :id + data = connection.reboot_virtual_machine('id' => self.id) # FIXME: does this ever fail? + job = Job.new(data["rebootvirtualmachineresponse"]) + job.connection= self.connection + job + end + def save requires :image_id, :flavor_id, :zone_id diff --git a/lib/fog/cloudstack/requests/compute/list_async_jobs.rb b/lib/fog/cloudstack/requests/compute/list_async_jobs.rb index 49455584a..c22b0dfbf 100644 --- a/lib/fog/cloudstack/requests/compute/list_async_jobs.rb +++ b/lib/fog/cloudstack/requests/compute/list_async_jobs.rb @@ -10,11 +10,23 @@ module Fog options.merge!( 'command' => 'listAsyncJobs' ) - + request(options) end - end - end - end -end + end # Real + class Mock + def list_async_jobs(options={}) + # FIXME: support paging + jobs = self.data[:jobs] + { + 'listasyncjobsresponse' => { + 'count' => jobs.size, + 'asyncjobs' => jobs + } + } + end + end # Mock + end # Cloudstack + end # Compute +end # Fog diff --git a/lib/fog/cloudstack/requests/compute/list_service_offerings.rb b/lib/fog/cloudstack/requests/compute/list_service_offerings.rb index 691caa4c2..e138aca91 100644 --- a/lib/fog/cloudstack/requests/compute/list_service_offerings.rb +++ b/lib/fog/cloudstack/requests/compute/list_service_offerings.rb @@ -19,10 +19,17 @@ module Fog class Mock def list_service_offerings(options={}) - flavors = self.data[:flavors].values + flavors = [] + if service_offering_id = options['id'] + flavor = self.data[:flavors][service_offering_id] + raise Fog::Compute::Cloudstack::BadRequest unless flavor + flavors = [flavor] + else + flavors = self.data[:flavors].values + end { - "listserviceofferingsresponse" => + "listserviceofferingsresponse" => { "count" => flavors.size, "serviceoffering"=> flavors diff --git a/lib/fog/cloudstack/requests/compute/reboot_virtual_machine.rb b/lib/fog/cloudstack/requests/compute/reboot_virtual_machine.rb index 78b1360ae..c4f3dcd6d 100644 --- a/lib/fog/cloudstack/requests/compute/reboot_virtual_machine.rb +++ b/lib/fog/cloudstack/requests/compute/reboot_virtual_machine.rb @@ -15,6 +15,17 @@ module Fog end end + + class Mock + def reboot_virtual_machine(options={}) + job_id = Fog::Cloudstack.uuid + { + "rebootvirtualmachineresponse" => { + "jobid" => job_id + } + } + end + end end end end