-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subproject creation/deletion operations (#2556)
* Add subproject creation operations Signed-off-by: Juan Antonio Osorio <[email protected]> Add sub-project deletion capabilities Signed-off-by: Juan Antonio Osorio <[email protected]> Handle name constraints more gracefully Signed-off-by: Juan Antonio Osorio <[email protected]> Add sub-projects to project list output Signed-off-by: Juan Antonio Osorio <[email protected]> Aestetic changes to the CLI Signed-off-by: Juan Antonio Osorio <[email protected]> User visible errors are important! Signed-off-by: Juan Antonio Osorio <[email protected]> Use viper for reading projects in project sub-commands Signed-off-by: Juan Antonio Osorio <[email protected]> rename Signed-off-by: Juan Antonio Osorio <[email protected]> Use recursive algorithm to traverse project trees instead of serial Signed-off-by: Juan Antonio Osorio <[email protected]> * Add feature flags for project hierarchy operations Signed-off-by: Juan Antonio Osorio <[email protected]> --------- Signed-off-by: Juan Antonio Osorio <[email protected]>
- Loading branch information
Showing
21 changed files
with
2,448 additions
and
956 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// 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 project | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/stacklok/minder/cmd/cli/app" | ||
"github.com/stacklok/minder/internal/util" | ||
"github.com/stacklok/minder/internal/util/cli" | ||
"github.com/stacklok/minder/internal/util/cli/table" | ||
"github.com/stacklok/minder/internal/util/cli/table/layouts" | ||
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
// projectCreateCmd is the command for creating sub-projects | ||
var projectCreateCmd = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a sub-project within a minder control plane", | ||
Long: `The list command lists the projects available to you within a minder control plane.`, | ||
RunE: cli.GRPCClientWrapRunE(createCommand), | ||
} | ||
|
||
// listCommand is the command for listing projects | ||
func createCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error { | ||
client := minderv1.NewProjectsServiceClient(conn) | ||
|
||
format := viper.GetString("output") | ||
project := viper.GetString("project") | ||
name := viper.GetString("name") | ||
|
||
// No longer print usage on returned error, since we've parsed our inputs | ||
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413 | ||
cmd.SilenceUsage = true | ||
|
||
resp, err := client.CreateProject(ctx, &minderv1.CreateProjectRequest{ | ||
Context: &minderv1.Context{ | ||
Project: &project, | ||
}, | ||
Name: name, | ||
}) | ||
if err != nil { | ||
return cli.MessageAndError("Error creating sub-project", err) | ||
} | ||
|
||
switch format { | ||
case app.JSON: | ||
out, err := util.GetJsonFromProto(resp) | ||
if err != nil { | ||
return cli.MessageAndError("Error getting json from proto", err) | ||
} | ||
cmd.Println(out) | ||
case app.YAML: | ||
out, err := util.GetYamlFromProto(resp) | ||
if err != nil { | ||
return cli.MessageAndError("Error getting yaml from proto", err) | ||
} | ||
cmd.Println(out) | ||
case app.Table: | ||
t := table.New(table.Simple, layouts.Default, []string{"ID", "Name"}) | ||
t.AddRow(resp.Project.ProjectId, resp.Project.Name) | ||
t.Render() | ||
default: | ||
return fmt.Errorf("unsupported output format: %s", format) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
ProjectCmd.AddCommand(projectCreateCmd) | ||
|
||
projectCreateCmd.Flags().StringP("project", "j", "", "The project to create the sub-project within") | ||
projectCreateCmd.Flags().StringP("name", "n", "", "The name of the project to create") | ||
// mark as required | ||
if err := projectCreateCmd.MarkFlagRequired("name"); err != nil { | ||
panic(err) | ||
} | ||
projectCreateCmd.Flags().StringP("output", "o", app.Table, | ||
fmt.Sprintf("Output format (one of %s)", strings.Join(app.SupportedOutputFormats(), ","))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// 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 project | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/stacklok/minder/internal/util/cli" | ||
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1" | ||
) | ||
|
||
// projectDeleteCmd is the command for deleting sub-projects | ||
var projectDeleteCmd = &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete a sub-project within a minder control plane", | ||
Long: `Delete a sub-project within a minder control plane`, | ||
RunE: cli.GRPCClientWrapRunE(deleteCommand), | ||
} | ||
|
||
// listCommand is the command for listing projects | ||
func deleteCommand(ctx context.Context, cmd *cobra.Command, conn *grpc.ClientConn) error { | ||
client := minderv1.NewProjectsServiceClient(conn) | ||
|
||
project := viper.GetString("project") | ||
|
||
// No longer print usage on returned error, since we've parsed our inputs | ||
// See https://github.com/spf13/cobra/issues/340#issuecomment-374617413 | ||
cmd.SilenceUsage = true | ||
|
||
resp, err := client.DeleteProject(ctx, &minderv1.DeleteProjectRequest{ | ||
Context: &minderv1.Context{ | ||
Project: &project, | ||
}, | ||
}) | ||
if err != nil { | ||
return cli.MessageAndError("Error deleting sub-project", err) | ||
} | ||
|
||
cmd.Println("Successfully deleted profile with id:", resp.ProjectId) | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
ProjectCmd.AddCommand(projectDeleteCmd) | ||
|
||
projectDeleteCmd.Flags().StringP("project", "j", "", "The sub-project to delete") | ||
// mark as required | ||
if err := projectDeleteCmd.MarkFlagRequired("project"); err != nil { | ||
panic(err) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
database/migrations/000027_project_hierarchy_feature.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- 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. | ||
|
||
DELETE FROM features WHERE name = 'project_hierarchy_operations_enabled'; |
18 changes: 18 additions & 0 deletions
18
database/migrations/000027_project_hierarchy_feature.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- 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. | ||
|
||
-- hierarchy operations feature | ||
INSERT INTO features(name, settings) | ||
VALUES ('project_hierarchy_operations_enabled', '{}') | ||
ON CONFLICT DO NOTHING; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.