Skip to content

Commit

Permalink
Merge pull request #467 from kube-tarian/onboard-errimp
Browse files Browse the repository at this point in the history
improve onboard resource apis error handling
  • Loading branch information
vramk23 authored Apr 27, 2024
2 parents 81ce4e9 + abeb625 commit 7732aab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 159 deletions.
127 changes: 2 additions & 125 deletions capten/agent/internal/api/plugin_argocd_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"strings"

"github.com/intelops/go-common/logging"
captenstore "github.com/kube-tarian/kad/capten/agent/internal/capten-store"
"github.com/kube-tarian/kad/capten/agent/internal/pb/captenpluginspb"
"github.com/kube-tarian/kad/capten/common-pkg/plugins/argocd"
"github.com/kube-tarian/kad/capten/model"
)

const (
Expand All @@ -19,133 +17,12 @@ const (

func (a *Agent) RegisterArgoCDProject(ctx context.Context, request *captenpluginspb.RegisterArgoCDProjectRequest) (
*captenpluginspb.RegisterArgoCDProjectResponse, error) {
if err := validateArgs(request.Id); err != nil {
a.log.Infof("request validation failed", err)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}
a.log.Infof("Register ArgoCD Git project %s request recieved", request.Id)

argoCDProject, err := a.as.GetArgoCDProjectForID(request.Id)
if err != nil {
a.log.Infof("failed to get argocd project %s, %v", request.Id, err)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}

accessToken, userID, _, _, err := a.getGitProjectCredential(ctx, argoCDProject.GitProjectId)
if err != nil {
a.log.Errorf("failed to get credential, %v", err)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "Error occured while adding Repository",
}, nil
}

if err := a.addProjectToArgoCD(ctx, argoCDProject.GitProjectUrl, userID, accessToken); err != nil {
a.log.Errorf("failed to add ArgoCD Repository: %v ", err)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_NOT_FOUND,
StatusMessage: "Error occured while adding Repository",
}, err
}

argoCDProject.Status = string(model.ArgoCDProjectConfigured)
if err := a.as.UpsertArgoCDProject(argoCDProject); err != nil {
a.log.Errorf("failed to store argoCD git Project %s, %v ", argoCDProject.GitProjectUrl, err)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_NOT_FOUND,
StatusMessage: "Error occured while adding ArgoCD project Data",
}, err
}

a.log.Infof("ArgoCD Git project %s. %s Registered", request.Id, argoCDProject.GitProjectUrl)
return &captenpluginspb.RegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_OK,
StatusMessage: "Sucessfully registered ArgoCD Repository",
}, nil
}

func (a *Agent) UnRegisterArgoCDProject(ctx context.Context, request *captenpluginspb.UnRegisterArgoCDProjectRequest) (
*captenpluginspb.UnRegisterArgoCDProjectResponse, error) {
if err := validateArgs(request.Id); err != nil {
a.log.Infof("request validation failed", err)
return &captenpluginspb.UnRegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}
a.log.Infof("UnRegister ArgoCD Git project %s request recieved", request.Id)

argoCDProject, err := a.as.GetArgoCDProjectForID(request.Id)
if err != nil {
if !captenstore.IsObjectNotFound(err) {
a.log.Infof("faile to get argocd project %s, %v", request.Id, err)
return &captenpluginspb.UnRegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}
}

if err := a.deleteProjectFromArgoCD(ctx, argoCDProject.GitProjectUrl); err != nil {
a.log.Errorf("failed to delete ArgoCD Repository: %v ", err)
return &captenpluginspb.UnRegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_NOT_FOUND,
StatusMessage: "Error occured while deleting Repository",
}, err
}

argoCDProject.Status = string(model.ArgoCDProjectAvailable)
if err := a.as.UpsertArgoCDProject(argoCDProject); err != nil {
a.log.Errorf("failed to store argoCD git Project %s, %v ", argoCDProject.GitProjectUrl, err)
return &captenpluginspb.UnRegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_NOT_FOUND,
StatusMessage: "Error occured while adding ArgoCD project Data",
}, err
}

a.log.Infof("ArgoCD Git project %s. %s UnRegistered", request.Id, argoCDProject.GitProjectUrl)
return &captenpluginspb.UnRegisterArgoCDProjectResponse{
Status: captenpluginspb.StatusCode_OK,
StatusMessage: "Successfully unregisterted ArgoCD Repository",
}, nil
return nil, fmt.Errorf("unimplemented")
}

func (a *Agent) GetArgoCDProjects(ctx context.Context, request *captenpluginspb.GetArgoCDProjectsRequest) (
*captenpluginspb.GetArgoCDProjectsResponse, error) {
a.log.Infof("Get ArgoCD Git projects request recieved")

projects, err := a.as.GetArgoCDProjects()
if err != nil {
a.log.Errorf("failed to get argocd Project, %v", err)
return &captenpluginspb.GetArgoCDProjectsResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to get argocd Project",
}, err
}

argocdProjects := []*captenpluginspb.ArgoCDProject{}
for _, project := range projects {
argocdProject := &captenpluginspb.ArgoCDProject{
Id: project.Id,
ProjectUrl: project.GitProjectUrl,
Status: project.Status,
LastUpdateTime: project.LastUpdateTime,
}
argocdProjects = append(argocdProjects, argocdProject)
}

a.log.Infof("Fetched %d ArgoCD Git projects", len(argocdProjects))
return &captenpluginspb.GetArgoCDProjectsResponse{
Status: captenpluginspb.StatusCode_OK,
StatusMessage: "Successfully fetched the Repositories",
Projects: argocdProjects,
}, nil
return nil, fmt.Errorf("unimplemented")
}

func (a *Agent) addProjectToArgoCD(ctx context.Context, projectUrl, userID, accessToken string) error {
Expand Down
30 changes: 15 additions & 15 deletions capten/agent/internal/api/plugin_cloud_provider_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.A
return &captenpluginspb.AddCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}, err
}

a.log.Infof("Add Cloud Provider %s request received", request.CloudType)
Expand All @@ -28,7 +28,7 @@ func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.A
return &captenpluginspb.AddCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to add cloud provider credential in vault",
}, nil
}, err
}

CloudProvider := captenpluginspb.CloudProvider{
Expand All @@ -41,7 +41,7 @@ func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.A
return &captenpluginspb.AddCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to add CloudProvider in db",
}, nil
}, err
}

a.log.Infof("Cloud Provider %s added with id %s", request.GetCloudType(), id.String())
Expand All @@ -59,7 +59,7 @@ func (a *Agent) UpdateCloudProvider(ctx context.Context, request *captenpluginsp
return &captenpluginspb.UpdateCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}, err
}
a.log.Infof("Update Cloud Provider %s, %s request received", request.CloudType, request.Id)

Expand All @@ -69,14 +69,14 @@ func (a *Agent) UpdateCloudProvider(ctx context.Context, request *captenpluginsp
return &captenpluginspb.UpdateCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: fmt.Sprintf("invalid uuid: %s", request.Id),
}, nil
}, err
}

if err := a.storeCloudProviderCredential(ctx, request.Id, request.GetCloudAttributes()); err != nil {
return &captenpluginspb.UpdateCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to add CloudProvider credential in vault",
}, nil
}, err
}

CloudProvider := captenpluginspb.CloudProvider{
Expand All @@ -89,7 +89,7 @@ func (a *Agent) UpdateCloudProvider(ctx context.Context, request *captenpluginsp
return &captenpluginspb.UpdateCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to update CloudProvider in db",
}, nil
}, err
}

a.log.Infof("Cloud Provider %s, %s updated", request.CloudType, request.Id)
Expand All @@ -106,23 +106,23 @@ func (a *Agent) DeleteCloudProvider(ctx context.Context, request *captenpluginsp
return &captenpluginspb.DeleteCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}, err
}
a.log.Infof("Delete Cloud Provider %s request recieved", request.Id)

if err := a.deleteCloudProviderCredential(ctx, request.Id); err != nil {
return &captenpluginspb.DeleteCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to delete cloud provider credential in vault",
}, nil
}, err
}

if err := a.as.DeleteCloudProviderById(request.Id); err != nil {
a.log.Errorf("failed to delete CloudProvider from db, %v", err)
return &captenpluginspb.DeleteCloudProviderResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to delete CloudProvider from db",
}, nil
}, err
}

a.log.Infof("Cloud Provider %s deleted", request.Id)
Expand All @@ -141,7 +141,7 @@ func (a *Agent) GetCloudProviders(ctx context.Context, request *captenpluginspb.
return &captenpluginspb.GetCloudProvidersResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to fetch cloud providers",
}, nil
}, err
}

for _, r := range res {
Expand All @@ -151,7 +151,7 @@ func (a *Agent) GetCloudProviders(ctx context.Context, request *captenpluginspb.
return &captenpluginspb.GetCloudProvidersResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to fetch cloud providers",
}, nil
}, err
}
r.CloudAttributes = cloudAttributes
r.SecretePath = secretPath
Expand All @@ -174,7 +174,7 @@ func (a *Agent) GetCloudProvidersWithFilter(ctx context.Context, request *capten
return &captenpluginspb.GetCloudProvidersWithFilterResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
StatusMessage: "request validation failed",
}, nil
}, fmt.Errorf("labels cannot be empty")
}
a.log.Infof("Get Cloud providers with labels %v request recieved", request.Labels)

Expand All @@ -184,7 +184,7 @@ func (a *Agent) GetCloudProvidersWithFilter(ctx context.Context, request *capten
return &captenpluginspb.GetCloudProvidersWithFilterResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to fetch cloud providers",
}, nil
}, err
}

for _, r := range res {
Expand All @@ -194,7 +194,7 @@ func (a *Agent) GetCloudProvidersWithFilter(ctx context.Context, request *capten
return &captenpluginspb.GetCloudProvidersWithFilterResponse{
Status: captenpluginspb.StatusCode_INTERNAL_ERROR,
StatusMessage: "failed to fetch cloud providers",
}, nil
}, err
}
r.CloudAttributes = cloudAttributes
r.SecretePath = secretPath
Expand Down
Loading

0 comments on commit 7732aab

Please sign in to comment.