Fix Update Project Logic (#54)
* carve out scope into a separate branch for a PR * refactor getOpts to be private * narrowed to just the scope proto * wrap in a transaction * remove user from scopes PR * make scope type immutable * refactored with more referential integrity (thanks mike) * refactor based on pair programming with Mike * better VetForWrite that supports db.Options but that requires GetOpts and Options to be exported. * stop printing out all the db logs * refactor withScope to be private * refactor the VetForWrite a bit based on Todd's PR comment * fix typo Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com> * fix ctx propagation and Scope returns * fix ctx propagation and use a const for std retry count * change Std to Crud * fix ctx propagation * make sure update with field masks doesn't update a field not in the mask and force a read after update. * add description * remove comment about future * clean error strings * clean up enum string rep * stdMeta * fix return value to be just nil * fix returns and errors * update test to match new error string * change len of domain to 24 * provide common function for new public ids * use common db.NewPublicId() and provide the correct prefix * move list out of CrudActions and provide a CrudlActions shortcut as well * some comments about the purpose of VetForWrite() * move to random 10 * Adding project API interface. * Creation of Project handler and Tests * Move to using the iam objects. * Setting up testify framework for mocking and assertions. * Fix iam options typo references. * Adding tests for CreateProject and UpdateProject. This also adds some additional argument validation. * Merging in PR 40 * Renamed fakeRepo to mockRepo * Changing around TODOs to capture current plans with the code. * Remove the repo interface and use a real DB for tests. * Fixed a few tests. * Adding checks for properly formatted identifiers. * Updating code to account for recent merges. Relaxing some restrictions on what an id can look like. * Added small comment explaining why RegisterGrpcGatewayer interface exists. * Move project service up a call level. Long term I think it would make sense to define these services even higher up the call stack, potentially at the initialization of the controller server, near where the DB is initialized. * Making these fields align with those in the resources under hosts/... * Hard code the service initialization in the handler registration code. * Regenerate project related code proto code. * Removing type field for project. * Adds basic project handling in CLI/SDK (#47) * Adds SDK for creating and reading projects * Reorganizes a bit * Adds CLI for creating and reading projects * Fixes some some generation bugs * Adds redirect support for default org ID * Fix protobuf import references being moved. * Update client test to look for "orgs" and "projects" instead of "org" and "project" * Update expected segments test * Fix setAddr handling when default organization is in use * Set client in target after create or read * Adding functionality and tests for update masks. * Additional tests for update. Disable update non existant project test. * Make not providing an update mask be an invalid argument. * Make empty update mask paths and no mutable fields in the path an InvalidArgument error. * Fix get Project test. * Adding back in the Update a non existant project test case. * Adding seperate path structured tests. * Attach an issue id to field mask validation TODO. * Generate Project Update SDK code (#58) * Created the UpdateFunction method. * Regenerate protobuf derived code since pull #50 updated dependencies. * Special case IDs to not be passed for updates. * Updated the tools file since imports reordered it. * Add spaces between each tools import/comment grouping. Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com> Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
This commit is contained in:
31 files changed
+542
-101
No files matched your search
@@ -11,11 +11,19 @@ import (
|
||||
pb "github.com/hashicorp/watchtower/internal/gen/controller/api/resources/scopes"
|
||||
pbs "github.com/hashicorp/watchtower/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
var reInvalidID = regexp.MustCompile("[^A-Za-z0-9]")
|
||||
var (
|
||||
reInvalidID = regexp.MustCompile("[^A-Za-z0-9]")
|
||||
// TODO(ICU-28): Find a way to auto update these names and enforce the mappings between wire and storage.
|
||||
wireToStorageMask = map[string]string{
|
||||
"name": "Name",
|
||||
"description": "Description",
|
||||
}
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
pbs.UnimplementedProjectServiceServer
|
||||
@@ -107,22 +115,25 @@ func (s Service) createInRepo(ctx context.Context, req *pbs.CreateProjectRequest
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, req *pbs.UpdateProjectRequest) (*pb.Project, error) {
|
||||
item := req.GetItem()
|
||||
// TODO: convert field masks from API field masks with snake_case to db field masks casing.
|
||||
madeUp := []string{}
|
||||
opts := []iam.Option{iam.WithPublicId(req.GetId())}
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
madeUp = append(madeUp, "Description")
|
||||
opts = append(opts, iam.WithDescription(desc.GetValue()))
|
||||
}
|
||||
if name := item.GetName(); name != nil {
|
||||
madeUp = append(madeUp, "Name")
|
||||
opts = append(opts, iam.WithName(name.GetValue()))
|
||||
}
|
||||
p, err := iam.NewProject(req.GetOrgId(), opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, _, err := s.repo.UpdateScope(ctx, p, madeUp)
|
||||
dbMask, err := toDbUpdateMask(req.GetUpdateMask())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(dbMask) == 0 {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "No valid fields included in the update mask.")
|
||||
}
|
||||
out, _, err := s.repo.UpdateScope(ctx, p, dbMask)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -132,6 +143,25 @@ func (s Service) updateInRepo(ctx context.Context, req *pbs.UpdateProjectRequest
|
||||
return toProto(out), nil
|
||||
}
|
||||
|
||||
// toDbUpdateMask converts the wire format's FieldMask into a list of strings containing FieldMask paths used
|
||||
func toDbUpdateMask(fm *field_mask.FieldMask) ([]string, error) {
|
||||
dbPaths := []string{}
|
||||
invalid := []string{}
|
||||
for _, p := range fm.GetPaths() {
|
||||
for _, f := range strings.Split(p, ",") {
|
||||
if dbField, ok := wireToStorageMask[strings.TrimSpace(f)]; ok {
|
||||
dbPaths = append(dbPaths, dbField)
|
||||
} else {
|
||||
invalid = append(invalid, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(invalid) > 0 {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "Invalid fields passed in update_update mask: %v", invalid)
|
||||
}
|
||||
return dbPaths, nil
|
||||
}
|
||||
|
||||
func toProto(in *iam.Scope) *pb.Project {
|
||||
out := pb.Project{Id: in.GetPublicId()}
|
||||
if in.GetDescription() != "" {
|
||||
@@ -194,7 +224,9 @@ func validateUpdateProjectRequest(req *pbs.UpdateProjectRequest) error {
|
||||
if err := validateID(req.GetOrgId(), "o_"); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: Either require mask to be set or document in API that an unset mask updates all fields.
|
||||
if req.GetUpdateMask() == nil {
|
||||
return status.Errorf(codes.InvalidArgument, "UpdateMask not provided but is required to update a project.")
|
||||
}
|
||||
item := req.GetItem()
|
||||
if item == nil {
|
||||
// It is legitimate for no item to be specified in an update request as it indicates all fields provided in
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
pbs "github.com/hashicorp/watchtower/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/projects"
|
||||
"google.golang.org/genproto/protobuf/field_mask"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
@@ -42,7 +43,7 @@ func createDefaultProjectAndRepo(t *testing.T) (*iam.Scope, *iam.Repository) {
|
||||
t.Fatalf("Could not create org scope: %v", err)
|
||||
}
|
||||
|
||||
p, err := iam.NewProject(oRes.GetPublicId(), iam.WithName("default"))
|
||||
p, err := iam.NewProject(oRes.GetPublicId(), iam.WithName("default"), iam.WithDescription("default"))
|
||||
if err != nil {
|
||||
t.Fatalf("Could not get new project: %v", err)
|
||||
}
|
||||
@@ -64,6 +65,7 @@ func TestGet(t *testing.T) {
|
||||
pProject := &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
Name: &wrappers.StringValue{Value: proj.GetName()},
|
||||
Description: &wrappers.StringValue{Value: proj.GetDescription()},
|
||||
CreatedTime: proj.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: proj.UpdateTime.GetTimestamp(),
|
||||
}
|
||||
@@ -210,6 +212,15 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
proj, repo := createDefaultProjectAndRepo(t)
|
||||
tested := projects.NewService(repo)
|
||||
|
||||
var err error
|
||||
resetProject := func() {
|
||||
if proj, _, err = repo.UpdateScope(context.Background(), proj, []string{"Name", "Description"}); err != nil {
|
||||
t.Fatalf("Failed to reset the project")
|
||||
}
|
||||
}
|
||||
|
||||
projCreated, err := ptypes.Timestamp(proj.GetCreateTime().GetTimestamp())
|
||||
if err != nil {
|
||||
t.Fatalf("Error converting proto to timestamp: %v", err)
|
||||
@@ -227,10 +238,15 @@ func TestUpdate(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Update an Existing Project",
|
||||
req: &pbs.UpdateProjectRequest{Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
}},
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name", "description"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateProjectResponse{
|
||||
Item: &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
@@ -242,23 +258,142 @@ func TestUpdate(t *testing.T) {
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update a Non Existing Project",
|
||||
name: "Multiple Paths in single string",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
Id: "p_DoesntExis",
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name,description"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateProjectResponse{
|
||||
Item: &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
CreatedTime: proj.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "No Update Mask Is Invalid Argument",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "updated name"},
|
||||
Description: &wrappers.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "No Paths in Mask Is Invalid Argument",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{Paths: []string{}},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "updated name"},
|
||||
Description: &wrappers.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Only non-existant paths in Mask Is Invalid Argument",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{Paths: []string{"nonexistant_field"}},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "updated name"},
|
||||
Description: &wrappers.StringValue{Value: "updated desc"},
|
||||
},
|
||||
},
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Unset Name",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Description: &wrappers.StringValue{Value: "ignored"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateProjectResponse{
|
||||
Item: &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
Description: &wrappers.StringValue{Value: "default"},
|
||||
CreatedTime: proj.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update Only Name",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"name"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "updated"},
|
||||
Description: &wrappers.StringValue{Value: "ignored"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateProjectResponse{
|
||||
Item: &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
Name: &wrappers.StringValue{Value: "updated"},
|
||||
Description: &wrappers.StringValue{Value: "default"},
|
||||
CreatedTime: proj.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update Only Description",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"description"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "ignored"},
|
||||
Description: &wrappers.StringValue{Value: "notignored"},
|
||||
},
|
||||
},
|
||||
res: &pbs.UpdateProjectResponse{
|
||||
Item: &pb.Project{
|
||||
Id: proj.GetPublicId(),
|
||||
Name: &wrappers.StringValue{Value: "default"},
|
||||
Description: &wrappers.StringValue{Value: "notignored"},
|
||||
CreatedTime: proj.GetCreateTime().GetTimestamp(),
|
||||
},
|
||||
},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "Update a Non Existing Project",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
Id: "p_DoesntExis",
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"description"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
// TODO: Update this to be NotFound.
|
||||
errCode: codes.Unknown,
|
||||
},
|
||||
{
|
||||
name: "Cant change Id",
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
Id: "p_1234567890",
|
||||
Id: proj.GetPublicId(),
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"id"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
Id: "p_0987654321",
|
||||
Id: "p_somethinge",
|
||||
Name: &wrappers.StringValue{Value: "new"},
|
||||
Description: &wrappers.StringValue{Value: "new desc"},
|
||||
}},
|
||||
@@ -267,33 +402,43 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Cant specify Created Time",
|
||||
req: &pbs.UpdateProjectRequest{Item: &pb.Project{
|
||||
CreatedTime: ptypes.TimestampNow(),
|
||||
}},
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"created_time"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
CreatedTime: ptypes.TimestampNow(),
|
||||
},
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
{
|
||||
name: "Cant specify Updated Time",
|
||||
req: &pbs.UpdateProjectRequest{Item: &pb.Project{
|
||||
UpdatedTime: ptypes.TimestampNow(),
|
||||
}},
|
||||
req: &pbs.UpdateProjectRequest{
|
||||
UpdateMask: &field_mask.FieldMask{
|
||||
Paths: []string{"updated_time"},
|
||||
},
|
||||
Item: &pb.Project{
|
||||
UpdatedTime: ptypes.TimestampNow(),
|
||||
},
|
||||
},
|
||||
res: nil,
|
||||
errCode: codes.InvalidArgument,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer resetProject()
|
||||
assert := assert.New(t)
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateProjectRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
s := projects.NewService(repo)
|
||||
|
||||
got, gErr := s.UpdateProject(context.Background(), req)
|
||||
got, gErr := tested.UpdateProject(context.Background(), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateProject(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
assert.NotNilf(tc.res, "Expected UpdateProject response to be nil, but was %v", got)
|
||||
gotUpdateTime, err := ptypes.Timestamp(got.GetItem().GetUpdatedTime())
|
||||
if err != nil {
|
||||
t.Fatalf("Error converting proto to timestamp: %v", err)
|
||||
@@ -305,7 +450,7 @@ func TestUpdate(t *testing.T) {
|
||||
_ = projCreated
|
||||
|
||||
// Clear all values which are hard to compare against.
|
||||
got.Item.CreatedTime, got.Item.UpdatedTime, tc.res.Item.CreatedTime, tc.res.Item.UpdatedTime = nil, nil, nil, nil
|
||||
got.Item.UpdatedTime, tc.res.Item.UpdatedTime = nil, nil
|
||||
}
|
||||
assert.True(proto.Equal(got, tc.res), "UpdateProject(%q) got response %q, wanted %q", req, got, tc.res)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user