Skip to content

Commit

Permalink
Function to convert controlpb.folder attributes to gcs.Folder (#2139)
Browse files Browse the repository at this point in the history
* return folder name

* add method to convert controlpb folder to gcs folder

* formatting

* small fix

* review comments

* creting const

* rename method name

* fix nits

* fix nits

* fix nits
  • Loading branch information
Tulsishah authored Jul 12, 2024
1 parent 1a9541f commit 8d72ea9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
9 changes: 2 additions & 7 deletions internal/storage/bucket_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,8 @@ func (b *bucketHandle) GetFolder(ctx context.Context, folderName string) (*gcs.F
return nil, err
}

folder := gcs.Folder{
Name: clientFolder.Name,
Metageneration: clientFolder.Metageneration,
UpdateTime: clientFolder.GetUpdateTime().AsTime(),
}

return &folder, err
folderResponse := gcs.GCSFolder(b.bucketName, clientFolder)
return folderResponse, err
}

func isStorageConditionsNotEmpty(conditions storage.Conditions) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/bucket_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ func (testSuite *BucketHandleTest) TestGetFolderWhenFolderExistsForHierarchicalB

mockClient.AssertExpectations(testSuite.T())
assert.Nil(testSuite.T(), err)
assert.Equal(testSuite.T(), folderPath, result.Name)
assert.Equal(testSuite.T(), TestObjectName, result.Name)
}

func (testSuite *BucketHandleTest) TestGetFolderWhenFolderDoesNotExistsForHierarchicalBucket() {
Expand Down
23 changes: 23 additions & 0 deletions internal/storage/gcs/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,34 @@
package gcs

import (
"strings"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
)

type Folder struct {
Name string
Metageneration int64
UpdateTime time.Time
}

func GCSFolder(bucketName string, attrs *controlpb.Folder) *Folder {
// Setting the parameters in Folder and doing conversions as necessary.
return &Folder{
Name: getFolderName(bucketName, attrs.Name),
Metageneration: attrs.Metageneration,
UpdateTime: attrs.GetUpdateTime().AsTime(),
}
}

// In HNS, folder paths returned by control client APIs are in the form of:
//
// projects/_/buckets/{bucket}/folders/{folder}
// This method extracts the folder name from such a string.
func getFolderName(bucketName string, fullPath string) string {
prefix := "projects/_/buckets/" + bucketName + "/folders/"
folderName := strings.TrimPrefix(fullPath, prefix)

return folderName
}
53 changes: 53 additions & 0 deletions internal/storage/gcs/folder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 Google Inc. All Rights Reserved.
//
// 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 gcs

import (
"testing"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
)

const TestBucketName string = "testBucket"
const TestFolderName string = "testFolder"

func TestGetFolderName(t *testing.T) {
folderPath := "projects/_/buckets/" + TestBucketName + "/folders/" + TestFolderName

result := getFolderName(TestBucketName, folderPath)

assert.Equal(t, result, TestFolderName)
}

func TestGCSFolder(t *testing.T) {
timestamp := &timestamppb.Timestamp{
Seconds: time.Now().Unix(), // Number of seconds since Unix epoch (1970-01-01T00:00:00Z)
Nanos: int32(time.Now().Nanosecond()), // Nanoseconds (0 to 999,999,999)
}
attrs := controlpb.Folder{
Name: TestFolderName,
Metageneration: 10,
UpdateTime: timestamp,
}

gcsFolder := GCSFolder(TestBucketName, &attrs)

assert.Equal(t, attrs.Name, gcsFolder.Name)
assert.Equal(t, attrs.Metageneration, gcsFolder.Metageneration)
assert.Equal(t, attrs.UpdateTime.AsTime(), gcsFolder.UpdateTime)
}

0 comments on commit 8d72ea9

Please sign in to comment.