in functionaltests/internal/ecclient/client.go [88:153]
func (c *Client) RestartIntegrationServer(ctx context.Context, deploymentID string) error {
res, err := deploymentapi.Get(deploymentapi.GetParams{
API: c.ecAPI,
DeploymentID: deploymentID,
})
if err != nil {
return fmt.Errorf("cannot retrieve ref id of integrations server for deployment %s: %w", deploymentID, err)
}
refID := *res.Resources.IntegrationsServer[0].RefID
// https://www.elastic.co/docs/api/doc/cloud/operation/operation-restart-deployment-stateless-resource
url := fmt.Sprintf("%s/api/v1/deployments/%s/integrations_server/%s/_restart", c.endpoint, deploymentID, refID)
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return fmt.Errorf("cannot create integrations server restart request for deployment %s: %w", deploymentID, err)
}
req = c.ecAPI.AuthWriter.AuthRequest(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("cannot execute HTTP request for restarting deployment %s: %w", deploymentID, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
b, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("cannot read body after receiving a %d response while restarting integrations server: %w", resp.StatusCode, err)
}
return fmt.Errorf("restarting integrations server returned %d response with content: %s", resp.StatusCode, b)
}
// Wait until the integration server is back online.
status := func() (string, error) {
r, err := c.ecAPI.V1API.Deployments.GetDeploymentIntegrationsServerResourceInfo(
deployments.NewGetDeploymentIntegrationsServerResourceInfoParams().
WithDeploymentID(deploymentID).
WithRefID(refID),
c.ecAPI.AuthWriter)
if err != nil {
return "", err
}
return *r.Payload.Info.Status, nil
}
timeout := 10 * time.Minute
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
tctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
for {
select {
case <-tctx.Done():
return fmt.Errorf("timeout reached waiting for integrations server to restart")
case <-ticker.C:
s, err := status()
if err != nil {
return fmt.Errorf("cannot retrieve integrations server status: %w", err)
}
if s == "started" {
return nil
}
}
}
}