github.com/vmware/govmomi@v0.43.0/vapi/library/library_item_updatesession.go (about) 1 /* 2 Copyright (c) 2018 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package library 18 19 import ( 20 "context" 21 "net/http" 22 "time" 23 24 "github.com/vmware/govmomi/vapi/internal" 25 "github.com/vmware/govmomi/vapi/rest" 26 ) 27 28 // Session is used to create an initial update or download session 29 type Session struct { 30 ClientProgress int64 `json:"client_progress,omitempty"` 31 ErrorMessage *rest.LocalizableMessage `json:"error_message,omitempty"` 32 ExpirationTime *time.Time `json:"expiration_time,omitempty"` 33 ID string `json:"id,omitempty"` 34 LibraryItemContentVersion string `json:"library_item_content_version,omitempty"` 35 LibraryItemID string `json:"library_item_id,omitempty"` 36 State string `json:"state,omitempty"` 37 } 38 39 // CreateLibraryItemUpdateSession creates a new library item 40 func (c *Manager) CreateLibraryItemUpdateSession(ctx context.Context, session Session) (string, error) { 41 url := c.Resource(internal.LibraryItemUpdateSession) 42 spec := struct { 43 CreateSpec Session `json:"create_spec"` 44 }{session} 45 var res string 46 return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) 47 } 48 49 // GetLibraryItemUpdateSession gets the update session information with status 50 func (c *Manager) GetLibraryItemUpdateSession(ctx context.Context, id string) (*Session, error) { 51 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id) 52 var res Session 53 return &res, c.Do(ctx, url.Request(http.MethodGet), &res) 54 } 55 56 // ListLibraryItemUpdateSession gets the list of update sessions 57 func (c *Manager) ListLibraryItemUpdateSession(ctx context.Context) ([]string, error) { 58 url := c.Resource(internal.LibraryItemUpdateSession) 59 var res []string 60 return res, c.Do(ctx, url.Request(http.MethodGet), &res) 61 } 62 63 // CancelLibraryItemUpdateSession cancels an update session 64 func (c *Manager) CancelLibraryItemUpdateSession(ctx context.Context, id string) error { 65 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("cancel") 66 return c.Do(ctx, url.Request(http.MethodPost), nil) 67 } 68 69 // CompleteLibraryItemUpdateSession completes an update session 70 func (c *Manager) CompleteLibraryItemUpdateSession(ctx context.Context, id string) error { 71 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("complete") 72 return c.Do(ctx, url.Request(http.MethodPost), nil) 73 } 74 75 // DeleteLibraryItemUpdateSession deletes an update session 76 func (c *Manager) DeleteLibraryItemUpdateSession(ctx context.Context, id string) error { 77 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id) 78 return c.Do(ctx, url.Request(http.MethodDelete), nil) 79 } 80 81 // FailLibraryItemUpdateSession fails an update session 82 func (c *Manager) FailLibraryItemUpdateSession(ctx context.Context, id string) error { 83 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("fail") 84 return c.Do(ctx, url.Request(http.MethodPost), nil) 85 } 86 87 // KeepAliveLibraryItemUpdateSession keeps an inactive update session alive. 88 func (c *Manager) KeepAliveLibraryItemUpdateSession(ctx context.Context, id string) error { 89 url := c.Resource(internal.LibraryItemUpdateSession).WithID(id).WithAction("keep-alive") 90 return c.Do(ctx, url.Request(http.MethodPost), nil) 91 } 92 93 // WaitOnLibraryItemUpdateSession blocks until the update session is no longer 94 // in the ACTIVE state. 95 func (c *Manager) WaitOnLibraryItemUpdateSession( 96 ctx context.Context, sessionID string, 97 interval time.Duration, intervalCallback func()) error { 98 99 // Wait until the upload operation is complete to return. 100 for { 101 session, err := c.GetLibraryItemUpdateSession(ctx, sessionID) 102 if err != nil { 103 return err 104 } 105 106 if session.State != "ACTIVE" { 107 if session.State == "ERROR" { 108 return session.ErrorMessage 109 } 110 return nil 111 } 112 time.Sleep(interval) 113 if intervalCallback != nil { 114 intervalCallback() 115 } 116 } 117 } 118 119 // CreateLibraryItemDownloadSession creates a new library item 120 func (c *Manager) CreateLibraryItemDownloadSession(ctx context.Context, session Session) (string, error) { 121 url := c.Resource(internal.LibraryItemDownloadSession) 122 spec := struct { 123 CreateSpec Session `json:"create_spec"` 124 }{session} 125 var res string 126 return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) 127 } 128 129 // GetLibraryItemDownloadSession gets the download session information with status 130 func (c *Manager) GetLibraryItemDownloadSession(ctx context.Context, id string) (*Session, error) { 131 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id) 132 var res Session 133 return &res, c.Do(ctx, url.Request(http.MethodGet), &res) 134 } 135 136 // ListLibraryItemDownloadSession gets the list of download sessions 137 func (c *Manager) ListLibraryItemDownloadSession(ctx context.Context) ([]string, error) { 138 url := c.Resource(internal.LibraryItemDownloadSession) 139 var res []string 140 return res, c.Do(ctx, url.Request(http.MethodGet), &res) 141 } 142 143 // CancelLibraryItemDownloadSession cancels an download session 144 func (c *Manager) CancelLibraryItemDownloadSession(ctx context.Context, id string) error { 145 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("cancel") 146 return c.Do(ctx, url.Request(http.MethodPost), nil) 147 } 148 149 // DeleteLibraryItemDownloadSession deletes an download session 150 func (c *Manager) DeleteLibraryItemDownloadSession(ctx context.Context, id string) error { 151 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id) 152 return c.Do(ctx, url.Request(http.MethodDelete), nil) 153 } 154 155 // FailLibraryItemDownloadSession fails an download session 156 func (c *Manager) FailLibraryItemDownloadSession(ctx context.Context, id string) error { 157 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("fail") 158 return c.Do(ctx, url.Request(http.MethodPost), nil) 159 } 160 161 // KeepAliveLibraryItemDownloadSession keeps an inactive download session alive. 162 func (c *Manager) KeepAliveLibraryItemDownloadSession(ctx context.Context, id string) error { 163 url := c.Resource(internal.LibraryItemDownloadSession).WithID(id).WithAction("keep-alive") 164 return c.Do(ctx, url.Request(http.MethodPost), nil) 165 }