Skip to content

Commit

Permalink
feature: bscp support template manager. issue TencentBlueKing#454
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmgxxxx committed May 8, 2020
1 parent 6a3c6e8 commit d7d4483
Show file tree
Hide file tree
Showing 134 changed files with 40,921 additions and 7,237 deletions.
117 changes: 117 additions & 0 deletions bmsf-configuration/cmd/bscp-accessserver/actions/commit/preview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.,
* Copyright (C) 2019 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.
*/

package commit

import (
"bk-bscp/pkg/common"
"context"
"errors"
"fmt"

"github.com/spf13/viper"

pb "bk-bscp/internal/protocol/accessserver"
pbbusinessserver "bk-bscp/internal/protocol/businessserver"
pbcommon "bk-bscp/internal/protocol/common"
"bk-bscp/pkg/logger"
)

// PreviewAction preview target commit object.
type PreviewAction struct {
viper *viper.Viper
buSvrCli pbbusinessserver.BusinessClient

req *pb.PreviewCommitReq
resp *pb.PreviewCommitResp
}

// NewPreviewAction creates new PreviewAction.
func NewPreviewAction(viper *viper.Viper, buSvrCli pbbusinessserver.BusinessClient,
req *pb.PreviewCommitReq, resp *pb.PreviewCommitResp) *PreviewAction {
action := &PreviewAction{viper: viper, buSvrCli: buSvrCli, req: req, resp: resp}

action.resp.Seq = req.Seq
action.resp.ErrCode = pbcommon.ErrCode_E_OK
action.resp.ErrMsg = "OK"

return action
}

// Err setup error code message in response and return the error.
func (act *PreviewAction) Err(errCode pbcommon.ErrCode, errMsg string) error {
act.resp.ErrCode = errCode
act.resp.ErrMsg = errMsg
return errors.New(errMsg)
}

// Input handles the input messages.
func (act *PreviewAction) Input() error {
if err := act.verify(); err != nil {
return act.Err(pbcommon.ErrCode_E_AS_PARAMS_INVALID, err.Error())
}
return nil
}

// Output handles the output messages.
func (act *PreviewAction) Output() error {
// do nothing.
return nil
}

func (act *PreviewAction) verify() error {
if err := common.VerifyID(act.req.Bid, "bid"); err != nil {
return err
}

if err := common.VerifyID(act.req.Commitid, "commitid"); err != nil {
return err
}

if err := common.VerifyNormalName(act.req.Operator, "operator"); err != nil {
return err
}

return nil
}

func (act *PreviewAction) preview() (pbcommon.ErrCode, string) {
r := &pbbusinessserver.PreviewCommitReq{
Seq: act.req.Seq,
Bid: act.req.Bid,
Commitid: act.req.Commitid,
Operator: act.req.Operator,
}

ctx, cancel := context.WithTimeout(context.Background(), act.viper.GetDuration("businessserver.calltimeout"))
defer cancel()

logger.V(2).Infof("PreviewCommit[%d]| request to businessserver PreviewCommit, %+v", act.req.Seq, r)

resp, err := act.buSvrCli.PreviewCommit(ctx, r)
if err != nil {
return pbcommon.ErrCode_E_AS_SYSTEM_UNKONW, fmt.Sprintf("request to businessserver PreviewCommit, %+v", err)
}

act.resp.Cfgslist = resp.Cfgslist

return resp.ErrCode, resp.ErrMsg
}

// Do makes the workflows of this action base on input messages.
func (act *PreviewAction) Do() error {
// preivew commit.
if errCode, errMsg := act.preview(); errCode != pbcommon.ErrCode_E_OK {
return act.Err(errCode, errMsg)
}
return nil
}
144 changes: 144 additions & 0 deletions bmsf-configuration/cmd/bscp-accessserver/actions/template/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.,
* Copyright (C) 2019 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.
*/

package template

import (
"context"
"errors"
"fmt"

"github.com/spf13/viper"

pb "bk-bscp/internal/protocol/accessserver"
pbcommon "bk-bscp/internal/protocol/common"
pbtemplateserver "bk-bscp/internal/protocol/templateserver"
"bk-bscp/pkg/common"
"bk-bscp/pkg/logger"
)

// CreateAction create a template object
type CreateAction struct {
viper *viper.Viper
templateClient pbtemplateserver.TemplateClient

req *pb.CreateConfigTemplateReq
resp *pb.CreateConfigTemplateResp
}

// NewCreateAction creates new CreateAction
func NewCreateAction(viper *viper.Viper, templateClient pbtemplateserver.TemplateClient,
req *pb.CreateConfigTemplateReq, resp *pb.CreateConfigTemplateResp) *CreateAction {
action := &CreateAction{viper: viper, templateClient: templateClient, req: req, resp: resp}

action.resp.Seq = req.Seq
action.resp.ErrCode = pbcommon.ErrCode_E_OK
action.resp.ErrMsg = "OK"

return action
}

// Err setup error code message in response and return the error.
func (act *CreateAction) Err(errCode pbcommon.ErrCode, errMsg string) error {
act.resp.ErrCode = errCode
act.resp.ErrMsg = errMsg
return errors.New(errMsg)
}

// Input handles the input messages.
func (act *CreateAction) Input() error {
if err := act.verify(); err != nil {
return act.Err(pbcommon.ErrCode_E_AS_PARAMS_INVALID, err.Error())
}
return nil
}

// Output handles the output messages.
func (act *CreateAction) Output() error {
// do nothing.
return nil
}

func (act *CreateAction) verify() error {
if err := common.VerifyID(act.req.Bid, "bid"); err != nil {
return err
}

if err := common.VerifyID(act.req.Setid, "setid"); err != nil {
return err
}

if err := common.VerifyNormalName(act.req.Name, "name"); err != nil {
return err
}

if err := common.VerifyMemo(act.req.Memo); err != nil {
return err
}

if err := common.VerifyFileUser(act.req.User); err != nil {
return err
}

if err := common.VerifyFileUserGroup(act.req.Group); err != nil {
return err
}

if err := common.VerifyFileEncoding(act.req.FileEncoding); err != nil {
return err
}
return nil
}

func (act *CreateAction) createTemplate() (pbcommon.ErrCode, string) {
ctx, cancel := context.WithTimeout(context.Background(), act.viper.GetDuration("templateserver.calltimeout"))
defer cancel()

req := &pbtemplateserver.CreateConfigTemplateReq{
Seq: act.req.Seq,
Bid: act.req.Bid,
Setid: act.req.Setid,
Name: act.req.Name,
Memo: act.req.Memo,
User: act.req.User,
Group: act.req.Group,
Permission: act.req.Permission,
FileEncoding: act.req.FileEncoding,
EngineType: act.req.EngineType,
Creator: act.req.Creator,
}

logger.V(2).Infof("CreateConfigTemplate[%d]| request to templateserver CreateConfigTemplate, %+v", req.Seq, req)

resp, err := act.templateClient.CreateConfigTemplate(ctx, req)
if err != nil {
return pbcommon.ErrCode_E_AS_SYSTEM_UNKONW, fmt.Sprintf("request to templateserver CreateConfigTemplate, %+v", err)
}

act.resp.Templateid = resp.Templateid

if resp.ErrCode != pbcommon.ErrCode_E_OK {
return resp.ErrCode, resp.ErrMsg
}

return pbcommon.ErrCode_E_OK, "OK"
}

// Do do action
func (act *CreateAction) Do() error {

// create config template
if errCode, errMsg := act.createTemplate(); errCode != pbcommon.ErrCode_E_OK {
return act.Err(errCode, errMsg)
}
return nil
}
113 changes: 113 additions & 0 deletions bmsf-configuration/cmd/bscp-accessserver/actions/template/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.,
* Copyright (C) 2019 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.
*/

package template

import (
"context"
"errors"
"fmt"

"github.com/spf13/viper"

pb "bk-bscp/internal/protocol/accessserver"
pbcommon "bk-bscp/internal/protocol/common"
pbtemplateserver "bk-bscp/internal/protocol/templateserver"
"bk-bscp/pkg/common"
"bk-bscp/pkg/logger"
)

// DeleteAction delete a config template object
type DeleteAction struct {
viper *viper.Viper
templateClient pbtemplateserver.TemplateClient
req *pb.DeleteConfigTemplateReq
resp *pb.DeleteConfigTemplateResp
}

// NewDeleteAction creates new DeleteAction
func NewDeleteAction(viper *viper.Viper, templateClient pbtemplateserver.TemplateClient,
req *pb.DeleteConfigTemplateReq, resp *pb.DeleteConfigTemplateResp) *DeleteAction {
action := &DeleteAction{viper: viper, templateClient: templateClient, req: req, resp: resp}

action.resp.Seq = req.Seq
action.resp.ErrCode = pbcommon.ErrCode_E_OK
action.resp.ErrMsg = "OK"

return action
}

// Err setup error code message in response and return the error.
func (act *DeleteAction) Err(errCode pbcommon.ErrCode, errMsg string) error {
act.resp.ErrCode = errCode
act.resp.ErrMsg = errMsg
return errors.New(errMsg)
}

// Input handles the input messages.
func (act *DeleteAction) Input() error {
if err := act.verify(); err != nil {
return act.Err(pbcommon.ErrCode_E_AS_PARAMS_INVALID, err.Error())
}
return nil
}

// Output handles the output messages.
func (act *DeleteAction) Output() error {
// do nothing.
return nil
}

func (act *DeleteAction) verify() error {
if err := common.VerifyID(act.req.Bid, "bid"); err != nil {
return err
}

if err := common.VerifyID(act.req.Templateid, "templateid"); err != nil {
return err
}

return nil
}

func (act *DeleteAction) deleteTemplate() (pbcommon.ErrCode, string) {
req := &pbtemplateserver.DeleteConfigTemplateReq{
Seq: act.req.Seq,
Bid: act.req.Bid,
Templateid: act.req.Templateid,
}

ctx, cancel := context.WithTimeout(context.Background(), act.viper.GetDuration("templateserver.calltimeout"))
defer cancel()

logger.V(2).Infof("DeleteConfigTemplateReq[%d]| request to templateserver DeleteConfigTemplateReq, %+v", req.Seq, req)

resp, err := act.templateClient.DeleteConfigTemplate(ctx, req)
if err != nil {
return pbcommon.ErrCode_E_AS_SYSTEM_UNKONW, fmt.Sprintf("request to templateserver DeleteConfigTemplate, %+v", err)
}
if resp.ErrCode != pbcommon.ErrCode_E_OK {
return resp.ErrCode, resp.ErrMsg
}

return pbcommon.ErrCode_E_OK, "OK"
}

// Do do action
func (act *DeleteAction) Do() error {

// delete config template
if errCode, errMsg := act.deleteTemplate(); errCode != pbcommon.ErrCode_E_OK {
return act.Err(errCode, errMsg)
}
return nil
}
Loading

0 comments on commit d7d4483

Please sign in to comment.