From 2437f7de5653f1b16fe6a8ebeae143728f5adb2c Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Tue, 21 Nov 2023 17:56:37 +0800 Subject: [PATCH] refactor: details endpoint will retrieve driver defaults instead of metadata (#264) --- src/api/drivers.cr | 12 ++++++------ src/placeos-core/driver_manager.cr | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/api/drivers.cr b/src/api/drivers.cr index 43193cdc..d8984cb7 100644 --- a/src/api/drivers.cr +++ b/src/api/drivers.cr @@ -37,18 +37,18 @@ module PlaceOS::Core::Api ) : Nil Log.context.set(driver: driver_file, repository: repository, commit: commit, branch: branch) repo = Model::Repository.find!(repository) - metadata = store.metadata(driver_file, commit, branch, repo.uri) - if metadata.success + defaults = store.defaults(driver_file, commit, branch, repo.uri) + if defaults.success response.headers["Content-Type"] = "application/json" - render text: metadata.output + render text: defaults.output end password = repo.decrypt_password if repo.password.presence result = store.compile(driver_file, repo.uri, commit, branch, false, repo.username, password) if result.success - metadata = store.metadata(driver_file, commit, branch, repo.uri) - if metadata.success + defaults = store.defaults(driver_file, commit, branch, repo.uri) + if defaults.success response.headers["Content-Type"] = "application/json" - render text: metadata.output + render text: defaults.output end end render :internal_server_error, text: result.output diff --git a/src/placeos-core/driver_manager.cr b/src/placeos-core/driver_manager.cr index c2a07b30..ba210280 100644 --- a/src/placeos-core/driver_manager.cr +++ b/src/placeos-core/driver_manager.cr @@ -52,7 +52,15 @@ module PlaceOS::Core return Result.new(success: true, output: resp.body.as(String)) if resp.success? Result.new(output: "Metadata not found. Server returned #{resp.status_code}") rescue ex - return Result.new(output: ex.message.not_nil!, name: file_name) + Result.new(output: ex.message.not_nil!, name: file_name) + end + + def defaults(file_name : String, commit : String, branch : String, uri : String) + resp = BuildApi.defaults(file_name, commit, branch, uri) + return Result.new(success: true, output: resp.body.as(String)) if resp.success? + Result.new(output: "Driver defaults not found. Server returned #{resp.status_code}") + rescue ex + Result.new(output: ex.message.not_nil!, name: file_name) end def built?(file_name : String, commit : String, branch : String, uri : String) : String? @@ -130,6 +138,19 @@ module PlaceOS::Core end end + def self.defaults(file_name : String, commit : String, branch : String, uri : String) + host = URI.parse(Core.build_host) + file_name = URI.encode_www_form(file_name) + ConnectProxy::HTTPClient.new(host) do |client| + path = "#{BUILD_API_BASE}/defaults/#{file_name}" + params = URI::Params.encode({"url" => uri, "branch" => branch, "commit" => commit}) + uri = "#{path}?#{params}" + rep = client.get(uri) + Log.debug { {message: "Getting driver defaults. Server respose: #{rep.status_code}", file_name: file_name, commit: commit, branch: branch} } + rep + end + end + def self.compiled?(file_name : String, commit : String, branch : String, uri : String) host = URI.parse(Core.build_host) file_name = URI.encode_www_form(file_name)