Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
add health check (#44)
Browse files Browse the repository at this point in the history
* add health check

* use new var name

* improve healthcheck to detect invalid credentials

* simplify error msg

* add retry

* add sleep

* replace dots with colon
  • Loading branch information
umairidris authored Feb 24, 2020
1 parent 1ca915a commit 3b5d037
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions cdap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package cdap

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -72,16 +75,45 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
TokenType: "Bearer",
}))
}
httpClient.Timeout = 30 * time.Minute
httpClient.Timeout = 5 * time.Minute

storageClient, err := storage.NewClient(ctx, option.WithScopes(storage.ScopeReadOnly))
if err != nil {
return nil, err
}

return &Config{
c := &Config{
host: d.Get("host").(string),
httpClient: httpClient,
storageClient: storageClient,
}, nil
}

if err := healthcheck(c); err != nil {
log.Printf("failed health check, trying again after 5 seconds: %v", err)
time.Sleep(5 * time.Second)
if err := healthcheck(c); err != nil {
return nil, fmt.Errorf("failed health check, possibly due to an invalid host or credentials: %v", err)
}
}
return c, nil
}

func healthcheck(c *Config) error {
addr := urlJoin(c.host, "/v3/namespaces")
req, err := http.NewRequest(http.MethodGet, addr, nil)
if err != nil {
return err
}
b, err := httpCall(c.httpClient, req)
if err != nil {
return err
}

// Invalid credentials currently result in a redirect to sign in page instead of an error.
// So check for a valid return value by unmarshalling the JSON.
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return fmt.Errorf("failed to unmarshal response: %v\n%v", err, string(b))
}
return nil
}

0 comments on commit 3b5d037

Please sign in to comment.