mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-19 10:46:58 +08:00
添加创建server示例代码
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/mcclient/options"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/onecloud/pkg/mcclient"
|
||||
"yunion.io/x/onecloud/pkg/mcclient/modules"
|
||||
)
|
||||
|
||||
const (
|
||||
AuthURL = "https://10.168.222.209:5000/v3"
|
||||
DomainName = "Default"
|
||||
TenantDomain = ""
|
||||
TenantName = "system"
|
||||
Username = "a"
|
||||
Password = "a"
|
||||
Region = "Yunion"
|
||||
EndpointType = "publicURL"
|
||||
Debug = false
|
||||
)
|
||||
|
||||
func getSession() (*mcclient.ClientSession, error) {
|
||||
client := mcclient.NewClient(AuthURL, 10, Debug, true, "", "")
|
||||
token, err := client.Authenticate(Username, Password, DomainName, TenantName, TenantDomain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.NewSession(context.Background(), Region, "", EndpointType, token, "v2"), nil
|
||||
}
|
||||
|
||||
func CreateServerExample() error {
|
||||
session, err := getSession()
|
||||
if err != nil {
|
||||
log.Errorf("get session error: %v", err)
|
||||
return err
|
||||
}
|
||||
// params := options.ServerCreateOptions{}
|
||||
// params.NAME = "test-create"
|
||||
// params.VcpuCount = 1
|
||||
// params.MEMSPEC = "2G"
|
||||
// params.Disk = []string{"a4171f87-7e70-43bf-852e-4a3e1bf7deab:local"}
|
||||
|
||||
// 参数参考: docs/schemas/instance.yaml#InstanceCreate
|
||||
params := map[string]interface{}{
|
||||
"name": "test-create",
|
||||
"vcpu_count": 1,
|
||||
"vmem_size": 2048,
|
||||
"disks": []map[string]string{
|
||||
map[string]string{
|
||||
"image_id": "a4171f87-7e70-43bf-852e-4a3e1bf7deab",
|
||||
"disk_type": "sys",
|
||||
"backend": "local",
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := modules.Servers.Create(session, jsonutils.Marshal(params))
|
||||
if err != nil {
|
||||
log.Errorf("create server error: %v", err)
|
||||
return err
|
||||
}
|
||||
log.Infof("result: %s", resp.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetServerExample() error {
|
||||
session, err := getSession()
|
||||
if err != nil {
|
||||
log.Errorf("get session error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
pendingDelete := true //回收站实例
|
||||
opts := options.ServerListOptions{}
|
||||
opts.PendingDelete = &pendingDelete
|
||||
|
||||
params, err := options.StructToParams(opts)
|
||||
if err != nil {
|
||||
log.Errorf("params error: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
server, err := modules.Servers.Get(session, "test-create", params)
|
||||
if err != nil {
|
||||
log.Errorf("get server info error")
|
||||
return err
|
||||
}
|
||||
log.Infof("server info: %s", server.PrettyString())
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user