]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rgw-bucket.service.ts
fc88bfa7181649658c4c04005d7dd12ef750f7a7
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / api / rgw-bucket.service.ts
1 import { HttpClient, HttpParams } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
3
4 import _ from 'lodash';
5 import { of as observableOf } from 'rxjs';
6 import { catchError, mapTo } from 'rxjs/operators';
7
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';
11
12 @cdEncode
13 @Injectable({
14 providedIn: 'root'
15 })
16 export class RgwBucketService extends ApiClient {
17 private url = 'api/rgw/bucket';
18
19 constructor(private http: HttpClient, private rgwDaemonService: RgwDaemonService) {
20 super();
21 }
22
23 /**
24 * Get the list of buckets.
25 * @return Observable<Object[]>
26 */
27 list(stats: boolean = false, uid: string = '') {
28 return this.rgwDaemonService.request((params: HttpParams) => {
29 params = params.append('stats', stats.toString());
30 if (uid) {
31 params = params.append('uid', uid);
32 }
33 return this.http.get(this.url, {
34 headers: { Accept: this.getVersionHeaderValue(1, 1) },
35 params: params
36 });
37 });
38 }
39
40 get(bucket: string) {
41 return this.rgwDaemonService.request((params: HttpParams) => {
42 return this.http.get(`${this.url}/${bucket}`, { params: params });
43 });
44 }
45
46 create(
47 bucket: string,
48 uid: string,
49 zonegroup: string,
50 placementTarget: string,
51 lockEnabled: boolean,
52 lock_mode: 'GOVERNANCE' | 'COMPLIANCE',
53 lock_retention_period_days: string
54 ) {
55 return this.rgwDaemonService.request((params: HttpParams) => {
56 return this.http.post(this.url, null, {
57 params: new HttpParams({
58 fromObject: {
59 bucket,
60 uid,
61 zonegroup,
62 placement_target: placementTarget,
63 lock_enabled: String(lockEnabled),
64 lock_mode,
65 lock_retention_period_days,
66 daemon_name: params.get('daemon_name')
67 }
68 })
69 });
70 });
71 }
72
73 update(
74 bucket: string,
75 bucketId: string,
76 uid: string,
77 versioningState: string,
78 mfaDelete: string,
79 mfaTokenSerial: string,
80 mfaTokenPin: string,
81 lockMode: 'GOVERNANCE' | 'COMPLIANCE',
82 lockRetentionPeriodDays: string
83 ) {
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 });
94 });
95 }
96
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 });
101 });
102 }
103
104 /**
105 * Check if the specified bucket exists.
106 * @param {string} bucket The bucket name to check.
107 * @return Observable<boolean>
108 */
109 exists(bucket: string) {
110 return this.get(bucket).pipe(
111 mapTo(true),
112 catchError((error: Event) => {
113 if (_.isFunction(error.preventDefault)) {
114 error.preventDefault();
115 }
116 return observableOf(false);
117 })
118 );
119 }
120
121 getLockDays(bucketData: object): number {
122 if (bucketData['lock_retention_period_years'] > 0) {
123 return Math.floor(bucketData['lock_retention_period_years'] * 365.242);
124 }
125
126 return bucketData['lock_retention_period_days'] || 0;
127 }
128 }