Skip to content

Commit

Permalink
Added version and available_version methods
Browse files Browse the repository at this point in the history
https://bugzilla.redhat.com/show_bug.cgi?id=1283037

Added 2 new methods version and available_version to the
MiqAeDomain class that the UI can call to check the version
in the DB and the version on the disk.
  • Loading branch information
mkanoor committed Nov 20, 2015
1 parent 0fa77bc commit 4f622af
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/miq_automation_engine/models/miq_ae_domain.rb
@@ -1,3 +1,4 @@
include MiqAeYamlImportExportMixin
class MiqAeDomain < MiqAeNamespace
default_scope { where(:parent_id => nil).where(arel_table[:name].not_eq("$")) }
validates_inclusion_of :parent_id, :in => [nil], :message => 'should be nil for Domain'
Expand Down Expand Up @@ -31,6 +32,23 @@ def default_priority
self.priority = MiqAeDomain.highest_priority(tenant) + 1 unless priority
end

def version
system = ae_namespaces.detect { |ns| ns.name == 'System' }
about = system.try(:ae_classes).try(:detect) { |cls| cls.name == 'About' }
version_field = about.try(:ae_fields).try(:detect) { |fld| fld.name == 'version' }
version_field.try(:default_value)
end

def available_version
fname = File.join(MiqAeDatastore::DATASTORE_DIRECTORY, name.downcase, 'system',
"about#{CLASS_DIR_SUFFIX}", CLASS_YAML_FILENAME)
return nil unless File.exist?(fname)
class_yaml = YAML.load_file(fname)
fields = class_yaml.fetch_path('object', 'schema') if class_yaml.kind_of?(Hash)
version_field = fields.try(:detect) { |f| f.fetch_path('field', 'name') == 'version' }
version_field.try(:fetch_path, 'field', 'default_value')
end

private

def squeeze_priorities
Expand Down
@@ -0,0 +1,67 @@
require "spec_helper"
include AutomationSpecHelper

describe MiqAeDomain do
before do
EvmSpecHelper.local_guid_miq_server_zone
@user = FactoryGirl.create(:user_with_group)
@temp_dir = Dir.mktmpdir
require 'miq_ae_datastore'
stub_const("MiqAeDatastore::DATASTORE_DIRECTORY", @temp_dir)
end

after do
FileUtils.remove_entry_secure(@temp_dir) if Dir.exist?(@temp_dir)
end

context "version" do
it "no about class" do
d1 = FactoryGirl.create(:miq_ae_domain, :name => 'Obelix')
expect(d1.version).to be_nil
end

it "about class present" do
ver = '3.0.9'
expect(create_about_class('Dogmatix', ver).version).to eq(ver)
end
end

context "available_version" do
it "no version specified" do
d1 = FactoryGirl.create(:miq_ae_domain, :name => 'Asterix')
expect(d1.available_version).to be_nil
end

it "about file present" do
version = '7.1.1'
dom2 = create_about_class('Vitalstatistix', version)
write_about_file(dom2)
expect(dom2.available_version).to eq('7.1.1')
end
end

def write_about_file(domain)
obj = MiqAeClass.find_by_fqname("/#{domain.name}/System/About")
hash = {'object_type' => CLASS_OBJ_TYPE,
'version' => VERSION,
'object' => {'attributes' => obj.export_attributes,
'schema' => obj.export_schema}}
fname = File.join(MiqAeDatastore::DATASTORE_DIRECTORY, domain.name.downcase, 'system',
"about#{CLASS_DIR_SUFFIX}", CLASS_YAML_FILENAME)
dirname = File.dirname(fname)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
File.write(fname, hash.to_yaml)
end

def create_about_class(domain, version)
ae_fields = {'version' => {:aetype => 'attribute', :datatype => 'string', :default_value => version},
'tree' => {:aetype => 'attribute', :datatype => 'string', :default_value => "ast"}}
ae_instances = {'instance1' => {'version' => {:value => 'hello world'},
'tree' => {:value => 'adt'}}}
create_ae_model(:name => domain,
:ae_class => 'About',
:ae_namespace => 'System',
:ae_fields => ae_fields,
:ae_instances => ae_instances)
end
end
6 changes: 4 additions & 2 deletions spec/support/automation_spec_helper.rb
Expand Up @@ -30,8 +30,10 @@ def assert_method_executed(uri, value, user)
def create_ae_model(attrs = {})
attrs = default_ae_model_attributes(attrs)
instance_name = attrs.delete(:instance_name)
ae_fields = {'field1' => {:aetype => 'relationship', :datatype => 'string'}}
ae_instances = {instance_name => {'field1' => {:value => 'hello world'}}}
ae_fields = attrs.delete(:ae_fields)
ae_instances = attrs.delete(:ae_instances)
ae_fields ||= {'field1' => {:aetype => 'relationship', :datatype => 'string'}}
ae_instances ||= {instance_name => {'field1' => {:value => 'hello world'}}}

FactoryGirl.create(:miq_ae_domain, :with_small_model, :with_instances,
attrs.merge('ae_fields' => ae_fields, 'ae_instances' => ae_instances))
Expand Down

0 comments on commit 4f622af

Please sign in to comment.