internal.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 internal
  14. import (
  15. "bytes"
  16. "encoding/json"
  17. "io"
  18. "net/http"
  19. "net/url"
  20. "github.com/vmware/govmomi/vim25/mo"
  21. "github.com/vmware/govmomi/vim25/types"
  22. )
  23. const (
  24. Path = "/rest/com/vmware"
  25. SessionPath = "/cis/session"
  26. CategoryPath = "/cis/tagging/category"
  27. TagPath = "/cis/tagging/tag"
  28. AssociationPath = "/cis/tagging/tag-association"
  29. SessionCookieName = "vmware-api-session-id"
  30. )
  31. // AssociatedObject is the same structure as types.ManagedObjectReference,
  32. // just with a different field name (ID instead of Value).
  33. // In the API we use mo.Reference, this type is only used for wire transfer.
  34. type AssociatedObject struct {
  35. Type string `json:"type"`
  36. Value string `json:"id"`
  37. }
  38. // Reference implements mo.Reference
  39. func (o AssociatedObject) Reference() types.ManagedObjectReference {
  40. return types.ManagedObjectReference(o)
  41. }
  42. // Association for tag-association requests.
  43. type Association struct {
  44. ObjectID *AssociatedObject `json:"object_id,omitempty"`
  45. }
  46. // NewAssociation returns an Association, converting ref to an AssociatedObject.
  47. func NewAssociation(ref mo.Reference) Association {
  48. obj := AssociatedObject(ref.Reference())
  49. return Association{
  50. ObjectID: &obj,
  51. }
  52. }
  53. type CloneURL interface {
  54. URL() *url.URL
  55. }
  56. // Resource wraps url.URL with helpers
  57. type Resource struct {
  58. u *url.URL
  59. }
  60. func URL(c CloneURL, path string) *Resource {
  61. r := &Resource{u: c.URL()}
  62. r.u.Path = Path + path
  63. return r
  64. }
  65. // WithID appends id to the URL.Path
  66. func (r *Resource) WithID(id string) *Resource {
  67. r.u.Path += "/id:" + id
  68. return r
  69. }
  70. // WithAction sets adds action to the URL.RawQuery
  71. func (r *Resource) WithAction(action string) *Resource {
  72. r.u.RawQuery = url.Values{
  73. "~action": []string{action},
  74. }.Encode()
  75. return r
  76. }
  77. // Request returns a new http.Request for the given method.
  78. // An optional body can be provided for POST and PATCH methods.
  79. func (r *Resource) Request(method string, body ...interface{}) *http.Request {
  80. rdr := io.MultiReader() // empty body by default
  81. if len(body) != 0 {
  82. rdr = encode(body[0])
  83. }
  84. req, err := http.NewRequest(method, r.u.String(), rdr)
  85. if err != nil {
  86. panic(err)
  87. }
  88. return req
  89. }
  90. type errorReader struct {
  91. e error
  92. }
  93. func (e errorReader) Read([]byte) (int, error) {
  94. return -1, e.e
  95. }
  96. // encode body as JSON, deferring any errors until io.Reader is used.
  97. func encode(body interface{}) io.Reader {
  98. var b bytes.Buffer
  99. err := json.NewEncoder(&b).Encode(body)
  100. if err != nil {
  101. return errorReader{err}
  102. }
  103. return &b
  104. }