Skip to content

Commit

Permalink
Add CreateFromConfig to provider manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrozek committed May 20, 2024
1 parent 46cf293 commit d77e9ee
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/providers/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (

// ProviderManager encapsulates operations for manipulating Provider instances
type ProviderManager interface {
// CreateFromConfig creates a new Provider instance in the database with a given configuration or the provider default
CreateFromConfig(
ctx context.Context, providerClass db.ProviderClass, projectID uuid.UUID, name string, config json.RawMessage,
) (*db.Provider, error)
// InstantiateFromID creates the provider from the Provider's UUID
InstantiateFromID(ctx context.Context, providerID uuid.UUID) (v1.Provider, error)
// InstantiateFromNameProject creates the provider using the provider's name and
Expand Down Expand Up @@ -109,6 +113,22 @@ func NewProviderManager(
}, nil
}

func (p *providerManager) CreateFromConfig(
ctx context.Context, providerClass db.ProviderClass, projectID uuid.UUID, name string, config json.RawMessage,
) (*db.Provider, error) {
manager, err := p.getClassManager(providerClass)
if err != nil {
return nil, fmt.Errorf("error getting class manager: %w", err)
}

provConfig, err := manager.GetConfig(ctx, providerClass, config)
if err != nil {
return nil, fmt.Errorf("error getting provider config: %w", err)
}

return p.store.Create(ctx, providerClass, name, projectID, provConfig)
}

func (p *providerManager) InstantiateFromID(ctx context.Context, providerID uuid.UUID) (v1.Provider, error) {
config, err := p.store.GetByID(ctx, providerID)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions internal/providers/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package manager_test

import (
"context"
"encoding/json"
"errors"
"testing"

Expand All @@ -30,6 +31,62 @@ import (
"github.com/stacklok/minder/internal/providers/mock/fixtures"
)

func TestProviderManager_CreateFromConfig(t *testing.T) {
t.Parallel()

scenarios := []struct {
Name string
Provider *db.Provider
Config json.RawMessage
ExpectedError string
}{
{
Name: "CreateFromConfig returns error when provider class has no associated manager",
Provider: githubAppProvider,
ExpectedError: "unexpected provider class",
},
{
Name: "CreateFromConfig creates a github provider with default configuration",
Provider: githubProvider,
Config: json.RawMessage(`{ github: {} }`),
},
{
Name: "CreateFromConfig creates a github provider with custom configuration",
Provider: githubProvider,
Config: json.RawMessage(`{ github: { key: value} }`),
},
}

for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()

store := fixtures.NewProviderStoreMock()(ctrl)

classManager := mockmanager.NewMockProviderClassManager(ctrl)
classManager.EXPECT().GetSupportedClasses().Return([]db.ProviderClass{db.ProviderClassGithub}).MaxTimes(1)
classManager.EXPECT().GetConfig(gomock.Any(), scenario.Provider.Class, gomock.Any()).Return(scenario.Config, nil).MaxTimes(1)

store.EXPECT().Create(gomock.Any(), scenario.Provider.Class, scenario.Provider.Name, scenario.Provider.ProjectID, scenario.Config).Return(scenario.Provider, nil).MaxTimes(1)

provManager, err := manager.NewProviderManager(store, classManager)
require.NoError(t, err)

newProv, err := provManager.CreateFromConfig(ctx, scenario.Provider.Class, scenario.Provider.ProjectID, scenario.Provider.Name, scenario.Config)
if scenario.ExpectedError != "" {
require.ErrorContains(t, err, scenario.ExpectedError)
} else {
require.NoError(t, err)
scenario.Provider.Definition = scenario.Config
require.Equal(t, scenario.Provider, newProv)
}
})
}
}

// Test both create by name/project, and create by ID together.
// This is because the test logic is basically identical.
func TestProviderManager_Instantiate(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions internal/providers/manager/mock/manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d77e9ee

Please sign in to comment.