1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import _ from 'lodash';
5 import { of as observableOf } from 'rxjs';
6 import { catchError, mapTo } from 'rxjs/operators';
8 import { ApiClient } from '~/app/shared/api/api-client';
9 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
10 import { cdEncode } from '~/app/shared/decorators/cd-encode';
16 export class RgwBucketService extends ApiClient {
17 private url = 'api/rgw/bucket';
19 constructor(private http: HttpClient, private rgwDaemonService: RgwDaemonService) {
24 * Get the list of buckets.
25 * @return Observable<Object[]>
27 list(stats: boolean = false, uid: string = '') {
28 return this.rgwDaemonService.request((params: HttpParams) => {
29 params = params.append('stats', stats.toString());
31 params = params.append('uid', uid);
33 return this.http.get(this.url, {
34 headers: { Accept: this.getVersionHeaderValue(1, 1) },
41 return this.rgwDaemonService.request((params: HttpParams) => {
42 return this.http.get(`${this.url}/${bucket}`, { params: params });
50 placementTarget: string,
52 lock_mode: 'GOVERNANCE' | 'COMPLIANCE',
53 lock_retention_period_days: string
55 return this.rgwDaemonService.request((params: HttpParams) => {
56 return this.http.post(this.url, null, {
57 params: new HttpParams({
62 placement_target: placementTarget,
63 lock_enabled: String(lockEnabled),
65 lock_retention_period_days,
66 daemon_name: params.get('daemon_name')
77 versioningState: string,
79 mfaTokenSerial: string,
81 lockMode: 'GOVERNANCE' | 'COMPLIANCE',
82 lockRetentionPeriodDays: string
84 return this.rgwDaemonService.request((params: HttpParams) => {
85 params = params.append('bucket_id', bucketId);
86 params = params.append('uid', uid);
87 params = params.append('versioning_state', versioningState);
88 params = params.append('mfa_delete', mfaDelete);
89 params = params.append('mfa_token_serial', mfaTokenSerial);
90 params = params.append('mfa_token_pin', mfaTokenPin);
91 params = params.append('lock_mode', lockMode);
92 params = params.append('lock_retention_period_days', lockRetentionPeriodDays);
93 return this.http.put(`${this.url}/${bucket}`, null, { params: params });
97 delete(bucket: string, purgeObjects = true) {
98 return this.rgwDaemonService.request((params: HttpParams) => {
99 params = params.append('purge_objects', purgeObjects ? 'true' : 'false');
100 return this.http.delete(`${this.url}/${bucket}`, { params: params });
105 * Check if the specified bucket exists.
106 * @param {string} bucket The bucket name to check.
107 * @return Observable<boolean>
109 exists(bucket: string) {
110 return this.get(bucket).pipe(
112 catchError((error: Event) => {
113 if (_.isFunction(error.preventDefault)) {
114 error.preventDefault();
116 return observableOf(false);
121 getLockDays(bucketData: object): number {
122 if (bucketData['lock_retention_period_years'] > 0) {
123 return Math.floor(bucketData['lock_retention_period_years'] * 365.242);
126 return bucketData['lock_retention_period_days'] || 0;