# File lib/openshift-origin-controller/app/controllers/domains_controller.rb, line 62
  def update
    id = params[:existing_id]
    new_namespace = params[:id]
    domain = Domain.get(@cloud_user, id)

    new_domain = Domain.new(new_namespace, @cloud_user)
    if not new_domain.valid?
      messages = get_error_messages(new_domain, "namespace", "id")
      return render_format_error(:unprocessable_entity, nil, nil, "UPDATE_DOMAIN", nil, nil, messages)
    end
    return render_format_error(:not_found, "Domain '#{id}' not found", 127, 
                               "UPDATE_DOMAIN") if !domain || !domain.hasAccess?(@cloud_user)

    return render_format_error(:forbidden, "User does not have permission to modify domain '#{id}'",
                               132, "UPDATE_DOMAIN") if domain && !domain.hasFullAccess?(@cloud_user)

    Rails.logger.debug "Updating domain #{domain.namespace} to #{new_namespace}"
    begin
      dom_available = Domain.namespace_available?(new_namespace)
    rescue Exception => e
      return render_format_exception(e, "UPDATE_DOMAIN") 
    end

    return render_format_error(:unprocessable_entity, "Namespace '#{new_namespace}' already in use. Please choose another.",
                               103, "UPDATE_DOMAIN", "id") if !dom_available

    domain.namespace = new_namespace
    if domain.invalid?
      messages = get_error_messages(domain, "namespace", "id")
      return render_format_error(:unprocessable_entity, nil, nil, "UPDATE_DOMAIN", nil, nil, messages)
    end

    begin
      domain.save
    rescue Exception => e
      return render_format_exception(e, "UPDATE_DOMAIN") 
    end

    @cloud_user = CloudUser.find(@login)
    domain = RestDomain.new(domain, get_url, nolinks)
    render_format_success(:ok, "domain", domain, "UPDATE_DOMAIN", "Updated domain #{id} to #{new_namespace}")
  end