Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import LXD changes #155

Merged
merged 9 commits into from
Oct 11, 2023
25 changes: 14 additions & 11 deletions client/incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (r *ProtocolIncus) RawWebsocket(path string) (*websocket.Conn, error) {
// RawOperation allows direct querying of an Incus API endpoint returning
// background operations.
func (r *ProtocolIncus) RawOperation(method string, path string, data any, ETag string) (Operation, string, error) {
return r.queryOperation(method, path, data, ETag)
return r.queryOperation(method, path, data, ETag, true)
}

// Internal functions.
Expand Down Expand Up @@ -363,12 +363,14 @@ func (r *ProtocolIncus) queryStruct(method string, path string, data any, ETag s
}

// queryOperation sends a query to the Incus server and then converts the response metadata into an Operation object.
// It sets up an early event listener, performs the query, processes the response, and manages the lifecycle of the event listener.
func (r *ProtocolIncus) queryOperation(method string, path string, data any, ETag string) (Operation, string, error) {
// Attempt to setup an early event listener
listener, err := r.GetEvents()
if err != nil {
listener = nil
// If useEventListener is true it will set up an early event listener and manage its lifecycle.
// If useEventListener is false, it will not set up an event listener and calls to Operation.Wait will use the operations API instead.
// In this case the returned Operation will error if the user calls Operation.AddHandler or Operation.RemoveHandler.
func (r *ProtocolIncus) queryOperation(method string, path string, data any, ETag string, useEventListener bool) (Operation, string, error) {
// Attempt to setup an early event listener if requested.
var listener *EventListener
if useEventListener {
listener, _ = r.GetEvents()
}

// Send the query
Expand All @@ -393,10 +395,11 @@ func (r *ProtocolIncus) queryOperation(method string, path string, data any, ETa

// Setup an Operation wrapper
op := operation{
Operation: *respOperation,
r: r,
listener: listener,
chActive: make(chan bool),
Operation: *respOperation,
r: r,
listener: listener,
chActive: make(chan bool),
skipListener: !useEventListener,
}

// Log the data
Expand Down
2 changes: 1 addition & 1 deletion client/incus_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *ProtocolIncus) CreateCertificateToken(certificate api.CertificatesPost)
}

// Send the request
op, _, err := r.queryOperation("POST", "/certificates", certificate, "")
op, _, err := r.queryOperation("POST", "/certificates", certificate, "", true)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions client/incus_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *ProtocolIncus) UpdateCluster(cluster api.ClusterPut, ETag string) (Oper
}
}

op, _, err := r.queryOperation("PUT", "/cluster", cluster, "")
op, _, err := r.queryOperation("PUT", "/cluster", cluster, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (r *ProtocolIncus) CreateClusterMember(member api.ClusterMembersPost) (Oper
return nil, fmt.Errorf("The server is missing the required \"clustering_join_token\" API extension")
}

op, _, err := r.queryOperation("POST", "/cluster/members", member, "")
op, _, err := r.queryOperation("POST", "/cluster/members", member, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,7 +195,7 @@ func (r *ProtocolIncus) UpdateClusterMemberState(name string, state api.ClusterM
return nil, fmt.Errorf("The server is missing the required \"clustering_evacuation\" API extension")
}

op, _, err := r.queryOperation("POST", fmt.Sprintf("/cluster/members/%s/state", name), state, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/cluster/members/%s/state", name), state, "", true)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions client/incus_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (r *ProtocolIncus) CreateImage(image api.ImagesPost, args *ImageCreateArgs)

// Send the JSON based request
if args == nil {
op, _, err := r.queryOperation("POST", "/images", image, "")
op, _, err := r.queryOperation("POST", "/images", image, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -917,7 +917,7 @@ func (r *ProtocolIncus) UpdateImage(fingerprint string, image api.ImagePut, ETag
// DeleteImage requests that Incus removes an image from the store.
func (r *ProtocolIncus) DeleteImage(fingerprint string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/images/%s", url.PathEscape(fingerprint)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/images/%s", url.PathEscape(fingerprint)), nil, "", true)
if err != nil {
return nil, err
}
Expand All @@ -932,7 +932,7 @@ func (r *ProtocolIncus) RefreshImage(fingerprint string) (Operation, error) {
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/refresh", url.PathEscape(fingerprint)), nil, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/refresh", url.PathEscape(fingerprint)), nil, "", true)
if err != nil {
return nil, err
}
Expand All @@ -943,7 +943,7 @@ func (r *ProtocolIncus) RefreshImage(fingerprint string) (Operation, error) {
// CreateImageSecret requests that Incus issues a temporary image secret.
func (r *ProtocolIncus) CreateImageSecret(fingerprint string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/secret", url.PathEscape(fingerprint)), nil, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/secret", url.PathEscape(fingerprint)), nil, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (r *ProtocolIncus) ExportImage(fingerprint string, image api.ImageExportPos
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/export", url.PathEscape(fingerprint)), &image, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/images/%s/export", url.PathEscape(fingerprint)), &image, "", true)
if err != nil {
return nil, err
}
Expand Down
40 changes: 20 additions & 20 deletions client/incus_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (r *ProtocolIncus) UpdateInstances(state api.InstancesPut, ETag string) (Op
}

// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s?%s", path, v.Encode()), state, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s?%s", path, v.Encode()), state, ETag, true)
if err != nil {
return nil, err
}
Expand All @@ -209,7 +209,7 @@ func (r *ProtocolIncus) rebuildInstance(instanceName string, instance api.Instan
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/rebuild?project=%s", path, url.PathEscape(instanceName), r.project), instance, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/rebuild", path, url.PathEscape(instanceName)), instance, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -523,7 +523,7 @@ func (r *ProtocolIncus) CreateInstanceFromBackup(args InstanceBackupArgs) (Opera

if args.PoolName == "" && args.Name == "" {
// Send the request
op, _, err := r.queryOperation("POST", path, args.BackupFile, "")
op, _, err := r.queryOperation("POST", path, args.BackupFile, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -604,7 +604,7 @@ func (r *ProtocolIncus) CreateInstance(instance api.InstancesPost) (Operation, e
}

// Send the request
op, _, err := r.queryOperation("POST", path, instance, "")
op, _, err := r.queryOperation("POST", path, instance, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -943,7 +943,7 @@ func (r *ProtocolIncus) UpdateInstance(name string, instance api.InstancePut, ET
}

// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, ETag, true)
if err != nil {
return nil, err
}
Expand All @@ -964,7 +964,7 @@ func (r *ProtocolIncus) RenameInstance(name string, instance api.InstancePost) (
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1060,7 +1060,7 @@ func (r *ProtocolIncus) MigrateInstance(name string, instance api.InstancePost)
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), instance, "", true)
if err != nil {
return nil, err
}
Expand All @@ -1076,7 +1076,7 @@ func (r *ProtocolIncus) DeleteInstance(name string) (Operation, error) {
}

// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s", path, url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1112,7 +1112,7 @@ func (r *ProtocolIncus) ExecInstance(instanceName string, exec api.InstanceExecP
}

// Send the request
op, _, err := r.queryOperation("POST", uri, exec, "")
op, _, err := r.queryOperation("POST", uri, exec, "", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1681,7 +1681,7 @@ func (r *ProtocolIncus) CreateInstanceSnapshot(instanceName string, snapshot api
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots", path, url.PathEscape(instanceName)), snapshot, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots", path, url.PathEscape(instanceName)), snapshot, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1928,7 +1928,7 @@ func (r *ProtocolIncus) RenameInstanceSnapshot(instanceName string, name string,
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2004,7 +2004,7 @@ func (r *ProtocolIncus) MigrateInstanceSnapshot(instanceName string, name string
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, "", true)
if err != nil {
return nil, err
}
Expand All @@ -2020,7 +2020,7 @@ func (r *ProtocolIncus) DeleteInstanceSnapshot(instanceName string, name string)
}

// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand All @@ -2040,7 +2040,7 @@ func (r *ProtocolIncus) UpdateInstanceSnapshot(instanceName string, name string,
}

// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s/snapshots/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), instance, ETag, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2082,7 +2082,7 @@ func (r *ProtocolIncus) UpdateInstanceState(name string, state api.InstanceState
}

// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s/state", path, url.PathEscape(name)), state, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("%s/%s/state", path, url.PathEscape(name)), state, ETag, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2405,7 +2405,7 @@ func (r *ProtocolIncus) ConsoleInstance(instanceName string, console api.Instanc
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/console", path, url.PathEscape(instanceName)), console, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/console", path, url.PathEscape(instanceName)), console, "", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2493,7 +2493,7 @@ func (r *ProtocolIncus) ConsoleInstanceDynamic(instanceName string, console api.
}

// Send the request.
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/console", path, url.PathEscape(instanceName)), console, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/console", path, url.PathEscape(instanceName)), console, "", true)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -2699,7 +2699,7 @@ func (r *ProtocolIncus) CreateInstanceBackup(instanceName string, backup api.Ins
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/backups", path, url.PathEscape(instanceName)), backup, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/backups", path, url.PathEscape(instanceName)), backup, "", true)
if err != nil {
return nil, err
}
Expand All @@ -2719,7 +2719,7 @@ func (r *ProtocolIncus) RenameInstanceBackup(instanceName string, name string, b
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/backups/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), backup, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("%s/%s/backups/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), backup, "", true)
if err != nil {
return nil, err
}
Expand All @@ -2739,7 +2739,7 @@ func (r *ProtocolIncus) DeleteInstanceBackup(instanceName string, name string) (
}

// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s/backups/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("%s/%s/backups/%s", path, url.PathEscape(instanceName), url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client/incus_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (r *ProtocolIncus) RenameProject(name string, project api.ProjectPost) (Ope
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/projects/%s", url.PathEscape(name)), project, "", true)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading