docker-event-handler/events/volume.go
2022-03-25 12:17:47 +01:00

58 lines
1.3 KiB
Go

package events
import (
"context"
"log"
"github.com/docker/docker/api/types"
dockerEvents "github.com/docker/docker/api/types/events"
"github.com/docker/docker/client"
)
type Volume struct {
Action string
ID string
Destination *string
Volume *types.Volume
Container *types.ContainerJSON
}
//goland:noinspection ALL
func (c Volume) __interface_event() {
panic("interface event guard")
}
func makeVolume(message dockerEvents.Message, client *client.Client) Event {
var e Volume
e.ID = message.Actor.ID
e.Action = message.Action
if message.Action != "destroy" {
volume, err := client.VolumeInspect(context.TODO(), message.Actor.ID)
if err != nil {
log.Printf("error inspecting volume %v: %v", message.Actor.ID, err)
} else {
e.Volume = &volume
}
}
if message.Action == "mount" || message.Action == "unmount" {
if containerId, ok := message.Actor.Attributes["container"]; ok {
container, err := client.ContainerInspect(context.TODO(), containerId)
if err != nil {
log.Printf("error inspecting container %v: %v", containerId, err)
} else {
e.Container = &container
}
}
}
if message.Action == "mount" {
if dest, ok := message.Actor.Attributes["destination"]; ok {
e.Destination = &dest
}
}
return e
}