From 248b50b6939ee48ca7e65f7e9d54d45d4eb24a8c Mon Sep 17 00:00:00 2001 From: Jihoon Seo Date: Tue, 18 Jan 2022 18:50:38 +0900 Subject: [PATCH] Add registerExistingSSHKey feature --- src/api/grpc/server/mcir/sshkey.go | 2 +- src/api/rest/docs/docs.go | 19 ++++++ src/api/rest/docs/swagger.json | 19 ++++++ src/api/rest/docs/swagger.yaml | 13 +++++ src/api/rest/server/mcir/sshkey.go | 8 +-- src/core/mcir/common.go | 2 +- src/core/mcir/sshkey.go | 58 +++++++++++-------- .../scripts/5.sshKey/spider-get-sshKey.sh | 2 +- .../scripts/5.sshKey/test-register-sshKey.sh | 56 ++++++++++++++++++ 9 files changed, 149 insertions(+), 30 deletions(-) create mode 100755 src/testclient/scripts/5.sshKey/test-register-sshKey.sh diff --git a/src/api/grpc/server/mcir/sshkey.go b/src/api/grpc/server/mcir/sshkey.go index 64bc2d203..3ade5d71b 100644 --- a/src/api/grpc/server/mcir/sshkey.go +++ b/src/api/grpc/server/mcir/sshkey.go @@ -29,7 +29,7 @@ func (s *MCIRService) CreateSshKey(ctx context.Context, req *pb.TbSshKeyCreateRe return nil, gc.ConvGrpcStatusErr(err, "", "MCIRService.CreateSshKey()") } - content, err := mcir.CreateSshKey(req.NsId, &mcirObj) + content, err := mcir.CreateSshKey(req.NsId, &mcirObj, "") if err != nil { return nil, gc.ConvGrpcStatusErr(err, "", "MCIRService.CreateSshKey()") } diff --git a/src/api/rest/docs/docs.go b/src/api/rest/docs/docs.go index 8affe1ff0..5bb8544f0 100644 --- a/src/api/rest/docs/docs.go +++ b/src/api/rest/docs/docs.go @@ -5541,11 +5541,30 @@ var doc = `{ "connectionName": { "type": "string" }, + "cspSshKeyName": { + "description": "Fields for \"Register existing SSH keys\" feature", + "type": "string" + }, "description": { "type": "string" }, + "fingerprint": { + "type": "string" + }, "name": { "type": "string" + }, + "privateKey": { + "type": "string" + }, + "publicKey": { + "type": "string" + }, + "username": { + "type": "string" + }, + "verifiedUsername": { + "type": "string" } } }, diff --git a/src/api/rest/docs/swagger.json b/src/api/rest/docs/swagger.json index 287f127eb..dbc19f80c 100644 --- a/src/api/rest/docs/swagger.json +++ b/src/api/rest/docs/swagger.json @@ -5527,11 +5527,30 @@ "connectionName": { "type": "string" }, + "cspSshKeyName": { + "description": "Fields for \"Register existing SSH keys\" feature", + "type": "string" + }, "description": { "type": "string" }, + "fingerprint": { + "type": "string" + }, "name": { "type": "string" + }, + "privateKey": { + "type": "string" + }, + "publicKey": { + "type": "string" + }, + "username": { + "type": "string" + }, + "verifiedUsername": { + "type": "string" } } }, diff --git a/src/api/rest/docs/swagger.yaml b/src/api/rest/docs/swagger.yaml index 589f95a78..a6a424ccf 100644 --- a/src/api/rest/docs/swagger.yaml +++ b/src/api/rest/docs/swagger.yaml @@ -623,10 +623,23 @@ definitions: properties: connectionName: type: string + cspSshKeyName: + description: Fields for "Register existing SSH keys" feature + type: string description: type: string + fingerprint: + type: string name: type: string + privateKey: + type: string + publicKey: + type: string + username: + type: string + verifiedUsername: + type: string required: - connectionName - name diff --git a/src/api/rest/server/mcir/sshkey.go b/src/api/rest/server/mcir/sshkey.go index 1ad747916..c3272c220 100644 --- a/src/api/rest/server/mcir/sshkey.go +++ b/src/api/rest/server/mcir/sshkey.go @@ -36,18 +36,18 @@ import ( // @Failure 500 {object} common.SimpleMsg // @Router /ns/{nsId}/resources/sshKey [post] func RestPostSshKey(c echo.Context) error { + fmt.Println("[POST SshKey]") nsId := c.Param("nsId") + optionFlag := c.QueryParam("option") + u := &mcir.TbSshKeyReq{} if err := c.Bind(u); err != nil { return err } - fmt.Println("[POST SshKey") - //fmt.Println("[Creating SshKey]") - //content, responseCode, _, err := CreateSshKey(nsId, u) - content, err := mcir.CreateSshKey(nsId, u) + content, err := mcir.CreateSshKey(nsId, u, optionFlag) if err != nil { common.CBLog.Error(err) mapA := map[string]string{"message": err.Error()} diff --git a/src/core/mcir/common.go b/src/core/mcir/common.go index 1b5f0d8bd..49b3eea47 100644 --- a/src/core/mcir/common.go +++ b/src/core/mcir/common.go @@ -1990,7 +1990,7 @@ func LoadDefaultResource(nsId string, resType string, connectionName string) err common.PrintJsonPretty(reqTmp) - resultInfo, err := CreateSshKey(nsId, &reqTmp) + resultInfo, err := CreateSshKey(nsId, &reqTmp, "") if err != nil { common.CBLog.Error(err) // If already exist, error will occur diff --git a/src/core/mcir/sshkey.go b/src/core/mcir/sshkey.go index 36c49c909..af6e2af35 100644 --- a/src/core/mcir/sshkey.go +++ b/src/core/mcir/sshkey.go @@ -51,6 +51,14 @@ type TbSshKeyReq struct { Name string `json:"name" validate:"required"` ConnectionName string `json:"connectionName" validate:"required"` Description string `json:"description"` + + // Fields for "Register existing SSH keys" feature + CspSshKeyName string `json:"cspSshKeyName"` + Fingerprint string `json:"fingerprint"` + Username string `json:"username"` + VerifiedUsername string `json:"verifiedUsername"` + PublicKey string `json:"publicKey"` + PrivateKey string `json:"privateKey"` } // TbSshKeyReqStructLevelValidation is a function to validate 'TbSshKeyReq' object. @@ -83,7 +91,7 @@ type TbSshKeyInfo struct { } // CreateSshKey accepts SSH key creation request, creates and returns an TB sshKey object -func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) { +func CreateSshKey(nsId string, u *TbSshKeyReq, option string) (TbSshKeyInfo, error) { resourceType := common.StrSSHKey @@ -94,34 +102,31 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) { return temp, err } - // returns InvalidValidationError for bad validation input, nil or ValidationErrors ( []FieldError ) + if option == "register" { + errs := []error{} + errs = append(errs, validate.Var(u.Username, "required")) + errs = append(errs, validate.Var(u.PrivateKey, "required")) + + for _, err := range errs { + if err != nil { + temp := TbSshKeyInfo{} + if _, ok := err.(*validator.InvalidValidationError); ok { + fmt.Println(err) + return temp, err + } + return temp, err + } + } + } + err = validate.Struct(u) if err != nil { - - // this check is only needed when your code could produce - // an invalid value for validation such as interface with nil - // value most including myself do not usually have code like this. if _, ok := err.(*validator.InvalidValidationError); ok { fmt.Println(err) temp := TbSshKeyInfo{} return temp, err } - // for _, err := range err.(validator.ValidationErrors) { - - // fmt.Println(err.Namespace()) // can differ when a custom TagNameFunc is registered or - // fmt.Println(err.Field()) // by passing alt name to ReportError like below - // fmt.Println(err.StructNamespace()) - // fmt.Println(err.StructField()) - // fmt.Println(err.Tag()) - // fmt.Println(err.ActualTag()) - // fmt.Println(err.Kind()) - // fmt.Println(err.Type()) - // fmt.Println(err.Value()) - // fmt.Println(err.Param()) - // fmt.Println() - // } - temp := TbSshKeyInfo{} return temp, err } @@ -147,7 +152,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) { var tempSpiderKeyPairInfo *SpiderKeyPairInfo - if os.Getenv("SPIDER_CALL_METHOD") == "REST" { + if os.Getenv("SPIDER_CALL_METHOD") == "REST" && option != "register" { url := common.SpiderRestUrl + "/keypair" @@ -179,7 +184,7 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) { tempSpiderKeyPairInfo = resp.Result().(*SpiderKeyPairInfo) - } else { + } else if os.Getenv("SPIDER_CALL_METHOD") != "REST" && option != "register" { // Set CCM gRPC API ccm := api.NewCloudResourceHandler() @@ -211,6 +216,13 @@ func CreateSshKey(nsId string, u *TbSshKeyReq) (TbSshKeyInfo, error) { return TbSshKeyInfo{}, err } + } else { // option == "register" + tempSpiderKeyPairInfo = &SpiderKeyPairInfo{} + tempSpiderKeyPairInfo.IId.NameId = u.CspSshKeyName + tempSpiderKeyPairInfo.Fingerprint = u.Fingerprint + tempSpiderKeyPairInfo.VMUserID = u.Username + tempSpiderKeyPairInfo.PublicKey = u.PublicKey + tempSpiderKeyPairInfo.PrivateKey = u.PrivateKey } content := TbSshKeyInfo{} diff --git a/src/testclient/scripts/5.sshKey/spider-get-sshKey.sh b/src/testclient/scripts/5.sshKey/spider-get-sshKey.sh index 2ab2086fc..5dd0e6676 100755 --- a/src/testclient/scripts/5.sshKey/spider-get-sshKey.sh +++ b/src/testclient/scripts/5.sshKey/spider-get-sshKey.sh @@ -4,7 +4,7 @@ function CallSpider() { echo "- Get sshKey in ${MCIRRegionName}" resp=$( - curl -H "${AUTH}" -sX GET http://$SpiderServer/spider/keypair/${CONN_CONFIG[$INDEX,$REGION]}-${POSTFIX}?force=true -H 'Content-Type: application/json' -d @- <