// AddSchedule creates a new task schedule
func AddSchedule(w rest.ResponseWriter, r *rest.Request) {
	dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return

	}
	defer dbConn.Close()
	postMsg := AddSchedulePost{}
	err = r.DecodeJsonPayload(&postMsg)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if postMsg.ContainerName == "" {
		logit.Error.Println("node ContainerName required")
		rest.Error(w, "ContainerName required", 400)
		return
	}
	if postMsg.ProfileName == "" {
		logit.Error.Println("node ProfileName required")
		rest.Error(w, "ProfileName required", 400)
		return
	}
	if postMsg.Name == "" {
		logit.Error.Println("node Name required")
		rest.Error(w, "Name required", 400)
		return
	}

	//logit.Info.Println("in adminapi.backupmgmt.AddSchedule got serverIP of " + postMsg.Serverip)

	err = secimpl.Authorize(dbConn, postMsg.Token, "perm-backup")
	if err != nil {
		logit.Error.Println("validate token error " + err.Error())
		rest.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	s := task.TaskSchedule{}

	s.ContainerName = postMsg.ContainerName
	s.ProfileName = postMsg.ProfileName
	s.Name = postMsg.Name

	//defaults for any new schedule
	s.Enabled = "NO"
	s.Minutes = "00"
	s.Hours = "11"
	s.DayOfMonth = "1"
	s.Month = "*"
	s.DayOfWeek = "*"
	s.RestoreSet = postMsg.RestoreSet
	s.RestoreRemotePath = postMsg.RestoreRemotePath
	s.RestoreRemoteHost = postMsg.RestoreRemoteHost
	s.RestoreRemoteUser = postMsg.RestoreRemoteUser
	s.RestoreDbUser = postMsg.RestoreDbUser
	s.RestoreDbPass = postMsg.RestoreDbPass
	s.Serverip = postMsg.Serverip

	result, err := task.AddSchedule(dbConn, s)
	if err != nil {
		logit.Error.Println(err.Error())
		rest.Error(w, err.Error(), 400)
		return
	}

	logit.Info.Println("AddSchedule: new ID " + result)

	//we choose by design to not notify the task server
	//on schedule adds, instead we mark any new schedule
	//as DISABLED, forcing the user to change the defaults
	//and use the UpdateSchedule which does force a notify
	//to the task server

	w.WriteHeader(http.StatusOK)
	status := types.SimpleStatus{}
	status.Status = "OK"
	w.WriteJson(&status)
}