Example #1
0
func handleGET(token *config.Token, domain *api.Domain, l *log.RequestLogger, r *responder.Responder, recordName string) {
	// Get list of records for specified type and domain
	records, err := domain.ListRecords(token.Domain, token.Type)
	if err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Return list of all records if no specific record was asked for
	if recordName == "" {
		r.JSON(&RecordListResponse{
			Domain:  token.Domain,
			Records: records,
		})
		l.Message(fmt.Sprintf("Returning %d records of type '%s' for domain '%s'", len(records), token.Domain, token.Type))
		return
	}

	// Return specific record
	for _, record := range records {
		if record.Name == recordName {
			r.JSON(record)
			l.Message(fmt.Sprintf("Returning record '%s' of type '%s' for domain '%s'", recordName, token.Domain, token.Type))
			return
		}
	}

	// Record not found
	err = fmt.Errorf("Record with name '%s' not found", recordName)
	r.Error(err)
	l.Error(err)
}
Example #2
0
func handlePOST(token *config.Token, domain *api.Domain, l *log.RequestLogger, r *responder.Responder, request *Request) {
	// Start domain update transaction
	trans, err := domain.StartTransaction(token.Domain)
	if err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Delete all existing records with same name and type
	if err := trans.DeleteRecords(request.Name, token.Type); err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Add new record to domain
	if err := trans.AddRecord(request.Name, token.Type, request.Value, token.TTL); err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Commit changes to domain
	if err := trans.Commit(); err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Send success response
	r.Success()
	l.Message(fmt.Sprintf("Added [%s] '%s' - '%s' to domain '%s'", token.Type, request.Name, request.Value, token.Domain))
}
Example #3
0
func handleDELETE(token *config.Token, domain *api.Domain, l *log.RequestLogger, r *responder.Responder, recordName string) {
	// Start domain update transaction
	trans, err := domain.StartTransaction(token.Domain)
	if err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Delete all existing records with same name and type
	if err := trans.DeleteRecords(recordName, token.Type); err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Commit changes to domain
	if err := trans.Commit(); err != nil {
		r.Error(err)
		l.Error(err)
		return
	}

	// Send success response
	r.Success()
	l.Message(fmt.Sprintf("Removed all [%s] '%s' records from domain '%s'", token.Type, recordName, token.Domain))
}