Skip to content

Commit

Permalink
Merge pull request #2207 from influxdata/check_org_id_before_create_b…
Browse files Browse the repository at this point in the history
…ucket

fix(bolt): check org id exists before create bucket
  • Loading branch information
kelwang authored Jan 3, 2019
2 parents 52a6e7a + de708f3 commit 7751fb6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
19 changes: 15 additions & 4 deletions bolt/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,21 @@ func (c *Client) CreateBucket(ctx context.Context, b *platform.Bucket) error {
var err error
op := getOp(platform.OpCreateBucket)
return c.db.Update(func(tx *bolt.Tx) error {
if !b.OrganizationID.Valid() {
o, err := c.findOrganizationByName(ctx, tx, b.Organization)
if err != nil {
return err
if b.OrganizationID.Valid() {
_, pe := c.findOrganizationByID(ctx, tx, b.OrganizationID)
if pe != nil {
return &platform.Error{
Err: pe,
Op: op,
}
}
} else {
o, pe := c.findOrganizationByName(ctx, tx, b.Organization)
if pe != nil {
return &platform.Error{
Err: pe,
Op: op,
}
}
b.OrganizationID = o.ID
}
Expand Down
19 changes: 15 additions & 4 deletions inmem/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,21 @@ func (s *Service) FindBuckets(ctx context.Context, filter platform.BucketFilter,

// CreateBucket creates a new bucket and sets b.ID with the new identifier.
func (s *Service) CreateBucket(ctx context.Context, b *platform.Bucket) error {
if !b.OrganizationID.Valid() {
o, err := s.findOrganizationByName(ctx, b.Organization)
if err != nil {
return err
if b.OrganizationID.Valid() {
_, pe := s.FindOrganizationByID(ctx, b.OrganizationID)
if pe != nil {
return &platform.Error{
Err: pe,
Op: OpPrefix + platform.OpCreateBucket,
}
}
} else {
o, pe := s.findOrganizationByName(ctx, b.Organization)
if pe != nil {
return &platform.Error{
Err: pe,
Op: OpPrefix + platform.OpCreateBucket,
}
}
b.OrganizationID = o.ID
}
Expand Down
44 changes: 44 additions & 0 deletions testing/bucket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,50 @@ func CreateBucket(
},
},
},
{
name: "create bucket with orgID not exist",
fields: BucketFields{
IDGenerator: mock.NewIDGenerator(bucketOneID, t),
Buckets: []*platform.Bucket{},
Organizations: []*platform.Organization{},
},
args: args{
bucket: &platform.Bucket{
Name: "name1",
OrganizationID: MustIDBase16(orgOneID),
},
},
wants: wants{
buckets: []*platform.Bucket{},
err: &platform.Error{
Code: platform.ENotFound,
Msg: "organization not found",
Op: platform.OpCreateBucket,
},
},
},
{
name: "create bucket with org name not exist",
fields: BucketFields{
IDGenerator: mock.NewIDGenerator(bucketOneID, t),
Buckets: []*platform.Bucket{},
Organizations: []*platform.Organization{},
},
args: args{
bucket: &platform.Bucket{
Name: "name1",
Organization: "org1",
},
},
wants: wants{
buckets: []*platform.Bucket{},
err: &platform.Error{
Code: platform.ENotFound,
Msg: "organization not found",
Op: platform.OpCreateBucket,
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 7751fb6

Please sign in to comment.