client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) 2018 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 rest
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "net/http"
  22. "net/url"
  23. "github.com/vmware/govmomi/vapi/internal"
  24. "github.com/vmware/govmomi/vim25"
  25. "github.com/vmware/govmomi/vim25/soap"
  26. )
  27. // Client extends soap.Client to support JSON encoding, while inheriting security features, debug tracing and session persistence.
  28. type Client struct {
  29. *soap.Client
  30. }
  31. // NewClient creates a new Client instance.
  32. func NewClient(c *vim25.Client) *Client {
  33. sc := c.Client.NewServiceClient(internal.Path, "")
  34. return &Client{sc}
  35. }
  36. type Signer interface {
  37. SignRequest(*http.Request) error
  38. }
  39. type signerContext struct{}
  40. func (c *Client) WithSigner(ctx context.Context, s Signer) context.Context {
  41. return context.WithValue(ctx, signerContext{}, s)
  42. }
  43. // Do sends the http.Request, decoding resBody if provided.
  44. func (c *Client) Do(ctx context.Context, req *http.Request, resBody interface{}) error {
  45. switch req.Method {
  46. case http.MethodPost, http.MethodPatch:
  47. req.Header.Set("Content-Type", "application/json")
  48. }
  49. req.Header.Set("Accept", "application/json")
  50. if s, ok := ctx.Value(signerContext{}).(Signer); ok {
  51. if err := s.SignRequest(req); err != nil {
  52. return err
  53. }
  54. }
  55. return c.Client.Do(ctx, req, func(res *http.Response) error {
  56. switch res.StatusCode {
  57. case http.StatusOK:
  58. case http.StatusBadRequest:
  59. // TODO: structured error types
  60. detail, err := ioutil.ReadAll(res.Body)
  61. if err != nil {
  62. return err
  63. }
  64. return fmt.Errorf("%s: %s", res.Status, bytes.TrimSpace(detail))
  65. default:
  66. return fmt.Errorf("%s %s: %s", req.Method, req.URL, res.Status)
  67. }
  68. if resBody == nil {
  69. return nil
  70. }
  71. switch b := resBody.(type) {
  72. case io.Writer:
  73. _, err := io.Copy(b, res.Body)
  74. return err
  75. default:
  76. val := struct {
  77. Value interface{} `json:"value,omitempty"`
  78. }{
  79. resBody,
  80. }
  81. return json.NewDecoder(res.Body).Decode(&val)
  82. }
  83. })
  84. }
  85. // Login creates a new session via Basic Authentication with the given url.Userinfo.
  86. func (c *Client) Login(ctx context.Context, user *url.Userinfo) error {
  87. req := internal.URL(c, internal.SessionPath).Request(http.MethodPost)
  88. if user != nil {
  89. if password, ok := user.Password(); ok {
  90. req.SetBasicAuth(user.Username(), password)
  91. }
  92. }
  93. return c.Do(ctx, req, nil)
  94. }
  95. func (c *Client) LoginByToken(ctx context.Context) error {
  96. return c.Login(ctx, nil)
  97. }
  98. // Logout deletes the current session.
  99. func (c *Client) Logout(ctx context.Context) error {
  100. req := internal.URL(c, internal.SessionPath).Request(http.MethodDelete)
  101. return c.Do(ctx, req, nil)
  102. }