diff --git a/cancom/services/windows-os/data_deployment_progess.go b/cancom/services/windows-os/data_deployment_progess.go index 11a41b9..a2ea0ad 100644 --- a/cancom/services/windows-os/data_deployment_progess.go +++ b/cancom/services/windows-os/data_deployment_progess.go @@ -8,7 +8,6 @@ import ( func dataWindowsOSDeploymentProgress() *schema.Resource { return &schema.Resource{ - Read: WindowsOSDeploymentProgressRead, Schema: map[string]*schema.Schema{ "deployment_id": { @@ -17,6 +16,10 @@ func dataWindowsOSDeploymentProgress() *schema.Resource { ForceNew: true, Description: "ID of the deployment object.", }, + "state": { + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -26,14 +29,22 @@ func WindowsOSDeploymentProgressRead(d *schema.ResourceData, meta interface{}) e if err != nil { return err } + // if a status is already set, we can avoid calling the endpoint again. + if d.Get("state").(string) == "Finished" { + return nil + } else if d.Get("state").(string) == "Failed" { + return nil + } resp, err := (*client_windowsos.Client)(c).CreateWindowsDeploymentStatus(d.Get("deployment_id").(string)) if err != nil { + d.SetId(d.Get("deployment_id").(string)) + d.Set("state", "Failed") return err } d.SetId(resp.Id) + d.Set("state", "Finished") return nil - } diff --git a/client/services/windows-os/windows-os.go b/client/services/windows-os/windows-os.go index 93ea349..5221beb 100644 --- a/client/services/windows-os/windows-os.go +++ b/client/services/windows-os/windows-os.go @@ -5,9 +5,7 @@ import ( "encoding/json" "fmt" "net/http" - "os" - "slices" "time" @@ -71,28 +69,35 @@ func (c *Client) CreateWindowsDeploymentStatus(id string) (*WindowsOS_Deplyoment sucessstatus := []int{4} timeoutCount := 0 + generalErrorCount := 0 //validation ressource. Waits until the deployment of the software has been finished. //The deployment itself is run by the CANCOM Windows OS Service backend. for { - req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/%s", c.HostURL, urlPath, id), nil) + req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/%s/status", c.HostURL, urlPath, id), nil) + req.Header.Add("Content-Type", "application/json") resp, err := (*client.Client)(c).DoRequest(req) if err != nil { - // allow timeouts because of long running queries in background if os.IsTimeout(err) { timeoutCount++ - } else { - return nil, err + } else { // due to the long running deployments (might take some hours) we need to be tolerant for connection or other errors. + generalErrorCount++ } if timeoutCount > 10 { return nil, err } + if generalErrorCount > 3 { + return nil, err + } } else { + timeoutCount = 0 + generalErrorCount = 0 apiResultObject := WindowsOS_Deplyoment{} + err = json.Unmarshal(resp, &apiResultObject) if err != nil { return nil, err