Skip to content

Commit

Permalink
Merge pull request #1108 from stacklok/policy-to-profile
Browse files Browse the repository at this point in the history
cleanup: Rename policy to profile
  • Loading branch information
JAORMX authored Oct 5, 2023
2 parents 28b454b + 555983f commit 1028f16
Show file tree
Hide file tree
Showing 104 changed files with 2,951 additions and 2,947 deletions.
16 changes: 8 additions & 8 deletions cmd/cli/app/policy/policy.go → cmd/cli/app/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package policy provides the CLI subcommand for managing policies
package policy
// Package profile provides the CLI subcommand for managing profiles
package profile

import (
"github.com/spf13/cobra"

"github.com/stacklok/mediator/cmd/cli/app"
)

// PolicyCmd is the root command for the policy subcommands
var PolicyCmd = &cobra.Command{
Use: "policy",
Short: "Manage policies within a mediator control plane",
Long: `The medic policy subcommands allows the management of policies within
// ProfileCmd is the root command for the profile subcommands
var ProfileCmd = &cobra.Command{
Use: "profile",
Short: "Manage profiles within a mediator control plane",
Long: `The medic profile subcommands allows the management of profiles within
a mediator controlplane.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}

func init() {
app.RootCmd.AddCommand(PolicyCmd)
app.RootCmd.AddCommand(ProfileCmd)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package profile

import (
"fmt"
Expand All @@ -29,11 +29,11 @@ import (
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

// Policy_createCmd represents the policy create command
var Policy_createCmd = &cobra.Command{
// Profile_createCmd represents the profile create command
var Profile_createCmd = &cobra.Command{
Use: "create",
Short: "Create a policy within a mediator control plane",
Long: `The medic policy create subcommand lets you create new policies for a project
Short: "Create a profile within a mediator control plane",
Long: `The medic profile create subcommand lets you create new profiles for a project
within a mediator control plane.`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
Expand Down Expand Up @@ -70,13 +70,13 @@ within a mediator control plane.`,
util.ExitNicelyOnError(err, "Error getting grpc connection")
defer conn.Close()

client := pb.NewPolicyServiceClient(conn)
client := pb.NewProfileServiceClient(conn)
ctx, cancel := util.GetAppContext()
defer cancel()

p, err := engine.ParseYAML(preader)
if err != nil {
return fmt.Errorf("error reading policy from file: %w", err)
return fmt.Errorf("error reading profile from file: %w", err)
}

if proj != "" {
Expand All @@ -87,23 +87,23 @@ within a mediator control plane.`,
p.Context.Project = &proj
}

// create a policy
resp, err := client.CreatePolicy(ctx, &pb.CreatePolicyRequest{
Policy: p,
// create a profile
resp, err := client.CreateProfile(ctx, &pb.CreateProfileRequest{
Profile: p,
})
if err != nil {
return fmt.Errorf("error creating policy: %w", err)
return fmt.Errorf("error creating profile: %w", err)
}

table := initializeTable(cmd)
renderPolicyTable(resp.GetPolicy(), table)
renderProfileTable(resp.GetProfile(), table)
table.Render()
return nil
},
}

func init() {
PolicyCmd.AddCommand(Policy_createCmd)
Policy_createCmd.Flags().StringP("file", "f", "", "Path to the YAML defining the policy (or - for stdin)")
Policy_createCmd.Flags().StringP("project", "p", "", "Project to create the policy in")
ProfileCmd.AddCommand(Profile_createCmd)
Profile_createCmd.Flags().StringP("file", "f", "", "Path to the YAML defining the profile (or - for stdin)")
Profile_createCmd.Flags().StringP("project", "p", "", "Project to create the profile in")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package profile

import (
"fmt"
Expand All @@ -26,18 +26,18 @@ import (
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

var policy_deleteCmd = &cobra.Command{
var profile_deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete a policy within a mediator controlplane",
Long: `The medic policy delete subcommand lets you delete policies within a
Short: "delete a profile within a mediator controlplane",
Long: `The medic profile delete subcommand lets you delete profiles within a
mediator control plane.`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
fmt.Fprintf(os.Stderr, "Error binding flags: %s\n", err)
}
},
Run: func(cmd *cobra.Command, args []string) {
// delete the policy via GRPC
// delete the profile via GRPC
id := viper.GetString("id")
provider := viper.GetString("provider")

Expand All @@ -46,27 +46,27 @@ mediator control plane.`,
util.ExitNicelyOnError(err, "Error getting grpc connection")
defer conn.Close()

client := pb.NewPolicyServiceClient(conn)
client := pb.NewProfileServiceClient(conn)
ctx, cancel := util.GetAppContext()
defer cancel()

_, err = client.DeletePolicy(ctx, &pb.DeletePolicyRequest{
_, err = client.DeleteProfile(ctx, &pb.DeleteProfileRequest{
Context: &pb.Context{
Provider: provider,
},
Id: id,
})

util.ExitNicelyOnError(err, "Error deleting policy")
cmd.Println("Successfully deleted policy with id:", id)
util.ExitNicelyOnError(err, "Error deleting profile")
cmd.Println("Successfully deleted profile with id:", id)
},
}

func init() {
PolicyCmd.AddCommand(policy_deleteCmd)
policy_deleteCmd.Flags().StringP("id", "i", "", "id of policy to delete")
policy_deleteCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
err := policy_deleteCmd.MarkFlagRequired("id")
ProfileCmd.AddCommand(profile_deleteCmd)
profile_deleteCmd.Flags().StringP("id", "i", "", "id of profile to delete")
profile_deleteCmd.Flags().StringP("provider", "p", "github", "Provider for the profile")
err := profile_deleteCmd.MarkFlagRequired("id")
util.ExitNicelyOnError(err, "Error marking flag as required")
// TODO: add a flag for the policy name
// TODO: add a flag for the profile name
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package profile

import (
"fmt"
Expand All @@ -27,10 +27,10 @@ import (
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

var policy_getCmd = &cobra.Command{
var profile_getCmd = &cobra.Command{
Use: "get",
Short: "Get details for a policy within a mediator control plane",
Long: `The medic policy get subcommand lets you retrieve details for a policy within a
Short: "Get details for a profile within a mediator control plane",
Long: `The medic profile get subcommand lets you retrieve details for a profile within a
mediator control plane.`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
Expand All @@ -49,32 +49,32 @@ mediator control plane.`,
util.ExitNicelyOnError(err, "Error getting grpc connection")
defer conn.Close()

client := pb.NewPolicyServiceClient(conn)
client := pb.NewProfileServiceClient(conn)
ctx, cancel := util.GetAppContext()
defer cancel()

id := viper.GetString("id")
policy, err := client.GetPolicyById(ctx, &pb.GetPolicyByIdRequest{
profile, err := client.GetProfileById(ctx, &pb.GetProfileByIdRequest{
Context: &pb.Context{
Provider: provider,
// TODO set up project if specified
// Currently it's inferred from the authorization token
},
Id: id,
})
util.ExitNicelyOnError(err, "Error getting policy")
util.ExitNicelyOnError(err, "Error getting profile")

switch format {
case app.YAML:
out, err := util.GetYamlFromProto(policy)
out, err := util.GetYamlFromProto(profile)
util.ExitNicelyOnError(err, "Error getting yaml from proto")
fmt.Println(out)
case app.JSON:
out, err := util.GetJsonFromProto(policy)
out, err := util.GetJsonFromProto(profile)
util.ExitNicelyOnError(err, "Error getting json from proto")
fmt.Println(out)
case app.Table:
p := policy.GetPolicy()
p := profile.GetProfile()
handleGetTableOutput(cmd, p)
}

Expand All @@ -83,23 +83,23 @@ mediator control plane.`,
}

func init() {
PolicyCmd.AddCommand(policy_getCmd)
policy_getCmd.Flags().StringP("id", "i", "", "ID for the policy to query")
policy_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
policy_getCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
ProfileCmd.AddCommand(profile_getCmd)
profile_getCmd.Flags().StringP("id", "i", "", "ID for the profile to query")
profile_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
profile_getCmd.Flags().StringP("provider", "p", "github", "Provider for the profile")
// TODO set up project if specified

if err := policy_getCmd.MarkFlagRequired("id"); err != nil {
if err := profile_getCmd.MarkFlagRequired("id"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}

}

func handleGetTableOutput(cmd *cobra.Command, policy *pb.Policy) {
func handleGetTableOutput(cmd *cobra.Command, profile *pb.Profile) {
table := initializeTable(cmd)

renderPolicyTable(policy, table)
renderProfileTable(profile, table)

table.Render()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package profile

import (
"fmt"
Expand All @@ -27,10 +27,10 @@ import (
pb "github.com/stacklok/mediator/pkg/api/protobuf/go/mediator/v1"
)

var policy_listCmd = &cobra.Command{
var profile_listCmd = &cobra.Command{
Use: "list",
Short: "List policies within a mediator control plane",
Long: `The medic policy list subcommand lets you list policies within a
Short: "List profiles within a mediator control plane",
Long: `The medic profile list subcommand lets you list profiles within a
mediator control plane for an specific project.`,
PreRun: func(cmd *cobra.Command, args []string) {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
Expand All @@ -46,7 +46,7 @@ mediator control plane for an specific project.`,
}
defer conn.Close()

client := pb.NewPolicyServiceClient(conn)
client := pb.NewProfileServiceClient(conn)
ctx, cancel := util.GetAppContext()
defer cancel()

Expand All @@ -60,15 +60,15 @@ mediator control plane for an specific project.`,
return fmt.Errorf("invalid format: %s", format)
}

resp, err := client.ListPolicies(ctx, &pb.ListPoliciesRequest{
resp, err := client.ListProfiles(ctx, &pb.ListProfilesRequest{
Context: &pb.Context{
Provider: provider,
// TODO set up project if specified
// Currently it's inferred from the authorization token
},
})
if err != nil {
return fmt.Errorf("error getting policies: %w", err)
return fmt.Errorf("error getting profiles: %w", err)
}

switch format {
Expand All @@ -91,23 +91,23 @@ mediator control plane for an specific project.`,
}

func init() {
PolicyCmd.AddCommand(policy_listCmd)
policy_listCmd.Flags().StringP("provider", "p", "", "Provider to list policies for")
policy_listCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
ProfileCmd.AddCommand(profile_listCmd)
profile_listCmd.Flags().StringP("provider", "p", "", "Provider to list profiles for")
profile_listCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
// TODO: Take project ID into account
// policy_listCmd.Flags().Int32P("project-id", "g", 0, "project id to list roles for")
// profile_listCmd.Flags().Int32P("project-id", "g", 0, "project id to list roles for")

if err := policy_listCmd.MarkFlagRequired("provider"); err != nil {
if err := profile_listCmd.MarkFlagRequired("provider"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}
}

func handleListTableOutput(cmd *cobra.Command, resp *pb.ListPoliciesResponse) {
func handleListTableOutput(cmd *cobra.Command, resp *pb.ListProfilesResponse) {
table := initializeTable(cmd)

for _, v := range resp.Policies {
renderPolicyTable(v, table)
for _, v := range resp.Profiles {
renderProfileTable(v, table)
}
table.Render()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package policy
package profile

import (
"github.com/olekukonko/tablewriter"
Expand All @@ -36,8 +36,8 @@ func initializeTable(cmd *cobra.Command) *tablewriter.Table {
return table
}

func renderPolicyTable(
p *mediatorv1.Policy,
func renderProfileTable(
p *mediatorv1.Profile,
table *tablewriter.Table,
) {
// repositories
Expand All @@ -54,9 +54,9 @@ func renderPolicyTable(
}

func renderEntityRuleSets(
p *mediatorv1.Policy,
p *mediatorv1.Profile,
entType mediatorv1.EntityType,
rs []*mediatorv1.Policy_Rule,
rs []*mediatorv1.Profile_Rule,
table *tablewriter.Table,
) {
for idx := range rs {
Expand All @@ -67,9 +67,9 @@ func renderEntityRuleSets(
}

func renderRuleTable(
p *mediatorv1.Policy,
p *mediatorv1.Profile,
entType mediatorv1.EntityType,
rule *mediatorv1.Policy_Rule,
rule *mediatorv1.Profile_Rule,
table *tablewriter.Table,
) {

Expand Down
Loading

0 comments on commit 1028f16

Please sign in to comment.