Ejemplo n.º 1
0
func CreateBucket(db database.Database, clk common.Clock, bucketId string, owner string) (*model.Bucket, *HttpError) {
	if bucketId == "" {
		return nil, NewHttpError(http.StatusBadRequest, "Bucket ID not provided")
	}

	if len(owner) == 0 {
		return nil, NewHttpError(http.StatusBadRequest, "Bucket Owner not provided")
	}

	_, err := db.GetBucket(bucketId)
	if err != nil && !err.EntityNotFound() {
		return nil, NewWrappedHttpError(http.StatusInternalServerError, err)
	}
	if err == nil {
		return nil, NewHttpError(http.StatusBadRequest, "Entity exists")
	}

	var bucket model.Bucket
	bucket.Id = bucketId
	bucket.DateCreated = clk.Now()
	bucket.State = model.OPEN
	bucket.Owner = owner
	if err := db.InsertBucket(&bucket); err != nil {
		return nil, NewWrappedHttpError(http.StatusBadRequest, err)
	}
	return &bucket, nil
}
Ejemplo n.º 2
0
func CloseBucket(bucket *model.Bucket, db database.Database, s3Bucket *s3.Bucket, clk common.Clock) error {
	if bucket.State != model.OPEN {
		return fmt.Errorf("Bucket is already closed")
	}

	bucket.State = model.CLOSED
	bucket.DateClosed = clk.Now()
	if err := db.UpdateBucket(bucket); err != nil {
		return err
	}

	if artifacts, err := db.ListArtifactsInBucket(bucket.Id); err != nil {
		return err
	} else {
		for _, artifact := range artifacts {
			if err := CloseArtifact(&artifact, db, s3Bucket, false); err != nil {
				return err
			}
		}
	}

	return nil
}