Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7 compatible Zeitwerk loading #195

Merged
merged 2 commits into from
Dec 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(tenant, app_ident, secret_key, sub_id, azure_environment)
end

def resource_client
#resource_manager_endpoint_url
# resource_manager_endpoint_url
@resource_client ||= Resources::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

Expand All @@ -33,11 +33,12 @@ def subscription_client

def azure_credentials(base_url)
provider = MsRestAzure::ApplicationTokenProvider.new(
@tenant,
@app_ident,
@secret_key,
@ad_settings)

@tenant,
@app_ident,
@secret_key,
@ad_settings
)

credentials = MsRest::TokenCredentials.new(provider)

{
Expand All @@ -46,7 +47,7 @@ def azure_credentials(base_url)
client_id: @app_ident,
client_secret: @secret_key,
subscription_id: @sub_id,
base_url: base_url
base_url: base_url,
}
end

Expand Down Expand Up @@ -91,7 +92,7 @@ def list_resources(filter)
end

def rgs
rgs = resource_client.resource_groups.list
rgs = resource_client.resource_groups.list
rgs.map(&:name)
end

Expand All @@ -107,7 +108,7 @@ def public_ip(rg_name, pip_name)
network_client.public_ipaddresses.get(rg_name, pip_name)
end

def vm_nic(rg_name,nic_name)
def vm_nic(rg_name, nic_name)
network_client.network_interfaces.get(rg_name, nic_name)
end

Expand All @@ -120,14 +121,15 @@ def get_vm_extension(rg_name, vm_name, vm_extension_name)
end

def list_vm_sizes(region)
return [] unless region.present?
return [] if region.blank?

stripped_region = region.gsub(/\s+/, '').downcase
compute_client.virtual_machine_sizes.list(stripped_region).value()
compute_client.virtual_machine_sizes.list(stripped_region).value
end

def list_vms(region)
# List all VMs in a resource group
virtual_machines = compute_client.virtual_machines.list_by_location(region)
compute_client.virtual_machines.list_by_location(region)
end

def get_vm(rg_name, vm_name)
Expand Down Expand Up @@ -166,7 +168,7 @@ def list_gallery_image_versions(rg_name, gallery_name, gallery_image_name)
compute_client.gallery_image_versions.list_by_gallery_image(rg_name, gallery_name, gallery_image_name)
end

def get_storage_accts
def get_storage_accts # rubocop:disable Naming/AccessorMethodName
result = storage_client.storage_accounts.list
result.value
end
Expand All @@ -177,9 +179,9 @@ def create_or_update_vm(rg_name, vm_name, parameters)

def create_or_update_vm_extensions(rg_name, vm_name, vm_extension_name, extension_params)
compute_client.virtual_machine_extensions.create_or_update(rg_name,
vm_name,
vm_extension_name,
extension_params)
vm_name,
vm_extension_name,
extension_params)
end

def create_or_update_pip(rg_name, pip_name, parameters)
Expand Down Expand Up @@ -207,17 +209,15 @@ def delete_disk(rg_name, disk_name)
end

def check_vm_status(rg_name, vm_name)
virtual_machine = compute_client.virtual_machines.get(rg_name, vm_name, expand:'instanceView')
virtual_machine = compute_client.virtual_machines.get(rg_name, vm_name, expand: 'instanceView')
get_status(virtual_machine)
end

def get_status(virtual_machine)
vm_statuses = virtual_machine.instance_view.statuses
vm_status = nil
vm_statuses.each do |status|
if status.code.include? 'PowerState'
vm_status = status.code.split('/')[1]
end
vm_status = status.code.split('/')[1] if status.code.include? 'PowerState'
end
vm_status
end
Expand All @@ -238,11 +238,10 @@ def self.gallery_caching(rg_name)

def actual_gallery_image_id(rg_name, image_id)
gallery_names = list_galleries.map(&:name)
gallery_names.each do |gallery|
gallery_image = list_gallery_images(rg_name, gallery).detect { |image| image.name == image_id }
return gallery_image&.id
end
nil
return unless (gallery = gallery_names.first)

gallery_image = list_gallery_images(rg_name, gallery).detect { |image| image.name == image_id }
gallery_image&.id
end

def fetch_gallery_image_id(rg_name, image_id)
Expand Down
32 changes: 13 additions & 19 deletions lib/foreman_azure_rm/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ module ForemanAzureRm
class Engine < ::Rails::Engine
engine_name 'foreman_azure_rm'

#autoloading all files inside lib dir
config.eager_load_paths += Dir["#{config.root}/lib"]
config.eager_load_paths += Dir["#{config.root}/app/models/concerns/"]
config.eager_load_paths += Dir["#{config.root}/app/helpers/"]

initializer 'foreman_azure_rm.register_plugin', :before => :finisher_hook do
Foreman::Plugin.register :foreman_azure_rm do
requires_foreman '>= 3.13'
register_gettext
compute_resource ForemanAzureRm::AzureRm
parameter_filter ComputeResource, :azure_vm, :tenant, :app_ident, :secret_key, :sub_id, :region, :cloud
lib = config.root.join('app', 'lib')

config.autoload_paths << lib
config.eager_load_paths << lib

initializer 'foreman_azure_rm.register_plugin', :before => :finisher_hook do |app|
app.reloader.to_prepare do
Foreman::Plugin.register :foreman_azure_rm do
requires_foreman '>= 3.13'
register_gettext
compute_resource ForemanAzureRm::AzureRm
parameter_filter ComputeResource, :azure_vm, :tenant, :app_ident, :secret_key, :sub_id, :region, :cloud
end
end
end

Expand All @@ -29,14 +31,6 @@ class Engine < ::Rails::Engine
end
end

initializer "foreman_azure_rm.zeitwerk" do
Rails.autoloaders.each do |loader|
loader.ignore(
ForemanAzureRm::Engine.root.join('lib/foreman_azure_rm/version.rb')
)
end
end

config.to_prepare do
require 'azure_mgmt_resources'
require 'azure_mgmt_network'
Expand Down
Loading
Loading