Skip to content

Commit

Permalink
Create folders if not found in vsphere CPI
Browse files Browse the repository at this point in the history
  • Loading branch information
mariash committed Dec 18, 2014
1 parent 97ac475 commit 9221ece
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 193 deletions.
9 changes: 4 additions & 5 deletions bosh_vsphere_cpi/lib/cloud/vsphere/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
require 'cloud/vsphere/resources/datacenter'
require 'cloud/vsphere/resources/datastore'
require 'cloud/vsphere/resources/folder'
require 'cloud/vsphere/resources/multi_tenant_folder'
require 'cloud/vsphere/resources/resource_pool'
require 'cloud/vsphere/resources/scorer'
require 'cloud/vsphere/resources/util'
Expand Down Expand Up @@ -185,7 +184,7 @@ def disk_spec(persistent_disks)

def stemcell_vm(name)
dc = @resources.datacenters.values.first
client.find_by_inventory_path([dc.name, 'vm', dc.template_folder.name, name])
client.find_by_inventory_path([dc.name, 'vm', dc.template_folder.path, name])
end

def create_vm(agent_id, stemcell, cloud_properties, networks, disk_locality = nil, environment = nil)
Expand Down Expand Up @@ -599,23 +598,23 @@ def delete_disk(disk_cid)

def get_vm_by_cid(vm_cid)
@resources.datacenters.each_value do |datacenter|
vm = client.find_by_inventory_path([datacenter.name, 'vm', datacenter.vm_folder.name, vm_cid])
vm = client.find_by_inventory_path([datacenter.name, 'vm', datacenter.vm_folder.path, vm_cid])
return vm unless vm.nil?
end
raise Bosh::Clouds::VMNotFound, "VM `#{vm_cid}' not found"
end

def replicate_stemcell(cluster, datastore, stemcell)
stemcell_vm = client.find_by_inventory_path([cluster.datacenter.name, 'vm',
cluster.datacenter.template_folder.name, stemcell])
cluster.datacenter.template_folder.path, stemcell])
raise "Could not find stemcell: #{stemcell}" if stemcell_vm.nil?
stemcell_datastore = @cloud_searcher.get_property(stemcell_vm, Vim::VirtualMachine, 'datastore', ensure_all: true)

if stemcell_datastore != datastore.mob
@logger.info("Stemcell lives on a different datastore, looking for a local copy of: #{stemcell}.")
local_stemcell_name = "#{stemcell} %2f #{datastore.mob.__mo_id__}"
local_stemcell_path =
[cluster.datacenter.name, 'vm', cluster.datacenter.template_folder.name, local_stemcell_name]
[cluster.datacenter.name, 'vm', cluster.datacenter.template_folder.path, local_stemcell_name]
replicated_stemcell_vm = client.find_by_inventory_path(local_stemcell_path)

if replicated_stemcell_vm.nil?
Expand Down
8 changes: 4 additions & 4 deletions bosh_vsphere_cpi/lib/cloud/vsphere/resources/datacenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ def mob

def vm_folder
if @config.datacenter_use_sub_folder
MultiTenantFolder.new(@config.datacenter_vm_folder, Bosh::Clouds::Config.uuid, @config)
Folder.new([@config.datacenter_vm_folder, Bosh::Clouds::Config.uuid], @config)
else
master_vm_folder
end
end

def master_vm_folder
Folder.new(@config.datacenter_vm_folder, @config)
Folder.new([@config.datacenter_vm_folder], @config)
end

def template_folder
if @config.datacenter_use_sub_folder
MultiTenantFolder.new(@config.datacenter_template_folder, Bosh::Clouds::Config.uuid, @config)
Folder.new([@config.datacenter_template_folder, Bosh::Clouds::Config.uuid], @config)
else
master_template_folder
end
end

def master_template_folder
Folder.new(@config.datacenter_template_folder, @config)
Folder.new([@config.datacenter_template_folder], @config)
end

def name
Expand Down
41 changes: 34 additions & 7 deletions bosh_vsphere_cpi/lib/cloud/vsphere/resources/folder.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
module VSphereCloud
class Resources
class Folder
attr_reader :mob, :name
attr_reader :mob, :path, :name

def initialize(name, config)
@name = name
# path - an array of names starting from parent down to child folders
def initialize(path, config)
@path = path
@config = config

find_folder
@name = path.join('/')

@mob = find_or_create_folder
end

private

def find_or_create_folder
folder = find_folder

if folder.nil?
begin
@config.logger.debug("Creating folder #{@name}")
folder = parent_folder.create_folder(@path.last)
rescue VimSdk::SoapError => e
raise e unless VimSdk::Vim::Fault::DuplicateName === e.fault

@config.logger.debug("Folder already exists #{@name}")
folder = find_folder
end
end

folder
end

def find_folder
folder = @config.client.find_by_inventory_path([@config.datacenter_name, 'vm', @name])
raise "Missing folder: #{@name}" if folder.nil?
@mob = folder
@config.client.find_by_inventory_path([@config.datacenter_name, 'vm', @path].flatten)
end

def parent_folder
if @path.size > 1
Folder.new(@path[0..-2], @config).mob
else
@config.client.find_by_inventory_path([@config.datacenter_name, 'vm'])
end
end
end
end
Expand Down

This file was deleted.

10 changes: 5 additions & 5 deletions bosh_vsphere_cpi/spec/unit/cloud/vsphere/cloud_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def self.it_assigns_available_unit_numbers_for_devices_in_change_set

let(:template_folder) do
double(:template_folder,
name: 'fake_template_folder',
path: ['fake_template_folder'],
mob: 'fake_template_folder_mob'
)
end
Expand Down Expand Up @@ -247,7 +247,7 @@ def self.it_assigns_available_unit_numbers_for_devices_in_change_set
[
cluster.datacenter.name,
'vm',
cluster.datacenter.template_folder.name,
cluster.datacenter.template_folder.path,
stemcell_id,
]
).and_return(stemcell_vm)
Expand All @@ -260,7 +260,7 @@ def self.it_assigns_available_unit_numbers_for_devices_in_change_set
[
cluster.datacenter.name,
'vm',
cluster.datacenter.template_folder.name,
cluster.datacenter.template_folder.path,
"#{stemcell_id} %2f #{datastore.mob.__mo_id__}",
]
).and_return(double('fake stemcell vm'))
Expand All @@ -278,7 +278,7 @@ def self.it_assigns_available_unit_numbers_for_devices_in_change_set
[
cluster.datacenter.name,
'vm',
cluster.datacenter.template_folder.name,
cluster.datacenter.template_folder.path,
"#{stemcell_id} %2f #{datastore.mob.__mo_id__}",
]
)
Expand Down Expand Up @@ -709,7 +709,7 @@ def self.it_assigns_available_unit_numbers_for_devices_in_change_set
)
end
before { allow(disk_model).to receive(:first).with(uuid: disk_cid).and_return(disk) }
let(:vm_folder) { instance_double('VSphereCloud::Resources::Folder', name: 'vm') }
let(:vm_folder) { instance_double('VSphereCloud::Resources::Folder', path: ['vm']) }

before { allow(cloud_config).to receive(:datacenter_name).with(no_args).and_return('fake-folder/fake-datacenter-name') }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
datacenter_use_sub_folder: false,
) }
let(:client) { instance_double('VSphereCloud::Client') }
let(:multitenant_folder) { instance_double('VSphereCloud::Resources::MultiTenantFolder') }

let(:vm_folder) { instance_double('VSphereCloud::Resources::Folder') }
let(:vm_subfolder) { instance_double('VSphereCloud::Resources::Folder') }

let(:template_folder) { instance_double('VSphereCloud::Resources::Folder') }
let(:multitenant_template_folder) { instance_double('VSphereCloud::Resources::MultiTenantFolder') }
let(:template_subfolder) { instance_double('VSphereCloud::Resources::Folder') }
let(:datacenter_mob) { instance_double('VimSdk::Vim::Datacenter') }
let(:cluster_mob1) { instance_double('VimSdk::Vim::Cluster') }
let(:cluster_mob2) { instance_double('VimSdk::Vim::Cluster') }
Expand All @@ -35,14 +37,17 @@
before do
allow(client).to receive(:find_by_inventory_path).with('fake-datacenter-name').and_return(datacenter_mob)
allow(client).to receive(:cloud_searcher).and_return(cloud_searcher)

allow(VSphereCloud::Resources::Folder).to receive(:new).with(
['fake-vm-folder'], config).and_return(vm_folder)
allow(VSphereCloud::Resources::Folder).to receive(:new).with(
'fake-vm-folder', config).and_return(vm_folder)
allow(VSphereCloud::Resources::MultiTenantFolder).to receive(:new).with(
'fake-vm-folder', 'fake-uuid', config).and_return(multitenant_folder)
['fake-vm-folder', 'fake-uuid'], config).and_return(vm_subfolder)

allow(VSphereCloud::Resources::Folder).to receive(:new).with(
['fake-template-folder'], config).and_return(template_folder)
allow(VSphereCloud::Resources::Folder).to receive(:new).with(
'fake-template-folder', config).and_return(template_folder)
allow(VSphereCloud::Resources::MultiTenantFolder).to receive(:new).with(
'fake-template-folder', 'fake-uuid', config).and_return(multitenant_template_folder)
['fake-template-folder', 'fake-uuid'], config).and_return(template_subfolder)

allow(cloud_searcher).to receive(:get_managed_objects).with(
VimSdk::Vim::ClusterComputeResource,
root: datacenter_mob, include_name: true).and_return(
Expand Down Expand Up @@ -92,7 +97,7 @@
before { allow(config).to receive(:datacenter_use_sub_folder).and_return(true) }

it 'returns multi-tenant folder' do
expect(datacenter.vm_folder).to eq(multitenant_folder)
expect(datacenter.vm_folder).to eq(vm_subfolder)
end
end
end
Expand All @@ -115,8 +120,8 @@
context 'when datacenter uses subfolders' do
before { allow(config).to receive(:datacenter_use_sub_folder).and_return(true) }

it 'returns multi-tenant folder' do
expect(datacenter.template_folder).to eq(multitenant_template_folder)
it 'returns subfolder' do
expect(datacenter.template_folder).to eq(template_subfolder)
end
end
end
Expand Down
Loading

0 comments on commit 9221ece

Please sign in to comment.