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

client: Pass a flag into queryOperation to skip event listener setup if not required. #12349

Merged
merged 9 commits into from
Oct 10, 2023
25 changes: 14 additions & 11 deletions client/lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (r *ProtocolLXD) RawWebsocket(path string) (*websocket.Conn, error) {
// RawOperation allows direct querying of a LXD API endpoint returning
// background operations.
func (r *ProtocolLXD) 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 @@ -381,12 +381,14 @@ func (r *ProtocolLXD) queryStruct(method string, path string, data any, ETag str
}

// queryOperation sends a query to the LXD 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 *ProtocolLXD) 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 *ProtocolLXD) 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 @@ -411,10 +413,11 @@ func (r *ProtocolLXD) queryOperation(method string, path string, data any, ETag

// 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/lxd_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (r *ProtocolLXD) 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/lxd_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *ProtocolLXD) UpdateCluster(cluster api.ClusterPut, ETag string) (Operat
}
}

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 @@ -152,7 +152,7 @@ func (r *ProtocolLXD) CreateClusterMember(member api.ClusterMembersPost) (Operat
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 @@ -197,7 +197,7 @@ func (r *ProtocolLXD) UpdateClusterMemberState(name string, state api.ClusterMem
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
34 changes: 17 additions & 17 deletions client/lxd_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *ProtocolLXD) CreateContainerFromBackup(args ContainerBackupArgs) (Opera

if args.PoolName == "" {
// Send the request
op, _, err := r.queryOperation("POST", "/containers", args.BackupFile, "")
op, _, err := r.queryOperation("POST", "/containers", args.BackupFile, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (r *ProtocolLXD) CreateContainer(container api.ContainersPost) (Operation,
}

// Send the request
op, _, err := r.queryOperation("POST", "/containers", container, "")
op, _, err := r.queryOperation("POST", "/containers", container, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -505,7 +505,7 @@ func (r *ProtocolLXD) CopyContainer(source InstanceServer, container api.Contain
// UpdateContainer updates the container definition.
func (r *ProtocolLXD) UpdateContainer(name string, container api.ContainerPut, ETag string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, ETag, true)
if err != nil {
return nil, err
}
Expand All @@ -521,7 +521,7 @@ func (r *ProtocolLXD) RenameContainer(name string, container api.ContainerPost)
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -598,7 +598,7 @@ func (r *ProtocolLXD) MigrateContainer(name string, container api.ContainerPost)
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s", url.PathEscape(name)), container, "", true)
if err != nil {
return nil, err
}
Expand All @@ -609,7 +609,7 @@ func (r *ProtocolLXD) MigrateContainer(name string, container api.ContainerPost)
// DeleteContainer requests that LXD deletes the container.
func (r *ProtocolLXD) DeleteContainer(name string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/containers/%s", url.PathEscape(name)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/containers/%s", url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand All @@ -632,7 +632,7 @@ func (r *ProtocolLXD) ExecContainer(containerName string, exec api.ContainerExec
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/exec", url.PathEscape(containerName)), exec, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/exec", url.PathEscape(containerName)), exec, "", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -961,7 +961,7 @@ func (r *ProtocolLXD) CreateContainerSnapshot(containerName string, snapshot api
}

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

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), container, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), container, "", true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ func (r *ProtocolLXD) MigrateContainerSnapshot(containerName string, name string
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), container, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), container, "", true)
if err != nil {
return nil, err
}
Expand All @@ -1277,7 +1277,7 @@ func (r *ProtocolLXD) MigrateContainerSnapshot(containerName string, name string
// DeleteContainerSnapshot requests that LXD deletes the container snapshot.
func (r *ProtocolLXD) DeleteContainerSnapshot(containerName string, name string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), nil, "")
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/containers/%s/snapshots/%s", url.PathEscape(containerName), url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand All @@ -1293,7 +1293,7 @@ func (r *ProtocolLXD) UpdateContainerSnapshot(containerName string, name string,

// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("/containers/%s/snapshots/%s",
url.PathEscape(containerName), url.PathEscape(name)), container, ETag)
url.PathEscape(containerName), url.PathEscape(name)), container, ETag, true)
if err != nil {
return nil, err
}
Expand All @@ -1317,7 +1317,7 @@ func (r *ProtocolLXD) GetContainerState(name string) (*api.ContainerState, strin
// UpdateContainerState updates the container to match the requested state.
func (r *ProtocolLXD) UpdateContainerState(name string, state api.ContainerStatePut, ETag string) (Operation, error) {
// Send the request
op, _, err := r.queryOperation("PUT", fmt.Sprintf("/containers/%s/state", url.PathEscape(name)), state, ETag)
op, _, err := r.queryOperation("PUT", fmt.Sprintf("/containers/%s/state", url.PathEscape(name)), state, ETag, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1522,7 +1522,7 @@ func (r *ProtocolLXD) ConsoleContainer(containerName string, console api.Contain
}

// Send the request
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/console", url.PathEscape(containerName)), console, "")
op, _, err := r.queryOperation("POST", fmt.Sprintf("/containers/%s/console", url.PathEscape(containerName)), console, "", false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1698,7 +1698,7 @@ func (r *ProtocolLXD) CreateContainerBackup(containerName string, backup api.Con

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

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

// Send the request
op, _, err := r.queryOperation("DELETE", fmt.Sprintf("/containers/%s/backups/%s",
url.PathEscape(containerName), url.PathEscape(name)), nil, "")
url.PathEscape(containerName), url.PathEscape(name)), nil, "", true)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions client/lxd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ func (r *ProtocolLXD) 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 @@ -916,7 +916,7 @@ func (r *ProtocolLXD) UpdateImage(fingerprint string, image api.ImagePut, ETag s
// DeleteImage requests that LXD removes an image from the store.
func (r *ProtocolLXD) 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 @@ -931,7 +931,7 @@ func (r *ProtocolLXD) 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 @@ -942,7 +942,7 @@ func (r *ProtocolLXD) RefreshImage(fingerprint string) (Operation, error) {
// CreateImageSecret requests that LXD issues a temporary image secret.
func (r *ProtocolLXD) 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 @@ -1001,7 +1001,7 @@ func (r *ProtocolLXD) ExportImage(fingerprint string, image api.ImageExportPost)
}

// 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