lease.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright (c) 2015-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 nfc
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. "io"
  19. "path"
  20. "github.com/vmware/govmomi/property"
  21. "github.com/vmware/govmomi/vim25"
  22. "github.com/vmware/govmomi/vim25/methods"
  23. "github.com/vmware/govmomi/vim25/mo"
  24. "github.com/vmware/govmomi/vim25/progress"
  25. "github.com/vmware/govmomi/vim25/soap"
  26. "github.com/vmware/govmomi/vim25/types"
  27. )
  28. type Lease struct {
  29. types.ManagedObjectReference
  30. c *vim25.Client
  31. }
  32. func NewLease(c *vim25.Client, ref types.ManagedObjectReference) *Lease {
  33. return &Lease{ref, c}
  34. }
  35. // Abort wraps methods.Abort
  36. func (l *Lease) Abort(ctx context.Context, fault *types.LocalizedMethodFault) error {
  37. req := types.HttpNfcLeaseAbort{
  38. This: l.Reference(),
  39. Fault: fault,
  40. }
  41. _, err := methods.HttpNfcLeaseAbort(ctx, l.c, &req)
  42. if err != nil {
  43. return err
  44. }
  45. return nil
  46. }
  47. // Complete wraps methods.Complete
  48. func (l *Lease) Complete(ctx context.Context) error {
  49. req := types.HttpNfcLeaseComplete{
  50. This: l.Reference(),
  51. }
  52. _, err := methods.HttpNfcLeaseComplete(ctx, l.c, &req)
  53. if err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. // GetManifest wraps methods.GetManifest
  59. func (l *Lease) GetManifest(ctx context.Context) error {
  60. req := types.HttpNfcLeaseGetManifest{
  61. This: l.Reference(),
  62. }
  63. _, err := methods.HttpNfcLeaseGetManifest(ctx, l.c, &req)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. // Progress wraps methods.Progress
  70. func (l *Lease) Progress(ctx context.Context, percent int32) error {
  71. req := types.HttpNfcLeaseProgress{
  72. This: l.Reference(),
  73. Percent: percent,
  74. }
  75. _, err := methods.HttpNfcLeaseProgress(ctx, l.c, &req)
  76. if err != nil {
  77. return err
  78. }
  79. return nil
  80. }
  81. type LeaseInfo struct {
  82. types.HttpNfcLeaseInfo
  83. Items []FileItem
  84. }
  85. func (l *Lease) newLeaseInfo(li *types.HttpNfcLeaseInfo, items []types.OvfFileItem) (*LeaseInfo, error) {
  86. info := &LeaseInfo{
  87. HttpNfcLeaseInfo: *li,
  88. }
  89. for _, device := range li.DeviceUrl {
  90. u, err := l.c.ParseURL(device.Url)
  91. if err != nil {
  92. return nil, err
  93. }
  94. if device.SslThumbprint != "" {
  95. // TODO: prefer host management IP
  96. l.c.SetThumbprint(u.Host, device.SslThumbprint)
  97. }
  98. if len(items) == 0 {
  99. // this is an export
  100. item := types.OvfFileItem{
  101. DeviceId: device.Key,
  102. Path: device.TargetId,
  103. Size: device.FileSize,
  104. }
  105. if item.Size == 0 {
  106. item.Size = li.TotalDiskCapacityInKB * 1024
  107. }
  108. if item.Path == "" {
  109. item.Path = path.Base(device.Url)
  110. }
  111. info.Items = append(info.Items, NewFileItem(u, item))
  112. continue
  113. }
  114. // this is an import
  115. for _, item := range items {
  116. if device.ImportKey == item.DeviceId {
  117. info.Items = append(info.Items, NewFileItem(u, item))
  118. break
  119. }
  120. }
  121. }
  122. return info, nil
  123. }
  124. func (l *Lease) Wait(ctx context.Context, items []types.OvfFileItem) (*LeaseInfo, error) {
  125. var lease mo.HttpNfcLease
  126. pc := property.DefaultCollector(l.c)
  127. err := property.Wait(ctx, pc, l.Reference(), []string{"state", "info", "error"}, func(pc []types.PropertyChange) bool {
  128. done := false
  129. for _, c := range pc {
  130. if c.Val == nil {
  131. continue
  132. }
  133. switch c.Name {
  134. case "error":
  135. val := c.Val.(types.LocalizedMethodFault)
  136. lease.Error = &val
  137. done = true
  138. case "info":
  139. val := c.Val.(types.HttpNfcLeaseInfo)
  140. lease.Info = &val
  141. case "state":
  142. lease.State = c.Val.(types.HttpNfcLeaseState)
  143. if lease.State != types.HttpNfcLeaseStateInitializing {
  144. done = true
  145. }
  146. }
  147. }
  148. return done
  149. })
  150. if err != nil {
  151. return nil, err
  152. }
  153. if lease.State == types.HttpNfcLeaseStateReady {
  154. return l.newLeaseInfo(lease.Info, items)
  155. }
  156. if lease.Error != nil {
  157. return nil, errors.New(lease.Error.LocalizedMessage)
  158. }
  159. return nil, fmt.Errorf("unexpected nfc lease state: %s", lease.State)
  160. }
  161. func (l *Lease) StartUpdater(ctx context.Context, info *LeaseInfo) *LeaseUpdater {
  162. return newLeaseUpdater(ctx, l, info)
  163. }
  164. func (l *Lease) Upload(ctx context.Context, item FileItem, f io.Reader, opts soap.Upload) error {
  165. if opts.Progress == nil {
  166. opts.Progress = item
  167. } else {
  168. opts.Progress = progress.Tee(item, opts.Progress)
  169. }
  170. // Non-disk files (such as .iso) use the PUT method.
  171. // Overwrite: t header is also required in this case (ovftool does the same)
  172. if item.Create {
  173. opts.Method = "PUT"
  174. opts.Headers = map[string]string{
  175. "Overwrite": "t",
  176. }
  177. } else {
  178. opts.Method = "POST"
  179. opts.Type = "application/x-vnd.vmware-streamVmdk"
  180. }
  181. return l.c.Upload(ctx, f, item.URL, &opts)
  182. }
  183. func (l *Lease) DownloadFile(ctx context.Context, file string, item FileItem, opts soap.Download) error {
  184. if opts.Progress == nil {
  185. opts.Progress = item
  186. } else {
  187. opts.Progress = progress.Tee(item, opts.Progress)
  188. }
  189. return l.c.DownloadFile(ctx, file, item.URL, &opts)
  190. }