@@ -15,6 +15,8 @@ import (
1515 "github.com/elastic/elastic-package/internal/signal"
1616)
1717
18+ var waitForPolicyAssignedTimeout = 10 * time .Minute
19+
1820// Agent represents an Elastic Agent enrolled with fleet.
1921type Agent struct {
2022 ID string `json:"id"`
@@ -27,6 +29,15 @@ type Agent struct {
2729 } `json:"local_metadata"`
2830}
2931
32+ // String method returns string representation of an agent.
33+ func (a * Agent ) String () string {
34+ b , err := json .Marshal (a )
35+ if err != nil {
36+ return err .Error ()
37+ }
38+ return string (b )
39+ }
40+
3041// ListAgents returns the list of agents enrolled with Fleet.
3142func (c * Client ) ListAgents () ([]Agent , error ) {
3243 statusCode , respBody , err := c .get (fmt .Sprintf ("%s/agents" , FleetAPI ))
@@ -71,7 +82,12 @@ func (c *Client) AssignPolicyToAgent(a Agent, p Policy) error {
7182}
7283
7384func (c * Client ) waitUntilPolicyAssigned (a Agent , p Policy ) error {
85+ timeout := time .Now ().Add (waitForPolicyAssignedTimeout )
7486 for {
87+ if time .Now ().After (timeout ) {
88+ return errors .New ("timeout: policy hasn't been assigned in time" )
89+ }
90+
7591 if signal .SIGINT () {
7692 return errors .New ("SIGINT: cancel waiting for policy assigned" )
7793 }
@@ -80,6 +96,7 @@ func (c *Client) waitUntilPolicyAssigned(a Agent, p Policy) error {
8096 if err != nil {
8197 return errors .Wrap (err , "can't get the agent" )
8298 }
99+ logger .Debugf ("Agent data: %s" , agent .String ())
83100
84101 if agent .PolicyID == p .ID && agent .PolicyRevision == p .Revision {
85102 logger .Debugf ("Policy revision assigned to the agent (ID: %s)..." , a .ID )
0 commit comments