Skip to content

Commit

Permalink
client: Create events listeners before making the op request
Browse files Browse the repository at this point in the history
To ensure we have begun listening for events before the operation has
completed, we should explicitly create the events listener for the
operation before actually initiating the request that creates and starts
the operation.

Signed-off-by: Max Asnaashari <max.asnaashari@canonical.com>
  • Loading branch information
masnax committed Feb 13, 2024
1 parent 9e78234 commit af07b25
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/lxd_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func (r *ProtocolLXD) CreateContainerFromBackup(args ContainerBackupArgs) (Opera
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("X-LXD-pool", args.PoolName)

// Set up the events listener before making the request so that
// the operation doesn't complete and emit the event before we've begun listening.
listener, err := r.getEvents(false)
if err != nil {
return nil, err
}

// Send the request
resp, err := r.DoHTTP(req)
if err != nil {
Expand All @@ -141,6 +148,7 @@ func (r *ProtocolLXD) CreateContainerFromBackup(args ContainerBackupArgs) (Opera
// Setup an Operation wrapper
op := operation{
Operation: *respOperation,
listener: listener,
r: r,
chActive: make(chan bool),
}
Expand Down
8 changes: 8 additions & 0 deletions client/lxd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ func (r *ProtocolLXD) CreateImage(image api.ImagesPost, args *ImageCreateArgs) (
return nil, err
}

// Set up the events listener before making the request so that
// the operation doesn't complete and emit the event before we've begun listening.
listener, err := r.getEvents(false)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", reqURL, body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -567,6 +574,7 @@ func (r *ProtocolLXD) CreateImage(image api.ImagesPost, args *ImageCreateArgs) (
op := operation{
Operation: *respOperation,
r: r,
listener: listener,
chActive: make(chan bool),
}

Expand Down
8 changes: 8 additions & 0 deletions client/lxd_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,13 @@ func (r *ProtocolLXD) CreateInstanceFromBackup(args InstanceBackupArgs) (Operati
req.Header.Set("X-LXD-name", args.Name)
}

// Set up the events listener before making the request so that
// the operation doesn't complete and emit the event before we've begun listening.
listener, err := r.getEvents(false)
if err != nil {
return nil, err
}

// Send the request
resp, err := r.DoHTTP(req)
if err != nil {
Expand All @@ -614,6 +621,7 @@ func (r *ProtocolLXD) CreateInstanceFromBackup(args InstanceBackupArgs) (Operati
op := operation{
Operation: *respOperation,
r: r,
listener: listener,
chActive: make(chan bool),
}

Expand Down
16 changes: 16 additions & 0 deletions client/lxd_storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,13 @@ func (r *ProtocolLXD) CreateStoragePoolVolumeFromISO(pool string, args StoragePo
req.Header.Set("X-LXD-name", args.Name)
req.Header.Set("X-LXD-type", "iso")

// Set up the events listener before making the request so that
// the operation doesn't complete and emit the event before we've begun listening.
listener, err := r.getEvents(false)
if err != nil {
return nil, err
}

// Send the request.
resp, err := r.DoHTTP(req)
if err != nil {
Expand All @@ -1057,6 +1064,7 @@ func (r *ProtocolLXD) CreateStoragePoolVolumeFromISO(pool string, args StoragePo
// Setup an Operation wrapper.
op := operation{
Operation: *respOperation,
listener: listener,
r: r,
chActive: make(chan bool),
}
Expand Down Expand Up @@ -1097,6 +1105,13 @@ func (r *ProtocolLXD) CreateStoragePoolVolumeFromBackup(pool string, args Storag
req.Header.Set("X-LXD-name", args.Name)
}

// Set up the events listener before making the request so that
// the operation doesn't complete and emit the event before we've begun listening.
listener, err := r.getEvents(false)
if err != nil {
return nil, err
}

// Send the request.
resp, err := r.DoHTTP(req)
if err != nil {
Expand All @@ -1120,6 +1135,7 @@ func (r *ProtocolLXD) CreateStoragePoolVolumeFromBackup(pool string, args Storag
// Setup an Operation wrapper.
op := operation{
Operation: *respOperation,
listener: listener,
r: r,
chActive: make(chan bool),
}
Expand Down

0 comments on commit af07b25

Please sign in to comment.