Skip to content

Commit

Permalink
Merge pull request #47 from cancom/feat/windows-os-deployment-progress
Browse files Browse the repository at this point in the history
fix: data behavior at windows os deployment
  • Loading branch information
atddo authored Aug 1, 2024
2 parents 019854c + 831f31e commit 7021241
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
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

0 comments on commit 7021241

Please sign in to comment.