This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: added create and update methods to registries
Signed-off-by: Miquel Sabaté Solà <[email protected]>
- Loading branch information
Showing
7 changed files
with
297 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Registries | ||
class BaseService < ::BaseService | ||
attr_reader :messages | ||
attr_accessor :force | ||
|
||
def valid? | ||
@valid | ||
end | ||
|
||
def reachable? | ||
@reachable | ||
end | ||
|
||
protected | ||
|
||
def check_reachability! | ||
msg = @registry.reachable? | ||
return if msg.blank? | ||
|
||
Rails.logger.info "\nRegistry not reachable:\n#{@registry.inspect}\n#{msg}\n" | ||
@valid = false | ||
@reachable = false | ||
@messages[:hostname] = (@messages[:hostname] || []).push(msg) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Registries | ||
class CreateService < ::Registries::BaseService | ||
def execute | ||
@registry = Registry.new(params) | ||
@valid = @registry.valid? | ||
@messages = @registry.errors.messages | ||
@reachable = true | ||
|
||
check! | ||
if @valid | ||
if @registry.save | ||
Namespace.update_all(registry_id: @registry.id) | ||
else | ||
@messages.merge(@registry.errors.messages) | ||
end | ||
end | ||
|
||
@registry | ||
end | ||
|
||
protected | ||
|
||
def check! | ||
return unless @valid | ||
check_uniqueness! | ||
return unless @valid | ||
check_reachability! unless @force | ||
end | ||
|
||
def check_uniqueness! | ||
return unless Registry.any? | ||
@valid = false | ||
@messages[:status] = "You can only create one registry" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Registries | ||
class UpdateService < ::Registries::BaseService | ||
def execute | ||
@registry = Registry.find(params[:id]) | ||
@messages = @registry.errors.messages | ||
@registry.assign_attributes(params) | ||
@valid = @registry.valid? | ||
|
||
check! | ||
if @valid | ||
@messages.merge(@registry.errors.messages) unless @registry.save | ||
end | ||
|
||
@registry | ||
end | ||
|
||
protected | ||
|
||
def check! | ||
return unless @valid | ||
check_hostname! | ||
return unless @valid | ||
check_reachability! unless @force | ||
end | ||
|
||
def check_hostname! | ||
return if !Repository.any? || params[:hostname].blank? | ||
@valid = false | ||
@messages[:hostname] = "Registry is not empty, cannot change hostname" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters