Ejemplo n.º 1
0
Archivo: topics.go Proyecto: ovh/tat
// Create creates a new topic
func (*TopicsController) Create(ctx *gin.Context) {
	var topicIn tat.TopicCreateJSON
	ctx.Bind(&topicIn)

	var user = tat.User{}
	found, err := userDB.FindByUsername(&user, getCtxUsername(ctx))
	if !found {
		ctx.JSON(http.StatusUnauthorized, gin.H{"error": "User unknown"})
		return
	} else if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."})
		return
	}

	var topic tat.Topic
	topic.Topic = topicIn.Topic
	topic.Description = topicIn.Description

	err = topicDB.Insert(&topic, &user)
	if err != nil {
		log.Errorf("Error while InsertTopic %s", err)
		ctx.JSON(tat.Error(err))
		return
	}
	ctx.JSON(http.StatusCreated, topic)
}
Ejemplo n.º 2
0
Archivo: topics.go Proyecto: ovh/tat
// AddRoGroup add a readonly group on selected topic
func (t *TopicsController) AddRoGroup(ctx *gin.Context) {
	var paramJSON tat.ParamTopicGroupJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckGroup(ctx, &paramJSON)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}
	if err := topicDB.AddRoGroup(topic, getCtxUsername(ctx), paramJSON.Groupname, paramJSON.Recursive); err != nil {
		log.Errorf("Error while adding admin read only group: %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusCreated, "")
}
Ejemplo n.º 3
0
Archivo: topics.go Proyecto: ovh/tat
// TruncateLabels clear labels on one topic
func (t *TopicsController) TruncateLabels(ctx *gin.Context) {
	var paramJSON tat.TopicNameJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}

	if err := topicDB.TruncateLabels(topic); err != nil {
		log.Errorf("Error while clear labels on topic %s: %s", topic.Topic, err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while clear labels on topic " + topic.Topic})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("%d labels cleared", len(topic.Labels))})
}
Ejemplo n.º 4
0
Archivo: topics.go Proyecto: ovh/tat
// RemoveRwGroup removes a read write group on selected topic
func (t *TopicsController) RemoveRwGroup(ctx *gin.Context) {
	var paramJSON tat.ParamTopicGroupJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckGroup(ctx, &paramJSON)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}

	err := topicDB.RemoveRwGroup(topic, getCtxUsername(ctx), paramJSON.Groupname, paramJSON.Recursive)
	if err != nil {
		log.Errorf("Error while removing read write group: %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusOK, "")
}
Ejemplo n.º 5
0
Archivo: topics.go Proyecto: ovh/tat
// ComputeTags computes tags on one topic
func (t *TopicsController) ComputeTags(ctx *gin.Context) {
	var paramJSON tat.TopicNameJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}

	nbComputed, err := topicDB.ComputeTags(topic)
	if err != nil {
		log.Errorf("Error while compute tags on topic %s: %s", topic.Topic, err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while compute tags on topic " + topic.Topic})
		return
	}
	ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("%d tags computed", nbComputed)})
}
Ejemplo n.º 6
0
Archivo: topics.go Proyecto: ovh/tat
// AddParameter add a parameter on selected topic
func (t *TopicsController) AddParameter(ctx *gin.Context) {
	var topicParameterBind tat.TopicParameterJSON
	ctx.Bind(&topicParameterBind)
	topic, e := t.preCheckUserAdminOnTopic(ctx, topicParameterBind.Topic)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}

	err := topicDB.AddParameter(topic, getCtxUsername(ctx), topicParameterBind.Key, topicParameterBind.Value, topicParameterBind.Recursive)
	if err != nil {
		log.Errorf("Error while adding parameter: %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusCreated, "")
}
Ejemplo n.º 7
0
Archivo: topics.go Proyecto: ovh/tat
// Truncate deletes all messages in a topic only if user is Tat admin, or admin on topic
func (t *TopicsController) Truncate(ctx *gin.Context) {
	var paramJSON tat.TopicNameJSON
	ctx.Bind(&paramJSON)
	topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic)
	if e != nil {
		ctx.JSON(tat.Error(e))
		return
	}

	nbRemoved, err := topicDB.Truncate(topic)
	if err != nil {
		log.Errorf("Error while truncate topic %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while truncate topic " + topic.Topic})
		return
	}
	// 201 returns
	ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("%d messages removed", nbRemoved)})
}
Ejemplo n.º 8
0
Archivo: topics.go Proyecto: ovh/tat
// SetParam update Topic Parameters : MaxLength, CanForeceDate, CanUpdateMsg, CanDeleteMsg, CanUpdateAllMsg, CanDeleteAllMsg, AdminCanDeleteAllMsg
// admin only, except on Private topic
func (t *TopicsController) SetParam(ctx *gin.Context) {
	var paramsBind paramsJSON
	ctx.Bind(&paramsBind)

	topic := &tat.Topic{}
	var errFind, err error
	if strings.HasPrefix(paramsBind.Topic, "/Private/"+getCtxUsername(ctx)) {
		topic, errFind = topicDB.FindByTopic(paramsBind.Topic, false, false, false, nil)
		if errFind != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topic /Private/" + getCtxUsername(ctx)})
			return
		}
	} else {
		topic, err = t.preCheckUserAdminOnTopic(ctx, paramsBind.Topic)
		if err != nil {
			ctx.JSON(tat.Error(err))
			return
		}
	}

	for _, p := range paramsBind.Parameters {
		if strings.HasPrefix(p.Key, tat.HookTypeKafka) && !isTatAdmin(ctx) {
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Only Tat Admin can use tathook-kafka"})
			return
		}
	}

	err = topicDB.SetParam(topic, getCtxUsername(ctx),
		paramsBind.Recursive,
		paramsBind.MaxLength,
		paramsBind.CanForceDate,
		paramsBind.CanUpdateMsg,
		paramsBind.CanDeleteMsg,
		paramsBind.CanUpdateAllMsg,
		paramsBind.CanDeleteAllMsg,
		paramsBind.AdminCanUpdateAllMsg,
		paramsBind.AdminCanDeleteAllMsg,
		paramsBind.IsAutoComputeTags,
		paramsBind.IsAutoComputeLabels,
		paramsBind.Parameters)

	// add tat2xmpp_username RO or RW on this topic if a key is xmpp
	for _, p := range paramsBind.Parameters {
		if strings.HasPrefix(p.Key, tat.HookTypeXMPPOut) {
			found := false
			for _, u := range topic.ROUsers {
				if u == viper.GetString("tat2xmpp_username") {
					found = true
				}
			}
			for _, u := range topic.RWUsers {
				if u == viper.GetString("tat2xmpp_username") {
					found = true
				}
			}
			if !found {
				if errf := topicDB.AddRoUser(topic, getCtxUsername(ctx), viper.GetString("tat2xmpp_username"), false); errf != nil {
					log.Errorf("Error while adding read only user tat2xmpp_username: %s", errf)
					ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
					return
				}
			}
		} else if strings.HasPrefix(p.Key, tat.HookTypeXMPP) {
			found := false
			for _, u := range topic.RWUsers {
				if u == viper.GetString("tat2xmpp_username") {
					found = true
				}
			}
			if !found {
				if errf := topicDB.AddRwUser(topic, getCtxUsername(ctx), viper.GetString("tat2xmpp_username"), false); errf != nil {
					log.Errorf("Error while adding read write user tat2xmpp_username: %s", errf)
					ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
					return
				}
			}
		}
	}

	if err != nil {
		log.Errorf("Error while setting parameters: %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("Topic %s updated", topic.Topic)})
}