Skip to content
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
4 changes: 3 additions & 1 deletion app/lib/google_cloud_compute/compute_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ def for_create(instance)
end

def for_instance(instance)
first_nic = instance.network_interfaces[0]

{
name: instance.name, hostname: instance.name,
creation_timestamp: instance.creation_timestamp.to_datetime,
zone_name: instance.zone.split('/').last,
machine_type: instance.machine_type,
network: instance.network_interfaces[0].network.split('/').last,
network: first_nic&.network&.split('/')&.last,
network_interfaces: instance.network_interfaces,
volumes: instance.disks, metadata: instance.metadata
}
Expand Down
4 changes: 2 additions & 2 deletions app/models/foreman_google/google_compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def status
alias_method :state, :status

def start
raise Foreman::Exception('unable to start machine that is not persisted') unless persisted?
raise Foreman::Exception, N_('unable to start machine that is not persisted') unless persisted?
@client.start(@zone, identity)
end

def stop
raise Foreman::Exception('unable to stop machine that is not persisted') unless persisted?
raise Foreman::Exception, N_('unable to stop machine that is not persisted') unless persisted?
@client.stop(@zone, identity)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'test_google_helper'

module ForemanGoogle
module Api
module V2
class ComputeResourcesExtensionsTest < GoogleTestCase
let(:controller_class) do
Class.new do
def self.before_action(*)
end

include ForemanGoogle::Api::V2::ComputeResourcesExtensions

attr_accessor :params

def compute_resource_params
params[:compute_resource]
end
end
end

describe '#read_key' do
it 'reads key file content into password for GCE provider' do
key_content = '{"type": "service_account", "project_id": "test"}'

Tempfile.open('gce_key') do |f|
f.write(key_content)
f.flush

ctrl = controller_class.new
ctrl.params = { 'compute_resource' => { 'provider' => 'GCE', 'key_path' => f.path } }.with_indifferent_access
ctrl.send(:read_key)

assert_equal key_content, ctrl.params[:compute_resource][:password]
assert_not ctrl.params[:compute_resource].key?('key_path')
end
end

it 'skips when provider is not GCE' do
ctrl = controller_class.new
ctrl.params = { 'compute_resource' => { 'provider' => 'EC2', 'key_path' => '/nonexistent' } }.with_indifferent_access
ctrl.send(:read_key)

assert_nil ctrl.params[:compute_resource][:password]
assert_equal '/nonexistent', ctrl.params[:compute_resource]['key_path']
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_google_helper'

module ForemanGoogle
class HostManagedExtensionsTest < GoogleTestCase
let(:host_class) do
Class.new do
include ForemanGoogle::HostManagedExtensions
attr_accessor :vm
end
end

describe '#ip_addresses' do
it 'returns vm ip_addresses when vm exists' do
vm = mock('GoogleCompute')
vm.expects(:ip_addresses).returns(['1.2.3.4', '10.0.0.1'])

host = host_class.new
host.vm = vm
assert_equal ['1.2.3.4', '10.0.0.1'], host.ip_addresses
end

it 'returns empty array when vm is nil' do
host = host_class.new
host.vm = nil
assert_empty host.ip_addresses
end
end

describe '#vm_ip_address' do
it 'returns vm public IP when vm exists' do
vm = mock('GoogleCompute')
vm.expects(:vm_ip_address).returns('1.2.3.4')

host = host_class.new
host.vm = vm
assert_equal '1.2.3.4', host.vm_ip_address
end

it 'returns nil when vm is nil' do
host = host_class.new
host.vm = nil
assert_nil host.vm_ip_address
end
end
end
end
Loading
Loading