Skip to content

Commit 4222c9e

Browse files
author
Daniel Schmidt
committed
ISSUE-593: Conform Docker::Event to Engine spec
- Refactors Docker::Event to provide access to its source (JSON) data. - Adds `scope` to the Docker::Event class.
1 parent 7e19faf commit 4222c9e

2 files changed

Lines changed: 60 additions & 50 deletions

File tree

lib/docker/event.rb

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
# frozen_string_literal: true
22

33
# This class represents a Docker Event.
4+
# @see https://github.com/moby/moby/blob/master/api/types/events/events.go
5+
# @see https://docs.docker.com/reference/api/engine/version/v1.49/#tag/System/operation/SystemEvents
46
class Docker::Event
57
include Docker::Error
68

79
# Represents the actor object nested within an event
810
class Actor
9-
attr_accessor :ID, :Attributes
11+
attr_reader :info
1012

11-
def initialize(actor_attributes = {})
12-
[:ID, :Attributes].each do |sym|
13-
value = actor_attributes[sym]
14-
if value.nil?
15-
value = actor_attributes[sym.to_s]
16-
end
17-
send("#{sym}=", value)
18-
end
13+
def initialize(actor_data = {})
14+
@info = actor_data.transform_keys { |k| k.downcase.to_sym }
15+
end
1916

20-
if self.Attributes.nil?
21-
self.Attributes = {}
22-
end
17+
def id
18+
info[:id]
2319
end
20+
alias_method :ID, :id
2421

25-
alias_method :id, :ID
26-
alias_method :attributes, :Attributes
22+
def attributes
23+
info[:attributes] || {}
24+
end
25+
alias_method :Attributes, :attributes
2726
end
2827

2928
class << self
@@ -43,52 +42,61 @@ def since(since, opts = {}, conn = Docker.connection, &block)
4342

4443
def new_event(body, remaining, total)
4544
return if body.nil? || body.empty?
46-
json = Docker::Util.parse_json(body)
47-
Docker::Event.new(json)
45+
info = Docker::Util.parse_json(body)
46+
Docker::Event.new(info)
4847
end
4948
end
5049

51-
attr_accessor :Type, :Action, :time, :timeNano
52-
attr_reader :Actor
53-
# Deprecated interface
54-
attr_accessor :status, :from
55-
56-
def initialize(event_attributes = {})
57-
[:Type, :Action, :Actor, :time, :timeNano, :status, :from].each do |sym|
58-
value = event_attributes[sym]
59-
if value.nil?
60-
value = event_attributes[sym.to_s]
61-
end
62-
send("#{sym}=", value)
63-
end
50+
attr_reader :info
6451

65-
if @Actor.nil?
66-
value = event_attributes[:id]
67-
if value.nil?
68-
value = event_attributes['id']
69-
end
70-
self.Actor = Actor.new(ID: value)
71-
end
52+
def initialize(event_data = {})
53+
@info = event_data.transform_keys { |k| k.downcase.to_sym }
7254
end
7355

74-
def ID
75-
self.actor.ID
56+
def action
57+
info[:action]
7658
end
59+
alias_method :Action, :action
7760

78-
def Actor=(actor)
79-
return if actor.nil?
80-
if actor.is_a? Actor
81-
@Actor = actor
82-
else
83-
@Actor = Actor.new(actor)
84-
end
61+
def actor
62+
@actor = Actor.new(info[:actor] || {}) if !defined? @actor
63+
@actor
64+
end
65+
alias_method :Actor, :actor
66+
67+
def from
68+
# @deprecated Use `actor.attributes['image']` instead
69+
# Only applicable to container events. See Docker docs for details.
70+
info[:from] || actor.attributes['image']
71+
end
72+
73+
def id
74+
# @deprecated Use `actor.id` instead
75+
info[:id] || actor.id
76+
end
77+
78+
def scope
79+
info[:scope]
80+
end
81+
82+
def status
83+
# @deprecated Use `action` instead
84+
info[:status] || action
8585
end
8686

87-
alias_method :type, :Type
88-
alias_method :action, :Action
89-
alias_method :actor, :Actor
90-
alias_method :time_nano, :timeNano
91-
alias_method :id, :ID
87+
def time
88+
info[:time]
89+
end
90+
91+
def time_nano
92+
info[:timenano]
93+
end
94+
alias_method :timeNano, :time_nano
95+
96+
def type
97+
info[:type]
98+
end
99+
alias_method :Type, :type
92100

93101
def to_s
94102
if type.nil? && action.nil?

spec/docker/event_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'Type' => 'container',
1919
'from' => 'tianon/true',
2020
'id' => 'bb2c783a32330b726f18d1eb44d80c899ef45771b4f939326e0fefcfc7e05db8',
21+
'scope' => 'local',
2122
'status' => 'start',
2223
'time' => 1461083270,
2324
'timeNano' => 1461083270652069004
@@ -155,6 +156,7 @@
155156
event.actor.id
156157
).to eq('bb2c783a32330b726f18d1eb44d80c899ef45771b4f939326e0fefcfc7e05db8')
157158
expect(event.actor.attributes).to eq('image' => 'tianon/true', 'name' => 'true-dat')
159+
expect(event.scope).to eq('local')
158160
expect(event.time).to eq 1461083270
159161
expect(event.time_nano).to eq 1461083270652069004
160162
end

0 commit comments

Comments
 (0)