server.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package example
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  23. )
  24. const (
  25. AuthURL = "https://10.168.222.209:5000/v3"
  26. DomainName = "Default"
  27. TenantDomain = ""
  28. TenantName = "system"
  29. Username = "a"
  30. Password = "a"
  31. Region = "Yunion"
  32. EndpointType = "publicURL"
  33. Debug = false
  34. )
  35. func getSession() (*mcclient.ClientSession, error) {
  36. client := mcclient.NewClient(AuthURL, 10, Debug, true, "", "")
  37. token, err := client.Authenticate(Username, Password, DomainName, TenantName, TenantDomain)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return client.NewSession(context.Background(), Region, "", EndpointType, token), nil
  42. }
  43. func CreateServerExample() error {
  44. session, err := getSession()
  45. if err != nil {
  46. log.Errorf("get session error: %v", err)
  47. return err
  48. }
  49. // params := options.ServerCreateOptions{}
  50. // params.NAME = "test-create"
  51. // params.VcpuCount = 1
  52. // params.MEMSPEC = "2G"
  53. // params.Disk = []string{"a4171f87-7e70-43bf-852e-4a3e1bf7deab:local"}
  54. // 参数参考: docs/schemas/instance.yaml#InstanceCreate
  55. params := map[string]interface{}{
  56. "name": "test-create",
  57. "vcpu_count": 1,
  58. "vmem_size": 2048,
  59. "disks": []map[string]string{
  60. {
  61. "image_id": "a4171f87-7e70-43bf-852e-4a3e1bf7deab",
  62. "disk_type": "sys",
  63. "backend": "local",
  64. },
  65. },
  66. }
  67. resp, err := modules.Servers.Create(session, jsonutils.Marshal(params))
  68. if err != nil {
  69. log.Errorf("create server error: %v", err)
  70. return err
  71. }
  72. log.Infof("result: %s", resp.PrettyString())
  73. return nil
  74. }
  75. func GetServerExample() error {
  76. session, err := getSession()
  77. if err != nil {
  78. log.Errorf("get session error: %v", err)
  79. return err
  80. }
  81. pendingDelete := true //回收站实例
  82. opts := compute_options.ServerListOptions{}
  83. opts.PendingDelete = &pendingDelete
  84. params, err := options.StructToParams(opts)
  85. if err != nil {
  86. log.Errorf("params error: %v", err)
  87. return err
  88. }
  89. server, err := modules.Servers.Get(session, "test-create", params)
  90. if err != nil {
  91. log.Errorf("get server info error")
  92. return err
  93. }
  94. log.Infof("server info: %s", server.PrettyString())
  95. return nil
  96. }