From a9b5ad0023d1609b00fe2ccd51365ebfa8ff711e Mon Sep 17 00:00:00 2001 From: vijeyash Date: Fri, 1 Mar 2024 16:15:41 +0530 Subject: [PATCH] graphqpis bugs resolved --- graphqlserver/graph/generated.go | 277 ++++++++++++++++++++++++ graphqlserver/graph/model/models_gen.go | 5 + graphqlserver/graph/schema.graphqls | 6 + graphqlserver/graph/schema.resolvers.go | 40 +++- 4 files changed, 320 insertions(+), 8 deletions(-) diff --git a/graphqlserver/graph/generated.go b/graphqlserver/graph/generated.go index 8b58dd37..7a1d76b0 100644 --- a/graphqlserver/graph/generated.go +++ b/graphqlserver/graph/generated.go @@ -60,6 +60,11 @@ type ComplexityRoot struct { DeletedAPICount func(childComplexity int) int } + ClusterDeprecatedAPICount struct { + ClusterName func(childComplexity int) int + DeprecatedAPICount func(childComplexity int) int + } + ClusterNamespaceMisconfigCount struct { ClusterName func(childComplexity int) int MisconfigCount func(childComplexity int) int @@ -230,6 +235,7 @@ type ComplexityRoot struct { AllTrivyVuls func(childComplexity int) int DeletedAPICount func(childComplexity int, clusterName string) int DeletedAPIs func(childComplexity int, clusterName string) int + DeprecatedAPICount func(childComplexity int, clusterName string) int DeprecatedAPIs func(childComplexity int, clusterName string) int EventsByClusterAndNamespace func(childComplexity int, clusterName string, namespace string) int GetAllResources func(childComplexity int, clusterName string, namespace string) int @@ -395,6 +401,7 @@ type QueryResolver interface { TrivyMisconfigCount(ctx context.Context, clusterName string, namespace string) (*model.ClusterNamespaceMisconfigCount, error) DeletedAPICount(ctx context.Context, clusterName string) (*model.ClusterDeletedAPICount, error) TrivyImageCount(ctx context.Context, clusterName string) (*model.TrivyImageCount, error) + DeprecatedAPICount(ctx context.Context, clusterName string) (*model.ClusterDeprecatedAPICount, error) } type executableSchema struct { @@ -451,6 +458,20 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.ClusterDeletedAPICount.DeletedAPICount(childComplexity), true + case "ClusterDeprecatedAPICount.clusterName": + if e.complexity.ClusterDeprecatedAPICount.ClusterName == nil { + break + } + + return e.complexity.ClusterDeprecatedAPICount.ClusterName(childComplexity), true + + case "ClusterDeprecatedAPICount.deprecatedAPICount": + if e.complexity.ClusterDeprecatedAPICount.DeprecatedAPICount == nil { + break + } + + return e.complexity.ClusterDeprecatedAPICount.DeprecatedAPICount(childComplexity), true + case "ClusterNamespaceMisconfigCount.clusterName": if e.complexity.ClusterNamespaceMisconfigCount.ClusterName == nil { break @@ -1350,6 +1371,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Query.DeletedAPIs(childComplexity, args["clusterName"].(string)), true + case "Query.deprecatedAPICount": + if e.complexity.Query.DeprecatedAPICount == nil { + break + } + + args, err := ec.field_Query_deprecatedAPICount_args(context.TODO(), rawArgs) + if err != nil { + return 0, false + } + + return e.complexity.Query.DeprecatedAPICount(childComplexity, args["clusterName"].(string)), true + case "Query.deprecatedAPIs": if e.complexity.Query.DeprecatedAPIs == nil { break @@ -2315,6 +2348,21 @@ func (ec *executionContext) field_Query_deletedAPIs_args(ctx context.Context, ra return args, nil } +func (ec *executionContext) field_Query_deprecatedAPICount_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { + var err error + args := map[string]interface{}{} + var arg0 string + if tmp, ok := rawArgs["clusterName"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("clusterName")) + arg0, err = ec.unmarshalNString2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["clusterName"] = arg0 + return args, nil +} + func (ec *executionContext) field_Query_deprecatedAPIs_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -2864,6 +2912,94 @@ func (ec *executionContext) fieldContext_ClusterDeletedAPICount_deletedAPICount( return fc, nil } +func (ec *executionContext) _ClusterDeprecatedAPICount_clusterName(ctx context.Context, field graphql.CollectedField, obj *model.ClusterDeprecatedAPICount) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_ClusterDeprecatedAPICount_clusterName(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.ClusterName, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + fc.Result = res + return ec.marshalNString2string(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_ClusterDeprecatedAPICount_clusterName(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "ClusterDeprecatedAPICount", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type String does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _ClusterDeprecatedAPICount_deprecatedAPICount(ctx context.Context, field graphql.CollectedField, obj *model.ClusterDeprecatedAPICount) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_ClusterDeprecatedAPICount_deprecatedAPICount(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.DeprecatedAPICount, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalNInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_ClusterDeprecatedAPICount_deprecatedAPICount(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "ClusterDeprecatedAPICount", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _ClusterNamespaceMisconfigCount_clusterName(ctx context.Context, field graphql.CollectedField, obj *model.ClusterNamespaceMisconfigCount) (ret graphql.Marshaler) { fc, err := ec.fieldContext_ClusterNamespaceMisconfigCount_clusterName(ctx, field) if err != nil { @@ -9739,6 +9875,67 @@ func (ec *executionContext) fieldContext_Query_trivyImageCount(ctx context.Conte return fc, nil } +func (ec *executionContext) _Query_deprecatedAPICount(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Query_deprecatedAPICount(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Query().DeprecatedAPICount(rctx, fc.Args["clusterName"].(string)) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*model.ClusterDeprecatedAPICount) + fc.Result = res + return ec.marshalNClusterDeprecatedAPICount2ᚖgithubᚗcomᚋintelopsᚋkubvizᚋgraphqlserverᚋgraphᚋmodelᚐClusterDeprecatedAPICount(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Query_deprecatedAPICount(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Query", + Field: field, + IsMethod: true, + IsResolver: true, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "clusterName": + return ec.fieldContext_ClusterDeprecatedAPICount_clusterName(ctx, field) + case "deprecatedAPICount": + return ec.fieldContext_ClusterDeprecatedAPICount_deprecatedAPICount(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type ClusterDeprecatedAPICount", field.Name) + }, + } + defer func() { + if r := recover(); r != nil { + err = ec.Recover(ctx, r) + ec.Error(ctx, err) + } + }() + ctx = graphql.WithFieldContext(ctx, fc) + if fc.Args, err = ec.field_Query_deprecatedAPICount_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { + ec.Error(ctx, err) + return fc, err + } + return fc, nil +} + func (ec *executionContext) _Query___type(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Query___type(ctx, field) if err != nil { @@ -15561,6 +15758,50 @@ func (ec *executionContext) _ClusterDeletedAPICount(ctx context.Context, sel ast return out } +var clusterDeprecatedAPICountImplementors = []string{"ClusterDeprecatedAPICount"} + +func (ec *executionContext) _ClusterDeprecatedAPICount(ctx context.Context, sel ast.SelectionSet, obj *model.ClusterDeprecatedAPICount) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, clusterDeprecatedAPICountImplementors) + + out := graphql.NewFieldSet(fields) + deferred := make(map[string]*graphql.FieldSet) + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("ClusterDeprecatedAPICount") + case "clusterName": + out.Values[i] = ec._ClusterDeprecatedAPICount_clusterName(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "deprecatedAPICount": + out.Values[i] = ec._ClusterDeprecatedAPICount_deprecatedAPICount(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch(ctx) + if out.Invalids > 0 { + return graphql.Null + } + + atomic.AddInt32(&ec.deferred, int32(len(deferred))) + + for label, dfs := range deferred { + ec.processDeferredGroup(graphql.DeferredGroup{ + Label: label, + Path: graphql.GetPath(ctx), + FieldSet: dfs, + Context: ctx, + }) + } + + return out +} + var clusterNamespaceMisconfigCountImplementors = []string{"ClusterNamespaceMisconfigCount"} func (ec *executionContext) _ClusterNamespaceMisconfigCount(ctx context.Context, sel ast.SelectionSet, obj *model.ClusterNamespaceMisconfigCount) graphql.Marshaler { @@ -17097,6 +17338,28 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) } + out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) }) + case "deprecatedAPICount": + field := field + + innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Query_deprecatedAPICount(ctx, field) + if res == graphql.Null { + atomic.AddUint32(&fs.Invalids, 1) + } + return res + } + + rrm := func(ctx context.Context) graphql.Marshaler { + return ec.OperationContext.RootResolverMiddleware(ctx, + func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) + } + out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) }) case "__type": out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { @@ -18100,6 +18363,20 @@ func (ec *executionContext) marshalNClusterDeletedAPICount2ᚖgithubᚗcomᚋint return ec._ClusterDeletedAPICount(ctx, sel, v) } +func (ec *executionContext) marshalNClusterDeprecatedAPICount2githubᚗcomᚋintelopsᚋkubvizᚋgraphqlserverᚋgraphᚋmodelᚐClusterDeprecatedAPICount(ctx context.Context, sel ast.SelectionSet, v model.ClusterDeprecatedAPICount) graphql.Marshaler { + return ec._ClusterDeprecatedAPICount(ctx, sel, &v) +} + +func (ec *executionContext) marshalNClusterDeprecatedAPICount2ᚖgithubᚗcomᚋintelopsᚋkubvizᚋgraphqlserverᚋgraphᚋmodelᚐClusterDeprecatedAPICount(ctx context.Context, sel ast.SelectionSet, v *model.ClusterDeprecatedAPICount) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._ClusterDeprecatedAPICount(ctx, sel, v) +} + func (ec *executionContext) marshalNClusterNamespaceMisconfigCount2githubᚗcomᚋintelopsᚋkubvizᚋgraphqlserverᚋgraphᚋmodelᚐClusterNamespaceMisconfigCount(ctx context.Context, sel ast.SelectionSet, v model.ClusterNamespaceMisconfigCount) graphql.Marshaler { return ec._ClusterNamespaceMisconfigCount(ctx, sel, &v) } diff --git a/graphqlserver/graph/model/models_gen.go b/graphqlserver/graph/model/models_gen.go index d31563ce..41e4076b 100644 --- a/graphqlserver/graph/model/models_gen.go +++ b/graphqlserver/graph/model/models_gen.go @@ -16,6 +16,11 @@ type ClusterDeletedAPICount struct { DeletedAPICount int `json:"deletedAPICount"` } +type ClusterDeprecatedAPICount struct { + ClusterName string `json:"clusterName"` + DeprecatedAPICount int `json:"deprecatedAPICount"` +} + type ClusterNamespaceMisconfigCount struct { ClusterName string `json:"clusterName"` Namespace string `json:"namespace"` diff --git a/graphqlserver/graph/schema.graphqls b/graphqlserver/graph/schema.graphqls index 07e588fa..9b6e3258 100644 --- a/graphqlserver/graph/schema.graphqls +++ b/graphqlserver/graph/schema.graphqls @@ -30,6 +30,12 @@ type Query { trivyMisconfigCount(clusterName: String!, namespace: String!): ClusterNamespaceMisconfigCount! deletedAPICount(clusterName: String!): ClusterDeletedAPICount! trivyImageCount(clusterName: String!): TrivyImageCount! + deprecatedAPICount(clusterName: String!): ClusterDeprecatedAPICount! +} + +type ClusterDeprecatedAPICount { + clusterName: String! + deprecatedAPICount: Int! } type TrivyImageCount { diff --git a/graphqlserver/graph/schema.resolvers.go b/graphqlserver/graph/schema.resolvers.go index 9ded4e4c..71cb5330 100644 --- a/graphqlserver/graph/schema.resolvers.go +++ b/graphqlserver/graph/schema.resolvers.go @@ -626,7 +626,7 @@ func (r *queryResolver) Vulnerabilities(ctx context.Context, clusterName string, return nil, fmt.Errorf("clusterName and namespace cannot be empty") } query := ` - SELECT id, cluster_name, namespace, kind, name, vul_id, vul_vendor_ids, vul_pkg_id, vul_pkg_name, vul_pkg_path, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date, ExpiryDate, ExportedAt + SELECT id, cluster_name, namespace, kind, name, vul_id, vul_vendor_ids, vul_pkg_id, vul_pkg_name, vul_pkg_path, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date, ExpiryDate FROM trivy_vul WHERE cluster_name = ? AND namespace = ? ` @@ -661,7 +661,7 @@ func (r *queryResolver) Misconfigurations(ctx context.Context, clusterName strin } query := ` - SELECT id, cluster_name, namespace, kind, name, misconfig_id, misconfig_avdid, misconfig_type, misconfig_title, misconfig_desc, misconfig_msg, misconfig_query, misconfig_resolution, misconfig_severity, misconfig_status, EventTime, ExpiryDate, ExportedAt + SELECT id, cluster_name, namespace, kind, name, misconfig_id, misconfig_avdid, misconfig_type, misconfig_title, misconfig_desc, misconfig_msg, misconfig_query, misconfig_resolution, misconfig_severity, misconfig_status, EventTime, ExpiryDate FROM trivy_misconfig WHERE cluster_name = ? AND namespace = ? ` @@ -698,7 +698,7 @@ func (r *queryResolver) Kubescores(ctx context.Context, clustername string, name } query := ` - SELECT id, clustername, object_name, kind, apiVersion, name, namespace, target_type, description, path, summary, file_name, file_row, EventTime, ExpiryDate, ExportedAt + SELECT id, clustername, object_name, kind, apiVersion, name, namespace, target_type, description, path, summary, file_name, file_row, EventTime, ExpiryDate FROM kubescore WHERE clustername = ? AND namespace = ? ` @@ -735,7 +735,7 @@ func (r *queryResolver) GetAllResources(ctx context.Context, clusterName string, } query := ` - SELECT ClusterName, Namespace, Kind, Resource, Age, EventTime, ExpiryDate, ExportedAt + SELECT ClusterName, Namespace, Kind, Resource, Age, EventTime, ExpiryDate FROM getall_resources WHERE ClusterName = ? AND Namespace = ? ` @@ -772,7 +772,7 @@ func (r *queryResolver) TrivyImages(ctx context.Context, clusterName string) ([] } query := ` - SELECT id, cluster_name, artifact_name, vul_id, vul_pkg_id, vul_pkg_name, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date, ExpiryDate, ExportedAt + SELECT id, cluster_name, artifact_name, vul_id, vul_pkg_id, vul_pkg_name, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date, ExpiryDate FROM trivyimage WHERE cluster_name = ? ` @@ -809,7 +809,7 @@ func (r *queryResolver) DeprecatedAPIs(ctx context.Context, clusterName string) } query := ` - SELECT ClusterName, ObjectName, Description, Kind, Deprecated, Scope, EventTime, ExpiryDate, ExportedAt + SELECT ClusterName, ObjectName, Description, Kind, Deprecated, Scope, EventTime, ExpiryDate FROM DeprecatedAPIs WHERE ClusterName = ? ` @@ -848,7 +848,7 @@ func (r *queryResolver) DeletedAPIs(ctx context.Context, clusterName string) ([] } query := ` - SELECT ClusterName, ObjectName, Group, Kind, Version, Name, Deleted, Scope, EventTime, ExpiryDate, ExportedAt + SELECT ClusterName, ObjectName, Group, Kind, Version, Name, Deleted, Scope, EventTime, ExpiryDate FROM DeletedAPIs WHERE ClusterName = ? ` @@ -886,7 +886,7 @@ func (r *queryResolver) TrivySBOMs(ctx context.Context, clusterName string) ([]* } query := ` - SELECT id, cluster_name, image_name, package_name, package_url, bom_ref, serial_number, version, bom_format, ExpiryDate, ExportedAt + SELECT id, cluster_name, image_name, package_name, package_url, bom_ref, serial_number, version, bom_format, ExpiryDate FROM trivysbom WHERE cluster_name = ? ` @@ -1010,6 +1010,30 @@ func (r *queryResolver) TrivyImageCount(ctx context.Context, clusterName string) }, nil } +// DeprecatedAPICount is the resolver for the deprecatedAPICount field. +func (r *queryResolver) DeprecatedAPICount(ctx context.Context, clusterName string) (*model.ClusterDeprecatedAPICount, error) { + if r.DB == nil { + return nil, fmt.Errorf("database connection is not initialized") + } + + if clusterName == "" { + return nil, fmt.Errorf("ClusterName cannot be empty") + } + + query := `SELECT COUNT(*) FROM DeprecatedAPIs WHERE ClusterName = ?` + + var count int + err := r.DB.QueryRowContext(ctx, query, clusterName).Scan(&count) + if err != nil { + return nil, fmt.Errorf("error executing query: %v", err) + } + + return &model.ClusterDeprecatedAPICount{ + ClusterName: clusterName, + DeprecatedAPICount: count, + }, nil +} + // Query returns QueryResolver implementation. func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }