Skip to content

Commit

Permalink
Implement profile update (#1566)
Browse files Browse the repository at this point in the history
This implements the profile update method in the minder server.

This was done last as it's kinda complex... as you can tell from the PR.

We have to verify that the new profile is valid, that certain values like the
project, name and provider don't change. Then we update the rules, and clean up
the unused ones. And keep the instantiations in check.

This, however, will be a very nice usability improvement.

This also adds a profile update command
  • Loading branch information
JAORMX authored Nov 13, 2023
1 parent 7392747 commit c5b0767
Show file tree
Hide file tree
Showing 16 changed files with 2,604 additions and 1,369 deletions.
109 changes: 109 additions & 0 deletions cmd/cli/app/profile/profile_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// Copyright 2023 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 profile

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

// Profile_updateCmd represents the profile update command
var Profile_updateCmd = &cobra.Command{
Use: "update",
Short: "Update a profile within a minder control plane",
Long: `The minder profile update subcommand lets you update profiles for a project
within a minder 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)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
f := util.GetConfigValue(viper.GetViper(), "file", "file", cmd, "").(string)
proj := viper.GetString("project")

var err error

var preader io.Reader

if f == "" {
return fmt.Errorf("error: file must be set")
}

if f == "-" {
preader = os.Stdin
} else {
f = filepath.Clean(f)
fopen, err := os.Open(f)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}

defer fopen.Close()

preader = fopen
}

conn, err := util.GrpcForCommand(cmd, viper.GetViper())
util.ExitNicelyOnError(err, "Error getting grpc connection")
defer conn.Close()

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

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

if proj != "" {
if p.Context == nil {
p.Context = &pb.Context{}
}

p.Context.Project = &proj
}

// update a profile
resp, err := client.UpdateProfile(ctx, &pb.UpdateProfileRequest{
Profile: p,
})
if err != nil {
return fmt.Errorf("error updating profile: %w", err)
}

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

func init() {
ProfileCmd.AddCommand(Profile_updateCmd)
Profile_updateCmd.Flags().StringP("file", "f", "", "Path to the YAML defining the profile (or - for stdin)")
Profile_updateCmd.Flags().StringP("project", "p", "", "Project to update the profile in")
}
16 changes: 16 additions & 0 deletions database/migrations/000006_entity_profiles_unique.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Copyright 2023 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.

-- drop index
DROP INDEX IF EXISTS entity_profiles_unique;
18 changes: 18 additions & 0 deletions database/migrations/000006_entity_profiles_unique.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Copyright 2023 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.

-- We may only have a single entity profile per profile+entity combination,
-- so we can use a unique index to enforce this.
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS entity_profiles_unique ON
entity_profiles (entity, profile_id);
131 changes: 124 additions & 7 deletions database/mock/store.go

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

9 changes: 9 additions & 0 deletions database/query/profile_status.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ SELECT p.id, p.name, ps.profile_status, ps.last_updated FROM profile_status ps
INNER JOIN profiles p ON p.id = ps.profile_id
WHERE p.project_id = $1;

-- DeleteRuleStatusesForProfileAndRuleType deletes a rule evaluation
-- but locks the table before doing so.

-- name: DeleteRuleStatusesForProfileAndRuleType :exec
DELETE FROM rule_evaluations
WHERE id IN (
SELECT id FROM rule_evaluations as re
WHERE re.profile_id = $1 AND re.rule_type_id = $2 FOR UPDATE);

-- name: ListRuleEvaluationsByProfileId :many
WITH
eval_details AS (
Expand Down
Loading

0 comments on commit c5b0767

Please sign in to comment.