Skip to content

Commit

Permalink
feat: core-api add publish event report api
Browse files Browse the repository at this point in the history
fix: fix param binding
  • Loading branch information
Han-Ya-Jun committed Jul 12, 2023
1 parent f05d342 commit 58cd676
Show file tree
Hide file tree
Showing 18 changed files with 1,035 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.6
github.com/prometheus/client_golang v1.15.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
Expand Down Expand Up @@ -67,7 +68,6 @@ require (
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
Expand Down
62 changes: 62 additions & 0 deletions src/core-api/pkg/api/microgateway/publish_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package microgateway

import (
"core/pkg/service"
"core/pkg/util"

"github.com/gin-gonic/gin"
"github.com/spf13/cast"
)

type reportPublishEventSerializer struct {
Gateway string `json:"gateway" binding:"required" example:"benchmark"`
Stage string `json:"stage" binding:"required" example:"dev"`
Name string `json:"name" binding:"required" example:"generate_release_task"`
Status string `json:"status" binding:"required" example:"success" `
Detail map[string]interface{} `json:"detail"`
}

// ReportPublishEvent report publish event
func ReportPublishEvent(c *gin.Context) {
var query reportPublishEventSerializer
if err := c.ShouldBindJSON(&query); err != nil {
util.BadRequestErrorJSONResponse(c, util.ValidationErrorMessage(err))
return
}
svc := service.NewPublishEventService()
publishID := cast.ToInt64(c.Param("publish_id"))
event := service.Event{
Gateway: query.Gateway,
Stage: query.Stage,
Name: query.Name,
Status: query.Status,
PublishID: publishID,
DetailMap: query.Detail,
}
err := svc.Report(c.Request.Context(), event)
if err != nil {
util.SystemErrorJSONResponse(c, err)
return
}
util.SuccessJSONResponse(c, gin.H{
"result": "report success",
})
}
8 changes: 8 additions & 0 deletions src/core-api/pkg/cacheimpls/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ var (
newRandomDuration(10),
)

releaseHistoryCache = memory.NewCache(
"release_history",
DisableCache,
tracedFuncWrapper("release_history", retrieveReleaseHistory),
1*time.Minute,
newRandomDuration(10),
)

// app_code + gateway_id => permission, may change frequently
appGatewayPermissionCache = memory.NewCache(
"app_gateway_permission",
Expand Down
72 changes: 72 additions & 0 deletions src/core-api/pkg/cacheimpls/release_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package cacheimpls

import (
"context"
"errors"
"strconv"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
)

// ReleaseHistoryCacheKey is the key of jwt public key
type ReleaseHistoryCacheKey struct {
ReleaseID int64
}

// Key return the key string of release history
func (k ReleaseHistoryCacheKey) Key() string {
return strconv.FormatInt(k.ReleaseID, 10)
}

func retrieveReleaseHistory(ctx context.Context, k cache.Key) (interface{}, error) {
key := k.(ReleaseHistoryCacheKey)

manager := dao.NewReleaseHistoryManger()

releaseHistory, err := manager.Get(ctx, key.ReleaseID)
if err != nil {
return "", err
}

return releaseHistory, nil
}

// GetReleaseHistory will get the jwt public key from cache by ReleaseID
func GetReleaseHistory(ctx context.Context, ReleaseID int64) (releaseHistory dao.ReleaseHistory, err error) {
key := ReleaseHistoryCacheKey{
ReleaseID: ReleaseID,
}
var value interface{}
value, err = cacheGet(ctx, releaseHistoryCache, key)
if err != nil {
return
}

var ok bool
releaseHistory, ok = value.(dao.ReleaseHistory)
if !ok {
err = errors.New("not string in cache")
return
}
return
}
65 changes: 65 additions & 0 deletions src/core-api/pkg/cacheimpls/release_history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package cacheimpls

import (
"context"
"errors"
"testing"
"time"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/stretchr/testify/assert"
)

func TestReleaseHistoryCacheKey_Key(t *testing.T) {
k := ReleaseHistoryCacheKey{
ReleaseID: 1,
}
assert.Equal(t, "1", k.Key())
}

func TestGetReleaseHistory(t *testing.T) {
expiration := 5 * time.Minute

// valid
retrieveFunc := func(ctx context.Context, key cache.Key) (interface{}, error) {
return dao.ReleaseHistory{}, nil
}
mockCache := memory.NewCache(
"mockCache", false, retrieveFunc, expiration, nil)
releaseHistoryCache = mockCache

_, err := GetReleaseHistory(context.Background(), 1)
assert.NoError(t, err)

// error
retrieveFunc = func(ctx context.Context, key cache.Key) (interface{}, error) {
return false, errors.New("error here")
}
mockCache = memory.NewCache(
"mockCache", false, retrieveFunc, expiration, nil)
releaseHistoryCache = mockCache

_, err = GetReleaseHistory(context.Background(), 1)
assert.Error(t, err)
}
59 changes: 59 additions & 0 deletions src/core-api/pkg/constant/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* 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.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package constant

const (
// dashboard
EventNameGenerateTask = "generate_release_task"
EventNameDistributeConfiguration = "distribute_configuration"

// operator
EventNameParseConfiguration = "parse_configuration"
EventNameApplyConfiguration = "apply_configuration"

// apisix
EventNameLoadConfiguration = "load_configuration"
)

func GetStep(name string) int {
/**
publish event report chain:
GenerateTask-> DistributeConfiguration-> ParseConfiguration-> ApplyConfiguration-> LoadConfiguration
*/
switch name {
case EventNameGenerateTask:
return 1
case EventNameDistributeConfiguration:
return 2
case EventNameParseConfiguration:
return 3
case EventNameApplyConfiguration:
return 4
case EventNameLoadConfiguration:
return 5
}
return 0
}

const (
EventStatusSuccess = "success" // 执行成功
EventStatusFailure = "failure" // 执行失败
EventStatusPending = "pending" // 待执行
EventStatusDoing = "doing" // 执行中
)
51 changes: 51 additions & 0 deletions src/core-api/pkg/database/dao/mock/publish_event.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions src/core-api/pkg/database/dao/mock/release_history.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 58cd676

Please sign in to comment.