Beispiel #1
0
func deleteDocument(i *ec2.Instance, db *mongodb.MongoDB) error {
	if i == nil {
		return errors.New("nil instance")
	}

	var id bson.ObjectId

	for _, tag := range i.Tags {
		key := strings.ToLower(aws.StringValue(tag.Key))
		value := strings.ToLower(aws.StringValue(tag.Value))

		if key != "koding-machineid" || value == "" {
			continue
		}

		id = bson.ObjectIdHex(value)
		break
	}

	if !id.Valid() {
		return errors.New("unable to find valid jMachine.ObjectId")
	}

	return db.Run(modelhelper.MachinesColl, func(c *mgo.Collection) error { return c.RemoveId(id) })
}
Beispiel #2
0
func AuthorNameById(id bson.ObjectId, authors []Author) string {
	if !id.Valid() {
		return "anon"
	}
	for _, author := range authors {
		if author.ID == id {
			fmt.Printf("matched author name %s to %s\n", author.Name, id)
			return author.Name
		}
	}
	fmt.Printf("did not match author name to %s\n", id)
	return "anon"
}
Beispiel #3
0
func ToObjectIdOrBlankInterface(id bson.ObjectId) interface{} {
	if id.Valid() {
		return id
	}
	return ""
}
func permalinkHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" && r.Method != "POST" {
		w.Header().Set("Allow", "GET POST")
		w.WriteHeader(http.StatusMethodNotAllowed)
		w.Write([]byte(PERMALINK_HELP))
		return
	}

	var err error
	var jsonPermalink map[string]interface{}
	var id bson.ObjectId

	r.Body = http.MaxBytesReader(w, r.Body, MAX_REQSIZE)
	if r.Method == "GET" {
		r.ParseForm()
		var id64str string = r.Form.Get("id")
		if id64str == "" {
			w.Write([]byte(PERMALINK_HELP))
			return
		}

		/* For backwards-compatibility with permalinks from the Meteor plotter, only look at the first 16 bytes. */
		var idslice []byte = make([]byte, permalinkdlen)
		_, err = base64.URLEncoding.Decode(idslice, []byte(id64str)[:permalinklen])

		if err != nil {
			w.Write([]byte(PERMALINK_HELP))
			return
		}

		id = bson.ObjectId(idslice)

		if !id.Valid() {
			w.Write([]byte(PERMALINK_BAD_ID))
			return
		}

		var query *mgo.Query = permalinkConn.FindId(id)

		err = query.One(&jsonPermalink)
		if err != nil {
			w.Write([]byte(PERMALINK_BAD_ID))
			return
		}

		// I could do this asynchronously, but I think this is good enough
		err = permalinkConn.UpdateId(id, map[string]interface{}{
			"$set": map[string]interface{}{
				"lastAccessed": bson.Now(),
			},
		})

		if err != nil {
			// In the future I could try something like restarting the connection
			fmt.Printf("Could not update permalink record: %v\n", err)
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")

		var permalinkEncoder *json.Encoder = json.NewEncoder(w)
		err = permalinkEncoder.Encode(jsonPermalink)

		if err != nil {
			fmt.Printf("Could not encode permlink data: %v\n", err)
		}
	} else {
		var permalinkDecoder *json.Decoder = json.NewDecoder(r.Body)

		err = permalinkDecoder.Decode(&jsonPermalink)
		if err != nil {
			w.Write([]byte(fmt.Sprintf("Error: received invalid JSON: %v", err)))
			return
		}

		err = validatePermalinkJSON(jsonPermalink)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}

		id = bson.NewObjectId()
		jsonPermalink["_id"] = id
		jsonPermalink["lastAccessed"] = bson.Now()

		err = permalinkConn.Insert(jsonPermalink)

		if err == nil {
			id64len := base64.URLEncoding.EncodedLen(len(id))
			id64buf := make([]byte, id64len, id64len)
			base64.URLEncoding.Encode(id64buf, []byte(id))
			w.Write(id64buf)
		} else {
			w.WriteHeader(http.StatusInternalServerError)
			w.Write([]byte(fmt.Sprintf("Could not add permalink to database: %v", err)))
		}
	}
}