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

Create the provider when creating the config_manager #62

49 changes: 49 additions & 0 deletions app/models/manageiq/providers/foreman/configuration_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ class ManageIQ::Providers::Foreman::ConfigurationManager < ManageIQ::Providers::
:authentication_status,
:authentication_status_ok?,
:authentications,
:authentications=,
:connect,
:endpoints,
:endpoints=,
:url,
:url=,
agrare marked this conversation as resolved.
Show resolved Hide resolved
:verify_credentials,
:with_provider_connection,
:to => :provider

belongs_to :provider, :autosave => true, :dependent => :destroy

class << self
delegate :params_for_create,
:verify_credentials,
Expand All @@ -31,11 +37,54 @@ def self.description
@description ||= "Foreman Configuration".freeze
end

def self.create_from_params(params, endpoints, authentications)
new(params).tap do |ems|
endpoints.each { |endpoint| ems.assign_nested_endpoint(endpoint) }
authentications.each { |authentication| ems.assign_nested_authentication(authentication) }

ems.provider.save!
ems.save!
end
end

def edit_with_params(params, endpoints, authentications)
tap do |ems|
transaction do
# Remove endpoints/attributes that are not arriving in the arguments above
ems.endpoints.where.not(:role => nil).where.not(:role => endpoints.map { |ep| ep['role'] }).delete_all
ems.authentications.where.not(:authtype => nil).where.not(:authtype => authentications.map { |au| au['authtype'] }).delete_all

ems.assign_attributes(params)
ems.endpoints = endpoints.map(&method(:assign_nested_endpoint))
ems.authentications = authentications.map(&method(:assign_nested_authentication))

ems.provider.save!
ems.save!
end
end
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been having a lot of issues with getting the provider autosave working across all the different provider repos. I want to unblock @skateman while we work those out and the simplest way to do that is to manually save the provider while creating/editing the configuration_manager via the API

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ugly, but if it works, I'm good with that.


def provider
super || ensure_provider
end

def name
"#{provider.name} Configuration Manager"
end

delegate :name=, :zone, :zone=, :zone_id, :zone_id=, :to => :provider

def image_name
"foreman_configuration"
end

def self.display_name(number = 1)
n_('Configuration Manager (Foreman)', 'Configuration Managers (Foreman)', number)
end

private

def ensure_provider
Fryguy marked this conversation as resolved.
Show resolved Hide resolved
build_provider.tap { |p| p.configuration_manager = self }
end
end
14 changes: 7 additions & 7 deletions app/models/manageiq/providers/foreman/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ class ManageIQ::Providers::Foreman::Provider < ::Provider
has_one :configuration_manager,
:foreign_key => "provider_id",
:class_name => "ManageIQ::Providers::Foreman::ConfigurationManager",
:dependent => :destroy,
:autosave => true
has_one :provisioning_manager,
:foreign_key => "provider_id",
:class_name => "ManageIQ::Providers::Foreman::ProvisioningManager",
:dependent => :destroy,
:autosave => true

has_many :endpoints, :as => :resource, :dependent => :destroy, :autosave => true
Expand Down Expand Up @@ -153,20 +151,22 @@ def verify_credentials(auth_type = nil, options = {})
raise MiqException::MiqInvalidCredentialsError, err.message, err.backtrace
end

def name=(val)
super(val.sub(/ (Configuration|Provisioning) Manager$/, ''))
end

private

def ensure_managers
build_provisioning_manager unless provisioning_manager
provisioning_manager.name = "#{name} Provisioning Manager"
provisioning_manager.provider = self

build_configuration_manager unless configuration_manager
configuration_manager.name = "#{name} Configuration Manager"
configuration_manager.provider = self

if zone_id_changed?
provisioning_manager.enabled = Zone.maintenance_zone&.id != zone_id
provisioning_manager.zone_id = zone_id
provisioning_manager.enabled = Zone.maintenance_zone&.id != zone_id
configuration_manager.enabled = Zone.maintenance_zone&.id != zone_id
configuration_manager.zone_id = zone_id
end
end

Expand Down
18 changes: 18 additions & 0 deletions app/models/manageiq/providers/foreman/provisioning_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class ManageIQ::Providers::Foreman::ProvisioningManager < ManageIQ::Providers::P
:authentication_status,
:authentication_status_ok?,
:authentications,
:authentications=,
:connect,
:endpoints,
:endpoints=,
:verify_credentials,
:with_provider_connection,
:to => :provider
Expand All @@ -31,4 +33,20 @@ def self.description
def self.supported_for_create?
false
end

def provider
super || ensure_provider
end

def name
"#{provider.name} Provisioning Manager"
end

delegate :name=, :zone, :zone=, :zone_id, :zone_id=, :to => :provider

private

def ensure_provider
build_provider.tap { |p| p.provisioning_manager = self }
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
describe ManageIQ::Providers::Foreman::ConfigurationManager do
let(:provider) { configuration_manager.provider }
let(:configuration_manager) do
FactoryBot.build(:configuration_manager_foreman)
end

describe "#connect" do
let(:provider) { configuration_manager.provider }
let(:configuration_manager) do
FactoryBot.build(:configuration_manager_foreman)
end

it "delegates to the provider" do
expect(provider).to receive(:connect)
configuration_manager.connect
end
end

describe ".create_from_params" do
it "delegates endpoints, zone, name to provider" do
params = {:zone => FactoryBot.create(:zone), :name => "Foreman"}
endpoints = [{"role" => "default", "url" => "https://foreman", "verify_ssl" => 0}]
authentications = [{"authtype" => "default", "userid" => "admin", "password" => "smartvm"}]

config_manager = described_class.create_from_params(params, endpoints, authentications)

expect(config_manager.provider.name).to eq("Foreman")
expect(config_manager.provider.endpoints.count).to eq(1)
end
end

describe "#edit_with_params" do
let(:configuration_manager) do
FactoryBot.build(:configuration_manager_foreman, :name => "Foreman", :url => "https://localhost")
end

it "updates the provider" do
params = {:zone => FactoryBot.create(:zone), :name => "Foreman 2"}
endpoints = [{"role" => "default", "url" => "https://foreman", "verify_ssl" => 0}]
authentications = [{"authtype" => "default", "userid" => "admin", "password" => "smartvm"}]

provider = configuration_manager.provider
expect(provider.name).to eq("Foreman")
expect(provider.url).to eq("https://localhost")

configuration_manager.edit_with_params(params, endpoints, authentications)

provider.reload
expect(provider.name).to eq("Foreman 2")
expect(provider.url).to eq("https://foreman")
end
end
end