Skip to content

Commit

Permalink
Fix project association polling issue (AST-40286)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrShamirCM committed May 3, 2024
1 parent bd5d658 commit d33b38a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ func findProject(
}
projectID, err := createProject(projectName, cmd, projectsWrapper, groupsWrapper, accessManagementWrapper, applicationWrapper, applicationID)
if err != nil {
logger.PrintIfVerbose("error in creating project!")
return "", err
}
return projectID, nil
Expand All @@ -646,7 +647,9 @@ func createProject(
projModel.PrivatePackage, _ = strconv.ParseBool(projectPrivatePackage)
}
projModel.Tags = createTagMap(projectTags)
logger.PrintIfVerbose("Creating new project")
resp, errorModel, err := projectsWrapper.Create(&projModel)

projectID := ""
if errorModel != nil {
err = errors.Errorf(ErrorCodeFormat, failedCreatingProj, errorModel.Code, errorModel.Message)
Expand Down Expand Up @@ -678,19 +681,23 @@ func verifyApplicationAssociationDone(applicationID []string, projectID string,
params["id"] = applicationID[0]

logger.PrintIfVerbose("polling application until project association done or timeout of 2 min")
start := time.Now()
timeout := 2 * time.Minute
for applicationRes != nil && len(applicationRes.Applications) > 0 &&
!slices.Contains(applicationRes.Applications[0].ProjectIds, projectID) {
var timeoutDuration = 2 * time.Minute
timeout := time.Now().Add(timeoutDuration)
for time.Now().Before(timeout) {
applicationRes, err = applicationsWrapper.Get(params)
if err != nil {
return err
} else if time.Since(start) < timeout {
} else if time.Now().After(timeout) {
return errors.Errorf("%s: %v", failedProjectApplicationAssociation, "timeout of 2 min for association")
} else if applicationRes != nil && len(applicationRes.Applications) > 0 {
if slices.Contains(applicationRes.Applications[0].ProjectIds, projectID) {
logger.PrintIfVerbose("application association done successfully")
return nil
}
}
logger.PrintIfVerbose("application association polling - waiting for associating to complete")
}

logger.PrintIfVerbose("application association done successfully")
return nil
}

Expand Down Expand Up @@ -728,6 +735,8 @@ func updateProject(
if projectPrivatePackage != "" {
projModel.PrivatePackage, _ = strconv.ParseBool(projectPrivatePackage)
}

logger.PrintIfVerbose("Fetching existing Project for updating")
projModelResp, errModel, err := projectsWrapper.GetByID(projectID)
if errModel != nil {
err = errors.Errorf(ErrorCodeFormat, failedGettingProj, errModel.Code, errModel.Message)
Expand Down
1 change: 1 addition & 0 deletions internal/wrappers/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ func getClientCredentials(accessKeyID, accessKeySecret, astAPKey, authURI string

func getClientCredentialsFromCache(tokenExpirySeconds int) string {
logger.PrintIfVerbose("Checking cache for API access token.")

expired := time.Since(cachedAccessTime) > time.Duration(tokenExpirySeconds-expiryGraceSeconds)*time.Second
if !expired {
logger.PrintIfVerbose("Using cached API access token!")
Expand Down

0 comments on commit d33b38a

Please sign in to comment.