Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data behavior at windows os deployment #47

Merged
merged 12 commits into from
Aug 1, 2024
Merged
15 changes: 13 additions & 2 deletions cancom/services/windows-os/data_deployment_progess.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

func dataWindowsOSDeploymentProgress() *schema.Resource {
return &schema.Resource{

Read: WindowsOSDeploymentProgressRead,
Schema: map[string]*schema.Schema{
"deployment_id": {
Expand All @@ -17,6 +16,10 @@ func dataWindowsOSDeploymentProgress() *schema.Resource {
ForceNew: true,
Description: "ID of the deployment object.",
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -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

}
17 changes: 11 additions & 6 deletions client/services/windows-os/windows-os.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"

"os"

"slices"
"time"

Expand Down Expand Up @@ -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
Expand Down