in gke-windows-builder/builder/builder/bucket.go [32:81]
func NewGCSBucketIfNotExists(ctx context.Context, projectID string, workspaceBucket string, workspaceBucketLocation string) error {
if workspaceBucket == "" {
log.Printf("No bucket name specified, skip creating the bucket")
return nil
}
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("Storage client creation failed: %+v", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
attrs := &storage.BucketAttrs{
Lifecycle: storage.Lifecycle{
Rules: []storage.LifecycleRule{
{
Action: storage.LifecycleAction{Type: "Delete"},
Condition: storage.LifecycleCondition{
AgeInDays: 1,
},
},
},
},
}
if workspaceBucketLocation != "" {
attrs.Location = workspaceBucketLocation
}
bkt := client.Bucket(workspaceBucket)
// Retrieve the bucket's metadata to find if it already exists and
// that the code has access to the bucket
if _, err := bkt.Attrs(ctx); err == nil {
log.Printf("%v bucket already exists", workspaceBucket)
return nil
} else if err == storage.ErrBucketNotExist {
// The bucket does not exist. Try to create it
if err := bkt.Create(ctx, projectID, attrs); err == nil {
log.Printf("Bucket %v is setup", workspaceBucket)
return nil
} else {
return fmt.Errorf("Create bucket(%q) with error: %+v", workspaceBucket, err)
}
} else {
return fmt.Errorf("Find bucket(%q) with error: %+v", workspaceBucket, err)
}
}