Skip to content

Commit

Permalink
fix: move health check to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
renlulu committed Sep 16, 2024
1 parent 525ceda commit 8f947e9
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions das/google_cloud_storage_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,35 @@ func NewGoogleCloudStorageService(config GoogleCloudStorageServiceConfig) (Stora
if err != nil {
return nil, fmt.Errorf("error creating Google Cloud Storage client: %v", err)
}
return &GoogleCloudStorageService{
service := &GoogleCloudStorageService{
operator: &GoogleCloudStorageClient{client: client},
bucket: config.Bucket,
objectPrefix: config.ObjectPrefix,
enableExpiry: config.EnableExpiry,
maxRetention: config.MaxRetention,
}, nil
}
if config.EnableExpiry {
lifecycleRule := googlestorage.LifecycleRule{
Action: googlestorage.LifecycleAction{Type: "Delete"},
Condition: googlestorage.LifecycleCondition{AgeInDays: int64(config.MaxRetention.Hours() / 24)}, // Objects older than 30 days
}
ctx := context.Background()
bucket := service.operator.Bucket(service.bucket)
// check if bucket exists (and others), and update expiration policy if enabled
attrs, err := bucket.Attrs(ctx)
if err != nil {
return nil, fmt.Errorf("error getting bucket attributes: %v", err)
}
attrs.Lifecycle.Rules = append(attrs.Lifecycle.Rules, lifecycleRule)

bucketAttrsToUpdate := googlestorage.BucketAttrsToUpdate{
Lifecycle: &attrs.Lifecycle,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
return nil, fmt.Errorf("failed to update bucket lifecycle: %v", err)
}
}
return service, nil
}

func (gcs *GoogleCloudStorageService) Put(ctx context.Context, data []byte, expiry uint64) error {
Expand Down Expand Up @@ -177,25 +199,6 @@ func (gcs *GoogleCloudStorageService) HealthCheck(ctx context.Context) error {
if !cmp.Equal(perms, permissions) {
return fmt.Errorf("permissions mismatch (-want +got):\n%s", cmp.Diff(permissions, perms))
}
// check if bucket exists (and others), and update expiration policy if enabled
attrs, err := bucket.Attrs(ctx)
if err != nil {
return err
}
if gcs.enableExpiry {
lifecycleRule := googlestorage.LifecycleRule{
Action: googlestorage.LifecycleAction{Type: "Delete"},
Condition: googlestorage.LifecycleCondition{AgeInDays: int64(gcs.maxRetention.Hours() / 24)}, // Objects older than 30 days
}
attrs.Lifecycle.Rules = append(attrs.Lifecycle.Rules, lifecycleRule)

bucketAttrsToUpdate := googlestorage.BucketAttrsToUpdate{
Lifecycle: &attrs.Lifecycle,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
return fmt.Errorf("failed to update bucket lifecycle: %v", err)
}
}

return nil
}

0 comments on commit 8f947e9

Please sign in to comment.