Beispiel #1
0
func getPolicy(dbname, location string) (output []byte, code int, err error) {
	policy, err := backup.GetRetentionPolicy(dbname)
	if err != nil {
		exists, innerErr := backup.DatabaseExists(dbname)
		//if error is EQUAL to nil. That's not a typo
		if innerErr == nil && !exists {
			return nil, http.StatusBadRequest, errors.New(fmt.Sprintf("No such database: %s", dbname))
		}
		return nil, http.StatusInternalServerError, err
	}
	switch location {
	case "local":
		return []byte(strconv.FormatFloat(policy.LocalHours, 'f', -1, 64)), http.StatusOK, nil
	case "remote":
		return []byte(strconv.FormatFloat(policy.RemoteHours, 'f', -1, 64)), http.StatusOK, nil
	default:
		return nil, http.StatusBadRequest, errors.New(fmt.Sprintf("Unrecognized location: %s", location))
	}
}
Beispiel #2
0
func setPolicy(dbname, location string, value float64) (output []byte, code int, err error) {
	var remote bool
	switch location {
	case "local":
		remote = false
	case "remote":
		remote = true
	default:
		return nil, http.StatusBadRequest, errors.New(fmt.Sprintf("Unrecognized location: %s", location))
	}
	if value > twoYears || value < 0.0 {
		return nil, http.StatusBadRequest, errors.New(fmt.Sprintf("Value %f is out of bounds.", value))
	}
	row := backup.RetentionRuleRow{DBName: dbname, Hours: value, IsRemoteRule: remote}
	err = row.Put()
	if err != nil {
		exists, innerErr := backup.DatabaseExists(dbname)
		if innerErr == nil && !exists {
			return nil, http.StatusBadRequest, err
		}
		return nil, http.StatusInternalServerError, err
	}
	return []byte("OK"), http.StatusCreated, nil
}