Skip to content

Commit

Permalink
Merge a289fcf into a33f591
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonMatthes committed Mar 18, 2019
2 parents a33f591 + a289fcf commit 1e362f7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
11 changes: 9 additions & 2 deletions app/api/v_sphere/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ def initialize(rbvmomi_folder)
@folder = rbvmomi_folder
end

def subfolders
@folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::Folder }.map do |each|
def parent
@folder.parent
end

def subfolders(recursive: false)
folders = @folder.children.select { |folder_entry| folder_entry.is_a? RbVmomi::VIM::Folder }.map do |each|
Folder.new each
end

folders += folders.flat_map { |each| each.subfolders recursive: true } if recursive
folders
end

def vms(recursive: true)
Expand Down
16 changes: 16 additions & 0 deletions app/api/v_sphere/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ def name
@vm.name
end

def full_path
path = [name]
parent = parent_folder
until parent.nil?
path << parent.name
parent = parent.parent
end
path.reverse
end

def parent_folder
root_folder.subfolders(recursive: true).find do |folder|
folder.vms(recursive: false).include? self
end
end

# Guest OS communication
def vm_ware_tools?
tool_status = @vm&.guest&.toolsStatus
Expand Down
8 changes: 7 additions & 1 deletion app/models/app_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ class AppSetting < ApplicationRecord

after_commit :apply_settings, on: :update

def self.refresh_instance
@instance = nil
end

def self.instance
find(1)
@instance = find(1) if @instance.nil?
@instance
end

private

def apply_settings
self.class.refresh_instance
apply_mail_settings
GitHelper.reset
end
Expand Down
18 changes: 7 additions & 11 deletions app/models/server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'resolv'

class Server < ApplicationRecord
belongs_to :responsible, class_name: :User
validates :name, :cpu_cores, :ram_gb, :storage_gb, :mac_address, :fqdn, :responsible_id, presence: true
Expand All @@ -12,21 +14,15 @@ class Server < ApplicationRecord
validates :storage_gb, numericality: { greater_than: 0 }

validates :ipv4_address, format: {
with: /(^$)|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/,
message: 'Not a valid IPv4 address'
with: Resolv::IPv4::Regex,
message: 'is not a valid IPv4 address'
}
validates :ipv6_address, format: {
with: /(^$)|(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:)
{1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}
(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}
(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)
|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}
[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|
(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/x,
message: 'Not a valid IPv6 address'
with: Resolv::IPv6::Regex,
message: 'is not a valid IPv6 address'
}
validates :mac_address, format: {
with: /([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/,
message: 'Not a valid MAC address'
message: 'is not a valid MAC address'
}
end
10 changes: 8 additions & 2 deletions app/views/vms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
end %>

<div class="container mb-3 pl-0 pr-0 clearfix">
<h1 class="mb-0"><%= @vm.name unless @vm.nil? %></h1>
<ol class="breadcrumb" aria-label="The Virtual Machines full path">
<% @vm.full_path.each do |path_entry| %>
<li class="breadcrumb-item"><%= path_entry %></li>
<% end %>
</ol>

<h1 class="mb-0"><%= @vm.name %></h1>
<% os = @vm.summary&.config&.guestFullName %>
<% unless os.nil? || os.downcase.include?('other') %>
<div class="text-right align-bottom pt-2"><h2 class="mb-0"><%= "(#{os})"%></h2></div>
Expand Down Expand Up @@ -92,7 +98,7 @@ end %>

@hardware = {'IP Address' => @vm.ip,
'DNS' => @vm.dns,
'Heartbeat Status' => @vm.guest_heartbeat_status }
'Heartbeat Status' => @vm.guest_heartbeat_status}
%>

<div class ="table-active container pt-3 pb-3 mb-3 text-center" style="width: 100%">
Expand Down
5 changes: 3 additions & 2 deletions spec/api/v_sphere_api_mocker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def vim_folder_mock(name, subfolders, vms, clusters) # rubocop:disable Metrics/A
children = subfolders + vms + clusters
children = extract_vim_objects children
folder = double
allow(folder).to receive(:name).and_return(name)
allow(folder).to receive(:children).and_return(children)
allow(folder).to receive(:name).and_return name
allow(folder).to receive(:parent).and_return nil
allow(folder).to receive(:children).and_return children
allow(folder).to receive(:is_a?).and_return false
allow(folder).to receive(:is_a?).with(RbVmomi::VIM::Folder).and_return true
allow(folder).to receive_message_chain(:MoveIntoFolder_Task, :wait_for_completion)
Expand Down

0 comments on commit 1e362f7

Please sign in to comment.