Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add Autoclass support #6828

Merged
merged 13 commits into from Nov 1, 2022
36 changes: 20 additions & 16 deletions storage/bucket.go
Expand Up @@ -737,6 +737,10 @@ func newBucket(b *raw.Bucket) (*BucketAttrs, error) {
if err != nil {
return nil, err
}
ac, err := toAutoclassFromRaw(b.Autoclass)
if err != nil {
return nil, err
}
return &BucketAttrs{
Name: b.Name,
Location: b.Location,
Expand All @@ -763,7 +767,7 @@ func newBucket(b *raw.Bucket) (*BucketAttrs, error) {
ProjectNumber: b.ProjectNumber,
RPO: toRPO(b),
CustomPlacementConfig: customPlacementFromRaw(b.CustomPlacementConfig),
Autoclass: toAutoclass(b.Autoclass),
Autoclass: ac,
}, nil
}

Expand Down Expand Up @@ -851,7 +855,7 @@ func (b *BucketAttrs) toRawBucket() *raw.Bucket {
IamConfiguration: bktIAM,
Rpo: b.RPO.String(),
CustomPlacementConfig: b.CustomPlacementConfig.toRawCustomPlacement(),
Autoclass: b.Autoclass.toRawBucketAutoclass(),
Autoclass: b.Autoclass.toRawAutoclass(),
}
}

Expand Down Expand Up @@ -911,7 +915,7 @@ func (b *BucketAttrs) toProtoBucket() *storagepb.Bucket {
IamConfig: bktIAM,
Rpo: b.RPO.String(),
CustomPlacementConfig: b.CustomPlacementConfig.toProtoCustomPlacement(),
Autoclass: b.Autoclass.toProtoBucketAutoclass(),
Autoclass: b.Autoclass.toProtoAutoclass(),
}
}

Expand Down Expand Up @@ -994,7 +998,7 @@ func (ua *BucketAttrsToUpdate) toProtoBucket() *storagepb.Bucket {
Website: ua.Website.toProtoBucketWebsite(),
IamConfig: bktIAM,
Rpo: ua.RPO.String(),
Autoclass: ua.Autoclass.toProtoBucketAutoclass(),
Autoclass: ua.Autoclass.toProtoAutoclass(),
}
}

Expand Down Expand Up @@ -1111,7 +1115,7 @@ type BucketAttrsToUpdate struct {
RPO RPO

// If set, updates the autoclass configuration of the bucket.
// See <TBD> for more information.
// See https://cloud.google.com/storage/docs/using-autoclass for more information.
Autoclass *Autoclass

// acl is the list of access control rules on the bucket.
Expand Down Expand Up @@ -1229,7 +1233,8 @@ func (ua *BucketAttrsToUpdate) toRawBucket() *raw.Bucket {
}
if ua.Autoclass != nil {
rb.Autoclass = &raw.BucketAutoclass{
Enabled: ua.Autoclass.Enabled,
Enabled: ua.Autoclass.Enabled,
ForceSendFields: []string{"Enabled"},
}
}
if ua.PredefinedACL != "" {
Expand Down Expand Up @@ -1932,7 +1937,7 @@ func customPlacementFromProto(c *storagepb.Bucket_CustomPlacementConfig) *Custom
return &CustomPlacementConfig{DataLocations: c.GetDataLocations()}
}

func (a *Autoclass) toRawBucketAutoclass() *raw.BucketAutoclass {
func (a *Autoclass) toRawAutoclass() *raw.BucketAutoclass {
if a == nil {
return nil
}
Expand All @@ -1942,7 +1947,7 @@ func (a *Autoclass) toRawBucketAutoclass() *raw.BucketAutoclass {
}
}

func (a *Autoclass) toProtoBucketAutoclass() *storagepb.Bucket_Autoclass {
func (a *Autoclass) toProtoAutoclass() *storagepb.Bucket_Autoclass {
if a == nil {
return nil
}
Expand All @@ -1952,24 +1957,23 @@ func (a *Autoclass) toProtoBucketAutoclass() *storagepb.Bucket_Autoclass {
}
}

func toAutoclass(a *raw.BucketAutoclass) *Autoclass {
if a == nil {
return nil
func toAutoclassFromRaw(a *raw.BucketAutoclass) (*Autoclass, error) {
if a == nil || a.ToggleTime == "" {
return nil, nil
}
// Return Autoclass only if a valid ToggleTime is available.
t, err := time.Parse(time.RFC3339, a.ToggleTime)
if err != nil {
cojenco marked this conversation as resolved.
Show resolved Hide resolved
return &Autoclass{
Enabled: a.Enabled,
}
return nil, err
}
return &Autoclass{
Enabled: a.Enabled,
ToggleTime: t,
}
}, nil
}

func toAutoclassFromProto(a *storagepb.Bucket_Autoclass) *Autoclass {
if a == nil {
if a == nil || a.GetToggleTime().AsTime().Unix() == 0 {
return nil
}
return &Autoclass{
Expand Down