github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/presence.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/core/presence" 11 ) 12 13 // ModelPresence represents the API server connections for a model. 14 type ModelPresence interface { 15 // For a given non controller agent, return the Status for that agent. 16 AgentStatus(agent string) (presence.Status, error) 17 } 18 19 // ModelPresenceContext represents the known agent presence state for the 20 // entire model. 21 type ModelPresenceContext struct { 22 // Presence represents the API server connections for a model. 23 // If this is non-nil it is used in preference to the state AgentPresence method. 24 Presence ModelPresence 25 } 26 27 func (c *ModelPresenceContext) machinePresence(machine MachineStatusGetter) (bool, error) { 28 if c.Presence == nil { 29 return machine.AgentPresence() 30 } 31 agent := names.NewMachineTag(machine.Id()) 32 status, err := c.Presence.AgentStatus(agent.String()) 33 return status == presence.Alive, err 34 } 35 36 func (c *ModelPresenceContext) unitPresence(unit UnitStatusGetter) (bool, error) { 37 if c.Presence == nil { 38 return unit.AgentPresence() 39 } 40 agent := names.NewUnitTag(unit.Name()).String() 41 if !unit.ShouldBeAssigned() { 42 // Units in CAAS models rely on the operator pings. 43 // These are for the application itself. 44 appName, err := names.UnitApplication(unit.Name()) 45 if err != nil { 46 return false, errors.Trace(err) 47 } 48 agent = names.NewApplicationTag(appName).String() 49 } 50 status, err := c.Presence.AgentStatus(agent) 51 return status == presence.Alive, err 52 }