require "commands/bootstrap_templates"
require "app/api3/parsers/chef"
require_relative "request_handler"

module Devops
  module API3
    module Handler
      class Chef < RequestHandler

        set_parser Devops::API3::Parser::ChefParser

        include BootstrapTemplatesCommands

        def chef_nodes
          KnifeFactory.instance.chef_node_list
        end

        def tags node_name
          KnifeFactory.instance.tags_list(node_name)
        end

        def set_tags node_name
          tags = parser.tags
          tagsStr = tags.join(" ")
          cmd = KnifeFactory.instance.tags_create(node_name, tagsStr)
          halt_response("Cannot add tags #{tagsStr} to server #{node_name}", 500) unless cmd[1]
          tags
        end

        def unset_tags node_name
          tags = parser.tags
          tagsStr = tags.join(" ")
          cmd = KnifeFactory.instance.tags_delete(node_name, tagsStr)
          halt_response("Cannot delete tags #{tagsStr} from server #{node_name}: #{cmd[0]}", 500) unless cmd[1]
          tags
        end

      end
    end
  end
end

