コード例 #1
0
ファイル: itemcollect.go プロジェクト: ARodri/automi
func (c *ItemCollector) Init(ctx context.Context) error {
	// extract logger
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "ItemCollector")
		log.Error("Logger not set for component")
	}

	c.log = log.WithFields(logrus.Fields{
		"Component": c.Name,
		"Type":      fmt.Sprintf("%T", c),
	})

	if c.Name == "" {
		return api.ProcError{Err: fmt.Errorf("Missing Name attribute")}
	}

	if c.inputs == nil {
		return api.ProcError{
			ProcName: c.Name,
			Err:      fmt.Errorf("Missing Inputs attribute"),
		}
	}

	c.output = make(chan interface{})
	c.log.Info("Component initialized")
	return nil
}
コード例 #2
0
ファイル: probe.go プロジェクト: ARodri/automi
func (p *Probe) Init(ctx context.Context) error {
	// validation
	if p.Name == "" {
		return fmt.Errorf("Missing name identifier")
	}

	if p.input == nil {
		return fmt.Errorf("Probe [%s] input not set", p.Name)
	}

	p.output = make(chan interface{})

	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("ProcName", p.Name)
		log.Errorf("No valid logger set for %s", p.Name)
	}

	p.log = log.WithFields(logrus.Fields{
		"Component": p.Name,
		"Type":      fmt.Sprintf("%T", p),
	})
	p.log.Info("Component initialized")

	return nil
}
コード例 #3
0
ファイル: httpreq.go プロジェクト: ARodri/automi
func (req *Req) Init(ctx context.Context) error {
	// extract logger
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "Req (http)")
		log.Error("Logger not found in context, using default")
	}

	req.log = log.WithFields(logrus.Fields{
		"Component": req.Name,
		"Type":      fmt.Sprintf("%T", req),
	})

	// validation
	if req.Name == "" {
		return fmt.Errorf("Http.Req missing Name attribute")
	}

	if req.input == nil {
		return api.ProcError{
			ProcName: req.GetName(),
			Err:      fmt.Errorf("Missing Input attribute"),
		}
	}

	if req.Url == "" {
		return api.ProcError{
			ProcName: req.Name,
			Err:      fmt.Errorf("Missing Url attribute"),
		}
	}
	if req.Prepare == nil {
		return api.ProcError{
			ProcName: req.Name,
			Err:      fmt.Errorf("Missing Prepare function"),
		}
	}
	if req.Handle == nil {
		return api.ProcError{
			ProcName: req.Name,
			Err:      fmt.Errorf("Missing Handle function"),
		}
	}

	// setup http client
	if u, err := url.Parse(req.Url); err != nil {
		return fmt.Errorf("req [%s] unable to parse URL %s: %s", req.Url, err)
	} else {
		req.urlVal = u
	}
	req.client = &http.Client{Transport: http.DefaultTransport}

	req.output = make(chan interface{})

	req.log.Info("Component initialized: URL = ", req.Url)

	return nil
}
コード例 #4
0
ファイル: noop.go プロジェクト: ARodri/automi
func (n *NoopProc) Init(ctx context.Context) error {
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "Endpoint")
		log.Error("Logger not found in context")
	}

	n.log = log.WithFields(logrus.Fields{
		"Component": n.Name,
		"Type":      fmt.Sprintf("%T", n),
	})

	if n.input == nil {
		return api.ProcError{Err: fmt.Errorf("Input attribute not set")}
	}

	n.log.Info("Component initialized")

	return nil
}
コード例 #5
0
ファイル: endpoint.go プロジェクト: ARodri/automi
func (p *Endpoint) Init(ctx context.Context) error {
	// extract logger
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "Endpoint")
		log.Error("Logger not found in context")
	}

	p.log = log.WithFields(logrus.Fields{
		"Component": p.Name,
		"Type":      fmt.Sprintf("%T", p),
	})

	if p.Name == "" {
		return api.ProcError{Err: fmt.Errorf("Name attribute is required")}
	}
	if p.input == nil {
		return api.ProcError{
			ProcName: p.Name,
			Err:      fmt.Errorf("Input attribute required"),
		}
	}

	if p.Function == nil {
		return api.ProcError{
			ProcName: p.Name,
			Err:      fmt.Errorf("Function attribute must be provided"),
		}
	}

	if p.Concurrency == 0 {
		p.Concurrency = 1
	}

	p.done = make(chan struct{})
	p.log.Info("Component initialized")
	return nil
}
コード例 #6
0
ファイル: csvwrite.go プロジェクト: ARodri/automi
func (c *CsvWrite) Init(ctx context.Context) error {
	//extract log entry
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "CsvWrite")
		log.Error("No logger found in context")
	}

	c.log = log.WithFields(logrus.Fields{
		"Component": c.Name,
		"Type":      fmt.Sprintf("%T", c),
	})

	// validation
	if c.Name == "" {
		return fmt.Errorf("CsvWrite missing name attribute")
	}
	if c.FilePath == "" {
		return api.ProcError{
			ProcName: c.GetName(),
			Err:      fmt.Errorf("Missing required FilePath attribute"),
		}
	}

	if c.input == nil {
		return api.ProcError{
			ProcName: c.GetName(),
			Err:      fmt.Errorf("Input attribute not set"),
		}
	}

	// establish defaults
	if c.DelimiterChar == 0 {
		c.DelimiterChar = ','
	}

	// open file
	file, err := os.Create(c.FilePath)
	if err != nil {
		return api.ProcError{
			ProcName: c.GetName(),
			Err:      fmt.Errorf("Failed to create file: %s ", c.Name, err),
		}
	}

	c.file = file
	c.writer = csv.NewWriter(file)
	c.writer.Comma = c.DelimiterChar

	// write headers
	if len(c.Headers) > 0 {
		if err := c.writer.Write(c.Headers); err != nil {
			return err
		}
		c.log.Debug("Headers [", c.Headers, "]")
	}

	c.done = make(chan struct{})
	c.log.Info("Component initialized OK: writing to file ", c.file.Name())

	return nil
}
コード例 #7
0
ファイル: csvread.go プロジェクト: ARodri/automi
func (c *CsvRead) Init(ctx context.Context) error {
	// extract logger
	log, ok := autoctx.GetLogEntry(ctx)
	if !ok {
		log = logrus.WithField("Proc", "CsvRead")
		log.Error("No logger found incontext")
	}
	c.log = log.WithFields(logrus.Fields{
		"Component": c.Name,
		"Type":      fmt.Sprintf("%T", c),
	})

	// validation
	if c.Name == "" {
		return api.ProcError{
			Err: fmt.Errorf("CsvRead missing an identifying name"),
		}
	}

	if c.FilePath == "" {
		return api.ProcError{
			ProcName: c.Name,
			Err:      fmt.Errorf("Missing required FilePath attribute"),
		}
	}

	// establish defaults
	if c.DelimiterChar == 0 {
		c.DelimiterChar = ','
	}

	if c.CommentChar == 0 {
		c.CommentChar = '#'
	}

	// open file
	file, err := os.Open(c.FilePath)
	if err != nil {
		return api.ProcError{
			ProcName: c.Name,
			Err:      fmt.Errorf("Failed to open file: %s ", err),
		}
	}

	c.file = file
	c.reader = csv.NewReader(file)
	c.reader.Comment = c.CommentChar
	c.reader.Comma = c.DelimiterChar

	// resolve header and field count
	if c.HasHeaderRow {
		if headers, err := c.reader.Read(); err == nil {
			c.FieldCount = len(headers)
			c.Headers = headers
		} else {
			return api.ProcError{
				ProcName: c.Name,
				Err:      fmt.Errorf("Unable to read header row: %s", err),
			}
		}
	} else {
		if c.Headers != nil {
			c.FieldCount = len(c.Headers)
		}
	}
	log.Debug("HasHeaderRow ", c.HasHeaderRow, " Headers [", c.Headers, "]")

	// init channel
	c.output = make(chan interface{})

	c.log.Info("Component initiated OK: reading from file ", c.file.Name())

	return nil
}