From d6d569856c0019b2123f45ca17fc6e2a3c5e15e4 Mon Sep 17 00:00:00 2001 From: Michal Krzyz Date: Tue, 1 Oct 2024 12:01:54 +0200 Subject: [PATCH] feat(authN): Add to all DB entries 'Modified_by'... (#81) Add metadata for component --- internal/api/graphql/graph/generated.go | 91 +++++++++++++++++++ internal/api/graphql/graph/model/models.go | 7 +- .../api/graphql/graph/model/models_gen.go | 1 + .../graphql/graph/schema/component.graphqls | 3 +- internal/app/component/component_handler.go | 2 + internal/database/mariadb/component.go | 9 +- internal/database/mariadb/entity.go | 6 ++ internal/database/mariadb/init/schema.sql | 2 + 8 files changed, 115 insertions(+), 6 deletions(-) diff --git a/internal/api/graphql/graph/generated.go b/internal/api/graphql/graph/generated.go index f461bdd3..924e2d10 100644 --- a/internal/api/graphql/graph/generated.go +++ b/internal/api/graphql/graph/generated.go @@ -133,6 +133,7 @@ type ComplexityRoot struct { Component struct { ComponentVersions func(childComplexity int, filter *model.ComponentVersionFilter, first *int, after *string) int ID func(childComplexity int) int + Metadata func(childComplexity int) int Name func(childComplexity int) int Type func(childComplexity int) int } @@ -1063,6 +1064,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Component.ID(childComplexity), true + case "Component.metadata": + if e.complexity.Component.Metadata == nil { + break + } + + return e.complexity.Component.Metadata(childComplexity), true + case "Component.name": if e.complexity.Component.Name == nil { break @@ -12113,6 +12121,59 @@ func (ec *executionContext) fieldContext_Component_componentVersions(ctx context return fc, nil } +func (ec *executionContext) _Component_metadata(ctx context.Context, field graphql.CollectedField, obj *model.Component) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Component_metadata(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.Metadata, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*model.Metadata) + fc.Result = res + return ec.marshalOMetadata2ᚖgithubᚗcomᚋcloudoperatorsᚋheurekaᚋinternalᚋapiᚋgraphqlᚋgraphᚋmodelᚐMetadata(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Component_metadata(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Component", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "created_at": + return ec.fieldContext_Metadata_created_at(ctx, field) + case "created_by": + return ec.fieldContext_Metadata_created_by(ctx, field) + case "deleted_at": + return ec.fieldContext_Metadata_deleted_at(ctx, field) + case "updated_at": + return ec.fieldContext_Metadata_updated_at(ctx, field) + case "updated_by": + return ec.fieldContext_Metadata_updated_by(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Metadata", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _ComponentConnection_totalCount(ctx context.Context, field graphql.CollectedField, obj *model.ComponentConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_ComponentConnection_totalCount(ctx, field) if err != nil { @@ -12306,6 +12367,8 @@ func (ec *executionContext) fieldContext_ComponentEdge_node(_ context.Context, f return ec.fieldContext_Component_type(ctx, field) case "componentVersions": return ec.fieldContext_Component_componentVersions(ctx, field) + case "metadata": + return ec.fieldContext_Component_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Component", field.Name) }, @@ -13242,6 +13305,8 @@ func (ec *executionContext) fieldContext_ComponentVersion_component(_ context.Co return ec.fieldContext_Component_type(ctx, field) case "componentVersions": return ec.fieldContext_Component_componentVersions(ctx, field) + case "metadata": + return ec.fieldContext_Component_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Component", field.Name) }, @@ -19881,6 +19946,8 @@ func (ec *executionContext) fieldContext_Mutation_createComponent(ctx context.Co return ec.fieldContext_Component_type(ctx, field) case "componentVersions": return ec.fieldContext_Component_componentVersions(ctx, field) + case "metadata": + return ec.fieldContext_Component_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Component", field.Name) }, @@ -19946,6 +20013,8 @@ func (ec *executionContext) fieldContext_Mutation_updateComponent(ctx context.Co return ec.fieldContext_Component_type(ctx, field) case "componentVersions": return ec.fieldContext_Component_componentVersions(ctx, field) + case "metadata": + return ec.fieldContext_Component_metadata(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Component", field.Name) }, @@ -31269,6 +31338,28 @@ func (ec *executionContext) _Component(ctx context.Context, sel ast.SelectionSet continue } out.Values[i] = ec._Component_componentVersions(ctx, field, obj) + case "metadata": + field := field + + if field.Deferrable != nil { + dfs, ok := deferred[field.Deferrable.Label] + di := 0 + if ok { + dfs.AddField(field) + di = len(dfs.Values) - 1 + } else { + dfs = graphql.NewFieldSet([]graphql.CollectedField{field}) + deferred[field.Deferrable.Label] = dfs + } + dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler { + return ec._Component_metadata(ctx, field, obj) + }) + + // don't run the out.Concurrently() call below + out.Values[i] = graphql.Null + continue + } + out.Values[i] = ec._Component_metadata(ctx, field, obj) default: panic("unknown field " + strconv.Quote(field.Name)) } diff --git a/internal/api/graphql/graph/model/models.go b/internal/api/graphql/graph/model/models.go index cf35f5b2..9f3fa361 100644 --- a/internal/api/graphql/graph/model/models.go +++ b/internal/api/graphql/graph/model/models.go @@ -418,9 +418,10 @@ func NewEvidenceEntity(evidence *EvidenceInput) entity.Evidence { func NewComponent(component *entity.Component) Component { componentType, _ := ComponentTypeValue(component.Type) return Component{ - ID: fmt.Sprintf("%d", component.Id), - Name: &component.Name, - Type: &componentType, + ID: fmt.Sprintf("%d", component.Id), + Name: &component.Name, + Type: &componentType, + Metadata: getModelMetadata(component.Metadata), } } diff --git a/internal/api/graphql/graph/model/models_gen.go b/internal/api/graphql/graph/model/models_gen.go index b47a0df9..54a26178 100644 --- a/internal/api/graphql/graph/model/models_gen.go +++ b/internal/api/graphql/graph/model/models_gen.go @@ -118,6 +118,7 @@ type Component struct { Name *string `json:"name,omitempty"` Type *ComponentTypeValues `json:"type,omitempty"` ComponentVersions *ComponentVersionConnection `json:"componentVersions,omitempty"` + Metadata *Metadata `json:"metadata,omitempty"` } func (Component) IsNode() {} diff --git a/internal/api/graphql/graph/schema/component.graphqls b/internal/api/graphql/graph/schema/component.graphqls index e2805fd1..da8a11ea 100644 --- a/internal/api/graphql/graph/schema/component.graphqls +++ b/internal/api/graphql/graph/schema/component.graphqls @@ -6,6 +6,7 @@ type Component implements Node { name: String type: ComponentTypeValues componentVersions(filter: ComponentVersionFilter, first: Int, after: String): ComponentVersionConnection + metadata: Metadata } input ComponentInput { @@ -32,4 +33,4 @@ type ComponentConnection implements Connection { type ComponentEdge implements Edge { node: Component! cursor: String -} \ No newline at end of file +} diff --git a/internal/app/component/component_handler.go b/internal/app/component/component_handler.go index 2da923fd..2cc0a5c4 100644 --- a/internal/app/component/component_handler.go +++ b/internal/app/component/component_handler.go @@ -105,6 +105,7 @@ func (cs *componentHandler) ListComponents(filter *entity.ComponentFilter, optio } func (cs *componentHandler) CreateComponent(component *entity.Component) (*entity.Component, error) { + component.CreatedBy = "Creator" f := &entity.ComponentFilter{ Name: []*string{&component.Name}, } @@ -139,6 +140,7 @@ func (cs *componentHandler) CreateComponent(component *entity.Component) (*entit } func (cs *componentHandler) UpdateComponent(component *entity.Component) (*entity.Component, error) { + component.UpdatedBy = "Updater" l := logrus.WithFields(logrus.Fields{ "event": UpdateComponentEventName, "object": component, diff --git a/internal/database/mariadb/component.go b/internal/database/mariadb/component.go index 41773aae..6c8ccd97 100644 --- a/internal/database/mariadb/component.go +++ b/internal/database/mariadb/component.go @@ -64,6 +64,9 @@ func (s *SqlDatabase) getComponentUpdateFields(component *entity.Component) stri if component.Type != "" { fl = append(fl, "component_type = :component_type") } + if component.UpdatedBy != "" { + fl = append(fl, "component_updated_by = :component_updated_by") + } return strings.Join(fl, ", ") } @@ -202,10 +205,12 @@ func (s *SqlDatabase) CreateComponent(component *entity.Component) (*entity.Comp query := ` INSERT INTO Component ( component_name, - component_type + component_type, + component_created_by ) VALUES ( :component_name, - :component_type + :component_type, + :component_created_by ) ` diff --git a/internal/database/mariadb/entity.go b/internal/database/mariadb/entity.go index e0870777..d9f07e09 100644 --- a/internal/database/mariadb/entity.go +++ b/internal/database/mariadb/entity.go @@ -409,8 +409,10 @@ type ComponentRow struct { Name sql.NullString `db:"component_name" json:"name"` Type sql.NullString `db:"component_type" json:"type"` CreatedAt sql.NullTime `db:"component_created_at" json:"created_at"` + CreatedBy sql.NullString `db:"component_created_by" json:"created_by"` DeletedAt sql.NullTime `db:"component_deleted_at" json:"deleted_at,omitempty"` UpdatedAt sql.NullTime `db:"component_updated_at" json:"updated_at"` + UpdatedBy sql.NullString `db:"component_updated_by" json:"updated_by"` } func (cr *ComponentRow) AsComponent() entity.Component { @@ -420,8 +422,10 @@ func (cr *ComponentRow) AsComponent() entity.Component { Type: GetStringValue(cr.Type), Metadata: entity.Metadata{ CreatedAt: GetTimeValue(cr.CreatedAt), + CreatedBy: GetStringValue(cr.CreatedBy), DeletedAt: GetTimeValue(cr.DeletedAt), UpdatedAt: GetTimeValue(cr.UpdatedAt), + UpdatedBy: GetStringValue(cr.UpdatedBy), }, } } @@ -431,8 +435,10 @@ func (cr *ComponentRow) FromComponent(c *entity.Component) { cr.Name = sql.NullString{String: c.Name, Valid: true} cr.Type = sql.NullString{String: c.Type, Valid: true} cr.CreatedAt = sql.NullTime{Time: c.CreatedAt, Valid: true} + cr.CreatedBy = sql.NullString{String: c.CreatedBy, Valid: true} cr.DeletedAt = sql.NullTime{Time: c.DeletedAt, Valid: true} cr.UpdatedAt = sql.NullTime{Time: c.UpdatedAt, Valid: true} + cr.UpdatedBy = sql.NullString{String: c.UpdatedBy, Valid: true} } type ComponentVersionRow struct { diff --git a/internal/database/mariadb/init/schema.sql b/internal/database/mariadb/init/schema.sql index 9a91c794..cdb279d6 100644 --- a/internal/database/mariadb/init/schema.sql +++ b/internal/database/mariadb/init/schema.sql @@ -12,8 +12,10 @@ create table if not exists Component component_name varchar(256) not null, component_type varchar(256) not null, component_created_at timestamp default current_timestamp() not null, + component_created_by varchar(256) null, component_deleted_at timestamp null, component_updated_at timestamp default current_timestamp() not null on update current_timestamp(), + component_updated_by varchar(256) null, constraint id_UNIQUE unique (component_id), constraint name_UNIQUE