Skip to content

Commit

Permalink
Implement profile update
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 committed Nov 7, 2023
1 parent 99eaf2b commit ba7bb62
Show file tree
Hide file tree
Showing 12 changed files with 2,394 additions and 1,372 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")
}
116 changes: 102 additions & 14 deletions database/mock/store.go

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

25 changes: 24 additions & 1 deletion database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,40 @@ INSERT INTO profiles (
alert,
name) VALUES ($1, $2, $3, $4, $5) RETURNING *;

-- name: UpdateProfile :one
UPDATE profiles SET
remediate = $2,
alert = $3
WHERE id = $1 RETURNING *;

-- name: CreateProfileForEntity :one
INSERT INTO entity_profiles (
entity,
profile_id,
contextual_rules) VALUES ($1, $2, sqlc.arg(contextual_rules)::jsonb) RETURNING *;

-- name: UpdateProfileForEntity :one
UPDATE entity_profiles SET
contextual_rules = sqlc.arg(contextual_rules)::jsonb
WHERE profile_id = $1 AND entity = $2 RETURNING *;

-- name: DeleteProfileForEntity :exec
DELETE FROM entity_profiles WHERE profile_id = $1 AND entity = $2;

-- name: GetProfileByProjectAndID :many
SELECT * FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
WHERE profiles.project_id = $1 AND profiles.id = $2;

-- name: GetProfileByID :one
SELECT * FROM profiles WHERE id = $1;

-- name: GetProfileByProjectAndName :many
-- name: GetProfileByIDAndLock :one
SELECT * FROM profiles WHERE id = $1 FOR UPDATE;

-- name: GetProfileByNameAndLock :one
SELECT * FROM profiles WHERE name = $1 AND project_id = $2 FOR UPDATE;

-- name: GetEntityProfileByProjectAndName :many
SELECT * FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
WHERE profiles.project_id = $1 AND profiles.name = $2;

Expand All @@ -36,6 +56,9 @@ INSERT INTO entity_profile_rules (entity_profile_id, rule_type_id)
VALUES ($1, $2)
ON CONFLICT (entity_profile_id, rule_type_id) DO NOTHING RETURNING *;

-- name: DeleteRuleInstantiation :exec
DELETE FROM entity_profile_rules WHERE entity_profile_id = $1 AND rule_type_id = $2;

-- name: ListProfilesInstantiatingRuleType :many
-- get profile information that instantiate a rule. This is done by joining the profiles with entity_profiles, then correlating those
-- with entity_profile_rules. The rule_type_id is used to filter the results. Note that we only really care about the overal profile,
Expand Down
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.

Loading

0 comments on commit ba7bb62

Please sign in to comment.