From 5a9b49a6171f3217ccb078d2d7ced813c4d92525 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 15 Nov 2018 14:22:01 -0500 Subject: [PATCH] Add urls for provider endpoints This is just for completeness and probably won't be used by provider code. --- app/models/endpoint.rb | 14 ++++++++++++-- spec/models/endpoint_spec.rb | 12 +++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/models/endpoint.rb b/app/models/endpoint.rb index a1c76bd8ada..0f66fbbc65f 100644 --- a/app/models/endpoint.rb +++ b/app/models/endpoint.rb @@ -12,8 +12,6 @@ class Endpoint < ApplicationRecord after_create :endpoint_created after_destroy :endpoint_destroyed - delegate :to_s, :to => :url, :allow_nil => true - def endpoint_created resource.endpoint_created(role) if resource.respond_to?(:endpoint_created) end @@ -54,8 +52,20 @@ def ssl_cert_store nil # use system defaults end + def to_s + (url || generate_uri).to_s + end + private + def generate_uri + return unless resource.kind_of?(ExtManagementSystem) + host = hostname || ipaddress || resource.hostname + path = ["", resource.try(:api_version) || api_version, ""].compact.join("/") + query = {:verify_ssl => verify_ssl?, :security_protocol => security_protocol }.delete_blanks.to_query + URI::Generic.new(resource.emstype, nil, host, port, nil, path, nil, query, nil) + end + def resolve_verify_ssl_value(val) case val when true then OpenSSL::SSL::VERIFY_PEER diff --git a/spec/models/endpoint_spec.rb b/spec/models/endpoint_spec.rb index 90cae1df37c..425419733b7 100644 --- a/spec/models/endpoint_spec.rb +++ b/spec/models/endpoint_spec.rb @@ -129,13 +129,19 @@ end context "to_s" do - it "returns the url if set" do + it "returns the url with url set" do endpoint.url = 'https://www.foo.bar' expect(endpoint.to_s).to eql('https://www.foo.bar') end - it "returns a blank string if the url is not set" do - expect(endpoint.to_s).to eql('') + it "returns a blank string with no ems and no url" do + expect(endpoint.to_s).to eql("") + end + + it "returns a custom url with vmware uri set" do + ems = FactoryGirl.create(:ems_vmware, :hostname => 'example.com', :port => 443, :api_version => 5.5) + endpoint = FactoryGirl.create(:endpoint, :resource => ems) + expect(endpoint.to_s).to eql("vmwarews://example.com:443/5.5/?verify_ssl=true") end end end