github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/domain/entities/events.go (about)

     1  package entities
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	libpodEvents "github.com/containers/podman/v2/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  	return &libpodEvents.Event{
    34  		ContainerExitCode: exitCode,
    35  		ID:                e.Actor.ID,
    36  		Image:             e.Actor.Attributes["image"],
    37  		Name:              e.Actor.Attributes["name"],
    38  		Status:            status,
    39  		Time:              time.Unix(e.Time, e.TimeNano),
    40  		Type:              t,
    41  	}
    42  }
    43  
    44  // ConvertToEntitiesEvent converts a libpod event to an entities one.
    45  func ConvertToEntitiesEvent(e libpodEvents.Event) *Event {
    46  	return &Event{dockerEvents.Message{
    47  		Type:   e.Type.String(),
    48  		Action: e.Status.String(),
    49  		Actor: dockerEvents.Actor{
    50  			ID: e.ID,
    51  			Attributes: map[string]string{
    52  				"image":             e.Image,
    53  				"name":              e.Name,
    54  				"containerExitCode": strconv.Itoa(e.ContainerExitCode),
    55  			},
    56  		},
    57  		Scope:    "local",
    58  		Time:     e.Time.Unix(),
    59  		TimeNano: e.Time.UnixNano(),
    60  	}}
    61  }