Пример #1
0
func toggleHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	id := vars["id"]
	if id == "" {
		http.NotFound(w, req)
		return
	}

	// Check that the item exists
	res, err := r.Table("items").Get(id).Run(session)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if res.IsNil() {
		http.NotFound(w, req)
		return
	}

	// Toggle the item
	_, err = r.Table("items").Get(id).Update(map[string]interface{}{"Status": r.Branch(
		r.Row.Field("Status").Eq("active"),
		"complete",
		"active",
	)}).RunWrite(session)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	http.Redirect(w, req, "/", http.StatusFound)
}
Пример #2
0
func (s *HTTPServer) ToggleNode(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	id := vars["id"]
	if id == "" {
		http.NotFound(w, req)
		return
	}

	// Check that the item exists
	res, err := r.Table(tableIpxe).Get(id).RunRow(session)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if res.IsNil() {
		http.NotFound(w, req)
		return
	}

	_, err = r.Table(tableIpxe).Get(id).Update(map[string]interface{}{"Status": r.Branch(
		r.Row.Field("Status").Eq("AVAILABLE"),
		"UNAVAILABLE",
		"AVAILABLE",
	)}).RunWrite(session)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w, req, "/", http.StatusFound)
}
Пример #3
0
//Subscribe
func (rt *RethinkStorage) Subscribe(topic string, clientid string) error {

	/*
		var newDoc = { metaData: { ...}, results: [...] };

		r.db("foo").table("bar").get(1).replace( function(doc) {
		    return r.branch(
		        doc.eq(null),
		        newDoc,
		        doc.merge({ doc("results").add(newDoc("results")) })
		    )
		})
	*/

	newTopic := Topic{
		ID:      topic,
		Clients: []string{clientid},
	}

	response, err := r.Table(rt.TopicTable).Get(topic).Replace(func(doc r.Term) interface{} {
		return r.Branch(doc.Eq(nil),
			newTopic,
			doc.Merge(map[string]interface{}{"clients": doc.Field("clients").Add(newTopic.Clients)}))
	}).RunWrite(rt.Session)
	if err != nil {
		return fmt.Errorf("Error appending data: %s", err.Error())
	}

	if response.Errors >= 1 {
		return fmt.Errorf("Error appending data: %s", response.FirstError)
	}

	return nil
}
Пример #4
0
func (rd *rethinkDriver) PutBuild(build *datastore.Build) (string, error) {
	result, err := r.Table("builds").Insert(
		r.Table("builds").Filter(
			r.And(
				r.Row.Field("job_id").Eq(build.JobId),
				r.Row.Field("number").Eq(build.Number),
			),
		).CoerceTo("array").Do(func(docs r.Term) interface{} {
			return r.Branch(
				r.Or(docs.IsEmpty(), docs.Field("id").Contains(build.Id)),
				build,
				r.Error("Build with job_id and number exists"),
			)
		}),
		r.InsertOpts{Conflict: "replace"},
	).RunWrite(rd.session)
	if err != nil {
		return "", err
	}

	if len(result.GeneratedKeys) > 0 {
		return result.GeneratedKeys[0], nil
	} else {
		return build.Id, nil
	}
}
Пример #5
0
func InitDB(dbName string) (session *r.Session, err error) {

	var cursor *r.Cursor

	session, err = r.Connect(r.ConnectOpts{
		Address:  os.Getenv("RETHINKDB_URL"),
		Database: dbName,
		MaxIdle:  10,
		Timeout:  time.Second * 10,
	})

	if err != nil {
		l.Println("Connect", err)
		return
	}

	cursor, err = r.DBList().Contains(dbName).Do(func(row r.Term) r.Term {
		return r.Branch(
			row.Eq(true),
			nil,
			r.DBCreate(dbName),
		)
	}).Run(session)
	defer cursor.Close()

	for _, table := range tables {

		cursor, err = r.DB(dbName).TableList().Contains(table.name).Do(func(row r.Term) r.Term {
			return r.Branch(
				row.Eq(true),
				nil,
				r.DB(dbName).TableCreate(table.name, table.options),
			)
		}).Run(session)
		defer cursor.Close()

		if err != nil {
			return
		}
	}

	return
}
Пример #6
0
func createDatabase(dbName string) {
	_, err := r.Branch(
		r.DBList().Contains(dbName),
		nil,
		r.DBCreate(dbName),
	).Run(Session)
	if err != nil {
		panic(err)
	}
}
Пример #7
0
func (a *rethinkAPIServer) StartJob(ctx context.Context, job *ppsclient.Job) (response *google_protobuf.Empty, err error) {
	_, err = a.getTerm(jobInfosTable).Get(job.ID).Update(gorethink.Branch(
		gorethink.Row.Field("State").Eq(ppsclient.JobState_JOB_PULLING),
		map[string]interface{}{
			"State": ppsclient.JobState_JOB_RUNNING,
		},
		map[string]interface{}{},
	)).RunWrite(a.session)
	return google_protobuf.EmptyInstance, err
}
func (d *Deployment) CheckByName(name string) (check Check, err error) {
	cur, err := r.Branch(
		r.Table("deployments").Get(d.Id).Field("checks").Filter(map[string]interface{}{"name": name}).IsEmpty(),
		r.Table("checks").GetAllByIndex("type_name", []string{d.Type, name}),
		r.Table("deployments").Get(d.Id).Field("checks").Filter(map[string]interface{}{"name": name}),
	).Run(session)
	defer cur.Close()
	err = cur.One(&check)
	if err != nil {
		return check, err
	}
	return check, nil
}
func (d *Deployment) MergedAlertSettings() (settings Settings, err error) {
	cur, err := r.Branch(
		r.Table("groups").Get(d.GroupId),
		r.Table("groups").EqJoin("id", r.Table("deployments"), r.EqJoinOpts{Index: "group_id"}).Zip().Filter(map[string]interface{}{"id": d.Id}).Field("settings"),
		r.Table("deployments").Get(d.Id).Field("settings"),
	).Run(session)
	defer cur.Close()
	err = cur.One(&settings)
	if err != nil {
		return settings, err
	}
	return settings, nil
}
Пример #10
0
func createTableIfNotExists(tableName, primaryKey string) {
	if primaryKey == "" {
		primaryKey = "id"
	}
	_, err := r.Branch(
		Db.TableList().Contains(tableName),
		nil,
		Db.TableCreate(tableName, r.TableCreateOpts{
			PrimaryKey: primaryKey,
		}),
	).Run(Session)

	if err != nil {
		panic(err)
	}
}
Пример #11
0
func (rd rethinkDriver) PutJob(job *datastore.Job) (string, error) {
	result, err := r.Table("jobs").Insert(
		r.Table("jobs").Filter(r.Row.Field("key").Eq(job.Key)).CoerceTo("array").Do(func(docs r.Term) interface{} {
			return r.Branch(
				r.Or(docs.IsEmpty(), docs.Field("id").Contains(job.Id)),
				job,
				r.Error("Job with key exists"),
			)
		}),
		r.InsertOpts{Conflict: "replace"},
	).RunWrite(rd.session)
	if err != nil {
		return "", err
	}

	if len(result.GeneratedKeys) > 0 {
		return result.GeneratedKeys[0], nil
	} else {
		return job.Id, nil
	}
}
Пример #12
0
// CpTable copies all data from dbSrc.src to dbDest.dest
func CpTable(conn *r.Session, dbSrc, src, dbDest, dest string, force bool) {
	conflict := "error"
	if force {
		conflict = "update"
	}

	fmt.Printf("Copying table %s.%s to %s.%s\n", dbSrc, src, dbDest, dest)
	_, err := r.DB(dbDest).TableList().Contains(dest).Do(func(tableExists r.Term) r.Term {
		return r.Branch(tableExists,
			map[string]interface{}{"created": 0},
			r.DB(dbDest).TableCreate(dest))
	}).RunWrite(conn)
	if err != nil {
		fmt.Printf("Couldn't create table %s.%s\n", dbDest, dest)
		panic(err)
	}

	_, err = r.DB(dbDest).Table(dest).Insert(r.DB(dbSrc).Table(src), r.InsertOpts{Conflict: conflict}).RunWrite(conn)
	if err != nil {
		fmt.Printf("CpTable couldn't copy data from %s.%s to %s.%s\n", dbSrc, src, dbDest, dest)
	}
}