Files
cloudpods/pkg/mcp-server/adapters/adapter.go
2026-07-21 16:19:09 +08:00

80 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 adapters
import (
"context"
"github.com/golang-plus/errors"
api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon/policy"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcp-server/options"
"yunion.io/x/pkg/gotypes"
)
// CloudpodsAdapter 负责 Cloudpods 认证并创建 mcclient.ClientSession供 climc 工具执行使用。
type CloudpodsAdapter struct {
client *mcclient.Client
}
// NewCloudpodsAdapter 创建一个新的 Cloudpods 适配器实例
func NewCloudpodsAdapter() *CloudpodsAdapter {
client := mcclient.NewClient(
options.Options.AuthURL,
options.Options.Timeout,
false,
true,
"",
"",
)
return &CloudpodsAdapter{client: client}
}
func (a *CloudpodsAdapter) authenticate(ak string, sk string) (mcclient.TokenCredential, error) {
return a.client.AuthenticateByAccessKey(ak, sk, "")
}
// GetSession 获取 Cloudpods API 会话
func (a *CloudpodsAdapter) GetSession(ctx context.Context, ak string, sk string) (*mcclient.ClientSession, error) {
if ak == "" && sk == "" {
ak, sk = GetAKSKFromContext(ctx)
}
var userCred mcclient.TokenCredential
if auth.IsAuthed() {
userCred = policy.FetchUserCredential(ctx)
if gotypes.IsNil(userCred) {
token, err := a.authenticate(ak, sk)
if err != nil {
return nil, errors.Wrap(err, "authenticate")
}
userCred = token
}
return auth.GetSession(ctx, userCred, ""), nil
}
token, err := a.authenticate(ak, sk)
if err != nil {
return nil, errors.Wrap(err, "authenticate")
}
return a.client.NewSession(
context.Background(),
"",
"",
api.EndpointInterfaceApigateway,
token,
), nil
}