All files / src/app/shared/api rgw-bucket.service.ts

100% Statements 43/43
90% Branches 9/10
100% Functions 12/12
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 853x 3x   3x 3x 3x   3x 3x           3x 33x   33x           19x 19x   2x 1x   2x       1x                 3x 21x     3x 3x     3x 1x 1x 1x 1x     3x 1x 1x 1x 1x     3x 2x 2x 2x               3x 3x   2x 2x       3x  
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
 
import * as _ from 'lodash';
import { forkJoin as observableForkJoin, of as observableOf } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
 
import { cdEncode } from '../decorators/cd-encode';
import { ApiModule } from './api.module';
 
@cdEncode
@Injectable({
  providedIn: ApiModule
})
export class RgwBucketService {
  private url = 'api/rgw/bucket';
 
  constructor(private http: HttpClient) {}
 
  /**
   * Get the list of buckets.
   * @return {Observable<Object[]>}
   */
  list() {
    return this.enumerate().pipe(
      mergeMap((buckets: string[]) => {
        if (buckets.length > 0) {
          return observableForkJoin(
            buckets.map((bucket: string) => {
              return this.get(bucket);
            })
          );
        }
        return observableOf([]);
      })
    );
  }
 
  /**
   * Get the list of bucket names.
   * @return {Observable<string[]>}
   */
  enumerate() {
    return this.http.get(this.url);
  }
 
  get(bucket: string) {
    return this.http.get(`${this.url}/${bucket}`);
  }
 
  create(bucket: string, uid: string) {
    let params = new HttpParams();
    params = params.append('bucket', bucket);
    params = params.append('uid', uid);
    return this.http.post(this.url, null, { params: params });
  }
 
  update(bucket: string, bucketId: string, uid: string) {
    let params = new HttpParams();
    params = params.append('bucket_id', bucketId);
    params = params.append('uid', uid);
    return this.http.put(`${this.url}/${bucket}`, null, { params: params });
  }
 
  delete(bucket: string, purgeObjects = true) {
    let params = new HttpParams();
    params = params.append('purge_objects', purgeObjects ? 'true' : 'false');
    return this.http.delete(`${this.url}/${bucket}`, { params: params });
  }
 
  /**
   * Check if the specified bucket exists.
   * @param {string} uid The bucket name to check.
   * @return {Observable<boolean>}
   */
  exists(bucket: string) {
    return this.enumerate().pipe(
      mergeMap((resp: string[]) => {
        const index = _.indexOf(resp, bucket);
        return observableOf(-1 !== index);
      })
    );
  }
}