Class: OMF::SFA::AM::AMManager
- Inherits:
-
Base::LObject
- Object
- Base::LObject
- OMF::SFA::AM::AMManager
- Defined in:
- lib/omf-sfa/am/am_manager.rb
Overview
The manager is where all the AM related policies and resource management is concentrated. Testbeds with their own ways of dealing with resources and components should only need to extend this class.
Instance Method Summary collapse
- #_get_nil_account ⇒ Object
-
#close_account(account_descr, authorizer) ⇒ OAccount
Close the account described by
account
hash. -
#create_resource(resource_descr, type_to_create, oproperties, authorizer) ⇒ OResource
Create a resource.
-
#find_account(account_descr, authorizer) ⇒ OAccount
Return the account described by
account_descr
. -
#find_active_account(account_descr, authorizer) ⇒ OAccount
Return the account described by
account_descr
if it is active. -
#find_all_accounts(authorizer) ⇒ Array<OAccount>
Return all accounts visible to the requesting user.
-
#find_all_components_for_account(account, authorizer) ⇒ Array<OComponent>
Find all components for a specific account.
- #find_all_leases(authorizer) ⇒ Object
-
#find_all_leases_for_account(account, authorizer) ⇒ Array<OLease>
Return all leases of the specified account.
-
#find_all_resources_for_account(account = _get_nil_account, authorizer) ⇒ Array<OResource>
Find all resources for a specific account.
-
#find_lease(lease_descr, lease_oproperties, authorizer) ⇒ OLease
Return the lease described by
lease_descr
. -
#find_or_create_account(account_descr, authorizer) ⇒ OAccount
Return the account described by
account_descr
. -
#find_or_create_lease(lease_descr, lease_oproperties, authorizer) ⇒ OLease
Return the lease described by
lease_descr
. -
#find_or_create_resource(resource_descr, type_to_create, oproperties, authorizer) ⇒ Array<OComponent>
Find all components.
-
#find_or_create_resource_for_account(resource_descr, type_to_create, oproperties, authorizer) ⇒ OResource
Find or create a resource for authorizer’s account.
-
#find_or_create_user(user_descr) ⇒ User
Return the user described by
user_descr
. -
#find_resource(resource_descr, authorizer) ⇒ OResource
Find a resource.
-
#find_resource_for_account(resource_descr, authorizer) ⇒ OResource
Find a resource which has been assigned to the authorizer’s account.
-
#find_user(user_descr) ⇒ User
Return the user described by
user_descr
. -
#initialize(scheduler) ⇒ AMManager
constructor
Create an instance of this manager.
-
#manage_resource(resource) ⇒ Object
Register a resource to be managed by this AM.
-
#manage_resources(resources) ⇒ Object
Register an array of resources to be managed by this AM.
-
#modify_lease(lease_oproperties, lease, authorizer) ⇒ OLease
Modify lease described by
lease_descr
hash. -
#release_all_components_for_account(account, authorizer) ⇒ Object
This method finds all the components of the specific account and detaches them.
-
#release_lease(lease, authorizer) ⇒ Object
cancel
lease
. -
#release_resource(resource, authorizer) ⇒ Object
Release ‘resource’.
-
#release_resources(resources, authorizer) ⇒ Object
Release an array of resources.
-
#renew_account_until(account_descr, expiration_time, authorizer) ⇒ OAccount
Renew account described by
account_descr
hash untilexpiration_time
. -
#update_lease_from_rspec(lease_el, authorizer) ⇒ OLease
Create or Modify leases through RSpecs.
-
#update_leases_from_rspec(leases, authorizer) ⇒ Hash{String => OLease}
Update the leases described in
leases
. -
#update_resource_from_rspec(resource_el, leases, clean_state, authorizer) ⇒ Object
Update a single resource described in
resource_el
. -
#update_resources_from_rspec(descr_el, clean_state, authorizer) ⇒ OResource
Update the resources described in
resource_el
.
Constructor Details
#initialize(scheduler) ⇒ AMManager
Create an instance of this manager
34 35 36 |
# File 'lib/omf-sfa/am/am_manager.rb', line 34 def initialize(scheduler) @scheduler = scheduler end |
Instance Method Details
#_get_nil_account ⇒ Object
853 854 855 |
# File 'lib/omf-sfa/am/am_manager.rb', line 853 def _get_nil_account() @scheduler.get_nil_account() end |
#close_account(account_descr, authorizer) ⇒ OAccount
Close the account described by account
hash.
Make sure that all associated resources are freed as well
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/omf-sfa/am/am_manager.rb', line 174 def close_account(account_descr, ) account = find_account(account_descr, ) .can_close_account?(account) # TODO: Free all resources associated with this account!!!! # OMF::SFA::Resource::OComponent.all(:account => account).each do |c| # c.account = def_account # c.save # end account.close account.save account end |
#create_resource(resource_descr, type_to_create, oproperties, authorizer) ⇒ OResource
Create a resource
540 541 542 543 544 545 546 |
# File 'lib/omf-sfa/am/am_manager.rb', line 540 def create_resource(resource_descr, type_to_create, oproperties, ) .can_create_resource?(resource_descr, type_to_create) unless resource = @scheduler.create_resource(resource_descr, type_to_create, oproperties, ) raise UnknownResourceException.new "Resource '#{resource_descr.inspect}' cannot be created" end resource end |
#find_account(account_descr, authorizer) ⇒ OAccount
Return the account described by account_descr
. Create if it doesn’t exist.
100 101 102 103 104 105 106 |
# File 'lib/omf-sfa/am/am_manager.rb', line 100 def find_account(account_descr, ) unless account = OMF::SFA::Resource::OAccount.first(account_descr) raise UnavailableResourceException.new "Unknown account '#{account_descr.inspect}'" end .can_view_account?(account) account end |
#find_active_account(account_descr, authorizer) ⇒ OAccount
Return the account described by account_descr
if it is active.
136 137 138 139 140 141 142 |
# File 'lib/omf-sfa/am/am_manager.rb', line 136 def find_active_account(account_descr, ) account = find_account(account_descr, ) if account.closed? raise UnavailableResourceException.new "Account '#{account.inspect}' is closed" end account end |
#find_all_accounts(authorizer) ⇒ Array<OAccount>
Return all accounts visible to the requesting user
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/omf-sfa/am/am_manager.rb', line 113 def find_all_accounts() accounts = OMF::SFA::Resource::OAccount.all() nil_account = _get_nil_account() accounts.map do |a| next if a == nil_account begin .can_view_account?(a) a rescue InsufficientPrivilegesException nil end end.compact end |
#find_all_components_for_account(account, authorizer) ⇒ Array<OComponent>
Find all components for a specific account.
497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/omf-sfa/am/am_manager.rb', line 497 def find_all_components_for_account(account, ) res = OMF::SFA::Resource::OComponent.all(:account => account) res.map do |r| begin .can_view_resource?(r) r rescue InsufficientPrivilegesException nil end end.compact end |
#find_all_leases(authorizer) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/omf-sfa/am/am_manager.rb', line 294 def find_all_leases() leases = OMF::SFA::Resource::OLease.all leases.map do |l| begin .can_view_lease?(l) l rescue InsufficientPrivilegesException nil end end.compact end |
#find_all_leases_for_account(account, authorizer) ⇒ Array<OLease>
Return all leases of the specified account
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/omf-sfa/am/am_manager.rb', line 281 def find_all_leases_for_account(account, ) debug "find_all_leases_for_account: account:'#{account.inspect}' authorizer:'#{.inspect}'" leases = OMF::SFA::Resource::OLease.all(:account => account) leases.map do |l| begin .can_view_lease?(l) l rescue InsufficientPrivilegesException nil end end.compact end |
#find_all_resources_for_account(account = _get_nil_account, authorizer) ⇒ Array<OResource>
Find all resources for a specific account.
478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/omf-sfa/am/am_manager.rb', line 478 def find_all_resources_for_account(account = _get_nil_account, ) debug "find_all_resources_for_account: #{account.inspect}" res = OMF::SFA::Resource::OResource.all(:account => account) res.map do |r| begin .can_view_resource?(r) r rescue InsufficientPrivilegesException nil end end.compact end |
#find_lease(lease_descr, lease_oproperties, authorizer) ⇒ OLease
Return the lease described by lease_descr
.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/omf-sfa/am/am_manager.rb', line 257 def find_lease(lease_descr, lease_oproperties, ) if lease_oproperties.empty? lease = OMF::SFA::Resource::OLease.first(lease_descr) raise UnavailableResourceException.new "Unknown lease '#{lease_descr.inspect}'" if lease.nil? .can_view_lease?(lease) return lease end leases = OMF::SFA::Resource::OLease.all(lease_descr) leases.each do |l| if (l[:valid_from] == lease_oproperties[:valid_from] && l[:valid_until] == lease_oproperties[:valid_until]) .can_view_lease?(l) return l end end raise UnavailableResourceException.new "Unknown lease '#{lease_descr.inspect}'" end |
#find_or_create_account(account_descr, authorizer) ⇒ OAccount
Return the account described by account_descr
. Create if it doesn’t exist.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/omf-sfa/am/am_manager.rb', line 76 def find_or_create_account(account_descr, ) debug "find_or_create_account: '#{account_descr.inspect}'" begin return find_account(account_descr, ) rescue UnavailableResourceException end .can_create_account? account = OMF::SFA::Resource::OAccount.create(account_descr) # We have an 1-to-1 relationship between account and project for the moment project = OMF::SFA::Resource::Project.create account.project = project account.save raise UnavailableResourceException.new "Cannot create '#{account_descr.inspect}'" unless account account end |
#find_or_create_lease(lease_descr, lease_oproperties, authorizer) ⇒ OLease
Return the lease described by lease_descr
. Create if it doesn’t exist.
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/omf-sfa/am/am_manager.rb', line 236 def find_or_create_lease(lease_descr, lease_oproperties, ) debug "find_or_create_lease: '#{lease_descr.inspect}', '#{lease_oproperties.inspect}'" begin return find_lease(lease_descr, lease_oproperties, ) rescue UnavailableResourceException end unless lease_oproperties.has_key?(:valid_from) && lease_oproperties.has_key?(:valid_until) raise UnavailablePropertiesException.new "Cannot create lease without ':valid_from' and 'valid_until' oproperties #{lease_oproperties.inspect}" end lease = create_resource(lease_descr, 'OLease', lease_oproperties, ) end |
#find_or_create_resource(resource_descr, type_to_create, oproperties, authorizer) ⇒ Array<OComponent>
518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/omf-sfa/am/am_manager.rb', line 518 def find_or_create_resource(resource_descr, type_to_create, oproperties, ) debug "find_or_create_resource: resource '#{resource_descr.inspect}' type: '#{type_to_create}'" unless resource_descr.is_a? Hash raise FormatException.new "Unknown resource description '#{resource_descr.inspect}'" end begin return find_resource(resource_descr, ) rescue UnknownResourceException end create_resource(resource_descr, type_to_create, oproperties, ) end |
#find_or_create_resource_for_account(resource_descr, type_to_create, oproperties, authorizer) ⇒ OResource
Find or create a resource for authorizer’s account. If it doesn’t exist, is already assigned to someone else, or cannot be created, throws UnknownResourceException
.
559 560 561 562 563 564 |
# File 'lib/omf-sfa/am/am_manager.rb', line 559 def find_or_create_resource_for_account(resource_descr, type_to_create, oproperties, ) debug "find_or_create_resource_for_account: r_descr:'#{resource_descr}' type:'#{type_to_create}' authorizer:'#{.inspect}'" rdescr = resource_descr.dup rdescr[:account] = .account find_or_create_resource(rdescr, type_to_create, oproperties, ) end |
#find_or_create_user(user_descr) ⇒ User
Return the user described by user_descr
. Create if it doesn’t exist.
Note: This is an unprivileged operation as creating a user doesn’t imply anything else beyond opening a record.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/omf-sfa/am/am_manager.rb', line 198 def find_or_create_user(user_descr) debug "find_or_create_user: '#{user_descr.inspect}'" begin return find_user(user_descr) rescue UnavailableResourceException end user = OMF::SFA::Resource::User.create(user_descr) raise UnavailableResourceException.new "Cannot create '#{user_descr.inspect}'" unless user user end |
#find_resource(resource_descr, authorizer) ⇒ OResource
This will assign the resource automatically to the requesting account
Find a resource. If it doesn’t exist throws UnknownResourceException
If it’s not visible to requester throws InsufficientPrivilegesException
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/omf-sfa/am/am_manager.rb', line 427 def find_resource(resource_descr, ) debug "find_resource: descr: '#{resource_descr.inspect}'" if resource_descr.kind_of? OMF::SFA::Resource::OResource resource = resource_descr elsif resource_descr.kind_of? Hash resource = OMF::SFA::Resource::OResource.first(resource_descr) elsif resource_descr.kind_of? String # assume to be UUID begin uuid = UUIDTools::UUID.parse(resource_descr) descr = {:uuid => uuid} rescue ArgumentError # doesn't seem to be a UUID, try it as a name - be aware of non-uniqueness descr = {:name => resource_descr} end resource = OMF::SFA::Resource::OResource.first(descr) else raise FormatException.new "Unknown resource description type '#{resource_descr.class}' (#{resource_descr})" end unless resource raise UnknownResourceException.new "Resource '#{resource_descr.inspect}' is not available or doesn't exist" end .can_view_resource?(resource) resource end |
#find_resource_for_account(resource_descr, authorizer) ⇒ OResource
This will assign the resource automatically to the requesting account
Find a resource which has been assigned to the authorizer’s account. If it doesn’t exist, or is not visible to requester throws UnknownResourceException
.
465 466 467 468 469 |
# File 'lib/omf-sfa/am/am_manager.rb', line 465 def find_resource_for_account(resource_descr, ) rdescr = resource_descr.dup rdescr[:account] = .account find_resource(rdescr, ) end |
#find_user(user_descr) ⇒ User
Return the user described by user_descr
.
Note: This is an unprivileged operation as creating a user doesn’t imply anything else beyond opening a record.
218 219 220 221 222 223 |
# File 'lib/omf-sfa/am/am_manager.rb', line 218 def find_user(user_descr) unless user = OMF::SFA::Resource::User.first(user_descr) raise UnavailableResourceException.new "Unknown user '#{user_descr.inspect}'" end user end |
#manage_resource(resource) ⇒ Object
Register a resource to be managed by this AM.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/omf-sfa/am/am_manager.rb', line 44 def manage_resource(resource) unless resource.is_a?(OMF::SFA::Resource::OResource) raise "Resource '#{resource}' needs to be of type 'OResource', but is '#{resource.class}'" end null_account = _get_nil_account resource.account = null_account resource.save # rg = get_root_group_for_account(def_account) # rg.contains_resources << resource # rg.save resource end |
#manage_resources(resources) ⇒ Object
Register an array of resources to be managed by this AM.
62 63 64 |
# File 'lib/omf-sfa/am/am_manager.rb', line 62 def manage_resources(resources) resources.map {|r| manage_resource(r) } end |
#modify_lease(lease_oproperties, lease, authorizer) ⇒ OLease
Modify lease described by lease_descr
hash
313 314 315 316 317 318 319 |
# File 'lib/omf-sfa/am/am_manager.rb', line 313 def modify_lease(lease_oproperties, lease, ) .can_modify_lease?(lease) lease.valid_from = lease_oproperties[:valid_from] lease.valid_until = lease_oproperties[:valid_until] lease.save lease end |
#release_all_components_for_account(account, authorizer) ⇒ Object
This method finds all the components of the specific account and detaches them.
789 790 791 792 |
# File 'lib/omf-sfa/am/am_manager.rb', line 789 def release_all_components_for_account(account, ) components = find_all_components_for_account(account, ) release_resources(components, ) end |
#release_lease(lease, authorizer) ⇒ Object
cancel lease
This implementation simply frees the lease record.
328 329 330 331 332 333 334 335 336 |
# File 'lib/omf-sfa/am/am_manager.rb', line 328 def release_lease(lease, ) debug "release_lease: lease:'#{lease.inspect}' authorizer:'#{.inspect}'" .can_release_lease?(lease) lease.component_leases.each do |l| l.destroy # unlink the lease with the corresponding components end lease.status = :cancelled end |
#release_resource(resource, authorizer) ⇒ Object
Release ‘resource’.
This implementation simply frees the resource record.
767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/omf-sfa/am/am_manager.rb', line 767 def release_resource(resource, ) .can_release_resource?(resource) @scheduler.release_resource(resource, ) #resource.remove_from_all_groups # if r.kind_of? OMF::SFA::Resource::CompGroup # # groups don't go back in the pool, they are created per account # r.destroy # else # r.account = def_account # r.save # end #resource.destroy end |
#release_resources(resources, authorizer) ⇒ Object
Release an array of resources.
754 755 756 757 758 |
# File 'lib/omf-sfa/am/am_manager.rb', line 754 def release_resources(resources, ) resources.each do |r| release_resource(r, ) end end |
#renew_account_until(account_descr, expiration_time, authorizer) ⇒ OAccount
Renew account described by account_descr
hash until expiration_time
. ALready closed or expired accounts can’t be renewed.
155 156 157 158 159 160 161 |
# File 'lib/omf-sfa/am/am_manager.rb', line 155 def renew_account_until(account_descr, expiration_time, ) account = find_active_account(account_descr, ) .can_renew_account?(account, expiration_time) account.valid_until = expiration_time account.save account end |
#update_lease_from_rspec(lease_el, authorizer) ⇒ OLease
Create or Modify leases through RSpecs
When a uuid is provided, then the corresponding lease is modified. Otherwise a new lease is created with the properties described in the RSpecs.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/omf-sfa/am/am_manager.rb', line 350 def update_lease_from_rspec(lease_el, ) lease_properties = {:valid_from => Time.parse(lease_el[:valid_from]), :valid_until => Time.parse(lease_el[:valid_until])} begin raise UnavailableResourceException unless UUID.validate(lease_el[:id]) lease = find_lease({:uuid => lease_el[:id]}, {}, ) if lease.valid_from != lease_properties[:valid_from] || lease.valid_until != lease_properties[:valid_until] lease = modify_lease(lease_properties, lease, ) return { lease_el[:id] => lease } else return { lease_el[:id] => lease } end rescue UnavailableResourceException lease_descr = {:name => .account.name} lease = find_or_create_lease(lease_descr, lease_properties, ) return { lease_el[:id] => lease } end #unless lease_el[:uuid].nil? # lease = find_lease({:uuid => lease_el[:uuid]}, {}, authorizer) # raise UnavailableResourceException.new "Unknown lease uuid'#{lease_el[:uuid]}'" unless lease # if lease.valid_from != lease_properties[:valid_from] || lease.valid_until != lease_properties[:valid_until] # lease = modify_lease(lease_properties, lease, authorizer) # { lease_el[:leaseID] => lease } # else # { lease_el[:leaseID] => lease } # end #else # lease_descr = {:name => authorizer.account.name} # lease = find_or_create_lease(lease_descr, lease_properties, authorizer) # { lease_el[:leaseID] => lease } #end end |
#update_leases_from_rspec(leases, authorizer) ⇒ Hash{String => OLease}
Update the leases described in leases
. Any lease not already assigned to the requesting account will be added. If clean_state
is true, the state of all described leases is set to the state described with all other properties set to their default values. Any leases not mentioned are canceled. Returns the list of leases requested or throw an error if ANY of the requested leases isn’t available.
397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/omf-sfa/am/am_manager.rb', line 397 def update_leases_from_rspec(leases, ) debug "update_leases_from_rspec: leases:'#{leases.inspect}' authorizer:'#{.inspect}'" leases_hash = {} unless leases.empty? leases.each do |lease| l = update_lease_from_rspec(lease, ) leases_hash.merge!(l) end end leases_hash end |
#update_resource_from_rspec(resource_el, leases, clean_state, authorizer) ⇒ Object
Update a single resource described in resource_el
. The respective account is extracted from opts
. Any mentioned resources not already available to the requesting account will be created. If clean_state
is set to true, all state of a resource not specifically described will be reset to it’s default value. Returns the resource updated.
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/omf-sfa/am/am_manager.rb', line 689 def update_resource_from_rspec(resource_el, leases, clean_state, ) if uuid_attr = (resource_el.attributes['uuid'] || resource_el.attributes['idref']) uuid = UUIDTools::UUID.parse(uuid_attr.value) resource = find_resource({:uuid => uuid}, ) # wouldn't know what to create elsif comp_id_attr = resource_el.attributes['component_id'] comp_id = comp_id_attr.value comp_gurn = OMF::SFA::Resource::GURN.parse(comp_id) #if uuid = comp_gurn.uuid # resource_descr = {:uuid => uuid} #else # resource_descr = {:name => comp_gurn.short_name} #end resource_descr = {:urn => comp_gurn} resource = find_or_create_resource_for_account(resource_descr, comp_gurn.type, {}, ) unless resource raise UnknownResourceException.new "Resource '#{resource_el.to_s}' is not available or doesn't exist" end elsif name_attr = resource_el.attributes['component_name'] # the only resource we can find by a name attribute is a group # TODO: Not sure about the 'group' assumption name = name_attr.value resource = find_or_create_resource_for_account({:name => name}, 'unknown', {}, ) else raise FormatException.new "Unknown resource description '#{resource_el.attributes.inspect}" end leases_el = resource_el.xpath('child::ol:lease_ref', 'ol' => OL_NAMESPACE) leases_el.each do |lease_el| #TODO: provide the scheduler with the resource and the lease to attach them according to its policy. # if the scheduler refuses to attach the lease to the resource, we should release both of them. lease_id = lease_el['id_ref'] lease = leases[lease_id] @scheduler.lease_component(lease, resource) end if resource.group? members = resource_el.children.collect do |el| if el.kind_of?(Nokogiri::XML::Element) # ignore any text elements update_resource_from_rspec(el, clean_state, ) end end.compact debug "update_resource_from_rspec: Creating members '#{members}' for group '#{resource}'" if clean_state resource.members = members else resource.add_members(members) end else if clean_state # Set state to what's described in +resource_el+ ONLY resource.create_from_xml(resource_el, ) else resource.update_from_xml(resource_el, ) end end resource.save resource end |
#update_resources_from_rspec(descr_el, clean_state, authorizer) ⇒ OResource
Throws exception if a contained resource doesn’t exist, but will not roll back any
Update the resources described in resource_el
. Any resource not already assigned to the requesting account will be added. If clean_state
is true, the state of all described resources is set to the state described with all other properties set to their default values. Any resources not mentioned are released. Returns the list of resources requested or throw an error if ANY of the requested resources isn’t available.
Find or create a resource. If it doesn’t exist, is already assigned to someone else, or cannot be created, throws UnknownResourceException
.
already performed modifications performed on other resources.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
# File 'lib/omf-sfa/am/am_manager.rb', line 609 def update_resources_from_rspec(descr_el, clean_state, ) debug "update_resources_from_rspec: descr_el:'#{descr_el}' clean_state:'#{clean_state}' authorizer:'#{}'" if !descr_el.nil? && descr_el.name.downcase == 'rspec' xsd_path = File.join(File.dirname(__FILE__), '../../../schema/rspec-v3', 'request.xsd') schema = Nokogiri::XML::Schema(File.open(xsd_path)) res = schema.validate(descr_el.document) raise FormatException.new("RSpec format is not valid: '#{res}'") unless res.empty? unless descr_el.xpath('//ol:*', 'ol' => OL_NAMESPACE).empty? #TODO: make proper schemas and validate them #lease_xsd_path = File.join(File.dirname(__FILE__), '../../../schema/rspec-v3', 'request-reservation.xsd') #lease_rng_path = File.join(File.dirname(__FILE__), '../../../schema/rspec-v3', 'request-reservation.rng') #lease_schema = Nokogiri::XML::Schema(File.open(lease_xsd_path)) #lease_schema = Nokogiri::XML::RelaxNG(File.open(lease_rng_path)) #res = lease_schema.validate(descr_el.document) #raise FormatException.new("RSpec format is not valid: '#{res}'") unless res.empty? end if descr_el.namespaces.values.include?(OL_NAMESPACE) #leases = descr_el.xpath('/rspec//ol:lease', 'ol' => OL_NAMESPACE) leases = descr_el.xpath('/xmlns:rspec/ol:lease', 'ol' => OL_NAMESPACE, 'xmlns' => "http://www.geni.net/resources/rspec/3") leases = update_leases_from_rspec(leases, ) end resources = descr_el.xpath('//xmlns:node').collect do |el| #debug "create_resources_from_xml::EL: #{el.inspect}" if el.kind_of?(Nokogiri::XML::Element) # ignore any text elements #if el[:lease_name].nil? # update_resource_from_rspec(el, nil, clean_state, authorizer) #else # This node has a lease # lease = leases.find { |l| l[:name].eql?(el[:lease_name]) } #leases = el.xpath('child::ol:lease', 'ol' => OL_NAMESPACE) #leases = update_leases_from_rspec(leases, authorizer) update_resource_from_rspec(el, leases, clean_state, ) #end end end.compact # channel reservation #resources = descr_el.xpath('/xmlns:rspec/ol:channel', 'ol' => OL_NAMESPACE, 'xmlns' => "http://www.geni.net/resources/rspec/3").collect do |el| # update_resource_from_rspec(el, leases, clean_state, authorizer) #end.compact # TODO: release the unused leases. The leases we have created but we never managed # to attach them to a resource because the scheduler denied it. if clean_state # Now free any leases owned by this account but not contained in +leases+ all_leases = Set.new #leases = descr_el.xpath('//ol:lease', 'ol' => OL_NAMESPACE).collect do |l| # update_leases_from_rspec(leases, authorizer) #end.compact leases.each {|l| l.all_resources(all_leases)} unused = find_all_leases_for_account(.account, ).to_set - all_leases unused.each do |u| release_lease(u, ) end # Now free any resources owned by this account but not contained in +resources+ rspec_resources = Set.new resources.each {|r| r.all_resources(rspec_resources)} all_components = find_all_components_for_account(.account, ) unused = all_components.to_set - rspec_resources release_resources(unused, ) end return resources else raise FormatException.new "Unknown resources description root '#{descr_el}'" end end |