Skip to content

Commit

Permalink
Add provider classes to API (#2762)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Mar 22, 2024
1 parent c4106cd commit febf35f
Show file tree
Hide file tree
Showing 19 changed files with 1,400 additions and 958 deletions.
5 changes: 4 additions & 1 deletion cmd/server/app/migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func init() {
}

func ensureGitHubProvidersHaveAuthFlows(ctx context.Context, cmd *cobra.Command, store db.Store) error {
providers, err := store.GlobalListProvidersByClass(ctx, "github")
providers, err := store.GlobalListProvidersByClass(ctx, db.NullProviderClass{
ProviderClass: db.ProviderClassGithub,
Valid: true,
})
if err != nil {
return fmt.Errorf("error while listing providers: %w", err)
}
Expand Down
16 changes: 16 additions & 0 deletions database/migrations/000035_provider_class_nullable.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TABLE providers
ALTER COLUMN class SET NOT NULL;
16 changes: 16 additions & 0 deletions database/migrations/000035_provider_class_nullable.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2024 Stacklok, Inc
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

ALTER TABLE providers
ALTER COLUMN class DROP NOT NULL;
2 changes: 1 addition & 1 deletion database/mock/store.go

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

23 changes: 23 additions & 0 deletions docs/docs/ref/proto.md

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

2 changes: 1 addition & 1 deletion internal/controlplane/handlers_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestCreateProfile(t *testing.T) {
provider, err := dbStore.CreateProvider(ctx, db.CreateProviderParams{
Name: "github",
ProjectID: dbproj.ID,
Class: db.ProviderClassGithub,
Class: db.NullProviderClass{ProviderClass: db.ProviderClassGithub, Valid: true},
Implements: []db.ProviderType{db.ProviderTypeGithub},
AuthFlows: []db.AuthorizationFlow{db.AuthorizationFlowUserInput},
Definition: []byte(`{}`),
Expand Down
13 changes: 13 additions & 0 deletions internal/controlplane/handlers_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (s *Server) GetProvider(ctx context.Context, req *minderv1.GetProviderReque
AuthFlows: protobufProviderAuthFlowFromDB(ctx, prov),
Config: cfg,
CredentialsState: providers.GetCredentialStateForProvider(ctx, prov, s.store, s.cryptoEngine, &s.cfg.Provider),
Class: providers.GetProviderClassString(prov),
},
}, nil
}
Expand Down Expand Up @@ -132,6 +133,7 @@ func (s *Server) ListProviders(ctx context.Context, req *minderv1.ListProvidersR
AuthFlows: protobufProviderAuthFlowFromDB(ctx, p),
CredentialsState: providers.GetCredentialStateForProvider(ctx, p, s.store, s.cryptoEngine, &s.cfg.Provider),
Config: cfg,
Class: providers.GetProviderClassString(p),
})
}

Expand All @@ -149,6 +151,17 @@ func (s *Server) ListProviders(ctx context.Context, req *minderv1.ListProvidersR
}, nil
}

// ListProviderClasses lists the provider classes available in the system.
func (_ *Server) ListProviderClasses(
_ context.Context, _ *minderv1.ListProviderClassesRequest,
) (*minderv1.ListProviderClassesResponse, error) {
// Note: New provider classes should be added to the providers package.
classes := providers.ListProviderClasses()
return &minderv1.ListProviderClassesResponse{
ProviderClasses: classes,
}, nil
}

func protobufProviderImplementsFromDB(ctx context.Context, p db.Provider) []minderv1.ProviderType {
impls := make([]minderv1.ProviderType, 0, len(p.Implements))
for _, i := range p.Implements {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/models.go

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

4 changes: 2 additions & 2 deletions internal/db/providers.sql.go

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

2 changes: 1 addition & 1 deletion internal/db/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func createRandomProvider(t *testing.T, projectID uuid.UUID) Provider {
prov, err := testQueries.CreateProvider(context.Background(), CreateProviderParams{
Name: rand.RandomName(seed),
ProjectID: projectID,
Class: ProviderClassGithub,
Class: NullProviderClass{ProviderClass: ProviderClassGithub, Valid: true},
Implements: []ProviderType{ProviderTypeGithub, ProviderTypeGit},
AuthFlows: []AuthorizationFlow{AuthorizationFlowUserInput},
Definition: json.RawMessage("{}"),
Expand Down
2 changes: 1 addition & 1 deletion internal/db/querier.go

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

2 changes: 1 addition & 1 deletion internal/eea/eea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func createNeededEntities(ctx context.Context, t *testing.T, testQueries db.Stor
prov, err := testQueries.CreateProvider(ctx, db.CreateProviderParams{
Name: providerName,
ProjectID: proj.ID,
Class: db.ProviderClassGithub,
Class: db.NullProviderClass{ProviderClass: db.ProviderClassGithub, Valid: true},
Implements: []db.ProviderType{db.ProviderTypeRest},
AuthFlows: []db.AuthorizationFlow{db.AuthorizationFlowUserInput},
Definition: json.RawMessage(`{}`),
Expand Down
2 changes: 1 addition & 1 deletion internal/projects/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func ProvisionSelfEnrolledProject(
_, err = qtx.CreateProvider(ctx, db.CreateProviderParams{
Name: github.Github,
ProjectID: project.ID,
Class: db.ProviderClassGithub,
Class: db.NullProviderClass{ProviderClass: db.ProviderClassGithub, Valid: true},
Implements: github.Implements,
Definition: json.RawMessage(`{"github": {}}`),
AuthFlows: github.AuthorizationFlows,
Expand Down
33 changes: 33 additions & 0 deletions internal/providers/classes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Stacklok, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package providers

import "github.com/stacklok/minder/internal/db"

// GetProviderClassString returns the string representation of the provider class.
func GetProviderClassString(prov db.Provider) string {
if prov.Class.Valid {
return string(prov.Class.ProviderClass)
}
return ""
}

// ListProviderClasses returns a list of provider classes.
func ListProviderClasses() []string {
return []string{
string(db.ProviderClassGithub),
string(db.ProviderClassGithubApp),
}
}
50 changes: 50 additions & 0 deletions pkg/api/openapi/minder/v1/minder.swagger.json

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

Loading

0 comments on commit febf35f

Please sign in to comment.