mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #2199 from nosborn/vcloud_director_media
[vcoud_director] Add media requests and model
This commit is contained in:
commit
ad60e4f547
11 changed files with 377 additions and 1 deletions
|
@ -69,6 +69,8 @@ module Fog
|
|||
collection :vm_networks
|
||||
model :tag # this is called metadata in vcloud
|
||||
collection :tags
|
||||
model :media
|
||||
collection :medias # sic
|
||||
|
||||
request_path 'fog/vcloud_director/requests/compute'
|
||||
request :get_organizations
|
||||
|
@ -108,6 +110,10 @@ module Fog
|
|||
request :delete_vapp
|
||||
request :get_current_session
|
||||
request :get_supported_versions
|
||||
request :delete_media
|
||||
request :get_media
|
||||
request :get_media_owner
|
||||
request :post_upload_media
|
||||
|
||||
class Model < Fog::Model
|
||||
def initialize(attrs={})
|
||||
|
@ -385,6 +391,10 @@ module Fog
|
|||
@user_name ||= @vcloud_director_username.split('@').first
|
||||
end
|
||||
|
||||
def user_uuid
|
||||
@user_uuid ||= uuid
|
||||
end
|
||||
|
||||
def uuid
|
||||
[8,4,4,4,12].map {|i| Fog::Mock.random_hex(i)}.join('-')
|
||||
end
|
||||
|
|
28
lib/fog/vcloud_director/models/compute/media.rb
Normal file
28
lib/fog/vcloud_director/models/compute/media.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
|
||||
class Media < Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :href
|
||||
attribute :type
|
||||
attribute :name
|
||||
attribute :status, :type => :integer
|
||||
attribute :image_type, :aliases => :imageType
|
||||
attribute :size, :type => :integer
|
||||
attribute :description, :aliases => :Description
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
response = service.delete_media(id)
|
||||
service.process_task(response.body)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
35
lib/fog/vcloud_director/models/compute/medias.rb
Normal file
35
lib/fog/vcloud_director/models/compute/medias.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/vcloud_director/models/compute/media'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
|
||||
class Medias < Collection
|
||||
model Fog::Compute::VcloudDirector::Media
|
||||
|
||||
attribute :vdc
|
||||
|
||||
private
|
||||
|
||||
def get_by_id(item_id)
|
||||
item = service.get_media(item_id).body
|
||||
%w(:Link).each {|key_to_delete| item.delete(key_to_delete)}
|
||||
service.add_id_from_href!(item)
|
||||
item
|
||||
end
|
||||
|
||||
def item_list
|
||||
data = service.get_vdc(vdc.id).body
|
||||
return [] if data[:ResourceEntities].empty?
|
||||
items = ensure_list(data[:ResourceEntities][:ResourceEntity]).select do |resource|
|
||||
resource[:type] == 'application/vnd.vmware.vcloud.media+xml'
|
||||
end
|
||||
items.each {|item| service.add_id_from_href!(item)}
|
||||
items
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -22,6 +22,11 @@ module Fog
|
|||
attribute :vm_quota ,:aliases => :VmQuota, :type => :integer
|
||||
attribute :is_enabled ,:aliases => :IsEnabled, :type => :boolean
|
||||
|
||||
def medias
|
||||
requires :id
|
||||
service.medias(:vdc => self)
|
||||
end
|
||||
|
||||
def vapps
|
||||
requires :id
|
||||
service.vapps(:vdc => self)
|
||||
|
|
44
lib/fog/vcloud_director/requests/compute/delete_media.rb
Normal file
44
lib/fog/vcloud_director/requests/compute/delete_media.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
class Real
|
||||
# Delete a media object.
|
||||
#
|
||||
# This operation is asynchronous and returns a task that you can
|
||||
# monitor to track the progress of the request.
|
||||
#
|
||||
# @param [String] media_id Object identifier of the media object.
|
||||
# @return [Excon::Response]
|
||||
# * body<~Hash>:
|
||||
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/DELETE-Media.html
|
||||
# vCloud API Documentation
|
||||
# @since vCloud API version 0.9
|
||||
def delete_media(media_id)
|
||||
request(
|
||||
:expects => 202,
|
||||
:method => 'DELETE',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "media/#{media_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def delete_media(media_id)
|
||||
response = Excon::Response.new
|
||||
|
||||
unless valid_uuid?(media_id)
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 202}, response)
|
||||
end
|
||||
unless data[:medias].has_key?(media_id)
|
||||
response.status = 403
|
||||
raise Excon::Errors.status_error({:expects => 202}, response)
|
||||
end
|
||||
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/vcloud_director/requests/compute/get_media.rb
Normal file
42
lib/fog/vcloud_director/requests/compute/get_media.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
class Real
|
||||
# Retrieve a media object.
|
||||
#
|
||||
# @param [String] media_id Object identifier of the media object.
|
||||
# @return [Excon::Response]
|
||||
# * body<~Hash>:
|
||||
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/GET-Media.html
|
||||
# vCloud API Documentation
|
||||
# @since vCloud API version 0.9
|
||||
def get_media(media_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:idempotent => true,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "media/#{media_id}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_media(media_id)
|
||||
response = Excon::Response.new
|
||||
|
||||
unless valid_uuid?(media_id)
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
end
|
||||
unless data[:medias].has_key?(media_id)
|
||||
response.status = 403
|
||||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
end
|
||||
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
42
lib/fog/vcloud_director/requests/compute/get_media_owner.rb
Normal file
42
lib/fog/vcloud_director/requests/compute/get_media_owner.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
class Real
|
||||
# Retrieve the owner of a media object.
|
||||
#
|
||||
# @param [String] media_id Object identifier of the media object.
|
||||
# @return [Excon::Response]
|
||||
# * body<~Hash>:
|
||||
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/GET-MediaOwner.html
|
||||
# vCloud API Documentation
|
||||
# @since vCloud API version 1.5
|
||||
def get_media_owner(media_id)
|
||||
request(
|
||||
:expects => 200,
|
||||
:idempotent => true,
|
||||
:method => 'GET',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "media/#{media_id}/owner"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def get_media_owner(media_id)
|
||||
response = Excon::Response.new
|
||||
|
||||
unless valid_uuid?(media_id)
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
end
|
||||
unless data[:medias].has_key?(media_id)
|
||||
response.status = 403
|
||||
raise Excon::Errors.status_error({:expects => 200}, response)
|
||||
end
|
||||
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,66 @@
|
|||
module Fog
|
||||
module Compute
|
||||
class VcloudDirector
|
||||
class Real
|
||||
# Upload a media image.
|
||||
#
|
||||
# The response includes an upload link for the media image.
|
||||
#
|
||||
# @param [String] vdc_id Object identifier of the vDC.
|
||||
# @param [String] name The name of the media image.
|
||||
# @param [String] image_type Media image type. One of: iso, floppy.
|
||||
# @param [Integer] size Size of the media file, in bytes.
|
||||
# @param [Hash] options
|
||||
# @option options [String] :Description Optional description.
|
||||
# @return [Excon::Response]
|
||||
# * body<~Hash>:
|
||||
# @see http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/POST-UploadMedia.html
|
||||
# vCloud API Documentation
|
||||
# @since vCloud API version 0.9
|
||||
def post_upload_media(vdc_id, name, image_type, size, options={})
|
||||
body = <<-END
|
||||
<Media xmlns="http://www.vmware.com/vcloud/v1.5"
|
||||
name="#{name}"
|
||||
imageType="#{image_type}"
|
||||
size="#{size}">
|
||||
</Media>
|
||||
END
|
||||
|
||||
request(
|
||||
:body => body,
|
||||
:expects => 201,
|
||||
:headers => {'Content-Type' => 'application/vnd.vmware.vcloud.media+xml'},
|
||||
:method => 'POST',
|
||||
:parser => Fog::ToHashDocument.new,
|
||||
:path => "vdc/#{vdc_id}/media"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
class Mock
|
||||
def post_upload_media(vdc_id, name, image_type, size, options={})
|
||||
response = Excon::Response.new
|
||||
|
||||
unless valid_uuid?(vdc_id)
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 201}, response)
|
||||
end
|
||||
unless ['iso','floppy'].include?(image_type)
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 201}, response)
|
||||
end
|
||||
unless size =~ /^\d+$/
|
||||
response.status = 400
|
||||
raise Excon::Errors.status_error({:expects => 201}, response)
|
||||
end
|
||||
unless data[:vdcs].has_key?(vdc_id)
|
||||
response.status = 403
|
||||
raise Excon::Errors.status_error({:expects => 201}, response)
|
||||
end
|
||||
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
47
tests/vcloud_director/models/compute/media_tests.rb
Normal file
47
tests/vcloud_director/models/compute/media_tests.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
||||
|
||||
VCR.use_cassette(File.basename(__FILE__)) do
|
||||
|
||||
Shindo.tests('Compute::VcloudDirector | media', ['vclouddirector']) do
|
||||
pending if Fog.mocking?
|
||||
|
||||
medias = vdc.medias
|
||||
media = medias.first
|
||||
|
||||
tests('Compute::VcloudDirector | media') do
|
||||
tests('#href').returns(String) { media.href.class }
|
||||
tests('#type').returns('application/vnd.vmware.vcloud.media+xml') { media.type }
|
||||
tests('#id').returns(String) { media.id.class }
|
||||
tests('#name').returns(String) { media.name.class }
|
||||
end
|
||||
|
||||
tests('Compute::VcloudDirector | media', ['lazy load attrs']) do
|
||||
media.lazy_load_attrs.each do |lazy_attr|
|
||||
tests("##{lazy_attr} is not loaded yet").returns(NonLoaded) { media.attributes[lazy_attr] }
|
||||
end
|
||||
end
|
||||
|
||||
tests('Compute::VcloudDirector | media', ['load on demand']) do
|
||||
tests("#description is not loaded yet").returns(NonLoaded) { media.attributes[:description] }
|
||||
tests("#description is loaded on demand").returns(String) { media.description.class }
|
||||
tests("#description is now loaded").returns(true) { media.attributes[:description] != NonLoaded }
|
||||
end
|
||||
|
||||
tests('Compute::VcloudDirector | media', ['lazy load attrs']) do
|
||||
media.lazy_load_attrs.each do |lazy_attr|
|
||||
tests("##{lazy_attr} is now loaded").returns(true) { media.attributes[lazy_attr] != NonLoaded }
|
||||
end
|
||||
end
|
||||
|
||||
tests('Compute::VcloudDirector | media' ['attributes']) do
|
||||
tests('#status').returns(Fixnum) { media.status.class }
|
||||
tests('#image_type').returns(String) { media.image_type.class }
|
||||
tests('#size').returns(Fixnum) { media.size.class }
|
||||
end
|
||||
|
||||
tests('Compute::VcloudDirector | media', ['get']) do
|
||||
tests('#get_by_name').returns(media.name) { medias.get_by_name(media.name).name }
|
||||
tests('#get').returns(media.id) { medias.get(media.id).id }
|
||||
end
|
||||
end
|
||||
end
|
57
tests/vcloud_director/requests/compute/media_tests.rb
Normal file
57
tests/vcloud_director/requests/compute/media_tests.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
Shindo.tests('Compute::VcloudDirector | media requests', ['vclouddirector']) do
|
||||
|
||||
@service = Fog::Compute::VcloudDirector.new
|
||||
|
||||
tests('Get current organization') do
|
||||
session = @service.get_current_session.body
|
||||
link = session[:Link].detect do |l|
|
||||
l[:type] == 'application/vnd.vmware.vcloud.org+xml'
|
||||
end
|
||||
@org = @service.get_organization(link[:href].split('/').last).body
|
||||
end
|
||||
|
||||
tests('Each vDC') do
|
||||
@org[:Link].select do |l|
|
||||
l[:type] == 'application/vnd.vmware.vcloud.vdc+xml'
|
||||
end.each do |link|
|
||||
@vdc = @service.get_vdc(link[:href].split('/').last).body
|
||||
if @vdc[:ResourceEntities].is_a?(String)
|
||||
@vdc[:ResourceEntities] = {:ResourceEntity => []}
|
||||
elsif @vdc[:ResourceEntities][:ResourceEntity].is_a?(Hash)
|
||||
@vdc[:ResourceEntities][:ResourceEntity] = [@vdc[:ResourceEntities][:ResourceEntity]]
|
||||
end
|
||||
tests('Each Media') do
|
||||
@vdc[:ResourceEntities][:ResourceEntity].select do |r|
|
||||
r[:type] == 'application/vnd.vmware.vcloud.media+xml'
|
||||
end.each do |m|
|
||||
@media_id = m[:href].split('/').last
|
||||
tests("#get_media(#{@media_id})").data_matches_schema(VcloudDirector::Compute::Schema::MEDIA_TYPE) do
|
||||
pending if Fog.mocking?
|
||||
@service.get_media(@media_id).body
|
||||
end
|
||||
tests("#get_media_owner(#{@media_id})").data_matches_schema(VcloudDirector::Compute::Schema::OWNER_TYPE) do
|
||||
pending if Fog.mocking?
|
||||
@service.get_media_owner(@media_id).body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
tests('Upload to non-existent vDC').raises(Excon::Errors::Forbidden) do
|
||||
@service.get_media('00000000-0000-0000-0000-000000000000')
|
||||
end
|
||||
|
||||
tests('Retrieve non-existent Media').raises(Excon::Errors::Forbidden) do
|
||||
@service.get_media('00000000-0000-0000-0000-000000000000')
|
||||
end
|
||||
|
||||
tests('Retrieve owner of non-existent Media').raises(Excon::Errors::Forbidden) do
|
||||
@service.get_media_owner('00000000-0000-0000-0000-000000000000')
|
||||
end
|
||||
|
||||
tests('Delete non-existent Media').raises(Excon::Errors::Forbidden) do
|
||||
@service.delete_media('00000000-0000-0000-0000-000000000000')
|
||||
end
|
||||
|
||||
end
|
|
@ -115,7 +115,7 @@ class VcloudDirector
|
|||
:status => Fog::Nullable::String,
|
||||
:Description => Fog::Nullable::String,
|
||||
# :Tasks => TASKS_IN_PROGRESS_TYPE,
|
||||
:Files => FILES_LIST_TYPE
|
||||
# :Files => FILES_LIST_TYPE
|
||||
})
|
||||
|
||||
# Container for references to ResourceEntity objects in this vDC.
|
||||
|
|
Loading…
Add table
Reference in a new issue