示例#1
0
// parseAuthInRules will expect a payload from the inrules channel and
// attempt to pull the SMTP auth credentials out it. If successful, this
// will also create and set the block's auth.
func (e *ToEmail) parseAuthRules(msgI interface{}) error {
	var err error
	e.host, err = util.ParseRequiredString(msgI, "Host")
	if err != nil {
		return err
	}

	e.port, err = util.ParseInt(msgI, "Port")
	if err != nil {
		return err
	}

	e.username, err = util.ParseRequiredString(msgI, "Username")
	if err != nil {
		return err
	}

	e.password, err = util.ParseRequiredString(msgI, "Password")
	if err != nil {
		return err
	}

	return nil
}
示例#2
0
// parseEmailInRules will expect a payload from the inrules channel and
// attempt to pull and set the block's to, from and subject paths from it.
func (e *ToEmail) parseEmailRules(msgI interface{}) error {
	var err error
	e.toPath, err = util.ParseRequiredString(msgI, "ToPath")
	if err != nil {
		return err
	}

	e.fromPath, err = util.ParseRequiredString(msgI, "FromPath")
	if err != nil {
		return err
	}

	e.subjectPath, err = util.ParseString(msgI, "SubjectPath")
	if err != nil {
		return err
	}

	e.msgPath, err = util.ParseString(msgI, "MessagePath")
	if err != nil {
		return err
	}

	return nil
}
示例#3
0
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ToMongoDB) Run() {
	var collectionname string
	var dbname string
	var collection *mgo.Collection
	var session *mgo.Session
	var host = ""
	var err error
	var batch = 0
	var count = 0
	var maxindex = 0
	var list []interface{}
	for {
		select {
		case msgI := <-b.inrule:
			// set host string for MongoDB server
			host, err = util.ParseRequiredString(msgI, "Host")
			if err != nil {
				b.Error(err.Error())
				continue
			}
			// set database name
			dbname, err = util.ParseRequiredString(msgI, "Database")
			if err != nil {
				b.Error(err.Error())
				continue
			}
			// set collection name
			collectionname, err = util.ParseRequiredString(msgI, "Collection")
			if err != nil {
				b.Error(err.Error())
				continue
			}
			// set number of records to insert at a time
			batch, err = util.ParseInt(msgI, "BatchSize")
			if err != nil || batch < 0 {
				b.Error(errors.New("Error parsing batch size....setting to 0"))
				batch = 0
			} else {
				if batch > 1 {
					list = make([]interface{}, batch, batch)
					// set maxindex to 1 minus batch size
					// use maxindex for looping everywhere for consistency
					maxindex = batch - 1
				}
			}
			// create MongoDB connection
			session, err = mgo.Dial(host)
			if err != nil {
				// swallowing a panic from mgo here - streamtools must not die
				b.Error(errors.New("Could not initiate connection with MongoDB service"))
				continue
			}
			// use the specified DB and collection
			collection = session.DB(dbname).C(collectionname)
		case <-b.quit:
			// close connection to MongoDB and quit
			if session != nil {
				session.Close()
			}
			return
		case msg := <-b.in:
			// deal with inbound data
			if session != nil {
				// mgo is so cool - it will check if the message can be serialized to valid bson.
				// So, no need to do a json.Marshal on the inbound. just append to the list and
				// batch insert
				if maxindex >= 1 {
					if count <= maxindex {
						list[count] = msg
						count = count + 1
					}
					if count == maxindex {
						// insert batch if count reaches batch size
						err = collection.Insert(list...)
						if err != nil {
							b.Error(err.Error())
						}
						// reset list and count
						list = make([]interface{}, batch, batch)
						count = 0
					}
				} else {
					// mgo coolness again. No need to do a json.Marshal on the inbound.
					err = collection.Insert(msg)
					if err != nil {
						b.Error(err.Error())
					}
				}
			} else {
				b.Error(errors.New("MongoDB connection not initated or lost. Please check your MongoDB server or block settings."))
			}
		case respChan := <-b.queryrule:
			// deal with a query request
			respChan <- map[string]interface{}{
				"Collection": collectionname,
				"Database":   dbname,
				"Host":       host,
				"BatchSize":  batch,
			}
		}
	}
}