Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gather AWS labels and create CustomAttributes #162

Merged
merged 2 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def parse_image(image, is_public)
:vendor => "amazon",
:raw_power_state => "never",
:template => true,
:labels => parse_labels(image.tags),
# the is_public flag here avoids having to make an additional API call
# per image, since we already know whether it's a public image
:publicly_available => is_public,
Expand Down Expand Up @@ -231,7 +232,7 @@ def parse_instance(instance)
:vendor => "amazon",
:raw_power_state => status,
:boot_time => instance.launch_time,

:labels => parse_labels(instance.tags),
:hardware => {
:bitness => architecture_to_bitness(instance.architecture),
:virtualization_type => instance.virtualization_type,
Expand Down Expand Up @@ -387,4 +388,20 @@ def parse_stack_resource(resource)
}
return uid, new_result
end

def parse_labels(tags)
result = []
return result if tags.empty?
tags.each do |tag|
custom_attr = {
:section => 'labels',
:name => tag[:key],
:value => tag[:value],
:source => 'amazon'
}
result << custom_attr
end

result
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def get_images(images, is_public = false)
process_inventory_collection(images, :miq_templates) do |image|
get_image_hardware(image)

resource = persister.miq_templates.lazy_find(image['image_id'])
get_labels(resource, image["tags"])

parse_image(image, is_public)
end
end
Expand Down Expand Up @@ -102,6 +105,8 @@ def get_instances
flavor = persister.flavors.find(instance['instance_type']) || persister.flavors.find("unknown")

get_instance_hardware(instance, flavor)
resource = persister.vms.lazy_find(instance['instance_id'])
get_labels(resource, instance["tags"])

parse_instance(instance, flavor)
end
Expand All @@ -116,6 +121,12 @@ def get_instance_hardware(instance, flavor)
end
end

def get_labels(resource, tags)
process_inventory_collection(tags, :vm_and_template_labels) do |tag|
parse_label(resource, tag)
end
end

def get_hardware_networks(instance)
process_inventory_collection([instance], :networks) { |i| parse_hardware_private_network(i) }
process_inventory_collection([instance], :networks) { |i| parse_hardware_public_network(i) }
Expand Down Expand Up @@ -368,6 +379,16 @@ def parse_stack_resource(resource, stack)
}
end

def parse_label(resource, tag)
{
:resource => resource,
:section => 'labels',
:name => tag["key"],
:value => tag["value"],
:source => 'amazon'
}
end

# Overridden helper methods, we should put them in helper once we get rid of old refresh
def get_from_tags(resource, item)
(resource['tags'] || []).detect { |tag, _| tag['key'].downcase == item.to_s.downcase }.try(:[], 'value')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ManageIQ::Providers::Amazon::Inventory::Persister::CloudManager < ManageIQ
def initialize_inventory_collections
add_inventory_collections(
cloud,
%i(vms miq_templates hardwares networks disks availability_zones
%i(vms miq_templates hardwares networks disks availability_zones vm_and_template_labels
flavors key_pairs orchestration_stacks orchestration_stacks_resources
orchestration_stacks_outputs orchestration_stacks_parameters orchestration_templates)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def key_pairs(extra_attributes = {})
super(attributes.merge!(extra_attributes))
end

def vm_and_template_labels(extra_attributes = {})
attributes = {
:model_class => CustomAttribute,
:association => :vm_and_template_labels,
:manager_ref => [:resource, :name]
}

attributes.merge!(extra_attributes)
end

def orchestration_stacks(extra_attributes = {})
attributes = {
:model_class => ::ManageIQ::Providers::Amazon::CloudManager::OrchestrationStack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def expected_table_counts
:availability_zone => 5,
:cloud_network => 5,
:cloud_subnet => 10,
:custom_attribute => 0,
:custom_attribute => 43,
:disk => 44,
:ext_management_system => 4,
:firewall_rule => 119,
Expand Down Expand Up @@ -336,7 +336,7 @@ def assert_specific_vm_powered_on
:cpu_reserve_expand => nil,
:cpu_limit => nil,
:cpu_shares => nil,
:cpu_shares_level => nil
:cpu_shares_level => nil,
)

expect(v.ext_management_system).to eq(@ems)
Expand All @@ -358,7 +358,7 @@ def assert_specific_vm_powered_on
.to match_array [sg_2, @sg]

expect(v.operating_system).to be_nil # TODO: This should probably not be nil
expect(v.custom_attributes.size).to eq(0)
expect(v.custom_attributes.size).to eq(1)
expect(v.snapshots.size).to eq(0)

expect(v.hardware).to have_attributes(
Expand Down Expand Up @@ -403,6 +403,7 @@ def assert_specific_vm_powered_on
v.with_relationship_type("genealogy") do
expect(v.parent).to eq(@template)
end
expect(v.custom_attributes.find_by(:name => "Name").value).to eq("EmsRefreshSpec-PoweredOn-Basic3")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice if we could add 1 more tag to the testing data, but that can be done as follow up in the future, since it will require new VCRs

end

def assert_specific_vm_powered_off
Expand Down Expand Up @@ -443,7 +444,7 @@ def assert_specific_vm_powered_off
expect(v.cloud_subnet).to be_nil
expect(v.security_groups).to match_array([@sg])
expect(v.operating_system).to be_nil # TODO: This should probably not be nil
expect(v.custom_attributes.size).to eq(0)
expect(v.custom_attributes.size).to eq(1)
expect(v.snapshots.size).to eq(0)

expect(v.hardware).to have_attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def assert_table_counts
expect(VmOrTemplate.count).to eq(3)
expect(Vm.count).to eq(2)
expect(MiqTemplate.count).to eq(1)

expect(CustomAttribute.count).to eq(0)
expect(CustomAttribute.count).to eq(1)
expect(Disk.count).to eq(3)
expect(GuestDevice.count).to eq(0)
expect(Hardware.count).to eq(3)
Expand Down Expand Up @@ -228,7 +227,7 @@ def assert_specific_vm_powered_on
expect(v.security_groups).to eq([@sg])
expect(v.key_pairs).to eq([@kp])
expect(v.operating_system).to be_nil # TODO: This should probably not be nil
expect(v.custom_attributes.size).to eq(0)
expect(v.custom_attributes.size).to eq(1)
expect(v.snapshots.size).to eq(0)

expect(v.hardware).to have_attributes(
Expand Down