Class: Pvectl::Services::EditContainer
- Inherits:
-
Object
- Object
- Pvectl::Services::EditContainer
- Defined in:
- lib/pvectl/services/edit_container.rb
Overview
Orchestrates the interactive container configuration editing flow.
Fetches current config, opens it in an editor as structured YAML, validates changes, computes a diff, and applies updates via the API. Supports dry-run mode and optimistic locking via digest.
Instance Method Summary collapse
-
#execute(ctid:) ⇒ Models::ContainerOperationResult?
Executes the interactive container edit flow.
-
#initialize(container_repository:, editor_session: nil, options: {}) ⇒ EditContainer
constructor
Creates a new EditContainer service.
Constructor Details
#initialize(container_repository:, editor_session: nil, options: {}) ⇒ EditContainer
Creates a new EditContainer service.
26 27 28 29 30 |
# File 'lib/pvectl/services/edit_container.rb', line 26 def initialize(container_repository:, editor_session: nil, options: {}) @container_repository = container_repository @editor_session = editor_session @options = end |
Instance Method Details
#execute(ctid:) ⇒ Models::ContainerOperationResult?
Executes the interactive container edit flow.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pvectl/services/edit_container.rb', line 36 def execute(ctid:) container = @container_repository.get(ctid) return not_found_result(ctid) unless container config = @container_repository.fetch_config(container.node, ctid) resource_info = { ctid: ctid, node: container.node, status: container.status } yaml_content = ConfigSerializer.to_yaml(config, type: :container, resource: resource_info) validator = ->(content) { ConfigSerializer.validate(content, type: :container) } session = @editor_session || EditorSession.new(validator: validator) edited = session.edit(yaml_content) return nil unless edited original_roundtrip = ConfigSerializer.from_yaml(yaml_content, type: :container) edited_flat = ConfigSerializer.from_yaml(edited, type: :container) violations = ConfigSerializer.readonly_violations(original_roundtrip, edited_flat, type: :container) unless violations.empty? return build_result(resource_info, success: false, error: "Read-only fields cannot be changed: #{violations.join(', ')}") end changes = ConfigSerializer.diff(original_roundtrip, edited_flat) if changes[:changed].empty? && changes[:added].empty? && changes[:removed].empty? return nil end params = build_update_params(changes, config) resource_info[:diff] = changes if @options[:dry_run] return build_result(resource_info, success: true) end @container_repository.update(ctid, container.node, params) build_result(resource_info, success: true) rescue StandardError => e build_result({ ctid: ctid }, success: false, error: e.) end |