示例#1
0
func AddCollector(cmd *m.AddCollectorCommand) error {

	return inTransaction(func(sess *xorm.Session) error {
		l := &m.Collector{
			OrgId:         cmd.OrgId,
			Name:          cmd.Name,
			Public:        cmd.Public,
			Latitude:      cmd.Latitude,
			Longitude:     cmd.Longitude,
			Created:       time.Now(),
			Updated:       time.Now(),
			Online:        cmd.Online,
			OnlineChange:  time.Now(),
			Enabled:       cmd.Enabled,
			EnabledChange: time.Now(),
		}
		l.UpdateCollectorSlug()
		sess.UseBool("public")
		sess.UseBool("online")
		sess.UseBool("enabled")
		if _, err := sess.Insert(l); err != nil {
			return err
		}
		collectorTags := make([]m.CollectorTag, 0, len(cmd.Tags))
		for _, tag := range cmd.Tags {
			collectorTags = append(collectorTags, m.CollectorTag{
				OrgId:       cmd.OrgId,
				CollectorId: l.Id,
				Tag:         tag,
			})
		}
		if len(collectorTags) > 0 {
			sess.Table("collector_tag")
			if _, err := sess.Insert(&collectorTags); err != nil {
				return err
			}
		}

		cmd.Result = &m.CollectorDTO{
			Id:            l.Id,
			OrgId:         l.OrgId,
			Name:          l.Name,
			Slug:          l.Slug,
			Tags:          cmd.Tags,
			Latitude:      l.Latitude,
			Longitude:     l.Longitude,
			Public:        l.Public,
			Online:        l.Online,
			OnlineChange:  l.OnlineChange,
			Enabled:       l.Enabled,
			EnabledChange: l.EnabledChange,
		}
		return nil
	})
}
示例#2
0
func AddCollector(c *middleware.Context, cmd m.AddCollectorCommand) Response {
	cmd.OrgId = c.OrgId
	if cmd.Name == "" {
		return ApiError(400, "Collector Name not set.", nil)
	}

	if cmd.Public {
		if !c.IsGrafanaAdmin {
			return ApiError(400, "Only admins can make public collectors", nil)
		}
	}
	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to add collector", err)
	}

	return Json(200, cmd.Result)
}