Skip to content

Workspace and Workmachine Api Implementation #393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: release-v1.1.3
Choose a base branch
from

Conversation

nxtcoder19
Copy link
Contributor

@nxtcoder19 nxtcoder19 commented Mar 27, 2025

Summary by Sourcery

Implement Workspace and Workmachine API functionality in the infrastructure service, adding support for creating, updating, deleting, and listing workspaces and workmachines

New Features:

  • Add GraphQL resolvers for Workspace and Workmachine operations
  • Implement domain logic for Workspace and Workmachine CRUD operations
  • Create database repositories for Workspace and Workmachine entities

Enhancements:

  • Extend existing infrastructure service with new entity support
  • Add pagination and filtering for Workspace and Workmachine listings

Copy link

sourcery-ai bot commented Mar 27, 2025

Reviewer's Guide by Sourcery

This pull request introduces the Workspace and Workmachine APIs, including GraphQL resolvers, schema definitions, and domain logic. It enables CRUD operations and data retrieval for Workspace and Workmachine entities.

Sequence diagram for creating a Workspace

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workspace Repo

    Client->>GraphQL Resolver: infra_createWorkspace(workspace: Workspace)
    GraphQL Resolver->>Domain: CreateWorkspace(ctx, workspace)
    Domain->>Workspace Repo: Create(ctx, workspace)
    Workspace Repo-->>Domain: Workspace
    Domain-->>GraphQL Resolver: Workspace
    GraphQL Resolver-->>Client: Workspace
Loading

Sequence diagram for updating a Workspace

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workspace Repo

    Client->>GraphQL Resolver: infra_updateWorkspace(workspace: Workspace)
    GraphQL Resolver->>Domain: UpdateWorkspace(ctx, workspace)
    Domain->>Workspace Repo: Patch(ctx, filter, patchForUpdate)
    Workspace Repo-->>Domain: Workspace
    Domain-->>GraphQL Resolver: Workspace
    GraphQL Resolver-->>Client: Workspace
Loading

Sequence diagram for deleting a Workspace

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workspace Repo

    Client->>GraphQL Resolver: infra_deleteWorkspace(name: string)
    GraphQL Resolver->>Domain: DeleteWorkspace(ctx, name)
    Domain->>Workspace Repo: DeleteOne(ctx, filter)
    Workspace Repo-->>Domain: error
    Domain-->>GraphQL Resolver: error
    GraphQL Resolver-->>Client: bool, error
Loading

Sequence diagram for getting a Workspace

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workspace Repo

    Client->>GraphQL Resolver: infra_getWorkspace(name: string)
    GraphQL Resolver->>Domain: GetWorkspace(ctx, name)
    Domain->>Workspace Repo: FindOne(ctx, filter)
    Workspace Repo-->>Domain: Workspace
    Domain-->>GraphQL Resolver: Workspace
    GraphQL Resolver-->>Client: Workspace
Loading

Sequence diagram for upserting a Workmachine

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workmachine Repo

    Client->>GraphQL Resolver: infra_upsertWorkMachine(workmachine: Workmachine)
    GraphQL Resolver->>Domain: UpsertWorkMachine(ctx, workmachine)
    Domain->>Workmachine Repo: Create(ctx, workmachine)
    Workmachine Repo-->>Domain: Workmachine
    Domain-->>GraphQL Resolver: Workmachine
    GraphQL Resolver-->>Client: Workmachine
Loading

Sequence diagram for updating a Workmachine

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workmachine Repo

    Client->>GraphQL Resolver: infra_updateWorkMachine(workmachine: Workmachine)
    GraphQL Resolver->>Domain: UpdateWorkMachine(ctx, workmachine)
    Domain->>Workmachine Repo: Patch(ctx, filter, patchForUpdate)
    Workmachine Repo-->>Domain: Workmachine
    Domain-->>GraphQL Resolver: Workmachine
    GraphQL Resolver-->>Client: Workmachine
Loading

Sequence diagram for updating a Workmachine status

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workmachine Repo

    Client->>GraphQL Resolver: infra_updateWorkMachineStatus(status: bool, name: string)
    GraphQL Resolver->>Domain: UpdateWorkmachineStatus(ctx, status, name)
    Domain->>Workmachine Repo: Patch(ctx, filter, patchForUpdate)
    Workmachine Repo-->>Domain: Workmachine
    Domain-->>GraphQL Resolver: bool, error
    GraphQL Resolver-->>Client: bool, error
Loading

Sequence diagram for getting a Workmachine

sequenceDiagram
    participant Client
    participant GraphQL Resolver
    participant Domain
    participant Workmachine Repo

    Client->>GraphQL Resolver: infra_getWorkmachine(name: string)
    GraphQL Resolver->>Domain: GetWorkmachine(ctx, name)
    Domain->>Workmachine Repo: FindOne(ctx, filter)
    Workmachine Repo-->>Domain: Workmachine
    Domain-->>GraphQL Resolver: Workmachine
    GraphQL Resolver-->>Client: Workmachine
Loading

Class diagram for Workspace entity

classDiagram
    class Workspace {
        Name: string
        AccountName: string
    }
    Workspace : - BaseEntity
    Workspace : - ResourceMetadata
Loading

Class diagram for Workmachine entity

classDiagram
    class Workmachine {
        Name: string
        AccountName: string
        MachineSize: string
        AuthorizedKeys: string
        MachineStatus: bool
    }
    Workmachine : - BaseEntity
    Workmachine : - ResourceMetadata
Loading

File-Level Changes

Change Details Files
Introduced GraphQL resolvers and schema definitions for Workspace and Workmachine entities, enabling CRUD operations and data retrieval through GraphQL.
  • Added InfraCreateWorkspace, InfraUpdateWorkspace, InfraDeleteWorkspace, InfraUpsertWorkMachine, InfraUpdateWorkMachine, InfraUpdateWorkMachineStatus, InfraListWorkspaces, InfraGetWorkspace, and InfraGetWorkmachine resolvers in schema.resolvers.go.
  • Updated schema.graphqls to include definitions for Workspace and Workmachine types and related input types.
  • Added SearchWorkspaces and SearchWorkmachines input types in models_gen.go.
  • Added WorkspaceEdge, WorkspacePaginatedRecords, WorkmachineEdge, and WorkmachinePaginatedRecords types in models_gen.go.
  • Added constants for Workspace and Workmachine fields in generated_constants.go.
  • Updated Taskfile.yml to include Workspace and Workmachine structs in the struct-to-graphql generation.
  • Updated app.go to include new mongo repos for Workspace and Workmachine.
  • Created workmachine.go and workspace.go in the domain directory to implement business logic for workmachines and workspaces.
  • Created workmachine.resolvers.go and workspace.resolvers.go in the graph directory to implement resolvers for workmachines and workspaces.
  • Created workmachine.graphqls and workspace.graphqls in the struct-to-graphql directory to define graphql schema for workmachines and workspaces.
apps/infra/internal/app/graph/schema.resolvers.go
apps/infra/internal/app/graph/model/models_gen.go
apps/infra/internal/entities/field-constants/generated_constants.go
apps/infra/Taskfile.yml
apps/infra/internal/app/app.go
apps/infra/internal/domain/workmachine.go
apps/infra/internal/domain/workspace.go
apps/infra/internal/app/graph/workmachine.resolvers.go
apps/infra/internal/app/graph/workspace.resolvers.go
apps/infra/internal/entities/workmachine.go
apps/infra/internal/entities/workspace.go
apps/infra/internal/app/graph/struct-to-graphql/workmachine.graphqls
apps/infra/internal/app/graph/struct-to-graphql/workspace.graphqls
apps/infra/internal/app/graph/schema.graphqls
Implemented domain logic for Workspace and Workmachine entities, including data access and business rules.
  • Added ListWorkspaces, GetWorkspace, CreateWorkspace, UpdateWorkspace, and DeleteWorkspace methods to the Domain interface in api.go.
  • Added GetWorkmachine, UpsertWorkMachine, UpdateWorkMachine, and UpdateWorkmachineStatus methods to the Domain interface in api.go.
  • Implemented the Domain interface methods for Workspace in workspace.go.
  • Implemented the Domain interface methods for Workmachine in workmachine.go.
apps/infra/internal/domain/api.go
apps/infra/internal/domain/workmachine.go
apps/infra/internal/domain/workspace.go
Added workspace and workmachine yaml files.
  • Added workspace and workmachine yaml files.
.tools/nvim/__http__/workmachine/workmachine.yaml
.tools/nvim/__http__/workmachine/workspace.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nxtcoder19 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a description to the GraphQL schema for each field to improve discoverability and understanding.
  • It would be helpful to add some examples in the workspace.yaml and workmachine.yaml files.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -301,6 +302,65 @@ func (r *mutationResolver) InfraDeletePv(ctx context.Context, clusterName string
return true, nil
}

// InfraCreateWorkspace is the resolver for the infra_createWorkspace field.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Refactor repeated context conversion.

The pattern to extract 'ictx, err := toInfraContext(ctx)' is repeated in multiple resolver functions. Consider extracting this logic into a shared helper or middleware to reduce duplication and enhance consistency.

Suggested implementation:

	"encoding/base64"
	"fmt"

	// You may need to add any necessary import for the infra context type if not already imported.
+// InfraCreateWorkspace is the resolver for the infra_createWorkspace field.
+func (r *mutationResolver) InfraCreateWorkspace(ctx context.Context, workspace entities.Workspace) (*entities.Workspace, error) {
+	ictx, err := r.getInfraContext(ctx)
+	if err != nil {
+		return nil, err
+	}
+	return r.Domain.CreateWorkspace(ictx, workspace)
+}
+// InfraUpdateWorkspace is the resolver for the infra_updateWorkspace field.
+func (r *mutationResolver) InfraUpdateWorkspace(ctx context.Context, workspace entities.Workspace) (*entities.Workspace, error) {
+	ictx, err := r.getInfraContext(ctx)
+	if err != nil {
+		return nil, err
+	}
+	// ... additional logic ...
+}
+// getInfraContext extracts the infra-specific context and wraps any conversion errors.
+

Adjust the return type of getInfraContext (currently declared as interface{}) to the appropriate infra context type used throughout your codebase if available.

Comment on lines 108 to 115
fmt.Println("stream....", args.Stream)
s, err := jc.Jetstream.Stream(ctx, args.Stream)
if err != nil {
return nil, errors.NewE(err)
}

fmt.Println("stream....", err)
c, err := s.CreateOrUpdateConsumer(ctx, jetstream.ConsumerConfig(args.ConsumerConfig))
fmt.Println("logge....", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Remove or replace debug print statements.

The use of fmt.Println for logging debug information could leak unnecessary details in production. Consider using a structured logging package or remove these prints if they were temporary for debugging.

Suggested implementation:

return wm, nil
}

func (d *domain) UpsertWorkMachine(ctx InfraContext, workmachine entities.Workmachine) (*entities.Workmachine, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Clarify upsert behavior for workmachine.

The current UpsertWorkMachine implementation always calls Create without checking for an existing record. If the intent is to perform a true upsert, consider checking for existence and then updating as necessary, or use a repository method that supports upsert semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants