class ArsesController < ApplicationController

  respond_to :json

  # create or update a client_artifact row
  def get_or_create
    name = params[:name]
    provider = params[:provider]
    active = params[:active]
    ip = params[:ip]
    username = params[:username]
    password = params[:password]
    topology = params[:topology]
    ars_id = params[:ars_id]
    puts "TOPOLOGY #{topology}"

    if ars_id
      ars = Ars.find_by_id_int(ars_id)
    end
    if ars.nil?
      ars = Ars.new
      ars.name = name
    end

    ars.provider = provider
    ars.active = active
    ars.ip = ip
    ars.password = password
    ars.username = username
    if topology
      ars.city = topology['city']
      ars.country = topology['country']
      ars.continent = topology['continent']
      ars.latitude = topology['latitude']
      ars.longitude = topology['longitude']
      ars.subdivision = topology['subdivision']
    end
    ars.save

    @ars = ars
    unless @ars.errors.any?
      @ars = Ars.find_by_name(name)
      render :json => {id_int: @ars.id_int, id: @ars.id, name: @ars.name, provider: @ars.provider, active: @ars.active, ip: @ars.ip}, :status => :ok
    else
      response.status = :unprocessable_entity
      respond_with @ars
    end

  end
end
