func ApplicationConfigOperatorLogPut()

in pkg/admin/handler/application.go [138:213]


func ApplicationConfigOperatorLogPut(rt core_runtime.Runtime) gin.HandlerFunc {
	return func(c *gin.Context) {
		var (
			ApplicationName string
			OperatorLog     bool
			isNotExist      = false
		)
		ApplicationName = c.Query("appName")
		OperatorLog, err := strconv.ParseBool(c.Query("operatorLog"))
		if err != nil {
			c.JSON(http.StatusBadRequest, model.NewErrorResp(err.Error()))
			return
		}
		res, err := service.GetConfigurator(rt, ApplicationName)
		if err != nil {
			if core_store.IsResourceNotFound(err) {
				// for check app exist
				data, err := service.GetApplicationDetail(rt, &model.ApplicationDetailReq{AppName: ApplicationName})
				if err != nil || data == nil {
					c.JSON(http.StatusNotFound, model.NewErrorResp(err.Error()))
					return
				}
				res = generateDefaultConfigurator(ApplicationName, consts.ScopeApplication, consts.ConfiguratorVersionV3, true)
				isNotExist = true
			} else {
				c.JSON(http.StatusInternalServerError, model.NewErrorResp(err.Error()))
				return
			}
		}
		// append or remove
		if OperatorLog {
			// check is already exist
			alreadyExist := false
			res.Spec.RangeConfig(func(conf *mesh_proto.OverrideConfig) (isStop bool) {
				alreadyExist = isAppOperatorLogOpened(conf, ApplicationName)
				return alreadyExist
			})
			if alreadyExist {
				c.JSON(http.StatusOK, model.NewSuccessResp(nil))
				return
			}
			if res.Spec.Configs == nil {
				res.Spec.Configs = make([]*mesh_proto.OverrideConfig, 0)
			}
			res.Spec.Configs = append(res.Spec.Configs, &mesh_proto.OverrideConfig{
				Side:          consts.SideProvider,
				Parameters:    map[string]string{`accesslog`: `true`},
				Enabled:       true,
				Match:         &mesh_proto.ConditionMatch{Application: &mesh_proto.ListStringMatch{Oneof: []*mesh_proto.StringMatch{{Exact: ApplicationName}}}},
				XGenerateByCp: true,
			})
		} else {
			res.Spec.RangeConfigsToRemove(func(conf *mesh_proto.OverrideConfig) (IsRemove bool) {
				if conf == nil {
					return true
				}
				return isAppOperatorLogOpened(conf, ApplicationName)
			})
		}
		// restore
		if isNotExist {
			err = service.CreateConfigurator(rt, ApplicationName, res)
			if err != nil {
				c.JSON(http.StatusInternalServerError, model.NewErrorResp(err.Error()))
				return
			}
		} else {
			err = service.UpdateConfigurator(rt, ApplicationName, res)
			if err != nil {
				c.JSON(http.StatusInternalServerError, model.NewErrorResp(err.Error()))
				return
			}
		}
		c.JSON(http.StatusOK, model.NewSuccessResp(nil))
	}
}