client.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pbm
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/vmware/govmomi/pbm/methods"
  18. "github.com/vmware/govmomi/pbm/types"
  19. "github.com/vmware/govmomi/vim25"
  20. "github.com/vmware/govmomi/vim25/soap"
  21. vim "github.com/vmware/govmomi/vim25/types"
  22. )
  23. const (
  24. Namespace = "pbm"
  25. Path = "/pbm"
  26. )
  27. var (
  28. ServiceInstance = vim.ManagedObjectReference{
  29. Type: "PbmServiceInstance",
  30. Value: "ServiceInstance",
  31. }
  32. )
  33. type Client struct {
  34. *soap.Client
  35. ServiceContent types.PbmServiceInstanceContent
  36. }
  37. func NewClient(ctx context.Context, c *vim25.Client) (*Client, error) {
  38. sc := c.Client.NewServiceClient(Path, Namespace)
  39. req := types.PbmRetrieveServiceContent{
  40. This: ServiceInstance,
  41. }
  42. res, err := methods.PbmRetrieveServiceContent(ctx, sc, &req)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &Client{sc, res.Returnval}, nil
  47. }
  48. func (c *Client) QueryProfile(ctx context.Context, rtype types.PbmProfileResourceType, category string) ([]types.PbmProfileId, error) {
  49. req := types.PbmQueryProfile{
  50. This: c.ServiceContent.ProfileManager,
  51. ResourceType: rtype,
  52. ProfileCategory: category,
  53. }
  54. res, err := methods.PbmQueryProfile(ctx, c, &req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return res.Returnval, nil
  59. }
  60. func (c *Client) RetrieveContent(ctx context.Context, ids []types.PbmProfileId) ([]types.BasePbmProfile, error) {
  61. req := types.PbmRetrieveContent{
  62. This: c.ServiceContent.ProfileManager,
  63. ProfileIds: ids,
  64. }
  65. res, err := methods.PbmRetrieveContent(ctx, c, &req)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return res.Returnval, nil
  70. }
  71. type PlacementCompatibilityResult []types.PbmPlacementCompatibilityResult
  72. func (c *Client) CheckRequirements(ctx context.Context, hubs []types.PbmPlacementHub, ref *types.PbmServerObjectRef, preq []types.BasePbmPlacementRequirement) (PlacementCompatibilityResult, error) {
  73. req := types.PbmCheckRequirements{
  74. This: c.ServiceContent.PlacementSolver,
  75. HubsToSearch: hubs,
  76. PlacementSubjectRef: ref,
  77. PlacementSubjectRequirement: preq,
  78. }
  79. res, err := methods.PbmCheckRequirements(ctx, c, &req)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return res.Returnval, nil
  84. }
  85. func (l PlacementCompatibilityResult) CompatibleDatastores() []types.PbmPlacementHub {
  86. var compatibleDatastores []types.PbmPlacementHub
  87. for _, res := range l {
  88. if len(res.Error) == 0 {
  89. compatibleDatastores = append(compatibleDatastores, res.Hub)
  90. }
  91. }
  92. return compatibleDatastores
  93. }
  94. func (l PlacementCompatibilityResult) NonCompatibleDatastores() []types.PbmPlacementHub {
  95. var nonCompatibleDatastores []types.PbmPlacementHub
  96. for _, res := range l {
  97. if len(res.Error) > 0 {
  98. nonCompatibleDatastores = append(nonCompatibleDatastores, res.Hub)
  99. }
  100. }
  101. return nonCompatibleDatastores
  102. }
  103. func (c *Client) CreateProfile(ctx context.Context, capabilityProfileCreateSpec types.PbmCapabilityProfileCreateSpec) (*types.PbmProfileId, error) {
  104. req := types.PbmCreate{
  105. This: c.ServiceContent.ProfileManager,
  106. CreateSpec: capabilityProfileCreateSpec,
  107. }
  108. res, err := methods.PbmCreate(ctx, c, &req)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &res.Returnval, nil
  113. }
  114. func (c *Client) UpdateProfile(ctx context.Context, id types.PbmProfileId, updateSpec types.PbmCapabilityProfileUpdateSpec) error {
  115. req := types.PbmUpdate{
  116. This: c.ServiceContent.ProfileManager,
  117. ProfileId: id,
  118. UpdateSpec: updateSpec,
  119. }
  120. _, err := methods.PbmUpdate(ctx, c, &req)
  121. if err != nil {
  122. return err
  123. }
  124. return nil
  125. }
  126. func (c *Client) DeleteProfile(ctx context.Context, ids []types.PbmProfileId) ([]types.PbmProfileOperationOutcome, error) {
  127. req := types.PbmDelete{
  128. This: c.ServiceContent.ProfileManager,
  129. ProfileId: ids,
  130. }
  131. res, err := methods.PbmDelete(ctx, c, &req)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return res.Returnval, nil
  136. }
  137. func (c *Client) QueryAssociatedEntity(ctx context.Context, id types.PbmProfileId, entityType string) ([]types.PbmServerObjectRef, error) {
  138. req := types.PbmQueryAssociatedEntity{
  139. This: c.ServiceContent.ProfileManager,
  140. Profile: id,
  141. EntityType: entityType,
  142. }
  143. res, err := methods.PbmQueryAssociatedEntity(ctx, c, &req)
  144. if err != nil {
  145. return nil, err
  146. }
  147. return res.Returnval, nil
  148. }
  149. func (c *Client) QueryAssociatedEntities(ctx context.Context, ids []types.PbmProfileId) ([]types.PbmQueryProfileResult, error) {
  150. req := types.PbmQueryAssociatedEntities{
  151. This: c.ServiceContent.ProfileManager,
  152. Profiles: ids,
  153. }
  154. res, err := methods.PbmQueryAssociatedEntities(ctx, c, &req)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return res.Returnval, nil
  159. }
  160. func (c *Client) ProfileIDByName(ctx context.Context, profileName string) (string, error) {
  161. resourceType := types.PbmProfileResourceType{
  162. ResourceType: string(types.PbmProfileResourceTypeEnumSTORAGE),
  163. }
  164. category := types.PbmProfileCategoryEnumREQUIREMENT
  165. ids, err := c.QueryProfile(ctx, resourceType, string(category))
  166. if err != nil {
  167. return "", err
  168. }
  169. profiles, err := c.RetrieveContent(ctx, ids)
  170. if err != nil {
  171. return "", err
  172. }
  173. for i := range profiles {
  174. profile := profiles[i].GetPbmProfile()
  175. if profile.Name == profileName {
  176. return profile.ProfileId.UniqueId, nil
  177. }
  178. }
  179. return "", fmt.Errorf("no pbm profile found with name: %q", profileName)
  180. }