Skip to content

Commit

Permalink
Remove provider from profile (#2850)
Browse files Browse the repository at this point in the history
Fix #2848
  • Loading branch information
eleftherias authored Apr 2, 2024
1 parent 2058524 commit a9000d8
Show file tree
Hide file tree
Showing 41 changed files with 165 additions and 280 deletions.
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var applyCmd = &cobra.Command{
func applyCommand(_ context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
f := viper.GetString("file")

Expand Down Expand Up @@ -85,7 +84,7 @@ func applyCommand(_ context.Context, cmd *cobra.Command, conn *grpc.ClientConn)

// cmd.Context() is the root context. We need to create a new context for each file
// so we can avoid the timeout.
profile, err := ExecOnOneProfile(cmd.Context(), table, f, cmd.InOrStdin(), project, provider, applyFunc)
profile, err := ExecOnOneProfile(cmd.Context(), table, f, cmd.InOrStdin(), project, applyFunc)
if err != nil {
return cli.MessageAndError(fmt.Sprintf("error applying profile from %s", f), err)
}
Expand Down
15 changes: 3 additions & 12 deletions cmd/cli/app/profile/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// ExecOnOneProfile is a helper function to execute a function on a single profile
func ExecOnOneProfile(ctx context.Context, t table.Table, f string, dashOpen io.Reader, project string, provider string,
func ExecOnOneProfile(ctx context.Context, t table.Table, f string, dashOpen io.Reader, project string,
exec func(context.Context, string, *minderv1.Profile) (*minderv1.Profile, error),
) (*minderv1.Profile, error) {
ctx, cancel := cli.GetAppContext(ctx, viper.GetViper())
Expand All @@ -41,7 +41,7 @@ func ExecOnOneProfile(ctx context.Context, t table.Table, f string, dashOpen io.
}
defer closer()

p, err := parseProfile(reader, project, provider)
p, err := parseProfile(reader, project)
if err != nil {
return nil, fmt.Errorf("error parsing profile: %w", err)
}
Expand All @@ -56,7 +56,7 @@ func ExecOnOneProfile(ctx context.Context, t table.Table, f string, dashOpen io.
return profile, nil
}

func parseProfile(r io.Reader, proj string, provider string) (*minderv1.Profile, error) {
func parseProfile(r io.Reader, proj string) (*minderv1.Profile, error) {
p, err := engine.ParseYAML(r)
if err != nil {
return nil, fmt.Errorf("error reading profile from file: %w", err)
Expand All @@ -71,14 +71,5 @@ func parseProfile(r io.Reader, proj string, provider string) (*minderv1.Profile,
p.Context.Project = &proj
}

// Override the YAML specified provider with the command line argument
if provider != "" {
if p.Context == nil {
p.Context = &minderv1.Context{}
}

p.Context.Provider = &provider
}

return p, nil
}
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var createCmd = &cobra.Command{
func createCommand(_ context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
f := viper.GetString("file")
enableAlerts := viper.GetBool("enable-alerts")
Expand Down Expand Up @@ -74,7 +73,7 @@ func createCommand(_ context.Context, cmd *cobra.Command, conn *grpc.ClientConn)
}
// cmd.Context() is the root context. We need to create a new context for each file
// so we can avoid the timeout.
profile, err := ExecOnOneProfile(cmd.Context(), table, f, cmd.InOrStdin(), project, provider, createFunc)
profile, err := ExecOnOneProfile(cmd.Context(), table, f, cmd.InOrStdin(), project, createFunc)
if err != nil {
return cli.MessageAndError(fmt.Sprintf("error creating profile from %s", f), err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ var deleteCmd = &cobra.Command{
func deleteCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
id := viper.GetString("id")

Expand All @@ -48,7 +47,7 @@ func deleteCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientCon

// Delete profile
_, err := client.DeleteProfile(ctx, &minderv1.DeleteProfileRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
Context: &minderv1.Context{Project: &project},
Id: id,
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var getCmd = &cobra.Command{
func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
format := viper.GetString("output")
id := viper.GetString("id")
Expand All @@ -57,7 +56,7 @@ func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn)
cmd.SilenceUsage = true

p, err := client.GetProfileById(ctx, &minderv1.GetProfileByIdRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
Context: &minderv1.Context{Project: &project},
Id: id,
})
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var listCmd = &cobra.Command{
func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
format := viper.GetString("output")

Expand All @@ -55,7 +54,7 @@ func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn)
cmd.SilenceUsage = true

resp, err := client.ListProfiles(ctx, &minderv1.ListProfilesRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
Context: &minderv1.Context{Project: &project},
})
if err != nil {
return cli.MessageAndError("Error getting profiles", err)
Expand Down
2 changes: 0 additions & 2 deletions cmd/cli/app/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra"

"github.com/stacklok/minder/cmd/cli/app"
ghclient "github.com/stacklok/minder/internal/providers/github/oauth"
)

// ProfileCmd is the root command for the profile subcommands
Expand All @@ -36,6 +35,5 @@ var ProfileCmd = &cobra.Command{
func init() {
app.RootCmd.AddCommand(ProfileCmd)
// Flags for all subcommands
ProfileCmd.PersistentFlags().StringP("provider", "p", ghclient.Github, "Name of the provider, i.e. github")
ProfileCmd.PersistentFlags().StringP("project", "j", "", "ID of the project")
}
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/status/status_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var getCmd = &cobra.Command{
func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
profileName := viper.GetString("name")
entityId := viper.GetString("entity")
Expand All @@ -56,7 +55,7 @@ func getCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn)
}

resp, err := client.GetProfileStatusByName(ctx, &minderv1.GetProfileStatusByNameRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
Context: &minderv1.Context{Project: &project},
Name: profileName,
Entity: &minderv1.EntityTypedId{
Id: entityId,
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/profile/status/status_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ var listCmd = &cobra.Command{
func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error {
client := minderv1.NewProfileServiceClient(conn)

provider := viper.GetString("provider")
project := viper.GetString("project")
profileName := viper.GetString("name")
format := viper.GetString("output")
Expand All @@ -55,7 +54,7 @@ func listCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn)
}

resp, err := client.GetProfileStatusByName(ctx, &minderv1.GetProfileStatusByNameRequest{
Context: &minderv1.Context{Provider: &provider, Project: &project},
Context: &minderv1.Context{Project: &project},
Name: profileName,
All: detailed,
RuleType: ruleType,
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/app/profile/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewProfileSettingsTable() table.Table {

// RenderProfileSettingsTable renders the profile settings table
func RenderProfileSettingsTable(p *minderv1.Profile, t table.Table) {
t.AddRow(p.GetId(), p.GetName(), p.GetContext().GetProvider(), p.GetAlert(), p.GetRemediate())
t.AddRow(p.GetId(), p.GetName(), p.GetAlert(), p.GetRemediate())
}

// NewProfileTable creates a new table for rendering profiles
Expand Down
29 changes: 29 additions & 0 deletions database/migrations/000043_profiles_drop_provider.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- 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.

BEGIN;

ALTER TABLE profiles
ADD CONSTRAINT profiles_provider_id_name_fkey
FOREIGN KEY (provider_id, provider)
REFERENCES providers(id, name)
ON DELETE CASCADE;

ALTER TABLE profiles
ALTER COLUMN provider SET NOT NULL;

ALTER TABLE profiles
ALTER COLUMN provider_id SET NOT NULL;

COMMIT;
26 changes: 26 additions & 0 deletions database/migrations/000043_profiles_drop_provider.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- 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.

BEGIN;

-- Drop the existing foreign key constraint on provider
ALTER TABLE profiles DROP CONSTRAINT profiles_provider_id_name_fkey;

ALTER TABLE profiles
ALTER COLUMN provider DROP NOT NULL;

ALTER TABLE profiles
ALTER COLUMN provider_id DROP NOT NULL;

COMMIT;
4 changes: 1 addition & 3 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
-- name: CreateProfile :one
INSERT INTO profiles (
provider,
project_id,
remediate,
alert,
name,
provider_id,
subscription_id,
display_name,
labels
) VALUES ($1, $2, $3, $4, $5, sqlc.arg(provider_id), sqlc.narg(subscription_id), sqlc.arg(display_name), COALESCE(sqlc.arg(labels)::text[], '{}'::text[])) RETURNING *;
) VALUES ($1, $2, $3, $4, sqlc.narg(subscription_id), sqlc.arg(display_name), COALESCE(sqlc.arg(labels)::text[], '{}'::text[])) RETURNING *;

-- name: UpdateProfile :one
UPDATE profiles SET
Expand Down
5 changes: 2 additions & 3 deletions docs/docs/ref/cli/minder_profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ minder profile [flags]
### Options

```
-h, --help help for profile
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
-h, --help help for profile
-j, --project string ID of the project
```

### Options inherited from parent commands
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ minder profile apply [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ minder profile create [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ minder profile delete [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ minder profile get [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ minder profile list [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_status.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ minder profile status [flags]
--identity-client string Identity server client ID (default "minder-cli")
--identity-url string Identity server issuer URL (default "https://auth.stacklok.com")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_status_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ minder profile status get [flags]
-n, --name string Profile name to get profile status for
-o, --output string Output format (one of json,yaml,table) (default "table")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
1 change: 0 additions & 1 deletion docs/docs/ref/cli/minder_profile_status_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ minder profile status list [flags]
-n, --name string Profile name to get profile status for
-o, --output string Output format (one of json,yaml,table) (default "table")
-j, --project string ID of the project
-p, --provider string Name of the provider, i.e. github (default "github")
```

### SEE ALSO
Expand Down
Loading

0 comments on commit a9000d8

Please sign in to comment.