From beb616ac7cd51c40c62e18b7936573dda160e1d9 Mon Sep 17 00:00:00 2001 From: sol-dev-abhi Date: Tue, 24 Oct 2023 18:52:15 +0530 Subject: [PATCH] validating mapping too --- capten/agent/pkg/agent/agent.go | 18 +++++++++++++++--- .../pkg/agent/plugin_cloud_provider_apis.go | 8 ++++---- capten/agent/pkg/capten-store/git_projects.go | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/capten/agent/pkg/agent/agent.go b/capten/agent/pkg/agent/agent.go index 1257a592..d3dbe76f 100644 --- a/capten/agent/pkg/agent/agent.go +++ b/capten/agent/pkg/agent/agent.go @@ -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 } diff --git a/capten/agent/pkg/agent/plugin_cloud_provider_apis.go b/capten/agent/pkg/agent/plugin_cloud_provider_apis.go index 8958636d..2a2283cf 100644 --- a/capten/agent/pkg/agent/plugin_cloud_provider_apis.go +++ b/capten/agent/pkg/agent/plugin_cloud_provider_apis.go @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/capten/agent/pkg/capten-store/git_projects.go b/capten/agent/pkg/capten-store/git_projects.go index 004da119..1d75e1fc 100644 --- a/capten/agent/pkg/capten-store/git_projects.go +++ b/capten/agent/pkg/capten-store/git_projects.go @@ -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= ?"