From 678825c5b201d659c3d0d52374d4990a0c0f3d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Ple=C5=A1ko?= Date: Tue, 27 Feb 2018 15:39:54 +0100 Subject: [PATCH] Inventory VM snapshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this commit we support VM snapshot inventoring. Since vCloud is quite modes with information it reveals about VM snapshots (only creation time, size and poweredOn, which tells if if the virtual machine was powered on when the snapshot was created) we need to compose `uid` and `ems_ref` dynamically. Signed-off-by: Miha Pleško Signed-off-by: Sašo Cvitkovič --- .../vmware/cloud_manager/refresh_parser.rb | 18 +++ .../vmware/cloud_manager/refresher_spec.rb | 19 ++- .../vmware/cloud_manager/refresher.yml | 131 ++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) diff --git a/app/models/manageiq/providers/vmware/cloud_manager/refresh_parser.rb b/app/models/manageiq/providers/vmware/cloud_manager/refresh_parser.rb index 7141b35eb..8721d2353 100644 --- a/app/models/manageiq/providers/vmware/cloud_manager/refresh_parser.rb +++ b/app/models/manageiq/providers/vmware/cloud_manager/refresh_parser.rb @@ -141,6 +141,7 @@ def parse_vm(vm) :name => name, :vendor => "vmware", :raw_power_state => status, + :snapshots => [parse_snapshot(vm)].compact, :hardware => { :guest_os => guest_os, @@ -217,4 +218,21 @@ def parse_vapp_template(vapp_template) return uid, new_result end + + def parse_snapshot(vm) + resp = @connection.get_snapshot_section(vm.id).data + if (snapshot_resp = resp.fetch_path(:body, :Snapshot)) + { + :name => "#{vm.name} (snapshot)", + :uid => "#{vm.id}_#{snapshot_resp[:created]}", + :ems_ref => "#{vm.id}_#{snapshot_resp[:created]}", + :parent_id => vm.id, + :parent_uid => vm.id, + :create_time => snapshot_resp[:created], + :total_size => snapshot_resp[:size] + } + else + return nil + end + end end diff --git a/spec/models/manageiq/providers/vmware/cloud_manager/refresher_spec.rb b/spec/models/manageiq/providers/vmware/cloud_manager/refresher_spec.rb index 41dbf099e..afec575c4 100644 --- a/spec/models/manageiq/providers/vmware/cloud_manager/refresher_spec.rb +++ b/spec/models/manageiq/providers/vmware/cloud_manager/refresher_spec.rb @@ -55,6 +55,7 @@ assert_specific_vm_powered_on assert_specific_vm_powered_off assert_specific_orchestration_template + assert_specific_vm_with_snapshot end end @@ -82,7 +83,7 @@ def assert_table_counts expect(GuestDevice.count).to eq(0) expect(Hardware.count).to eq(3) expect(OperatingSystem.count).to eq(3) - expect(Snapshot.count).to eq(0) + expect(Snapshot.count).to eq(2) expect(SystemService.count).to eq(0) expect(Relationship.count).to eq(0) @@ -280,7 +281,7 @@ def assert_specific_vm_powered_off ) expect(v.custom_attributes.size).to eq(0) - expect(v.snapshots.size).to eq(0) + expect(v.snapshots.size).to eq(1) expect(v.hardware).to have_attributes( :config_version => nil, @@ -348,4 +349,18 @@ def assert_specific_orchestration_template expect(@template.content.include?('ovf:Envelope')).to be_truthy expect(@template.md5).to eq('729bfcafe52065bdee376931efe104d9') end + + def assert_specific_vm_with_snapshot + vm = ManageIQ::Providers::Vmware::CloudManager::Vm.find_by(:name => "spec2-vm1") + + expect(vm.snapshots.first).not_to be_nil + expect(vm.snapshots.first).to have_attributes( + :ems_ref => 'vm-a28be0c0-d70d-4047-92f8-fc217bbaa7f6_2018-02-22T12:22:20.784+01:00', + :uid => 'vm-a28be0c0-d70d-4047-92f8-fc217bbaa7f6_2018-02-22T12:22:20.784+01:00', + :parent_uid => 'vm-a28be0c0-d70d-4047-92f8-fc217bbaa7f6', + :name => 'spec2-vm1 (snapshot)', + :total_size => 4_294_967_296 + ) + expect(vm.snapshots.first.create_time.to_s).to eq('2018-02-22 11:22:20 UTC') + end end diff --git a/spec/vcr_cassettes/manageiq/providers/vmware/cloud_manager/refresher.yml b/spec/vcr_cassettes/manageiq/providers/vmware/cloud_manager/refresher.yml index e1447080e..e130cd3dc 100644 --- a/spec/vcr_cassettes/manageiq/providers/vmware/cloud_manager/refresher.yml +++ b/spec/vcr_cassettes/manageiq/providers/vmware/cloud_manager/refresher.yml @@ -5509,4 +5509,135 @@ http_interactions: http_version: recorded_at: Fri, 16 Sep 2016 08:55:36 GMT +- request: + method: get + uri: https://VMWARE_CLOUD_HOST/api/vApp/vm-f9ceb77c-b9d9-400c-8c06-72c785e884af/snapshotSection + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - fog-core/1.45.0 + Accept: + - application/*+xml;version=5.1 + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + response: + status: + code: 200 + message: OK + headers: + Date: + - Fri, 23 Feb 2018 12:50:19 GMT + X-Vmware-Vcloud-Request-Id: + - 5c9eda34-c719-4e5f-b2ad-3639a9d9be6e + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + Set-Cookie: + - vcloud-token=c52a60a15bd3434096cb4d4cc1ccea15; Secure; Path=/ + Content-Type: + - application/vnd.vmware.vcloud.snapshotsection+xml;version=5.1 + X-Vmware-Vcloud-Request-Execution-Time: + - '62' + Vary: + - Accept-Encoding, User-Agent + Content-Length: + - '1062' + body: + encoding: ASCII-8BIT + string: | + + + Snapshot information section + + http_version: + recorded_at: Fri, 23 Feb 2018 12:50:19 GMT +- request: + method: get + uri: https://VMWARE_CLOUD_HOST/api/vApp/vm-8df2faf3-fd33-40de-9b26-da34c146f55d/snapshotSection + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - fog-core/1.45.0 + Accept: + - application/*+xml;version=5.1 + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + response: + status: + code: 200 + message: OK + headers: + Date: + - Fri, 23 Feb 2018 12:50:20 GMT + X-Vmware-Vcloud-Request-Id: + - 26083f04-68e1-4347-85b6-920148fbc41e + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + Set-Cookie: + - vcloud-token=c52a60a15bd3434096cb4d4cc1ccea15; Secure; Path=/ + Content-Type: + - application/vnd.vmware.vcloud.snapshotsection+xml;version=5.1 + X-Vmware-Vcloud-Request-Execution-Time: + - '109' + Vary: + - Accept-Encoding, User-Agent + Content-Length: + - '1154' + body: + encoding: ASCII-8BIT + string: | + + + Snapshot information section + + + http_version: + recorded_at: Fri, 23 Feb 2018 12:50:20 GMT +- request: + method: get + uri: https://VMWARE_CLOUD_HOST/api/vApp/vm-a28be0c0-d70d-4047-92f8-fc217bbaa7f6/snapshotSection + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - fog-core/1.45.0 + Accept: + - application/*+xml;version=5.1 + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + response: + status: + code: 200 + message: OK + headers: + Date: + - Fri, 23 Feb 2018 12:50:20 GMT + X-Vmware-Vcloud-Request-Id: + - 26083f04-68e1-4347-85b6-920148fbc41e + X-Vcloud-Authorization: + - c52a60a15bd3434096cb4d4cc1ccea15 + Set-Cookie: + - vcloud-token=c52a60a15bd3434096cb4d4cc1ccea15; Secure; Path=/ + Content-Type: + - application/vnd.vmware.vcloud.snapshotsection+xml;version=5.1 + X-Vmware-Vcloud-Request-Execution-Time: + - '109' + Vary: + - Accept-Encoding, User-Agent + Content-Length: + - '1154' + body: + encoding: ASCII-8BIT + string: | + + + Snapshot information section + + + http_version: + recorded_at: Fri, 23 Feb 2018 12:50:20 GMT recorded_with: VCR 3.0.3