github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/status/status_history.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package status 5 6 import ( 7 "time" 8 9 "github.com/juju/collections/set" 10 "github.com/juju/errors" 11 ) 12 13 // StatusHistoryFilter holds arguments that can be use to filter a status history backlog. 14 type StatusHistoryFilter struct { 15 // Size indicates how many results are expected at most. 16 Size int 17 // FromDate indicates the earliest date from which logs are expected. 18 FromDate *time.Time 19 // Delta indicates the age of the oldest log expected. 20 Delta *time.Duration 21 // Exclude indicates the status messages that should be excluded 22 // from the returned result. 23 Exclude set.Strings 24 } 25 26 // Validate checks that the minimum requirements of a StatusHistoryFilter are met. 27 func (f *StatusHistoryFilter) Validate() error { 28 s := f.Size > 0 29 t := f.FromDate != nil 30 d := f.Delta != nil 31 32 switch { 33 case !(s || t || d): 34 return errors.NotValidf("missing filter parameters") 35 case s && t: 36 return errors.NotValidf("Size and Date together") 37 case s && d: 38 return errors.NotValidf("Size and Delta together") 39 case t && d: 40 return errors.NotValidf("Date and Delta together") 41 } 42 return nil 43 } 44 45 // StatusHistoryGetter instances can fetch their status history. 46 type StatusHistoryGetter interface { 47 StatusHistory(filter StatusHistoryFilter) ([]StatusInfo, error) 48 } 49 50 // InstanceStatusHistoryGetter instances can fetch their instance status history. 51 type InstanceStatusHistoryGetter interface { 52 InstanceStatusHistory(filter StatusHistoryFilter) ([]StatusInfo, error) 53 } 54 55 // DetailedStatus holds status info about a machine or unit agent. 56 type DetailedStatus struct { 57 Status Status 58 Info string 59 Data map[string]interface{} 60 Since *time.Time 61 Kind HistoryKind 62 // TODO(perrito666) make sure this is not used and remove. 63 Version string 64 Life string 65 Err error 66 } 67 68 // History holds many DetailedStatus, 69 type History []DetailedStatus 70 71 // HistoryKind represents the possible types of 72 // status history entries. 73 // 74 type HistoryKind string 75 76 // IMPORTANT DEV NOTE: when changing this HistoryKind list in anyway, these may need to be revised: 77 // 78 // * HistoryKind.Valid() 79 // * AllHistoryKind() 80 // * command help for 'show-status-log' describing these kinds. 81 const ( 82 // KindUnit represents agent and workload combined. 83 KindUnit HistoryKind = "unit" 84 // KindUnitAgent represent a unit agent status history entry. 85 KindUnitAgent HistoryKind = "juju-unit" 86 // KindWorkload represents a charm workload status history entry. 87 KindWorkload HistoryKind = "workload" 88 // KindMachineInstance represents an entry for a machine instance. 89 KindMachineInstance HistoryKind = "machine" 90 // KindMachine represents an entry for a machine agent. 91 KindMachine HistoryKind = "juju-machine" 92 // KindContainerInstance represents an entry for a container instance. 93 KindContainerInstance HistoryKind = "container" 94 // KindContainer represents an entry for a container agent. 95 KindContainer HistoryKind = "juju-container" 96 ) 97 98 // String returns a string representation of the HistoryKind. 99 func (k HistoryKind) String() string { 100 return string(k) 101 } 102 103 // Valid will return true if the current kind is a valid one. 104 func (k HistoryKind) Valid() bool { 105 switch k { 106 case KindUnit, KindUnitAgent, KindWorkload, 107 KindMachineInstance, KindMachine, 108 KindContainerInstance, KindContainer: 109 return true 110 } 111 return false 112 } 113 114 // AllHistoryKind will return all valid HistoryKinds. 115 func AllHistoryKind() map[HistoryKind]string { 116 return map[HistoryKind]string{ 117 KindUnit: "statuses for specified unit and its workload", 118 KindUnitAgent: "statuses from the agent that is managing a unit", 119 KindWorkload: "statuses for unit's workload", 120 KindMachineInstance: "statuses that occur due to provisioning of a machine", 121 KindMachine: "status of the agent that is managing a machine", 122 KindContainerInstance: "statuses from the agent that is managing containers", 123 KindContainer: "statuses from the containers only and not their host machines", 124 } 125 }