parser

package
v0.0.0-...-cb472e6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BizIDRegex = regexp.MustCompile("bk_biz_id/([0-9]+)")
)
View Source
var ConfigAdminConfigs = []AuthConfig{
	{
		Name:           "findConfigAdmin",
		Description:    "查询配置管理",
		Pattern:        "/api/v3/admin/find/system/config_admin",
		HTTPMethod:     http.MethodGet,
		ResourceType:   meta.ConfigAdmin,
		ResourceAction: meta.Find,
	}, {
		Name:           "updateConfigAdmin",
		Description:    "更新配置管理",
		Pattern:        "/api/v3/admin/update/system/config_admin",
		HTTPMethod:     http.MethodPut,
		ResourceType:   meta.ConfigAdmin,
		ResourceAction: meta.Update,
	},
}
View Source
var HostApplyAuthConfigs = []AuthConfig{
	{
		Name:           "CreateHostApplyRuleRegex",
		Description:    "添加主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/create/host_apply_rule/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.HostApply,
		ResourceAction: meta.Update,
	}, {
		Name:           "UpdateHostApplyRuleRegex",
		Description:    "更新主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/update/host_apply_rule/([0-9]+)/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.HostApply,
		ResourceAction: meta.Update,
	}, {
		Name:           "DeleteHostApplyRuleRegex",
		Description:    "删除主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/deletemany/host_apply_rule/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.HostApply,
		ResourceAction: meta.Update,
	}, {
		Name:           "GetHostApplyRuleRegex",
		Description:    "获取主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/find/host_apply_rule/([0-9]+)/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.MainlineInstanceTopology,
		ResourceAction: meta.SkipAction,
	}, {
		Name:           "ListHostApplyRuleRegex",
		Description:    "列表查询主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/host_apply_rule/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.MainlineInstanceTopology,
		ResourceAction: meta.SkipAction,
	}, {
		Name:           "BatchUpdateOrCreateHostApplyRuleRegex",
		Description:    "批量创建/更新主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/createmany/host_apply_rule/bk_biz_id/([0-9]+)/batch_create_or_update/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.HostApply,
		ResourceAction: meta.Update,
	}, {
		Name:           "PreviewApplyHostApplyRuleRegex",
		Description:    "预览主机属性自动应用",
		Regex:          regexp.MustCompile(`^/api/v3/createmany/host_apply_plan/bk_biz_id/([0-9]+)/preview/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.MainlineInstanceTopology,
		ResourceAction: meta.SkipAction,
	}, {
		Name:           "RunHostApplyRuleRegex",
		Description:    "执行主机属性自动应用",
		Regex:          regexp.MustCompile(`^/api/v3/updatemany/host_apply_plan/bk_biz_id/([0-9]+)/run/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.HostApply,
		ResourceAction: meta.Update,
	}, {
		Name:           "FindHostRelatedHostApplyRuleRegex",
		Description:    "查询主机关联的主机属性自动应用规则",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/host_apply_rule/bk_biz_id/([0-9]+)/host_related_rules/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.MainlineInstanceTopology,
		ResourceAction: meta.SkipAction,
	},
}
View Source
var OperationStatisticAuthConfigs = []AuthConfig{
	{
		Name:           "CreateOperationStatisticRegex",
		Description:    "创建运营统计",
		Regex:          regexp.MustCompile(`^/api/v3/create/operation/chart/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Update,
	},
	{
		Name:           "DeleteOperationStatisticRegex",
		Description:    "删除运营统计",
		Regex:          regexp.MustCompile(`^/api/v3/delete/operation/chart/([0-9]+)/?$`),
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Update,
	},
	{
		Name:           "UpdateOperationStatisticRegex",
		Description:    "更新运营统计",
		Regex:          regexp.MustCompile(`^/api/v3/update/operation/chart/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Update,
	},
	{
		Name:           "SearchOperationStatisticChartRegex",
		Description:    "查看运营统计图表配置",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/operation/chart/?$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Find,
	},
	{
		Name:           "SearchOperationStatisticDataRegex",
		Description:    "查看运营统计数据",
		Regex:          regexp.MustCompile(`^/api/v3/find/operation/chart/data/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Find,
	},
	{
		Name:           "UpdateOperationStatisticPositionRegex",
		Description:    "更新运营统计图表位置",
		Regex:          regexp.MustCompile(`^/api/v3/update/operation/chart/position/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    nil,
		ResourceType:   meta.OperationStatistic,
		ResourceAction: meta.Update,
	},
}

http.MethodPost, "/create/operation/chart" http.MethodDelete, "/delete/operation/chart/{id}" http.MethodPost, "/update/operation/chart" http.MethodGet, "/search/operation/chart" http.MethodPost, "/search/operation/chart/data"

View Source
var ProcessInstanceAuthConfigs = []AuthConfig{
	{
		Name:           "createProcessInstances",
		Description:    "创建进程实例",
		Pattern:        "/api/v3/create/proc/process_instance",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Update,
	}, {
		Name:           "updateProcessInstances",
		Description:    "更新进程实例",
		Pattern:        "/api/v3/update/proc/process_instance",
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Update,
	}, {
		Name:           "updateProcessInstancesByIDs",
		Description:    "根据进程ID批量更新进程实例",
		Pattern:        "/api/v3/update/proc/process_instance/by_ids",
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Update,
	}, {
		Name:           "deleteProcessInstance",
		Description:    "删除进程实例",
		Pattern:        "/api/v3/delete/proc/process_instance",
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Update,
	}, {
		Name:           "listProcessInstances",
		Description:    "查找进程实例",
		Pattern:        "/api/v3/findmany/proc/process_instance",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Find,
	}, {
		Name:           "listProcessInstancesWithHost",
		Description:    "查询业务下的主机和进程信息",
		Pattern:        "/api/v3/findmany/proc/process_instance/with_host",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Find,
	}, {
		Name:           "listProcessInstancesNameIDsInModule",
		Description:    "查询模块下的进程名和对应的进程ID列表",
		Pattern:        "/api/v3/findmany/proc/process_instance/name_ids",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Find,
	}, {
		Name:           "listProcessInstancesDetailsByIDs",
		Description:    "根据进程ID列表批量查询这些进程的详情",
		Pattern:        "/api/v3/findmany/proc/process_instance/detail/by_ids",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   ProcessInstanceIAMResourceType,
		ResourceAction: meta.Find,
	},
}
View Source
var ProcessInstanceIAMResourceType = meta.ProcessServiceInstance
View Source
var ProcessTemplateAuthConfigs = []AuthConfig{
	{
		Name:         "createProcessTemplateBatchPattern",
		Description:  "创建进程模板",
		Pattern:      "/api/v3/createmany/proc/proc_template",
		HTTPMethod:   http.MethodPost,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessTemplate,

		ResourceAction: meta.SkipAction,
	}, {
		Name:         "updateProcessTemplatePattern",
		Description:  "更新进程模板",
		Pattern:      "/api/v3/update/proc/proc_template",
		HTTPMethod:   http.MethodPut,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessTemplate,

		ResourceAction: meta.SkipAction,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			procTemplateID := gjson.GetBytes(request.Body, common.BKProcessTemplateIDField).Int()
			if procTemplateID <= 0 {
				return nil, errors.New("invalid process template id")
			}
			return []int64{procTemplateID}, nil
		},
	}, {
		Name:         "deleteProcessTemplateBatchPattern",
		Description:  "删除进程模板",
		Pattern:      "/api/v3/deletemany/proc/proc_template",
		HTTPMethod:   http.MethodDelete,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessServiceTemplate,

		ResourceAction: meta.SkipAction,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			procTemplateIDs := gjson.GetBytes(request.Body, "process_templates").Array()
			ids := make([]int64, 0)
			for _, procTemplateID := range procTemplateIDs {
				id := procTemplateID.Int()
				if id <= 0 {
					return nil, errors.New("invalid process template id")
				}
				ids = append(ids, id)
			}
			return ids, nil
		},
	}, {
		Name:         "findProcessTemplateBatchPattern",
		Description:  "查找进程模板",
		Pattern:      "/api/v3/findmany/proc/proc_template",
		HTTPMethod:   http.MethodPost,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessTemplate,

		ResourceAction: meta.SkipAction,
	}, {
		Name:         "findProcessTemplateRegexp",
		Description:  "获取进程模板",
		Regex:        regexp.MustCompile(`/api/v3/find/proc/proc_template/id/([0-9]+)/?$`),
		HTTPMethod:   http.MethodPost,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessTemplate,

		ResourceAction: meta.SkipAction,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			subMatch := re.FindStringSubmatch(request.URI)
			for _, subStr := range subMatch {
				if strings.Contains(subStr, "api") {
					continue
				}
				id, err := strconv.ParseInt(subStr, 10, 64)
				if err != nil {
					return nil, fmt.Errorf("parse template id to int64 failed, err: %s", err)
				}
				return []int64{id}, nil
			}
			blog.Errorf("unexpected error: this code shouldn't be reached, rid: %s", request.Rid)
			return nil, errors.New("unexpected error: this code shouldn't be reached")
		},
	},
}

NOTE: 进程模板增删改操作检查服务模板的编辑权限

View Source
var ServiceCategoryAuthConfigs = []AuthConfig{
	{
		Name:           "findmanyServiceCategoryPattern",
		Description:    "list 服务分类",
		Pattern:        "/api/v3/findmany/proc/service_category",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceCategory,
		ResourceAction: meta.Find,
	}, {
		Name:           "findmanyServiceCategoryPattern",
		Description:    "list 服务分类(含引用统计)",
		Pattern:        "/api/v3/findmany/proc/service_category/with_statistics",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceCategory,
		ResourceAction: meta.Find,
	}, {
		Name:           "createServiceCategoryPattern",
		Description:    "创建服务分类",
		Pattern:        "/api/v3/create/proc/service_category",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceCategory,
		ResourceAction: meta.Create,
	}, {
		Name:           "deleteServiceCategoryPattern",
		Description:    "修改服务分类",
		Pattern:        "/api/v3/update/proc/service_category",
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceCategory,
		ResourceAction: meta.Update,
	}, {
		Name:           "deleteServiceCategoryPattern",
		Description:    "删除服务分类",
		Pattern:        "/api/v3/delete/proc/service_category",
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceCategory,
		ResourceAction: meta.Delete,
	},
}

utility.AddHandler(rest.Action{Verb: , Path: , Handler: ps.UpdateServiceCategory})

View Source
var ServiceInstanceAuthConfigs = []AuthConfig{
	{
		Name:           "createServiceInstancePattern",
		Description:    "创建服务实例",
		Pattern:        "/api/v3/create/proc/service_instance",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Create,
	}, {
		Name:           "createServiceInstancePreviewPattern",
		Description:    "创建服务实例预览",
		Pattern:        "/api/v3/create/proc/service_instance/preview",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Create,
	}, {
		Name:           "findServiceInstancePattern",
		Description:    "list 服务实例",
		Pattern:        "/api/v3/findmany/proc/service_instance",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "findServiceInstanceWebPattern",
		Description:    "list 服务实例",
		Pattern:        "/api/v3/findmany/proc/web/service_instance",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "findServiceInstanceDetailsPattern",
		Description:    "list 服务实例详情",
		Pattern:        "/api/v3/findmany/proc/service_instance/details",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "findServiceInstanceByHostPattern",
		Description:    "根据主机服务实例",
		Pattern:        "/api/v3/findmany/proc/service_instance/with_host",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "findServiceInstanceByHostWebPattern",
		Description:    "根据主机服务实例-frontend",
		Pattern:        "/api/v3/findmany/proc/web/service_instance/with_host",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "deleteServiceInstancePattern",
		Description:    "删除服务实例",
		Pattern:        "/api/v3/deletemany/proc/service_instance",
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Delete,
	}, {
		Name:           "diffServiceInstanceWithTemplatePattern",
		Description:    "对比服务实例与模板",
		Pattern:        "/api/v3/find/proc/service_instance/difference",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Find,
	}, {
		Name:           "syncServiceInstanceAccordingToServiceTemplate",
		Description:    "用服务模板更新服务实例",
		Pattern:        "/api/v3/update/proc/service_instance/sync",
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Update,
	}, {
		Name:           "listServiceInstanceWithHostPattern",
		Description:    "根据主机查询服务实例",
		Pattern:        "/api/v3/findmany/proc/service_instance/with_host",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "aggregationServiceInstanceLabels",
		Description:    "聚合服务实例labels",
		Pattern:        "/api/v3/findmany/proc/service_instance/labels/aggregation",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "addServiceInstanceLabelsPattern",
		Description:    "服务实例添加label",
		Pattern:        "/api/v3/createmany/proc/service_instance/labels",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Find,
	}, {
		Name:           "removeServiceInstanceLabelsPattern",
		Description:    "服务实例删除label",
		Pattern:        "/api/v3/deletemany/proc/service_instance/labels",
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Find,
	}, {
		Name:           "deleteProcessInstanceInServiceInstanceRegexp",
		Description:    "删除进程实例",
		Regex:          regexp.MustCompile(`/api/v3/delete/proc/service_instance/[0-9]+/process/?$`),
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceInstance,
		ResourceAction: meta.Delete,
	}, {
		Name:         "deleteServiceInstancePreviewRegexp",
		Description:  "删除服务实例预览",
		Regex:        regexp.MustCompile(`/api/v3/deletemany/proc/service_instance/preview/?$`),
		HTTPMethod:   http.MethodPost,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessServiceInstance,

		ResourceAction: meta.SkipAction,
	},
}
View Source
var ServiceTemplateAuthConfigs = []AuthConfig{
	{
		Name:           "createServiceTemplatePattern",
		Description:    "创建服务模板",
		Pattern:        "/api/v3/create/proc/service_template",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.Create,
	}, {
		Name:           "updateServiceTemplate",
		Description:    "更新服务模板",
		Pattern:        "/api/v3/update/proc/service_template",
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.Update,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			templateID := gjson.GetBytes(request.Body, common.BKFieldID).Int()
			if templateID <= 0 {
				return nil, errors.New("invalid service template")
			}
			return []int64{templateID}, nil
		},
	}, {
		Name:           "getServiceTemplate",
		Description:    "获取服务模板",
		Regex:          regexp.MustCompile(`^/api/v3/find/proc/service_template/([0-9]+)$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.Find,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			subMatch := re.FindStringSubmatch(request.URI)
			for _, subStr := range subMatch {
				if strings.Contains(subStr, "api") {
					continue
				}
				id, err := strconv.ParseInt(subStr, 10, 64)
				if err != nil {
					return nil, fmt.Errorf("parse template id to int64 failed, err: %s", err)
				}
				return []int64{id}, nil
			}
			blog.Errorf("unexpected error: this code shouldn't be reached, rid: %s", request.Rid)
			return nil, errors.New("unexpected error: this code shouldn't be reached")
		},
	}, {
		Name:           "getServiceTemplateDetail",
		Description:    "获取服务模板详情",
		Regex:          regexp.MustCompile(`^/api/v3/find/proc/service_template/([0-9]+)/detail$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.Find,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			subMatch := re.FindStringSubmatch(request.URI)
			for _, subStr := range subMatch {
				if strings.Contains(subStr, "api") {
					continue
				}
				id, err := strconv.ParseInt(subStr, 10, 64)
				if err != nil {
					return nil, fmt.Errorf("parse template id to int64 failed, err: %s", err)
				}
				return []int64{id}, nil
			}
			blog.Errorf("unexpected error: this code shouldn't be reached, rid: %s", request.Rid)
			return nil, errors.New("unexpected error: this code shouldn't be reached")
		},
	}, {
		Name:           "listServiceTemplatePattern",
		Description:    "查询服务模板",
		Pattern:        "/api/v3/findmany/proc/service_template",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "listServiceTemplateDetailPattern",
		Description:    "查询服务模板详情",
		Pattern:        "/api/v3/findmany/proc/service_template/with_detail",
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:         "unbindServiceTemplateOnModule",
		Description:  "解绑模块的服务模板",
		Pattern:      "/api/v3/delete/proc/template_binding_on_module",
		HTTPMethod:   http.MethodDelete,
		BizIDGetter:  DefaultBizIDGetter,
		ResourceType: meta.ProcessServiceTemplate,

		ResourceAction: meta.SkipAction,
	}, {
		Name:           "deleteServiceTemplatePattern",
		Description:    "删除服务模板",
		Pattern:        "/api/v3/delete/proc/service_template",
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    DefaultBizIDGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.Delete,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			templateID := gjson.GetBytes(request.Body, common.BKServiceTemplateIDField).Int()
			if templateID <= 0 {
				return nil, errors.New("invalid service template")
			}
			return []int64{templateID}, nil
		},
	},
}
View Source
var SetTemplateAuthConfigs = []AuthConfig{
	{
		Name:           "CreateSetTemplateRegex",
		Description:    "创建集群模板",
		Regex:          regexp.MustCompile(`^/api/v3/create/topo/set_template/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.Create,
	}, {
		Name:           "UpdateSetTemplateRegex",
		Description:    "更新集群模板",
		Regex:          regexp.MustCompile(`^/api/v3/update/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPut,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.Update,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			ss := re.FindStringSubmatch(request.URI)
			if len(ss) < 2 {
				return nil, errors.New("UpdateSetTemplateRegex regex doesn't match")
			}
			id, err := strconv.ParseInt(ss[1], 10, 64)
			if err != nil {
				return nil, fmt.Errorf("UpdateSetTemplateRegex regex parse match to int failed, err: %+v", err)
			}
			return []int64{id}, nil
		},
	}, {
		Name:           "DeleteSetTemplateRegex",
		Description:    "删除集群模板",
		Regex:          regexp.MustCompile(`^/api/v3/deletemany/topo/set_template/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodDelete,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.Delete,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			data := &struct {
				SetTemplateIDs []int64 `json:"set_template_ids" mapstructure:"set_template_ids"`
			}{}
			if err := json.Unmarshal(request.Body, data); err != nil {
				return nil, fmt.Errorf("unmarshal failed, err: %+v", err)
			}
			return data.SetTemplateIDs, nil
		},
	}, {
		Name:           "GetSetTemplateRegex",
		Description:    "获取集群模板",
		Regex:          regexp.MustCompile(`^/api/v3/find/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.Find,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			ss := re.FindStringSubmatch(request.URI)
			if len(ss) < 2 {
				return nil, errors.New("getSetTemplate regex match nothing")
			}
			id, err := strconv.ParseInt(ss[1], 10, 64)
			if err != nil {
				return nil, fmt.Errorf("getSetTemplate regex parse match to int failed, err: %+v", err)
			}
			return []int64{id}, nil
		},
	}, {
		Name:           "ListSetTemplateRegex",
		Description:    "列表查询集群模板",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "ListSetTemplateWebRegex",
		Description:    "列表查询集群模板-Web",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/bk_biz_id/([0-9]+)/web/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.SetTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "ListSetTplRelatedSvcTplRegex",
		Description:    "查询集群模板关联的服务模板列表",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/service_templates/?$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "ListSetTplRelatedSvcTplRegex",
		Description:    "查询集群模板关联的服务模板列表-Web",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/service_templates/with_statistics/?$`),
		HTTPMethod:     http.MethodGet,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "ListSetTplRelatedSetsWebRegex",
		Description:    "查询集群模板关联的集群列表-Web",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/sets/web/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ProcessServiceTemplate,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "DiffSetTplWithInstRegex",
		Description:    "对比集群模板与集群之间的差异",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/diff_with_instances/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ModelSet,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "SyncSetTplToInstRegex",
		Description:    "用集群模板同步集群",
		Regex:          regexp.MustCompile(`^/api/v3/updatemany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/sync_to_instances/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ModelSet,
		ResourceAction: meta.UpdateMany,
		InstanceIDGetter: func(request *RequestContext, re *regexp.Regexp) (int64s []int64, e error) {
			data := &struct {
				SetIDs []int64 `json:"bk_set_ids" mapstructure:"bk_set_ids"`
			}{}
			if err := json.Unmarshal(request.Body, data); err != nil {
				return nil, fmt.Errorf("unmarshal failed, err: %+v", err)
			}
			return data.SetIDs, nil
		},
	}, {
		Name:           "GetSetSyncStatusRegex",
		Description:    "获取集群同步状态",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/instances_sync_status/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ModelSet,
		ResourceAction: meta.FindMany,
	}, {
		Name:           "ListSetTemplateSyncStatusRegex",
		Description:    "获取集群同步状态",
		Regex:          regexp.MustCompile(`^/api/v3/findmany/topo/set_template_sync_status/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:     http.MethodPost,
		BizIDGetter:    BizIDFromURLGetter,
		ResourceType:   meta.ModelSet,
		ResourceAction: meta.FindMany,
	}, {
		Name:             "ListSetTemplateSyncHistoryRegex",
		Description:      "集群模板的同步历史记录",
		Regex:            regexp.MustCompile(`^/api/v3/findmany/topo/set_template_sync_history/bk_biz_id/([0-9]+)/?$`),
		HTTPMethod:       http.MethodPost,
		BizIDGetter:      BizIDFromURLGetter,
		ResourceType:     meta.ModelSet,
		ResourceAction:   meta.FindMany,
		InstanceIDGetter: nil,
	}, {
		Name:             "CheckSetInstUpdateToDateStatusRegex",
		Description:      "检查集群模板的同步状态",
		Regex:            regexp.MustCompile(`^/api/v3/findmany/topo/set_template/([0-9]+)/bk_biz_id/([0-9]+)/set_template_status/?$`),
		HTTPMethod:       http.MethodGet,
		BizIDGetter:      BizIDFromURLGetter,
		ResourceType:     meta.ModelSet,
		ResourceAction:   meta.FindMany,
		InstanceIDGetter: nil,
	}, {
		Name:             "BatchCheckSetInstUpdateToDateStatusRegex",
		Description:      "批量检查集群模板的同步状态",
		Regex:            regexp.MustCompile(`^/api/v3/findmany/topo/set_template/bk_biz_id/([0-9]+)/set_template_status/?$`),
		HTTPMethod:       http.MethodPost,
		BizIDGetter:      BizIDFromURLGetter,
		ResourceType:     meta.ModelSet,
		ResourceAction:   meta.FindMany,
		InstanceIDGetter: nil,
	},
}

Functions

func BizIDFromURLGetter

func BizIDFromURLGetter(request *RequestContext, config AuthConfig) (bizID int64, err error)

func DefaultBizIDGetter

func DefaultBizIDGetter(request *RequestContext, config AuthConfig) (bizID int64, err error)

func MatchAndGenerateIAMResource

func MatchAndGenerateIAMResource(authConfigs []AuthConfig, request *RequestContext) ([]meta.ResourceAttribute, error)

func ParseAttribute

func ParseAttribute(req *restful.Request, engine *backbone.Engine) (*meta.AuthAttribute, error)

func ParseCommonInfo

func ParseCommonInfo(requestHeader *http.Header) (*meta.CommonInfo, error)

ParseCommonInfo get common info from req, aims at avoiding too much repeat code

func ParseStreamWithFramework

func ParseStreamWithFramework(ps *parseStream, authConfigs []AuthConfig) *parseStream

func ParseUserInfo

func ParseUserInfo(requestHeader *http.Header) (*meta.UserInfo, error)

Types

type AuthConfig

type AuthConfig struct {
	Name             string
	Pattern          string
	Regex            *regexp.Regexp
	HTTPMethod       string
	ResourceType     meta.ResourceType
	ResourceAction   meta.Action
	InstanceIDGetter InstanceIDGetter
	BizIDGetter      BizIDGetter
	Description      string
}

func (*AuthConfig) Match

func (config *AuthConfig) Match(request *RequestContext) bool

type BizIDGetter

type BizIDGetter func(request *RequestContext, config AuthConfig) (bizID int64, err error)

type InstanceIDGetter

type InstanceIDGetter func(request *RequestContext, re *regexp.Regexp) ([]int64, error)

type RequestContext

type RequestContext struct {
	Rid string
	// http header
	Header http.Header
	// http method
	Method string
	// request's url path
	URI string
	// elements parsed from url, started with api field
	// 0: api field
	// 1: version field
	// 2: action field
	// >=3: resource fields
	Elements []string
	// http request body contents.
	Body []byte
	// BizID is business ID
	BizID int64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL