Skip to content

Commit

Permalink
Fix extract value from controller
Browse files Browse the repository at this point in the history
tristanrobert committed Apr 17, 2019
1 parent f06ea6b commit 6c53f68
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/fog/proxmox/helpers/controller_helper.rb
Original file line number Diff line number Diff line change
@@ -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}(?<name_value>[\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}(?<index>\d+)/)
index = matches ? matches[:index] : matches
index ? index.to_i : -1
end

def self.valid?(name, key)
19 changes: 17 additions & 2 deletions spec/helpers/controller_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6c53f68

Please sign in to comment.