Skip to content

Commit

Permalink
Merge pull request ManageIQ#56 from mkanoor/new_export_import
Browse files Browse the repository at this point in the history
Allows expressions to be exported and imported
  • Loading branch information
gmcculloug authored Jul 28, 2017
2 parents 6480698 + 69bf6cd commit 1439897
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
8 changes: 6 additions & 2 deletions app/models/miq_ae_yaml_export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,19 @@ def write_all_methods(ns_fqname, class_obj)
class_obj.ae_methods.sort_by(&:fqname).each do |meth_obj|
export_file_hash['created_on'] = meth_obj.created_on
export_file_hash['updated_on'] = meth_obj.updated_on
write_method_file(meth_obj, export_file_hash) unless meth_obj.location == 'builtin'
if meth_obj.location == 'inline'
write_method_file(meth_obj, export_file_hash)
end
write_method_attributes(meth_obj, export_file_hash)
end
end

def write_method_attributes(method_obj, export_file_hash)
envelope_hash = setup_envelope(method_obj, METHOD_OBJ_TYPE)
envelope_hash['object']['inputs'] = method_obj.method_inputs
envelope_hash['object']['attributes'].delete('data')
if method_obj.location == "inline"
envelope_hash['object']['attributes'].delete('data')
end
if method_obj.embedded_methods.empty?
envelope_hash['object']['attributes'].delete('embedded_methods')
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/miq_ae_yaml_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def process_instance(class_obj, instance_yaml)
def process_method(class_obj, ruby_method_file_name, method_yaml)
method_attributes = method_yaml.fetch_path('object', 'attributes')
if method_attributes['location'] == 'inline'
method_yaml.store_path('object', 'attributes', 'data', load_method_ruby(ruby_method_file_name))
data = load_method_ruby(ruby_method_file_name)
method_yaml.store_path('object', 'attributes', 'data', data) if data
end
method_obj = MiqAeMethod.find_by(:name => method_attributes['name'], :class_id => class_obj.id) unless class_obj.nil?
track_stats('method', method_obj)
Expand Down
60 changes: 44 additions & 16 deletions spec/models/miq_ae_yaml_import_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
'max_retries' => "10",
'collect' => "dinosaurs",
'max_time' => "100"}
@expression_data = "---\n:db: VmOrTemplate\n:expression:\n STARTS WITH:\n field: VmOrTemplate-name\n value: :user_input\n"
@clear_default_password = 'little_secret'
@clear_password = 'secret'
@relations_value = "bedrock relations"
@domain_counts = {'dom' => 1, 'ns' => 3, 'class' => 4, 'inst' => 10,
'meth' => 3, 'field' => 12, 'value' => 8}
'meth' => 4, 'field' => 12, 'value' => 8}
@domain_counts_with_extra_items = {'dom' => 1, 'ns' => 4, 'class' => 5, 'inst' => 11,
'meth' => 3, 'field' => 12, 'value' => 8}
'meth' => 4, 'field' => 12, 'value' => 8}
EvmSpecHelper.local_miq_server
@tenant = Tenant.seed
create_factory_data("manageiq", 0, MiqAeDomain::SYSTEM_SOURCE)
Expand Down Expand Up @@ -225,7 +226,7 @@ def assert_all_domains_imported(export_options, import_options)
export_model(MiqAeYamlImportExportMixin::ALL_DOMAINS, export_options)
reset_and_import(@export_dir, MiqAeYamlImportExportMixin::ALL_DOMAINS, import_options)
check_counts('dom' => 2, 'ns' => 6, 'class' => 8, 'inst' => 20,
'meth' => 6, 'field' => 24, 'value' => 16)
'meth' => 8, 'field' => 24, 'value' => 16)
end

it "import single domain, from directory" do
Expand Down Expand Up @@ -370,7 +371,7 @@ def assert_import_as(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 2, 'ns' => 6, 'class' => 8, 'inst' => 20,
'meth' => 6, 'field' => 24, 'value' => 16)
'meth' => 8, 'field' => 24, 'value' => 16)
expect(MiqAeDomain.find_by_fqname(import_options['import_as'])).not_to be_nil
end

Expand Down Expand Up @@ -419,7 +420,7 @@ def assert_import_namespace_only(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 1, 'ns' => 2, 'class' => 3, 'inst' => 6,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
end

it "domain, import only multi-part namespace, to directory" do
Expand Down Expand Up @@ -470,7 +471,7 @@ def assert_import_class_only(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 2,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
end

it "namespace, to directory" do
Expand All @@ -495,7 +496,7 @@ def assert_single_namespace_export(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 1, 'ns' => 2, 'class' => 3, 'inst' => 6,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
end

it "namespace, multi-part, to directory" do
Expand Down Expand Up @@ -528,7 +529,7 @@ def assert_multi_namespace_export(export_options, import_options)
export_model(@manageiq_domain.name, options)
reset_and_import(@export_dir, @manageiq_domain.name)
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 2,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
@manageiq_domain = MiqAeNamespace.find_by_fqname('manageiq', false)
@aen1_aec1 = MiqAeClass.find_by_name('manageiq_test_class_1')
@aen1_aec1_aei2 = FactoryGirl.create(:miq_ae_instance,
Expand All @@ -538,7 +539,7 @@ def assert_multi_namespace_export(export_options, import_options)
export_model(@manageiq_domain.name, options)
MiqAeImport.new(@manageiq_domain.name, 'preview' => false, 'import_dir' => @export_dir).import
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 3,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
end

it "class, with methods, to directory" do
Expand All @@ -565,7 +566,7 @@ def assert_class_with_methods_export(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 2,
'meth' => 2, 'field' => 6, 'value' => 4)
'meth' => 3, 'field' => 6, 'value' => 4)
end

it "class, with builtin methods, as directory" do
Expand All @@ -588,15 +589,35 @@ def assert_class_with_methods_export(export_options, import_options)
assert_class_with_builtin_methods_export(export_options, import_options)
end

def assert_class_with_builtin_methods_export(export_options, import_options)
it "class, with expression methods, as zip" do
export_options = {'namespace' => @aen1.name, 'class' => @aen1_aec1.name}
export_options['zip_file'] = @zip_file
import_options = {'zip_file' => @zip_file}
assert_class_with_expression_methods_export(export_options, import_options)
end

def assert_methods_export(export_options, import_options)
export_model(@manageiq_domain.name, export_options)
reset_and_import(@export_dir, @manageiq_domain.name, import_options)
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 2,
'meth' => 2, 'field' => 6, 'value' => 4)
check_counts('dom' => 1, 'ns' => 1, 'class' => 1, 'inst' => 2,
'meth' => 3, 'field' => 6, 'value' => 4)
end

def assert_class_with_builtin_methods_export(export_options, import_options)
assert_methods_export(export_options, import_options)
assert_method_data('test2', 'builtin', nil)
end

def assert_class_with_expression_methods_export(export_options, import_options)
assert_methods_export(export_options, import_options)
assert_method_data('test3', 'expression', @expression_data)
end

def assert_method_data(name, location, data)
aen1_aec1 = MiqAeClass.find_by_name('manageiq_test_class_1')
builtin_method = MiqAeMethod.find_by_class_id_and_name(aen1_aec1.id, 'test2')
expect(builtin_method.location).to eql 'builtin'
expect(builtin_method.data).to be_nil
method = MiqAeMethod.find_by_class_id_and_name(aen1_aec1.id, name)
expect(method.location).to eql location
expect(method.data).to eq(data)
end

it "class, without methods, to directory" do
Expand Down Expand Up @@ -771,6 +792,13 @@ def create_factory_data(domain_name, priority, source = MiqAeDomain::USER_SOURCE
:scope => "instance",
:language => "ruby",
:location => "builtin")
FactoryGirl.create(:miq_ae_method,
:class_id => n1_c1.id,
:name => 'test3',
:scope => "instance",
:language => "ruby",
:data => @expression_data,
:location => "expression")
FactoryGirl.create(:miq_ae_instance, :name => 'test_instance2', :class_id => n1_c1.id)
create_fields(n1_c1, n1_c1_i1, n1_c1_m1)

Expand Down

0 comments on commit 1439897

Please sign in to comment.