-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: core-api add publish event report api
fix: fix param binding
- Loading branch information
1 parent
f05d342
commit 58cd676
Showing
18 changed files
with
1,035 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" // 执行中 | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.