Skip to content

Commit

Permalink
Merge pull request ManageIQ#22668 from agrare/remote_ui_ws_address_po…
Browse files Browse the repository at this point in the history
…dified

Fix MiqRegion#remote_*_url returning invalid URL on podified
  • Loading branch information
kbrock authored Aug 22, 2023
2 parents 41533f9 + 458338c commit f6b1d9b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 25 deletions.
28 changes: 7 additions & 21 deletions app/models/miq_region.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,49 +189,35 @@ def remote_ui_miq_server
end

def remote_ui_ipaddress
server = remote_ui_miq_server
server.try(:ipaddress)
remote_ui_miq_server&.ui_ipaddress
end

def remote_ui_hostname
server = remote_ui_miq_server
server && (server.hostname || server.ipaddress)
remote_ui_miq_server&.ui_hostname
end

def remote_ui_url(contact_with = :hostname)
svr = remote_ui_miq_server
remote_ui_url_override = svr.settings_for_resource.ui.url if svr
return remote_ui_url_override if remote_ui_url_override

hostname = send("remote_ui_#{contact_with}")
hostname && "https://#{hostname}"
remote_ui_miq_server&.ui_url(contact_with)
end

def remote_ws_miq_server
MiqServer.in_region(region).recently_active.find_by(:has_active_webservices => true)
end

def remote_ws_address
::Settings.webservices.contactwith == 'hostname' ? remote_ws_hostname : remote_ws_ipaddress
remote_ws_miq_server&.ws_address
end

def remote_ws_ipaddress
miq_server = remote_ws_miq_server
miq_server.try(:ipaddress)
remote_ws_miq_server&.ws_ipaddress
end

def remote_ws_hostname
miq_server = remote_ws_miq_server
miq_server && (miq_server.hostname || miq_server.ipaddress)
remote_ws_miq_server&.ws_hostname
end

def remote_ws_url
svr = remote_ws_miq_server
remote_url_override = svr.settings_for_resource.webservices.url if svr
return remote_url_override if remote_url_override

hostname = remote_ws_address
hostname && URI::HTTPS.build(:host => hostname).to_s
remote_ws_miq_server&.ws_url
end

def api_system_auth_token(userid)
Expand Down
68 changes: 68 additions & 0 deletions app/models/miq_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,74 @@ def logon_status_details
result.merge(:message => message)
end

def ui_hostname
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
hostname || ipaddress
end
end

def ui_ipaddress
if MiqEnvironment::Command.is_podified?
nil
else
ipaddress
end
end

def ui_address(contact_with = :hostname)
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
contact_with == :hostname ? ui_hostname : ui_ipaddress
end
end

def ui_url(contact_with = :hostname)
url_override = settings_for_resource.ui.url
return url_override if url_override

host = ui_address(contact_with)
return if host.nil?

URI::HTTPS.build(:host => host).to_s
end

def ws_hostname
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
hostname || ipaddress
end
end

def ws_ipaddress
if MiqEnvironment::Command.is_podified?
nil
else
ipaddress
end
end

def ws_address
if MiqEnvironment::Command.is_podified?
ENV.fetch("APPLICATION_DOMAIN")
else
::Settings.webservices.contactwith == 'hostname' ? ws_hostname : ws_ipaddress
end
end

def ws_url
url_override = settings_for_resource.webservices.url
return url_override if url_override

host = ws_address
return if host.nil?

URI::HTTPS.build(:host => host).to_s
end

#
# Zone and Role methods
#
Expand Down
30 changes: 26 additions & 4 deletions spec/models/miq_region_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
let(:url) { "https://www.manageiq.org" }
let!(:web_server) do
FactoryBot.create(:miq_server, :has_active_webservices => true,
:hostname => hostname,
:ipaddress => ip)
:hostname => hostname,
:ipaddress => ip)
end

it "fetches the url from server" do
Expand All @@ -134,6 +134,17 @@
Vmdb::Settings.save!(web_server, :webservices => {:url => url})
expect(region.remote_ws_url).to eq(url)
end

context "podified" do
before do
expect(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
expect(ENV).to receive(:fetch).with("APPLICATION_DOMAIN").and_return("manageiq.apps.mycluster.com")
end

it "returns the applicationDomain from the CR" do
expect(region.remote_ws_url).to eq("https://manageiq.apps.mycluster.com")
end
end
end

it "with no recently active servers" do
Expand All @@ -151,8 +162,8 @@
let(:url) { "http://localhost:3000" }
let!(:ui_server) do
FactoryBot.create(:miq_server, :has_active_userinterface => true,
:hostname => hostname,
:ipaddress => ip)
:hostname => hostname,
:ipaddress => ip)
end

it "fetches the url from server" do
Expand All @@ -163,6 +174,17 @@
Vmdb::Settings.save!(ui_server, :ui => {:url => url})
expect(region.remote_ui_url).to eq(url)
end

context "podified" do
before do
expect(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
expect(ENV).to receive(:fetch).with("APPLICATION_DOMAIN").and_return("manageiq.apps.mycluster.com")
end

it "returns the applicationDomain from the CR" do
expect(region.remote_ui_url).to eq("https://manageiq.apps.mycluster.com")
end
end
end

it "with no recently active servers" do
Expand Down

0 comments on commit f6b1d9b

Please sign in to comment.