github.com/vmware/govmomi@v0.51.0/vapi/library/library_item_updatesession.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package library 6 7 import ( 8 "context" 9 "net/http" 10 "time" 11 12 "github.com/vmware/govmomi/vapi/internal" 13 "github.com/vmware/govmomi/vapi/rest" 14 ) 15 16 // Session is used to create an initial update or download session 17 type Session struct { 18 ClientProgress int64 `json:"client_progress,omitempty"` 19 ErrorMessage *rest.LocalizableMessage `json:"error_message,omitempty"` 20 ExpirationTime *time.Time `json:"expiration_time,omitempty"` 21 ID string `json:"id,omitempty"` 22 LibraryItemContentVersion string `json:"library_item_content_version,omitempty"` 23 LibraryItemID string `json:"library_item_id,omitempty"` 24 State string `json:"state,omitempty"` 25 } 26 27 // CreateLibraryItemUpdateSession creates a new library item 28 func (c *Manager) CreateLibraryItemUpdateSession(ctx context.Context, session Session) (string, error) { 29 url := c.Resource(internal.LibraryItemUpdateSession) 30 spec := struct { 31 CreateSpec Session `json:"create_spec"` 32 }{session} 33 var res string 34 return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) 35 } 36 37 // GetLibraryItemUpdateSession gets the update session information with status 38 func (c *Manager) GetLibraryItemUpdateSession(ctx context.Context, id string) (*Session, error) { 39 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id) 40 var res Session 41 return &res, c.Do(ctx, url.Request(http.MethodGet), &res) 42 } 43 44 // ListLibraryItemUpdateSession gets the list of update sessions 45 func (c *Manager) ListLibraryItemUpdateSession(ctx context.Context) ([]string, error) { 46 url := c.Resource(internal.LibraryItemUpdateSession) 47 var res []string 48 return res, c.Do(ctx, url.Request(http.MethodGet), &res) 49 } 50 51 // CancelLibraryItemUpdateSession cancels an update session 52 func (c *Manager) CancelLibraryItemUpdateSession(ctx context.Context, id string) error { 53 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("cancel") 54 return c.Do(ctx, url.Request(http.MethodPost), nil) 55 } 56 57 // CompleteLibraryItemUpdateSession completes an update session 58 func (c *Manager) CompleteLibraryItemUpdateSession(ctx context.Context, id string) error { 59 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("complete") 60 return c.Do(ctx, url.Request(http.MethodPost), nil) 61 } 62 63 // DeleteLibraryItemUpdateSession deletes an update session 64 func (c *Manager) DeleteLibraryItemUpdateSession(ctx context.Context, id string) error { 65 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id) 66 return c.Do(ctx, url.Request(http.MethodDelete), nil) 67 } 68 69 // FailLibraryItemUpdateSession fails an update session 70 func (c *Manager) FailLibraryItemUpdateSession(ctx context.Context, id string) error { 71 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("fail") 72 return c.Do(ctx, url.Request(http.MethodPost), nil) 73 } 74 75 // KeepAliveLibraryItemUpdateSession keeps an inactive update session alive. 76 func (c *Manager) KeepAliveLibraryItemUpdateSession(ctx context.Context, id string) error { 77 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("keep-alive") 78 return c.Do(ctx, url.Request(http.MethodPost), nil) 79 } 80 81 // WaitOnLibraryItemUpdateSession blocks until the update session is no longer 82 // in the ACTIVE state. 83 func (c *Manager) WaitOnLibraryItemUpdateSession( 84 ctx context.Context, sessionID string, 85 interval time.Duration, intervalCallback func()) error { 86 87 // Wait until the upload operation is complete to return. 88 for { 89 session, err := c.GetLibraryItemUpdateSession(ctx, sessionID) 90 if err != nil { 91 return err 92 } 93 94 if session.State != "ACTIVE" { 95 if session.State == "ERROR" { 96 return session.ErrorMessage 97 } 98 return nil 99 } 100 time.Sleep(interval) 101 if intervalCallback != nil { 102 intervalCallback() 103 } 104 } 105 } 106 107 // CreateLibraryItemDownloadSession creates a new library item 108 func (c *Manager) CreateLibraryItemDownloadSession(ctx context.Context, session Session) (string, error) { 109 url := c.Resource(internal.LibraryItemDownloadSession) 110 spec := struct { 111 CreateSpec Session `json:"create_spec"` 112 }{session} 113 var res string 114 return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) 115 } 116 117 // GetLibraryItemDownloadSession gets the download session information with status 118 func (c *Manager) GetLibraryItemDownloadSession(ctx context.Context, id string) (*Session, error) { 119 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id) 120 var res Session 121 return &res, c.Do(ctx, url.Request(http.MethodGet), &res) 122 } 123 124 // ListLibraryItemDownloadSession gets the list of download sessions 125 func (c *Manager) ListLibraryItemDownloadSession(ctx context.Context) ([]string, error) { 126 url := c.Resource(internal.LibraryItemDownloadSession) 127 var res []string 128 return res, c.Do(ctx, url.Request(http.MethodGet), &res) 129 } 130 131 // CancelLibraryItemDownloadSession cancels an download session 132 func (c *Manager) CancelLibraryItemDownloadSession(ctx context.Context, id string) error { 133 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("cancel") 134 return c.Do(ctx, url.Request(http.MethodPost), nil) 135 } 136 137 // DeleteLibraryItemDownloadSession deletes an download session 138 func (c *Manager) DeleteLibraryItemDownloadSession(ctx context.Context, id string) error { 139 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id) 140 return c.Do(ctx, url.Request(http.MethodDelete), nil) 141 } 142 143 // FailLibraryItemDownloadSession fails an download session 144 func (c *Manager) FailLibraryItemDownloadSession(ctx context.Context, id string) error { 145 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("fail") 146 return c.Do(ctx, url.Request(http.MethodPost), nil) 147 } 148 149 // KeepAliveLibraryItemDownloadSession keeps an inactive download session alive. 150 func (c *Manager) KeepAliveLibraryItemDownloadSession(ctx context.Context, id string) error { 151 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("keep-alive") 152 return c.Do(ctx, url.Request(http.MethodPost), nil) 153 }