Skip to content

Commit

Permalink
Merge pull request #17680 from slemrmartin/ic-builder-specs
Browse files Browse the repository at this point in the history
Persister: Specs changes for add_collection() interface
  • Loading branch information
agrare authored Jul 11, 2018
2 parents 9036816 + e167283 commit f433e67
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 130 deletions.
40 changes: 19 additions & 21 deletions spec/models/manager_refresh/persister/local_db_finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -406,46 +406,44 @@
context "check validity of defined index" do
it "checks primary index attributes exist" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:manager_ref => %i(ems_ref ems_gref))
persister.add_collection(persister.send(:cloud),
:vms,
:manager_ref => %i(ems_ref ems_gref))
end.to raise_error("Invalid definition of index :manager_ref, there is no attribute :ems_gref on model Vm")
end

it "checks secondary index attributes exist" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:secondary_refs => {:by_uid_ems_and_name => %i(uid_emsa name)})
persister.add_collection(persister.send(:cloud),
:vms,
:secondary_refs => {:by_uid_ems_and_name => %i(uid_emsa name)})
end.to raise_error("Invalid definition of index :by_uid_ems_and_name, there is no attribute :uid_emsa on model Vm")
end

it "checks relation is allowed in index" do
persister.send(:add_inventory_collection,
:model_class => ::ManageIQ::Providers::CloudManager::Vm,
:association => :vms,
:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
persister.add_collection(persister.send(:cloud), :vms) do |builder|
builder.add_properties(:model_class => ::ManageIQ::Providers::CloudManager::Vm,
:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
end

expect(persister.vms.index_proxy.send(:data_indexes).keys).to match_array(%i(manager_ref by_availability_zone_and_name))
end

it "checks relation is on model class" do
expect do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
persister.add_collection(persister.send(:cloud), :vms) do |builder|
builder.add_properties(:secondary_refs => {:by_availability_zone_and_name => %i(availability_zone name)})
end
end.to raise_error("Invalid definition of index :by_availability_zone_and_name, there is no attribute :availability_zone on model Vm")
end

it "checks we allow any index attributes when we use custom_saving block" do
persister.send(:add_inventory_collection,
:model_class => ::Vm,
:association => :vms,
:custom_save_block => ->(ems, _ic) { ems },
:manager_ref => %i(a b c))
persister.add_collection(persister.send(:cloud), :vms) do |builder|
builder.add_properties(
:custom_save_block => ->(ems, _ic) { ems },
:manager_ref => %i(a b c)
)
end

expect(persister.vms.index_proxy.send(:data_indexes).keys).to match_array([:manager_ref])
end
Expand Down
211 changes: 102 additions & 109 deletions spec/models/manager_refresh/persister/test_persister.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,149 +2,142 @@ class TestPersister < ManagerRefresh::Inventory::Persister
def initialize_inventory_collections
######### Cloud ##########
# Top level models with direct references for Cloud
add_inventory_collections_with_references(
cloud,
%i(vms miq_templates),
:secondary_refs => {:by_name => [:name], :by_uid_ems_and_name => %i(uid_ems name)}
)
add_inventory_collections_with_references(
cloud,
%i(availability_zones orchestration_stacks)
)

add_inventory_collection_with_references(
cloud,
:key_pairs,
name_references(:key_pairs)
)
%i(vms
miq_templates).each do |name|

add_collection(cloud, name) do |builder|
builder.add_properties(
:secondary_refs => {:by_name => [:name], :by_uid_ems_and_name => %i(uid_ems name)}
)
end
end

# Child models with references in the Parent InventoryCollections for Cloud
add_inventory_collections(
cloud,
%i(orchestration_stacks_resources),
:secondary_refs => {:by_stack_and_ems_ref => %i(stack ems_ref)}
)
add_key_pairs

add_inventory_collections(
cloud,
%i(hardwares networks disks vm_and_template_labels orchestration_stacks_outputs
orchestration_stacks_parameters)
)
# Child models with references in the Parent InventoryCollections for Cloud
%i(availability_zones
hardwares
networks
disks
vm_and_template_labels
orchestration_stacks
orchestration_templates
orchestration_stacks_outputs
orchestration_stacks_parameters).each do |name|

add_collection(cloud, name)
end

add_inventory_collection(cloud.orchestration_templates)
add_orchestration_stacks_resources

######### Network ################
# Top level models with direct references for Network
add_inventory_collections_with_references(
network,
%i(cloud_networks cloud_subnets security_groups load_balancers),
:parent => manager.network_manager
)

add_inventory_collection_with_references(
network,
:network_ports,
references(:vms) + references(:network_ports) + references(:load_balancers),
:parent => manager.network_manager,
:secondary_refs => {:by_device => [:device], :by_device_and_name => %i(device name)}
)

add_inventory_collection_with_references(
network,
:floating_ips,
references(:floating_ips) + references(:load_balancers),
:parent => manager.network_manager
)
%i(cloud_networks
cloud_subnets
security_groups
load_balancers).each do |name|

add_collection(network, name) do |builder|
builder.add_properties(:parent => manager.network_manager)
end
end

add_network_ports

add_floating_ips

# Child models with references in the Parent InventoryCollections for Network
add_inventory_collections(
network,
%i(firewall_rules cloud_subnet_network_ports load_balancer_pools load_balancer_pool_members
load_balancer_pool_member_pools load_balancer_listeners load_balancer_listener_pools
load_balancer_health_checks load_balancer_health_check_members),
:parent => manager.network_manager
)
%i(firewall_rules
cloud_subnet_network_ports
load_balancer_pools
load_balancer_pool_members
load_balancer_pool_member_pools
load_balancer_listeners
load_balancer_listener_pools
load_balancer_health_checks
load_balancer_health_check_members).each do |name|

add_collection(network, name) do |builder|
builder.add_properties(:parent => manager.network_manager)
end
end

# Model we take just from a DB, there is no flavors API
add_inventory_collections(
cloud,
%i(flavors),
:strategy => :local_db_find_references
)
add_flavors

######## Custom processing of Ancestry ##########
add_inventory_collection(
cloud.vm_and_miq_template_ancestry(
:dependency_attributes => {
:vms => [collections[:vms]],
:miq_templates => [collections[:miq_templates]]
}
)
)

add_inventory_collection(
cloud.orchestration_stack_ancestry(
:dependency_attributes => {
:orchestration_stacks => [collections[:orchestration_stacks]],
:orchestration_stacks_resources => [collections[:orchestration_stacks_resources]]
}
)
)
end
%i(vm_and_miq_template_ancestry
orchestration_stack_ancestry).each do |name|

private

def add_inventory_collections_with_references(inventory_collections_data, names, options = {})
names.each do |name|
add_inventory_collection_with_references(inventory_collections_data, name, references(name), options)
add_collection(cloud, name, {}, {:without_model_class => true})
end
end

def add_inventory_collection_with_references(inventory_collections_data, name, manager_refs, options = {})
options = shared_options.merge(inventory_collections_data.send(
name,
:manager_uuids => manager_refs,
).merge(options))
private

add_inventory_collection(options)
# Cloud InventoryCollection
def add_key_pairs
add_collection(cloud, :key_pairs) do |builder|
builder.add_properties(:manager_uuids => name_references(:key_pairs))
end
end

def add_inventory_collection(options)
super(options)
# Cloud InventoryCollection
def add_orchestration_stacks_resources
add_collection(cloud, :orchestration_stacks_resources) do |builder|
builder.add_properties(:secondary_refs => {:by_stack_and_ems_ref => %i(stack ems_ref)})
end
end

def targeted
true
# Cloud InventoryCollection
def add_flavors
add_collection(cloud, :flavors) do |builder|
builder.add_properties(:strategy => :local_db_find_references)
end
end

def strategy
:local_db_find_missing_references
# Network InventoryCollection
def add_network_ports
add_collection(network, :network_ports) do |builder|
builder.add_properties(
:manager_uuids => references(:vms) + references(:network_ports) + references(:load_balancers),
:parent => manager.network_manager,
:secondary_refs => {:by_device => [:device], :by_device_and_name => %i(device name)}
)
end
end

def shared_options
{
:strategy => strategy,
:targeted => targeted,
}
# Network InventoryCollection
def add_floating_ips
add_collection(network, :floating_ips) do |builder|
builder.add_properties(
:manager_uuids => references(:floating_ips) + references(:load_balancers),
:parent => manager.network_manager
)
end
end

def references(collection)
target.manager_refs_by_association.try(:[], collection).try(:[], :ems_ref).try(:to_a) || []
def options
{}
end

def name_references(collection)
target.manager_refs_by_association.try(:[], collection).try(:[], :name).try(:to_a) || []
def targeted?
true
end

def cloud
ManagerRefresh::InventoryCollectionDefault::CloudManager
def strategy
:local_db_find_missing_references
end

def network
ManagerRefresh::InventoryCollectionDefault::NetworkManager
def parent
manager.presence
end

def storage
ManagerRefresh::InventoryCollectionDefault::StorageManager
def shared_options
{
:strategy => strategy,
:targeted => targeted?,
:parent => parent
}
end
end

0 comments on commit f433e67

Please sign in to comment.