MCPcopy
hub / github.com/lxc/incus / CreateInstanceBackupStream

Method CreateInstanceBackupStream

client/incus_instances.go:3044–3109  ·  view source on GitHub ↗

CreateInstanceBackupStream requests that Incus creates and returns new direct backup for the instance.

(instanceName string, backup api.InstanceBackupsPost, req *BackupFileRequest)

Source from the content-addressed store, hash-verified

3042// CreateInstanceBackupStream requests that Incus creates and returns new direct backup for the
3043// instance.
3044func (r *ProtocolIncus) CreateInstanceBackupStream(instanceName string, backup api.InstanceBackupsPost, req *BackupFileRequest) error {
3045 if !r.HasExtension("direct_backup") {
3046 return errors.New("The server is missing the required \"direct_backup\" API extension")
3047 }
3048
3049 path, _, err := r.instanceTypeToPath(api.InstanceTypeAny)
3050 if err != nil {
3051 return err
3052 }
3053
3054 // Build the URL
3055 uri := fmt.Sprintf("%s/1.0%s/%s/backups", r.httpBaseURL.String(), path, url.PathEscape(instanceName))
3056 if r.project != "" {
3057 uri += fmt.Sprintf("?project=%s", url.QueryEscape(r.project))
3058 }
3059
3060 // Encode the backup data
3061 buf := bytes.Buffer{}
3062 err = json.NewEncoder(&buf).Encode(backup)
3063 if err != nil {
3064 return err
3065 }
3066
3067 // Prepare the download request
3068 request, err := http.NewRequest("POST", uri, bytes.NewReader(buf.Bytes()))
3069 if err != nil {
3070 return err
3071 }
3072
3073 request.Header.Set("Accept", "application/octet-stream")
3074 if r.httpUserAgent != "" {
3075 request.Header.Set("User-Agent", r.httpUserAgent)
3076 }
3077
3078 // Start the request
3079 response, doneCh, err := cancel.CancelableDownload(req.Canceler, r.DoHTTP, request)
3080 if err != nil {
3081 return err
3082 }
3083
3084 defer logger.WarnOnError(response.Body.Close, "Failed to close response body")
3085 defer close(doneCh)
3086
3087 if response.StatusCode != http.StatusOK {
3088 _, _, err = incusParseResponse(response)
3089 if err != nil {
3090 return err
3091 }
3092 }
3093
3094 // Handle the data
3095 body := response.Body
3096 if req.ProgressHandler != nil {
3097 body = &ioprogress.ProgressReader{
3098 ReadCloser: response.Body,
3099 Tracker: &ioprogress.ProgressTracker{
3100 Handler: func(received int64, speed int64) {
3101 req.ProgressHandler(ioprogress.ProgressData{Text: fmt.Sprintf("%s (%s/s)", units.GetByteSizeString(received, 2), units.GetByteSizeString(speed, 2))})

Callers

nothing calls this directly

Calls 9

HasExtensionMethod · 0.95
instanceTypeToPathMethod · 0.95
CancelableDownloadFunction · 0.92
WarnOnErrorFunction · 0.92
GetByteSizeStringFunction · 0.92
SafeCopyFunction · 0.92
incusParseResponseFunction · 0.85
StringMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected