Skip to content
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

Create ProviderStore interface, migrate controlplane to use it #2842

Closed
wants to merge 3 commits into from

Conversation

dmjb
Copy link
Contributor

@dmjb dmjb commented Mar 28, 2024

Relates to #2845

This provides an interface for retrieving Providers out of the database, and encapsulates the logic for doing so. This makes it easier to stub out the provider retrieval logic for testing, and decouples the controlplane from the details of finding the right provider for a request.

There are other places in the codebase where this should be used, but that is an exercise for a future PR.

Summary

Provide a brief overview of the changes and the issue being addressed.
Explain the rationale and any background necessary for understanding the changes.
List dependencies required by this change, if any.

Fixes #(related issue)

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

@dmjb dmjb requested a review from a team as a code owner March 28, 2024 12:49
@@ -0,0 +1,147 @@
// Copyright 2024 Stacklok, Inc
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similar to my other PR - this relies on the existing tests in the controlplane to validate it. It should get its own dedicated test suite in due course.

)

// ProviderStore provides methods for retrieving Providers from the database
type ProviderStore interface {
Copy link
Contributor Author

@dmjb dmjb Mar 28, 2024

Choose a reason for hiding this comment

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

The term Repository is often used for these types of interface. However, given that that word already means something in the minder codebase, I am reusing Store

Copy link
Member

Choose a reason for hiding this comment

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

We also have the term "Registry" used. I could also get behind Factory if we're retiring ProviderBuilder as an interface.

@coveralls
Copy link

coveralls commented Mar 28, 2024

Coverage Status

coverage: 47.565% (+0.01%) from 47.554%
when pulling 72c8477 on provider-store
into 71f9a29 on main.

evankanderson
evankanderson previously approved these changes Mar 28, 2024
Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

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

A bunch of small nits, but this seems like a straightforward step-along-the-way refactor.

)

// ProviderStore provides methods for retrieving Providers from the database
type ProviderStore interface {
Copy link
Member

Choose a reason for hiding this comment

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

We also have the term "Registry" used. I could also get behind Factory if we're retiring ProviderBuilder as an interface.

type ProviderStore interface {
// GetByName returns the provider instance in the database as identified
// by its project ID and name.
GetByName(ctx context.Context, projectID uuid.UUID, name string) (*db.Provider, error)
Copy link
Member

Choose a reason for hiding this comment

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

Future plan is to return an interface here that's not tied to the DB, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

Comment on lines +38 to +43
GetByNameAndTrait(
ctx context.Context,
projectID uuid.UUID,
name string,
trait db.ProviderType,
) ([]db.Provider, error)
Copy link
Member

Choose a reason for hiding this comment

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

❤️

Comment on lines +46 to +53
type providerStore struct {
store db.Store
}

// NewProviderStore returns a new instance of ProviderStore.
func NewProviderStore(store db.Store) ProviderStore {
return &providerStore{store: store}
}
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I'm fine with having an exported struct that matches the interface, rather than needing constructors for all of these. But not a big deal either way.

)
}

return &providers[0], nil
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check that len(providers) > 0 first?

Copy link
Member

Choose a reason for hiding this comment

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

It looks like we assume that the argument will always come from findProviders; since this is only used one place, I'd be inclined to just inline it.

return inferProvider(providers, nameFilter)
}

func (p *providerStore) GetByNameAndTrait(
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth a comment that GetByNameAndTrait will never return an empty list of Providers and a nil error?

This provides an interface for retrieving Providers out of the database,
and encapsulates the logic for doing so. This makes it easier to stub
out the provider retrieval logic for testing, and decouples the
controlplane from the details of finding the right provider for a
request.
@dmjb
Copy link
Contributor Author

dmjb commented Apr 2, 2024

Replaced by this PR: #2900

@dmjb dmjb closed this Apr 2, 2024
@dmjb dmjb deleted the provider-store branch April 2, 2024 09:35
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.

3 participants