# File lib/openshift-origin-controller/app/controllers/emb_cart_controller.rb, line 83
  def create
    domain_id = params[:domain_id]
    id = params[:application_id]

    name = params[:name]
    # :cartridge param is deprecated because it isn't consistent with
    # the rest of the apis which take :name. Leave it here because
    # some tools may still use it
    name = params[:cartridge] unless name
    colocate_with = params[:colocate_with]

    domain = Domain.get(@cloud_user, domain_id)
    return render_error(:not_found, "Domain #{domain_id} not found", 127,
                        "EMBED_CARTRIDGE") if !domain || !domain.hasAccess?(@cloud_user)

    application = get_application(id)
    return render_error(:not_found, "Application '#{id}' not found for domain '#{domain_id}'",
                        101, "EMBED_CARTRIDGE") unless application

    begin
      #container = OpenShift::ApplicationContainerProxy.find_available(application.server_identity)
      container = OpenShift::ApplicationContainerProxy.find_available(nil)
      if not check_cartridge_type(name, container, "embedded")
        carts = Application.get_available_cartridges("embedded")
        return render_error(:bad_request, "Invalid cartridge. Valid values are (#{carts.join(', ')})",
                            109, "EMBED_CARTRIDGE", "cartridge")
      end
    rescue Exception => e
      return render_exception(e, "EMBED_CARTRIDGE")
    end

    #TODO: Need a proper method to let us know if cart will get its own gear
    if application.scalable && colocate_with.nil? && (@cloud_user.consumed_gears >= @cloud_user.max_gears) && name != 'jenkins-client-1.4'
      return render_error(:unprocessable_entity, "#{@cloud_user.login} has already reached the gear limit of #{@cloud_user.max_gears}",
                          104, "EMBED_CARTRIDGE")
    end

    cart_create_reply = ""
    begin
      application.add_group_override(name, colocate_with) if colocate_with
      cart_create_reply = application.add_dependency(name)
    rescue OpenShift::NodeException => e
      if !e.resultIO.nil? && !e.resultIO.errorIO.nil?
        return render_error(:internal_server_error, e.resultIO.errorIO.string.strip, e.resultIO.exitcode,
                            "EMBED_CARTRIDGE", "cartridge")
      else
        return render_exception(e, "EMBED_CARTRIDGE")
      end
    rescue Exception => e
      return render_exception(e, "EMBED_CARTRIDGE")
    end

    application = get_application(id)

    application.embedded.each do |key, value|
      if key == name
        if $requested_api_version == 1.0
          cartridge = RestCartridge10.new("embedded", key, application, get_url, nil, nolinks)
        else
          cartridge = RestCartridge11.new("embedded", key, application, get_url, nil, nolinks)
        end
        messages = []
        log_msg = "Added #{name} to application #{id}"
        messages.push(Message.new(:info, log_msg))
        messages.push(Message.new(:info, cart_create_reply.resultIO.string, 0, :result))
        messages.push(Message.new(:info, cart_create_reply.appInfoIO.string, 0, :appinfo))
        return render_success(:created, "cartridge", cartridge, "EMBED_CARTRIDGE", log_msg, nil, nil, messages)

      end
    end if application.embedded
    render_error(:internal_server_error, "Cartridge #{name} not embedded within application #{id}", nil, "EMBED_CARTRIDGE")
  end