fluke/devops-client/lib/devops-client/output/project.rb

154 lines
4.8 KiB
Ruby

require "devops-client/output/base"
module Output
class Project < Base
NODE_HEADER = "Node number"
SUBPROJECT_HEADER = "Subproject"
def table
title = nil
with_separator = false
headers, rows = case options[:output_type]
when :show
title = I18n.t("output.title.project.show", :name => @data["name"])
with_separator = true
create_show(@data)
when :servers
title = ARGV[2]
title += " " + ARGV[3] unless ARGV[3].nil?
title = I18n.t("output.title.project.servers", :title => title)
create_servers(@data)
when :stacks
title = ARGV[2]
title += " " + ARGV[3] unless ARGV[3].nil?
title = I18n.t("output.title.project.stacks", :title => title)
create_stacks(@data)
when :test
title = I18n.t("output.title.project.test", :project => ARGV[2], :env => ARGV[3])
create_test(@data)
else
title = I18n.t("output.title.project.list")
create_list(@data)
end
create_table(headers, rows, title, with_num?, with_separator)
end
def csv
with_num = true
headers, rows = case @additional_options[:output_type]
when :list
create_list(@data)
when :show
with_num = false
create_show(@data)
when :servers
create_servers(@data)
when :test
create_test(@data)
end
create_csv(headers, rows, with_num?)
end
private
def create_list list
abort(I18n.t("output.not_found.project.list")) if list.empty?
rows = list.map {|l| [l["name"]]}
headers = [ I18n.t("output.table_header.id") ]
return headers, rows
end
def create_show show
rows = []
if show["type"] == "multi"
show["deploy_envs"].each do |de|
subprojects = []
nodes = []
de["servers"].each do |s|
s["subprojects"].each do |sp|
subprojects.push "#{sp["name"]} - #{sp["env"]}"
nodes.push sp["node"]
end
end
rows.push [ de["identifier"], subprojects.join("\n"), nodes.join("\n"), de["users"].join("\n") ]
end
headers = [
I18n.t("output.table_header.deploy_env"),
I18n.t("output.table_header.subproject") + " - " + I18n.t("output.table_header.deploy_env"),
I18n.t("output.table_header.node_number"),
I18n.t("output.table_header.users")
]
else
show["deploy_envs"].each_with_index do |de, i|
rows.push [
show["name"],
de["identifier"],
de["image"],
de["flavor"],
(de["run_list"] || []).join("\n"),
(de["groups"] || []).join("\n"),
(de["subnets"] || []).join("\n"),
(de["users"] || []).join("\n")
]
end
headers = [
I18n.t("output.table_header.id"),
I18n.t("output.table_header.deploy_env"),
I18n.t("output.table_header.image_id"),
I18n.t("output.table_header.flavor"),
I18n.t("output.table_header.run_list"),
I18n.t("output.table_header.groups"),
I18n.t("output.table_header.subnets"),
I18n.t("output.table_header.users")
]
end
return headers, rows
end
def create_servers servers
abort(I18n.t("output.not_found.project.servers", name: ARGV[2])) if servers.empty?
rows = []
servers.each do |s|
rows.push [ s["project"], s["deploy_env"], s["chef_node_name"], s["remote_user"], s["provider"], s["id"] ]
end
headers = [
I18n.t("output.table_header.id"),
I18n.t("output.table_header.deploy_env"),
I18n.t("output.table_header.node_name"),
I18n.t("output.table_header.remote_user"),
I18n.t("output.table_header.provider"),
I18n.t("output.table_header.instance_id")
]
return headers, rows
end
def create_test test
rows = []
headers = [
I18n.t("output.table_header.server"),
I18n.t("output.table_header.node_name"),
I18n.t("output.table_header.creation"),
I18n.t("output.table_header.bootstrap"),
I18n.t("output.table_header.deletion")
]
test["servers"].each do |s|
rows.push [ s["id"],
s["chef_node_name"],
"#{s["create"]["status"]}\n#{s["create"]["time"]}",
"#{s["bootstrap"]["status"]}\n#{s["bootstrap"]["time"]}",
"#{s["delete"]["status"]}\n#{s["delete"]["time"]}" ]
end
return headers, rows
end
def create_stacks(stacks)
abort(I18n.t("output.not_found.project.stacks", name: ARGV[2])) if stacks.empty?
fields_to_output = %w(id deploy_env stack_template cloud_stack_id stack_status provider)
headers_and_rows(stacks, fields_to_output)
end
end
end