1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

api: Hide UpdateStatus when it is not present

When UpdateStatus was not present, the empty values of the timestamps
would be present:

        "UpdateStatus": {
            "StartedAt": "0001-01-01T00:00:00Z",
            "CompletedAt": "0001-01-01T00:00:00Z"
        }

To fix this, make the timestamps pointers, so they can be set to nil
when they should not be shown.

Also make UpdateStatus itself a pointer, so an empty object does not
show up when there is no UpdateStatus.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2016-12-01 14:08:06 -08:00
parent 9c2f1669a0
commit 0e70d96a68
5 changed files with 33 additions and 16 deletions

View file

@ -6,10 +6,10 @@ import "time"
type Service struct { type Service struct {
ID string ID string
Meta Meta
Spec ServiceSpec `json:",omitempty"` Spec ServiceSpec `json:",omitempty"`
PreviousSpec *ServiceSpec `json:",omitempty"` PreviousSpec *ServiceSpec `json:",omitempty"`
Endpoint Endpoint `json:",omitempty"` Endpoint Endpoint `json:",omitempty"`
UpdateStatus UpdateStatus `json:",omitempty"` UpdateStatus *UpdateStatus `json:",omitempty"`
} }
// ServiceSpec represents the spec of a service. // ServiceSpec represents the spec of a service.
@ -50,8 +50,8 @@ const (
// UpdateStatus reports the status of a service update. // UpdateStatus reports the status of a service update.
type UpdateStatus struct { type UpdateStatus struct {
State UpdateState `json:",omitempty"` State UpdateState `json:",omitempty"`
StartedAt time.Time `json:",omitempty"` StartedAt *time.Time `json:",omitempty"`
CompletedAt time.Time `json:",omitempty"` CompletedAt *time.Time `json:",omitempty"`
Message string `json:",omitempty"` Message string `json:",omitempty"`
} }

View file

@ -28,7 +28,9 @@ Service Mode:
{{- if .HasUpdateStatus }} {{- if .HasUpdateStatus }}
UpdateStatus: UpdateStatus:
State: {{ .UpdateStatusState }} State: {{ .UpdateStatusState }}
{{- if .HasUpdateStatusStarted }}
Started: {{ .UpdateStatusStarted }} Started: {{ .UpdateStatusStarted }}
{{- end }}
{{- if .UpdateIsCompleted }} {{- if .UpdateIsCompleted }}
Completed: {{ .UpdateStatusCompleted }} Completed: {{ .UpdateStatusCompleted }}
{{- end }} {{- end }}
@ -172,23 +174,27 @@ func (ctx *serviceInspectContext) ModeReplicatedReplicas() *uint64 {
} }
func (ctx *serviceInspectContext) HasUpdateStatus() bool { func (ctx *serviceInspectContext) HasUpdateStatus() bool {
return ctx.Service.UpdateStatus.State != "" return ctx.Service.UpdateStatus != nil && ctx.Service.UpdateStatus.State != ""
} }
func (ctx *serviceInspectContext) UpdateStatusState() swarm.UpdateState { func (ctx *serviceInspectContext) UpdateStatusState() swarm.UpdateState {
return ctx.Service.UpdateStatus.State return ctx.Service.UpdateStatus.State
} }
func (ctx *serviceInspectContext) HasUpdateStatusStarted() bool {
return ctx.Service.UpdateStatus.StartedAt != nil
}
func (ctx *serviceInspectContext) UpdateStatusStarted() string { func (ctx *serviceInspectContext) UpdateStatusStarted() string {
return units.HumanDuration(time.Since(ctx.Service.UpdateStatus.StartedAt)) return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.StartedAt))
} }
func (ctx *serviceInspectContext) UpdateIsCompleted() bool { func (ctx *serviceInspectContext) UpdateIsCompleted() bool {
return ctx.Service.UpdateStatus.State == swarm.UpdateStateCompleted return ctx.Service.UpdateStatus.State == swarm.UpdateStateCompleted && ctx.Service.UpdateStatus.CompletedAt != nil
} }
func (ctx *serviceInspectContext) UpdateStatusCompleted() string { func (ctx *serviceInspectContext) UpdateStatusCompleted() string {
return units.HumanDuration(time.Since(ctx.Service.UpdateStatus.CompletedAt)) return units.HumanDuration(time.Since(*ctx.Service.UpdateStatus.CompletedAt))
} }
func (ctx *serviceInspectContext) UpdateStatusMessage() string { func (ctx *serviceInspectContext) UpdateStatusMessage() string {

View file

@ -74,9 +74,9 @@ func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time)
}, },
}, },
}, },
UpdateStatus: swarm.UpdateStatus{ UpdateStatus: &swarm.UpdateStatus{
StartedAt: now, StartedAt: &now,
CompletedAt: now, CompletedAt: &now,
}, },
} }

View file

@ -26,8 +26,8 @@ func ServiceFromGRPC(s swarmapi.Service) types.Service {
service.UpdatedAt, _ = ptypes.Timestamp(s.Meta.UpdatedAt) service.UpdatedAt, _ = ptypes.Timestamp(s.Meta.UpdatedAt)
// UpdateStatus // UpdateStatus
service.UpdateStatus = types.UpdateStatus{}
if s.UpdateStatus != nil { if s.UpdateStatus != nil {
service.UpdateStatus = &types.UpdateStatus{}
switch s.UpdateStatus.State { switch s.UpdateStatus.State {
case swarmapi.UpdateStatus_UPDATING: case swarmapi.UpdateStatus_UPDATING:
service.UpdateStatus.State = types.UpdateStateUpdating service.UpdateStatus.State = types.UpdateStateUpdating
@ -37,8 +37,16 @@ func ServiceFromGRPC(s swarmapi.Service) types.Service {
service.UpdateStatus.State = types.UpdateStateCompleted service.UpdateStatus.State = types.UpdateStateCompleted
} }
service.UpdateStatus.StartedAt, _ = ptypes.Timestamp(s.UpdateStatus.StartedAt) startedAt, _ := ptypes.Timestamp(s.UpdateStatus.StartedAt)
service.UpdateStatus.CompletedAt, _ = ptypes.Timestamp(s.UpdateStatus.CompletedAt) if !startedAt.IsZero() {
service.UpdateStatus.StartedAt = &startedAt
}
completedAt, _ := ptypes.Timestamp(s.UpdateStatus.CompletedAt)
if !completedAt.IsZero() {
service.UpdateStatus.CompletedAt = &completedAt
}
service.UpdateStatus.Message = s.UpdateStatus.Message service.UpdateStatus.Message = s.UpdateStatus.Message
} }

View file

@ -155,6 +155,9 @@ func (d *SwarmDaemon) checkServiceRunningTasks(service string) func(*check.C) (i
func (d *SwarmDaemon) checkServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) { func (d *SwarmDaemon) checkServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) {
return func(c *check.C) (interface{}, check.CommentInterface) { return func(c *check.C) (interface{}, check.CommentInterface) {
service := d.getService(c, service) service := d.getService(c, service)
if service.UpdateStatus == nil {
return "", nil
}
return service.UpdateStatus.State, nil return service.UpdateStatus.State, nil
} }
} }