Skip to content

Commit

Permalink
Moved selectors to rOCCI-core (beta.17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Parak committed Aug 15, 2017
1 parent be37272 commit b1e1717
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ gem 'rack-cors', '~> 0.4'
gem 'rails', '~> 5.1.1'
gem 'responders', '~> 2.4.0'

gem 'occi-core', '= 5.0.0.beta.16', require: 'occi/infrastructure-ext' # '~> 5.0.0'
gem 'occi-core', '= 5.0.0.beta.17', require: 'occi/infrastructure-ext' # '~> 5.0.0'

group :development, :test do
gem 'byebug'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ GEM
nio4r (2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
occi-core (5.0.0.beta.16)
occi-core (5.0.0.beta.17)
activesupport (>= 4.0, < 6)
json (>= 1.8, < 3)
json-schema (>= 2.5, < 3)
Expand Down Expand Up @@ -183,7 +183,7 @@ DEPENDENCIES
byebug
listen (>= 3.0.5, < 3.2)
logstasher (~> 1.2)
occi-core (= 5.0.0.beta.16)
occi-core (= 5.0.0.beta.17)
opennebula (~> 5.4)
puma (~> 3.7)
rack-attack (~> 5.0.1)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/dummy/networkinterface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def instance(identifier)
instance = super
instance['occi.core.source'] = URI.parse '/compute/a262ad95-c093-4814-8c0d-bc6d475bb845'
instance['occi.core.target'] = URI.parse '/network/8b3e4362-b761-4eed-a6f3-69e271f90286'
instance.target_kind = category_by_identifier!(
instance.target_kind = find_by_identifier!(
Occi::Infrastructure::Constants::NETWORK_KIND
)
instance
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/dummy/securitygrouplink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def instance(identifier)
instance = super
instance['occi.core.source'] = URI.parse '/compute/a262ad95-c093-4814-8c0d-bc6d475bb845'
instance['occi.core.target'] = URI.parse '/securitygroup/8b3e4362-b761-4eed-a6f3-69e271f90286'
instance.target_kind = category_by_identifier!(
instance.target_kind = find_by_identifier!(
Occi::InfrastructureExt::Constants::SECURITY_GROUP_KIND
)
instance
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/dummy/storagelink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def instance(identifier)
instance = super
instance['occi.core.source'] = URI.parse '/compute/a262ad95-c093-4814-8c0d-bc6d475bb845'
instance['occi.core.target'] = URI.parse '/storage/8b3e4362-b761-4eed-a6f3-69e271f90286'
instance.target_kind = category_by_identifier!(
instance.target_kind = find_by_identifier!(
Occi::Infrastructure::Constants::STORAGE_KIND
)
instance
Expand Down
62 changes: 1 addition & 61 deletions app/lib/backends/helpers/entitylike.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Entitylike
delegate :serves?, to: :class
delegate :server_model, to: :backend_proxy
delegate :instance_builder, to: :server_model
delegate :find_by_identifier!, to: :server_model
end

class_methods do
Expand Down Expand Up @@ -136,67 +137,6 @@ def delete(_identifier)
def delete_all(filter = Set.new)
Set.new(identifiers(filter).map { |id| delete(id) })
end

# Looks up given category (by identifier) in the current server model. This
# method will raise an error if no such category is present. In case of multiple
# categories (which should NOT happen), only the first one will be returned.
#
# @param identifier [String] category identifier in the form of `schema#term`
# @return [Occi::Core::Category] located category
def category_by_identifier!(identifier)
server_model.find_by_identifier! identifier
end

# Returns a list of term of mixins dependent on the given mixin specified by `identifier`.
#
# @param entity [Occi::Core::Entity] entity to search
# @param identifier [String] parent mixin identifier
# @return [Array] terms of mixins
def mixin_terms(entity, identifier)
mxn = server_model.find_by_identifier!(identifier)
entity.select_mixins(mxn).map(&:term)
end

# @see `mixin_terms`
def mixin_term(*args)
mixin_terms(*args).first
end

# :nodoc:
def links_by_klass(entity, klass)
entity.links.select { |l| l.is_a?(klass) }
end

# :nodoc:
def storagelinks(entity)
links_by_klass entity, Occi::Infrastructure::Storagelink
end

# :nodoc:
def networkinterfaces(entity)
links_by_klass entity, Occi::Infrastructure::Networkinterface
end

# :nodoc:
def securitygrouplinks(entity)
links_by_klass entity, Occi::InfrastructureExt::SecurityGroupLink
end

# :nodoc:
def link_target_id(link)
last_uri_segment link['occi.core.target']
end

# :nodoc:
def link_source_id(link)
last_uri_segment link['occi.core.source']
end

# :nodoc:
def last_uri_segment(uri)
raise 'Invalid URI' if uri.blank?
uri.path.split('/').last || raise('No entity ID found in URI')
end
end
end
end
20 changes: 12 additions & 8 deletions app/lib/backends/opennebula/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def compute_from(virtual_machine)
# @param storage [Occi::Infrastructure::Compute] instance to transform
# @return [String] ONe template
def virtual_machine_from(compute)
os_tpl = mixin_term(compute, Occi::Infrastructure::Constants::OS_TPL_MIXIN)
os_tpl = compute.dependent_term(find_by_identifier!(Occi::Infrastructure::Constants::OS_TPL_MIXIN))

template = ::OpenNebula::Template.new_with_id(os_tpl, raw_client)
client(Errors::Backend::EntityStateError) { template.info }
Expand All @@ -98,8 +98,8 @@ def virtual_machine_from(compute)

# :nodoc:
def attach_mixins!(virtual_machine, compute)
compute << category_by_identifier!(Occi::Infrastructure::Constants::USER_DATA_MIXIN)
compute << category_by_identifier!(Occi::Infrastructure::Constants::SSH_KEY_MIXIN)
compute << find_by_identifier!(Occi::Infrastructure::Constants::USER_DATA_MIXIN)
compute << find_by_identifier!(Occi::Infrastructure::Constants::SSH_KEY_MIXIN)
compute << server_model.find_regions.first

attach_optional_mixin! compute, virtual_machine['HISTORY_RECORDS/HISTORY[last()]/CID'], :availability_zone
Expand Down Expand Up @@ -149,7 +149,8 @@ def set_size!(template, compute)

# :nodoc:
def set_security_groups!(template, compute)
sgs = securitygrouplinks(compute).map { |l| link_target_id(l) }.join(',')
sgs = compute.links_by_kind_identifier(Occi::InfrastructureExt::Constants::SECURITY_GROUP_LINK_KIND)
sgs = sgs.map(&:target_id).join(',')

idx = 1
template.each('TEMPLATE/NIC') do |_nic|
Expand All @@ -160,7 +161,7 @@ def set_security_groups!(template, compute)

# :nodoc:
def set_cluster!(template, compute)
az = mixin_term(compute, Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN)
az = compute.dependent_term(find_by_identifier!(Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN))
return unless az

sched_reqs = template['TEMPLATE/SCHED_REQUIREMENTS'] || ''
Expand Down Expand Up @@ -193,14 +194,17 @@ def add_gpu!(template_str, compute)

# :nodoc:
def add_nics!(template_str, compute)
sg_ids = securitygrouplinks(compute).map { |sg| link_target_id(sg) }
data = { instances: networkinterfaces(compute), security_groups: sg_ids }
sgs = compute.links_by_kind_identifier(Occi::InfrastructureExt::Constants::SECURITY_GROUP_LINK_KIND)
data = {
instances: compute.links_by_kind_identifier(Occi::Infrastructure::Constants::NETWORKINTERFACE_KIND),
security_groups: sgs.map(&:target_id)
}
add_erb! template_str, data, 'compute_nic.erb'
end

# :nodoc:
def add_disks!(template_str, compute)
data = { instances: storagelinks(compute) }
data = { instances: compute.links_by_kind_identifier(Occi::Infrastructure::Constants::STORAGELINK_KIND) }
add_erb! template_str, data, 'compute_disk.erb'
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/ipreservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def instance(identifier)

# @see `Entitylike`
def create(instance)
fltp = mixin_term(instance, Occi::InfrastructureExt::Constants::FLOATINGIPPOOL_MIXIN)
fltp = instance.dependent_term(find_by_identifier!(Occi::InfrastructureExt::Constants::FLOATINGIPPOOL_MIXIN))

vnet = ::OpenNebula::VirtualNetwork.new_with_id(fltp, raw_client)
res_name = instance['occi.core.title'] || ::SecureRandom.uuid
Expand Down
6 changes: 3 additions & 3 deletions app/lib/backends/opennebula/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def create(instance)
vnet_template = virtual_network_from(instance)

# TODO: multi-cluster networks
az = mixin_term(instance, Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN)
az = instance.dependent_term(find_by_identifier!(Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN))
cid = (az || default_cluster).to_i

vnet = ::OpenNebula::VirtualNetwork.new(::OpenNebula::VirtualNetwork.build_xml, raw_client)
Expand Down Expand Up @@ -100,7 +100,7 @@ def virtual_network_from(network)
# :nodoc:
def attach_mixins!(virtual_network, network)
if virtual_network['AR_POOL/AR/IP']
network << category_by_identifier!(Occi::Infrastructure::Constants::IPNETWORK_MIXIN)
network << find_by_identifier!(Occi::Infrastructure::Constants::IPNETWORK_MIXIN)
end
network << server_model.find_regions.first

Expand All @@ -122,7 +122,7 @@ def set_network_type!(virtual_network, network)
Occi::InfrastructureExt::Constants::NAT_NET_MIXIN
end

network << category_by_identifier!(mxn) if mxn
network << find_by_identifier!(mxn) if mxn
end

# :nodoc:
Expand Down
4 changes: 2 additions & 2 deletions app/lib/backends/opennebula/networkinterface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def nic_from(networkinterface, virtual_machine)
# :nodoc:
def attach_mixins!(nic, virtual_machine, networkinterface)
if nic['IP']
networkinterface << category_by_identifier!(
networkinterface << find_by_identifier!(
Occi::Infrastructure::Constants::IPNETWORKINTERFACE_MIXIN
)
end
Expand All @@ -139,7 +139,7 @@ def fix_target!(nic, networkinterface)
else
Occi::Infrastructure::Constants::NETWORK_KIND
end
networkinterface.target_kind = category_by_identifier!(tk)
networkinterface.target_kind = find_by_identifier!(tk)
end

# :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/securitygrouplink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def securitygrouplink_from(sg, virtual_machine)
[sg, virtual_machine], sg_link,
Constants::Securitygrouplink::TRANSFERABLE_ATTRIBUTES
)
sg_link.target_kind = category_by_identifier!(
sg_link.target_kind = find_by_identifier!(
Occi::InfrastructureExt::Constants::SECURITY_GROUP_KIND
)

Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def attach_mixins!(image, storage)

# :nodoc:
def candidate_datastore(instance)
azs = mixin_terms(instance, Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN)
azs = instance.dependent_terms(find_by_identifier!(Occi::InfrastructureExt::Constants::AVAILABILITY_ZONE_MIXIN))
azs << default_cluster if azs.empty?

azs.sort!
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/storagelink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def storagelink_from(disk, virtual_machine)
[disk, virtual_machine], storagelink,
Constants::Storagelink::TRANSFERABLE_ATTRIBUTES
)
storagelink.target_kind = category_by_identifier!(
storagelink.target_kind = find_by_identifier!(
Occi::Infrastructure::Constants::STORAGE_KIND
)

Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/templates/compute_disk.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% data[:instances].each do |storagelink| %>
DISK = [
IMAGE_ID = "<%= link_target_id(storagelink) %>"
IMAGE_ID = "<%= storagelink.target_id %>"

<% if storagelink['occi.storagelink.deviceid'].present? %>
,TARGET = "<%= storagelink['occi.storagelink.deviceid'].gsub('/dev/', '') %>"
Expand Down
2 changes: 1 addition & 1 deletion app/lib/backends/opennebula/templates/compute_nic.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% sec_groups = data[:security_groups].join(',') %>
<% data[:instances].each do |networkinterface| %>
NIC = [
NETWORK_ID = "<%= link_target_id(networkinterface) %>"
NETWORK_ID = "<%= networkinterface.target_id %>"

<% if networkinterface['occi.networkinterface.address'].present? %>
,IP = "<%= networkinterface['occi.networkinterface.address'] %>"
Expand Down

0 comments on commit b1e1717

Please sign in to comment.