def domain_post
domain = get_domain(@cloud_user, @req.namespace)
domain = @cloud_user.domains.first if !domain && @req.alter
if (!domain or not domain.hasFullAccess?(@cloud_user)) && (@req.alter || @req.delete)
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", false, "Cannot alter or remove namespace #{@req.namespace}. Namespace does not exist.")
@reply.resultIO << "Cannot alter or remove namespace #{@req.namespace}. Namespace does not exist.\n"
@reply.exitcode = 106
render :json => @reply, :status => :bad_request
return
end
if @req.alter
Rails.logger.debug "Updating namespace for domain #{domain.uuid} from #{domain.namespace} to #{@req.namespace}"
raise OpenShift::UserException.new("The supplied namespace '#{@req.namespace}' is not allowed", 106) if OpenShift::ApplicationContainerProxy.blacklisted? @req.namespace
begin
if domain.namespace != @req.namespace
domain.namespace = @req.namespace
@reply.append domain.save
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", true, "Updated namespace for domain #{domain.uuid} to #{@req.namespace}")
end
rescue Exception => e
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", false, "Failed to updated namespace for domain #{domain.uuid} to #{@req.namespace}")
Rails.logger.error "Failed to update domain #{domain.uuid} from #{domain.namespace} to #{@req.namespace} #{e.message}"
Rails.logger.error e.backtrace
raise
end
if @req.ssh
@cloud_user.update_ssh_key(@req.ssh, @req.key_type, @req.key_name)
@cloud_user.save
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_ALTER_DOMAIN", true, "Updated SSH key '#{@req.key_name}' for domain #{domain.namespace}")
end
elsif @req.delete
if not domain.hasFullAccess?(@cloud_user)
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", false, "Domain #{domain.namespace} is not associated with user")
@reply.resultIO << "Cannot remove namespace #{@req.namespace}. This namespace is not associated with login: #{@cloud_user.login}\n"
@reply.exitcode = 106
render :json => @reply, :status => :bad_request
return
end
if not @cloud_user.applications.empty?
@cloud_user.applications.each do |app|
if app.domain.uuid == domain.uuid
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", false, "Domain #{domain.namespace} contains applications")
@reply.resultIO << "Cannot remove namespace #{@req.namespace}. Remove existing app(s) first: "
@reply.resultIO << @cloud_user.applications.map{|a| a.name}.join("\n")
@reply.exitcode = 106
render :json => @reply, :status => :bad_request
return
end
end
end
@reply.append domain.delete
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_DELETE_DOMAIN", true, "Deleted domain #{@req.namespace}")
render :json => @reply
return
else
raise OpenShift::UserException.new("The supplied namespace '#{@req.namespace}' is not allowed", 106) if OpenShift::ApplicationContainerProxy.blacklisted? @req.namespace
raise OpenShift::UserException.new("Domain already exists for user. Update the domain to modify.", 158) if !@cloud_user.domains.empty?
key = Key.new(Key::DEFAULT_SSH_KEY_NAME, @req.key_type, @req.ssh)
if key.invalid?
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_DOMAIN", false, "Failed to create domain #{@req.namespace}: #{key.errors.first[1][:message]}")
@reply.resultIO << key.errors.first[1][:message]
@reply.exitcode = key.errors.first[1][:exit_code]
render :json => @reply, :status => :bad_request
return
end
@cloud_user.add_ssh_key(Key::DEFAULT_SSH_KEY_NAME, @req.ssh, @req.key_type)
domain = Domain.new(@req.namespace, @cloud_user)
@reply.append domain.save
log_action(@request_id, @cloud_user.uuid, @login, "LEGACY_CREATE_DOMAIN", true, "Created domain #{@req.namespace}")
end
@reply.append @cloud_user.save
@reply.data = {
:rhlogin => @cloud_user.login,
:uuid => @cloud_user.uuid,
:rhc_domain => Rails.configuration.openshift[:domain_suffix]
}.to_json
render :json => @reply
end