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

Add retry for Internal Authentication Endpoint #12

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions sources/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strings"
"time"

sourcesapi "github.com/lindgrenj6/sources-api-client-go"
"github.com/redhatinsights/sources-superkey-worker/config"
Expand Down Expand Up @@ -37,6 +38,7 @@ func NewAPIClient(acct string) *sourcesapi.APIClient {
// returns: populated sources api Authentication object, error
func GetInternalAuthentication(tenant, authID string) (*sourcesapi.Authentication, error) {
conf := config.Get()
l.Log.Infof("Requesting SuperKey Authentication: %v", authID)

reqURL, _ := url.Parse(fmt.Sprintf(
"http://%v:%v/internal/v1.0/authentications/%v?expose_encrypted_attribute[]=password",
Expand All @@ -53,12 +55,24 @@ func GetInternalAuthentication(tenant, authID string) (*sourcesapi.Authenticatio
},
}

res, err := http.DefaultClient.Do(req)
if err != nil {
l.Log.Warnf("Error getting authentication: %v, tenant: %v, error: %v", authID, tenant, err)
return nil, err
var res *http.Response
var err error
for retry := 0; retry < 5; retry++ {
res, err = http.DefaultClient.Do(req)

if res.StatusCode == 200 {
break
} else {
l.Log.Warnf("Authentication %v unavailable, retrying...", authID)
time.Sleep(3 * time.Second)
}
}

defer res.Body.Close()
if res.StatusCode != 200 {
l.Log.Warnf("Error getting authentication: %v, tenant: %v", authID, tenant)
return nil, fmt.Errorf("Failed to get Authentication %v", authID)
}

data, _ := ioutil.ReadAll(res.Body)
auth := sourcesapi.Authentication{}
Expand All @@ -71,6 +85,7 @@ func GetInternalAuthentication(tenant, authID string) (*sourcesapi.Authenticatio
return nil, err
}

l.Log.Infof("Authentication %v found!", authID)
return &auth, nil
}

Expand Down
12 changes: 12 additions & 0 deletions superkey/create_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ func (req *CreateRequest) MarkSourceUnavailable(err error, newApplication *Forge
return err
}

l.Log.Infof("Marking Source %v Unavailable", req.SourceID)
srcRequest := client.DefaultApi.UpdateSource(context.Background(), req.SourceID)
srcRequest = srcRequest.Source(
sourcesapi.Source{AvailabilityStatus: &availabilityStatus},
)

r, err = srcRequest.Execute()
if r == nil || r.StatusCode != 204 {
l.Log.Errorf("Failed to update source with error message %v", err)
return err
}

l.Log.Infof("Finished Marking Source %v + Application %v Unavailable", req.SourceID, req.ApplicationID)
return nil
}