diff --git a/pkg/ansibleserver/models/ansibleplaybooks.go b/pkg/ansibleserver/models/ansibleplaybooks.go index 0709967251..0ef0f61b8c 100644 --- a/pkg/ansibleserver/models/ansibleplaybooks.go +++ b/pkg/ansibleserver/models/ansibleplaybooks.go @@ -30,6 +30,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + mcclient_modules "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/ansible" ) @@ -185,7 +186,7 @@ func (apb *SAnsiblePlaybook) runPlaybook(ctx context.Context, userCred mcclient. // init private key pb := apb.Playbook.Copy() - if k, err := fetchPrivateKey(ctx, userCred); err != nil { + if k, err := mcclient_modules.Sshkeypairs.FetchPrivateKey(ctx, userCred); err != nil { return err } else { pb.PrivateKey = []byte(k) diff --git a/pkg/ansibleserver/models/ansibleplaybooks_fetch_private_key.go b/pkg/ansibleserver/models/ansibleplaybooks_fetch_private_key.go deleted file mode 100644 index ee334ad83d..0000000000 --- a/pkg/ansibleserver/models/ansibleplaybooks_fetch_private_key.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2019 Yunion -// -// Licensed 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 models - -import ( - "context" - - "github.com/pkg/errors" - - "yunion.io/x/jsonutils" - - "yunion.io/x/onecloud/pkg/mcclient" - "yunion.io/x/onecloud/pkg/mcclient/auth" - mcclient_models "yunion.io/x/onecloud/pkg/mcclient/models" - mcclient_modules "yunion.io/x/onecloud/pkg/mcclient/modules" -) - -func fetchPrivateKey(ctx context.Context, userCred mcclient.TokenCredential) (string, error) { - s := auth.GetSession(ctx, userCred, "", "") - jd := jsonutils.NewDict() - var jr jsonutils.JSONObject - if userCred.HasSystemAdminPrivilege() { - jd.Set("admin", jsonutils.JSONTrue) - r, err := mcclient_modules.Sshkeypairs.List(s, jd) - if err != nil { - return "", errors.WithMessage(err, "get admin ssh key") - } - jr = r.Data[0] - } else { - r, err := mcclient_modules.Sshkeypairs.GetById(s, userCred.GetProjectId(), jd) - if err != nil { - return "", errors.WithMessage(err, "get project ssh key") - } - jr = r - } - kp := &mcclient_models.SshKeypair{} - if err := jr.Unmarshal(kp); err != nil { - return "", errors.WithMessage(err, "unmarshal ssh key") - } - if kp.PrivateKey == "" { - return "", errors.New("empty ssh key") - } - return kp.PrivateKey, nil -} diff --git a/pkg/ansibleserver/models/ansibleplaybooks_v2.go b/pkg/ansibleserver/models/ansibleplaybooks_v2.go index 2474a18e07..e1544c2b76 100644 --- a/pkg/ansibleserver/models/ansibleplaybooks_v2.go +++ b/pkg/ansibleserver/models/ansibleplaybooks_v2.go @@ -32,6 +32,7 @@ import ( "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" + mcclient_modules "yunion.io/x/onecloud/pkg/mcclient/modules" "yunion.io/x/onecloud/pkg/util/ansible" "yunion.io/x/onecloud/pkg/util/ansiblev2" ) @@ -179,7 +180,7 @@ func (apb *SAnsiblePlaybookV2) runPlaybook(ctx context.Context, userCred mcclien } } // init private key - if privateKey, err = fetchPrivateKey(ctx, userCred); err != nil { + if privateKey, err = mcclient_modules.Sshkeypairs.FetchPrivateKey(ctx, userCred); err != nil { return err }