Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 243 additions & 0 deletions pkg/console/handler/router_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package handler

import (
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/apache/dubbo-admin/pkg/common/bizerror"
"github.com/apache/dubbo-admin/pkg/common/constants"
consolectx "github.com/apache/dubbo-admin/pkg/console/context"
"github.com/apache/dubbo-admin/pkg/console/model"
"github.com/apache/dubbo-admin/pkg/console/service"
"github.com/apache/dubbo-admin/pkg/console/util"
meshresource "github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
)

func AffinityRuleSearch(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
req := model.NewSearchConditionRuleReq()
if err := c.ShouldBindQuery(req); err != nil {
util.HandleArgumentError(c, err)
return
}
result, err := service.SearchAffinityRules(ctx, req)
if err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.NewSuccessResp(result))
}
}

func GetAffinityRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.AffinityRuleDotSuffix) {
return
}
name, mesh := c.Param("ruleName"), c.Query("mesh")
res, err := service.GetAffinityRule(ctx, name, mesh)
if err != nil {
util.HandleServiceError(c, err)
return
}
if res == nil {
util.HandleNotFoundError(c, name)
return
}
c.JSON(http.StatusOK, model.GenAffinityRuleResp(res))
}
}

func PostAffinityRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return mutateAffinityRule(ctx, false)
}

func PutAffinityRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return mutateAffinityRule(ctx, true)
}

func mutateAffinityRule(ctx consolectx.Context, update bool) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.AffinityRuleDotSuffix) {
return
}
input := &model.AffinityRuleInput{}
if err := c.ShouldBindJSON(input); err != nil {
util.HandleArgumentError(c, err)
return
}
name, mesh := c.Param("ruleName"), c.Query("mesh")
res := meshresource.NewAffinityRouteResourceWithAttributes(name, mesh)
res.Spec = input.ToProto()
meshresource.SetAffinityRuleOptionalFields(res, input.Runtime, normalizedRuleEnabled(input.Enabled))
if err := meshresource.ValidateRule(res); err != nil {
util.HandleArgumentError(c, err)
return
}
if update {
existing, err := service.GetAffinityRule(ctx, name, mesh)
if err != nil {
util.HandleServiceError(c, err)
return
}
if existing == nil {
util.HandleNotFoundError(c, name)
return
}
if err := service.UpdateAffinityRuleWithOptions(ctx, res, mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
} else if err := service.CreateAffinityRuleWithOptions(ctx, res, mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.GenAffinityRuleResp(res))
}
}

func DeleteAffinityRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.AffinityRuleDotSuffix) {
return
}
if err := service.DeleteAffinityRuleWithOptions(ctx, c.Param("ruleName"), c.Query("mesh"), mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.NewSuccessResp(""))
}
}

func ScriptRuleSearch(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
req := model.NewSearchConditionRuleReq()
if err := c.ShouldBindQuery(req); err != nil {
util.HandleArgumentError(c, err)
return
}
result, err := service.SearchScriptRules(ctx, req)
if err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.NewSuccessResp(result))
}
}

func GetScriptRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.ScriptRuleDotSuffix) {
return
}
name, mesh := c.Param("ruleName"), c.Query("mesh")
res, err := service.GetScriptRule(ctx, name, mesh)
if err != nil {
util.HandleServiceError(c, err)
return
}
if res == nil {
util.HandleNotFoundError(c, name)
return
}
c.JSON(http.StatusOK, model.GenScriptRuleResp(res))
}
}

func PostScriptRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return mutateScriptRule(ctx, false)
}

func PutScriptRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return mutateScriptRule(ctx, true)
}

func mutateScriptRule(ctx consolectx.Context, update bool) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.ScriptRuleDotSuffix) {
return
}
input := &model.ScriptRuleInput{}
if err := c.ShouldBindJSON(input); err != nil {
util.HandleArgumentError(c, err)
return
}
name, mesh := c.Param("ruleName"), c.Query("mesh")
res := meshresource.NewScriptRouteResourceWithAttributes(name, mesh)
res.Spec = input.ToProto()
meshresource.SetScriptRuleOptionalEnabled(res, normalizedRuleEnabled(input.Enabled))
if err := meshresource.ValidateRule(res); err != nil {
util.HandleArgumentError(c, err)
return
}
if update {
existing, err := service.GetScriptRule(ctx, name, mesh)
if err != nil {
util.HandleServiceError(c, err)
return
}
if existing == nil {
util.HandleNotFoundError(c, name)
return
}
if err := service.UpdateScriptRuleWithOptions(ctx, res, mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
} else if err := service.CreateScriptRuleWithOptions(ctx, res, mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.GenScriptRuleResp(res))
}
}

func DeleteScriptRuleWithRuleName(ctx consolectx.Context) gin.HandlerFunc {
return func(c *gin.Context) {
if !validRouterRuleName(c, constants.ScriptRuleDotSuffix) {
return
}
if err := service.DeleteScriptRuleWithOptions(ctx, c.Param("ruleName"), c.Query("mesh"), mutationOptions(c)); err != nil {
util.HandleServiceError(c, err)
return
}
c.JSON(http.StatusOK, model.NewSuccessResp(""))
}
}

func validRouterRuleName(c *gin.Context, suffix string) bool {
if strings.HasSuffix(c.Param("ruleName"), suffix) {
return true
}
err := bizerror.New(bizerror.InvalidArgument, fmt.Sprintf("ruleName must end with %s", suffix))
c.JSON(http.StatusBadRequest, model.NewBizErrorResp(err))
return false
}

func normalizedRuleEnabled(enabled *bool) *bool {
if enabled != nil {
return enabled
}
defaultEnabled := true
return &defaultEnabled
}
71 changes: 71 additions & 0 deletions pkg/console/handler/router_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package handler

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"

"github.com/apache/dubbo-admin/pkg/common/bizerror"
)

func TestPostAffinityRuleRejectsInvalidRuntimeContract(t *testing.T) {
ctx, recorder := testGinContext()
ctx.Request = httptest.NewRequest(http.MethodPost, "/affinity-rule/demo.affinity-router", strings.NewReader(`{
"configVersion":"v3.1", "scope":"application", "key":"demo", "enabled":true,
"runtime":true, "affinityAware":{"key":"region", "ratio":101}
}`))
ctx.Request.Header.Set("Content-Type", "application/json")
ctx.Params = gin.Params{{Key: "ruleName", Value: "demo.affinity-router"}}

PostAffinityRuleWithRuleName(ruleVersionHandlerTestContext{})(ctx)

assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, string(bizerror.InvalidArgument), decodeCommonResp(t, recorder).Code)
}

func TestPostScriptRuleRejectsUnsupportedScriptType(t *testing.T) {
ctx, recorder := testGinContext()
ctx.Request = httptest.NewRequest(http.MethodPost, "/script-rule/demo.script-router", strings.NewReader(`{
"configVersion":"v3.0", "scope":"application", "key":"demo", "enabled":true,
"type":"groovy", "script":"return invokers;"
}`))
ctx.Request.Header.Set("Content-Type", "application/json")
ctx.Params = gin.Params{{Key: "ruleName", Value: "demo.script-router"}}

PostScriptRuleWithRuleName(ruleVersionHandlerTestContext{})(ctx)

assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, string(bizerror.InvalidArgument), decodeCommonResp(t, recorder).Code)
}

func TestRouterRuleHandlersRejectWrongRuleSuffix(t *testing.T) {
ctx, recorder := testGinContext()
ctx.Request = httptest.NewRequest(http.MethodPost, "/script-rule/demo.affinity-router", nil)
ctx.Params = gin.Params{{Key: "ruleName", Value: "demo.affinity-router"}}

PostScriptRuleWithRuleName(ruleVersionHandlerTestContext{})(ctx)

assert.Equal(t, http.StatusBadRequest, recorder.Code)
assert.Equal(t, string(bizerror.InvalidArgument), decodeCommonResp(t, recorder).Code)
}
Loading
Loading