github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/entities/events.go (about)

     1  package entities
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	libpodEvents "github.com/hanks177/podman/v4/libpod/events"
     8  	dockerEvents "github.com/docker/docker/api/types/events"
     9  )
    10  
    11  // Event combines various event-related data such as time, event type, status
    12  // and more.
    13  type Event struct {
    14  	// TODO: it would be nice to have full control over the types at some
    15  	// point and fork such Docker types.
    16  	dockerEvents.Message
    17  }
    18  
    19  // ConvertToLibpodEvent converts an entities event to a libpod one.
    20  func ConvertToLibpodEvent(e Event) *libpodEvents.Event {
    21  	exitCode, err := strconv.Atoi(e.Actor.Attributes["containerExitCode"])
    22  	if err != nil {
    23  		return nil
    24  	}
    25  	status, err := libpodEvents.StringToStatus(e.Action)
    26  	if err != nil {
    27  		return nil
    28  	}
    29  	t, err := libpodEvents.StringToType(e.Type)
    30  	if err != nil {
    31  		return nil
    32  	}
    33  	image := e.Actor.Attributes["image"]
    34  	name := e.Actor.Attributes["name"]
    35  	details := e.Actor.Attributes
    36  	delete(details, "image")
    37  	delete(details, "name")
    38  	delete(details, "containerExitCode")
    39  	return &libpodEvents.Event{
    40  		ContainerExitCode: exitCode,
    41  		ID:                e.Actor.ID,
    42  		Image:             image,
    43  		Name:              name,
    44  		Status:            status,
    45  		Time:              time.Unix(0, e.TimeNano),
    46  		Type:              t,
    47  		Details: libpodEvents.Details{
    48  			Attributes: details,
    49  		},
    50  	}
    51  }
    52  
    53  // ConvertToEntitiesEvent converts a libpod event to an entities one.
    54  func ConvertToEntitiesEvent(e libpodEvents.Event) *Event {
    55  	attributes := e.Details.Attributes
    56  	if attributes == nil {
    57  		attributes = make(map[string]string)
    58  	}
    59  	attributes["image"] = e.Image
    60  	attributes["name"] = e.Name
    61  	attributes["containerExitCode"] = strconv.Itoa(e.ContainerExitCode)
    62  	return &Event{dockerEvents.Message{
    63  		// Compatibility with clients that still look for deprecated API elements
    64  		Status: e.Status.String(),
    65  		ID:     e.ID,
    66  		From:   e.Image,
    67  		Type:   e.Type.String(),
    68  		Action: e.Status.String(),
    69  		Actor: dockerEvents.Actor{
    70  			ID:         e.ID,
    71  			Attributes: attributes,
    72  		},
    73  		Scope:    "local",
    74  		Time:     e.Time.Unix(),
    75  		TimeNano: e.Time.UnixNano(),
    76  	}}
    77  }