From 6c53f68da44e688cdb25da1b06d1bd0194601318 Mon Sep 17 00:00:00 2001 From: Tristan Robert Date: Wed, 17 Apr 2019 18:02:35 +0200 Subject: [PATCH] Fix extract value from controller --- lib/fog/proxmox/helpers/controller_helper.rb | 11 +++++------ spec/helpers/controller_helper_spec.rb | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/fog/proxmox/helpers/controller_helper.rb b/lib/fog/proxmox/helpers/controller_helper.rb index c74d9c7..44891e9 100644 --- a/lib/fog/proxmox/helpers/controller_helper.rb +++ b/lib/fog/proxmox/helpers/controller_helper.rb @@ -23,16 +23,15 @@ module Proxmox module ControllerHelper CONTROLLERS = %w[ide sata scsi virtio mp rootfs].freeze - def self.extract(name, controller_value) - values = controller_value.scan(/#{name}=(\w+)/) - name_value = values.first if values - name_value&.first + matches = controller_value.match(/[,]{0,1}#{name}[=]{1}(?[\w\/]+)/) + matches ? matches[:name_value] : matches end def self.extract_index(name, key) - idx_a = key.to_s.scan(/#{name}(\d+)/).first - idx_a.first.to_i if idx_a + matches = key.to_s.match(/#{name}(?\d+)/) + index = matches ? matches[:index] : matches + index ? index.to_i : -1 end def self.valid?(name, key) diff --git a/spec/helpers/controller_helper_spec.rb b/spec/helpers/controller_helper_spec.rb index cafa6e4..8351cf1 100644 --- a/spec/helpers/controller_helper_spec.rb +++ b/spec/helpers/controller_helper_spec.rb @@ -23,7 +23,7 @@ describe Fog::Proxmox::ControllerHelper do let(:net) do - { net0: 'virtio=66:89:C5:59:AA:96,bridge=vmbr0,firewall=1,link_down=1,queues=1,rate=1,tag=1' } + { net0: 'virtio=66:89:C5:59:AA:96,bridge=vmbr0,firewall=1,link_down=1,queues=1,rate=1,tag=1,mp=/opt/path' } end let(:net_no_options) do { net0: 'virtio=66:89:C5:59:AA:96' } @@ -35,7 +35,7 @@ { ide2: 'none,media=cdrom' } end let(:mp) do - { mp0: 'local-lvm:1' } + { mp0: 'local-lvm:1,mp=/opt/path' } end let(:rootfs) do { rootfs: 'local-lvm:1' } @@ -58,6 +58,21 @@ cache = Fog::Proxmox::ControllerHelper.extract('cache',scsi[:scsi10]) assert_equal 'none', cache end + it "returns mp" do + path = Fog::Proxmox::ControllerHelper.extract('mp',mp[:mp0]) + assert_equal '/opt/path', path + end + end + + describe '#extract_index' do + it "net0 returns 0" do + index = Fog::Proxmox::ControllerHelper.extract_index('net',:net0) + assert index == 0 + end + it "scsi10 returns 10" do + index = Fog::Proxmox::ControllerHelper.extract_index('scsi',:scsi10) + assert index == 10 + end end describe '#last_index' do