Skip to content

Commit

Permalink
validating mapping too
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-dev-abhi committed Oct 24, 2023
1 parent 55f5d7c commit beb616a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 15 additions & 3 deletions capten/agent/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,23 @@ func (a *Agent) Ping(ctx context.Context, request *agentpb.PingRequest) (*agentp
return &agentpb.PingResponse{Status: agentpb.StatusCode_OK}, nil
}

func validateArgs(args ...string) error {
func validateArgs(args ...any) error {
for _, arg := range args {
if len(arg) == 0 {
return fmt.Errorf("mandatory argument is empty")
switch item := arg.(type) {
case string:
if len(item) == 0 {
return fmt.Errorf("empty string not allowed")
}
case map[string]string:
for k, v := range item {
if len(v) == 0 {
return fmt.Errorf("map value empty for key: %v", k)
}
}
default:
return fmt.Errorf("validation not implemented for this type")
}

}
return nil
}
8 changes: 4 additions & 4 deletions capten/agent/pkg/agent/plugin_cloud_provider_apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const cloudProviderEntityName = "CloudProvider"

func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.AddCloudProviderRequest) (
*captenpluginspb.AddCloudProviderResponse, error) {
if err := validateArgs(request.CloudType); err != nil {
if err := validateArgs(request.GetCloudType(), request.GetCloudAttributes()); err != nil {
a.log.Infof("request validation failed", err)
return &captenpluginspb.AddCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
Expand Down Expand Up @@ -54,7 +54,7 @@ func (a *Agent) AddCloudProvider(ctx context.Context, request *captenpluginspb.A

func (a *Agent) UpdateCloudProviders(ctx context.Context, request *captenpluginspb.UpdateCloudProviderRequest) (
*captenpluginspb.UpdateCloudProviderResponse, error) {
if err := validateArgs(request.CloudType, request.Id); err != nil {
if err := validateArgs(request.GetCloudType(), request.GetId(), request.GetCloudAttributes()); err != nil {
a.log.Infof("request validation failed", err)
return &captenpluginspb.UpdateCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
Expand Down Expand Up @@ -101,7 +101,7 @@ func (a *Agent) UpdateCloudProviders(ctx context.Context, request *captenplugins

func (a *Agent) DeleteCloudProvider(ctx context.Context, request *captenpluginspb.DeleteCloudProviderRequest) (
*captenpluginspb.DeleteCloudProviderResponse, error) {
if err := validateArgs(request.Id); err != nil {
if err := validateArgs(request.GetId()); err != nil {
a.log.Infof("request validation failed", err)
return &captenpluginspb.DeleteCloudProviderResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
Expand Down Expand Up @@ -160,7 +160,7 @@ func (a *Agent) GetCloudProviders(ctx context.Context, request *captenpluginspb.

func (a *Agent) GetCloudProvidersForLabels(ctx context.Context, request *captenpluginspb.GetCloudProvidersForLabelsRequest) (
*captenpluginspb.GetCloudProvidersForLabelsResponse, error) {
if len(request.Labels) == 0 {
if len(request.GetLabels()) == 0 {
a.log.Infof("request validation failed")
return &captenpluginspb.GetCloudProvidersForLabelsResponse{
Status: captenpluginspb.StatusCode_INVALID_ARGUMENT,
Expand Down
2 changes: 1 addition & 1 deletion capten/agent/pkg/capten-store/git_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
// insertGitProject = "INSERT INTO %s.GitProjects(id, project_url, labels, last_update_time) VALUES (?)"
insertGitProject = "INSERT INTO %s.GitProjects(id, project_url, labels, last_update_time) VALUES (?)"
insertGitProjectId = "INSERT INTO %s.GitProjects(id) VALUES (?)"
updateGitProjectById = "UPDATE %s.GitProjects SET %s WHERE id = ?"
deleteGitProjectById = "DELETE FROM %s.GitProjects WHERE id= ?"
Expand Down

0 comments on commit beb616a

Please sign in to comment.