class Crowbar::Client::Command::Proposal::Edit

Implementation for the proposal edit command

Public Instance Methods

execute() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 39
def execute
  validate_barclamp! args.barclamp

  request.process do |request|
    case request.code
    when 200
      say "Successfully updated #{args.proposal} proposal"
    else
      err request.parsed_response["error"]
    end
  end
end
request() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 31
def request
  @request ||= Request::Proposal::Edit.new(
    args.easy_merge(
      payload: payload_content
    )
  )
end

Protected Instance Methods

check_nodes(configuration) click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 158
def check_nodes(configuration)
  invalid_nodes = []
  nodes_and_clusters = valid_elements

  configuration["deployment"].each do |service_name, service|
    next if service["elements"].nil?

    service["elements"].each do |role, nodes|
      nodes.each do |node|
        unless nodes_and_clusters.include?(node)
          invalid_nodes << { "node" => node, "role" => role,
                             "service_name" => service_name }
        end
      end
    end
  end

  return if invalid_nodes.empty?

  error_str = ""

  invalid_nodes.each do |error|
    error_str += "ERROR : #{error["node"]} is not available in the list of" \
    " possible nodes and clusters. Added in role #{error["role"]}" \
    " of service #{error["service_name"]}\r\n"
  end

  err error_str
end
from_data() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 74
def from_data
  json = begin
    JSON.load(
      options[:data]
    )
  rescue JSON::ParserError
    err "Failed to parse JSON"
  end

  # check if the nodes in the configuration are in the list of nodes
  check_nodes(json)

  if options[:merge]
    proposal_preload.easy_merge(
      json
    )
  else
    json
  end
end
from_editor() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 126
def from_editor
  editor = Util::Editor.new content: proposal_preload
  editor.edit!
  result = editor.result

  # check if the nodes in the configuration are in the list of nodes
  check_nodes(result)

  result
rescue EditorAbortError => e
  err e.message
rescue EditorStartupError => e
  err e.message
rescue InvalidJsonError => e
  err e.message
rescue SimpleCatchableError => e
  err e.message
rescue
  err "Editing content failed"
end
from_file() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 95
def from_file
  json = begin
    file = File.read(
      options[:file]
    )

    JSON.load(
      file
    )
  rescue Errno::EACCES
    err "Failed to access file"
  rescue Errno::ENOENT
    err "Failed to read file"
  rescue JSON::ParserError
    err "Failed to parse JSON"
  rescue
    err "Failed to process file"
  end

  # check if the nodes in the configuration are in the list of nodes
  check_nodes(json)

  if options[:merge]
    proposal_preload.easy_merge(
      json
    )
  else
    json
  end
end
payload_content() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 147
def payload_content
  case
  when options[:data]
    from_data
  when options[:file]
    from_file
  else
    from_editor
  end
end
proposal_preload() click to toggle source
# File lib/crowbar/client/command/proposal/edit.rb, line 54
def proposal_preload
  result = Request::Proposal::Show.new(
    barclamp: args.barclamp,
    proposal: args.proposal
  ).process

  case result.code
  when 200
    if options[:raw]
      result.parsed_response
    else
      deployment_cleanup result.parsed_response
    end
  when 404
    err "Failed to preload proposal"
  else
    err result.parsed_response["error"]
  end
end