1 import { HttpClient } from '@angular/common/http';
2 import { Injectable } from '@angular/core';
4 import _ from 'lodash';
5 import { map } from 'rxjs/operators';
7 import { ApiClient } from '~/app/shared/api/api-client';
8 import { cdEncode, cdEncodeNot } from '../decorators/cd-encode';
9 import { ImageSpec } from '../models/image-spec';
10 import { RbdConfigurationService } from '../services/rbd-configuration.service';
11 import { RbdPool } from './rbd.model';
17 export class RbdService extends ApiClient {
18 constructor(private http: HttpClient, private rbdConfigurationService: RbdConfigurationService) {
22 isRBDPool(pool: any) {
23 return _.indexOf(pool.application_metadata, 'rbd') !== -1 && !pool.pool_name.includes('/');
27 return this.http.post('api/block/image', rbd, { observe: 'response' });
30 delete(imageSpec: ImageSpec) {
31 return this.http.delete(`api/block/image/${imageSpec.toStringEncoded()}`, {
36 update(imageSpec: ImageSpec, rbd: any) {
37 return this.http.put(`api/block/image/${imageSpec.toStringEncoded()}`, rbd, {
42 get(imageSpec: ImageSpec) {
43 return this.http.get(`api/block/image/${imageSpec.toStringEncoded()}`);
48 .get<RbdPool[]>('api/block/image', {
50 headers: { Accept: this.getVersionHeaderValue(2, 0) },
54 map((response: any) => {
55 return response['body'].map((pool: any) => {
56 pool.value.map((image: any) => {
57 if (!image.configuration) {
60 image.configuration.map((option: any) =>
61 Object.assign(option, this.rbdConfigurationService.getOptionByName(option.name))
65 pool['headers'] = response.headers;
72 copy(imageSpec: ImageSpec, rbd: any) {
73 return this.http.post(`api/block/image/${imageSpec.toStringEncoded()}/copy`, rbd, {
78 flatten(imageSpec: ImageSpec) {
79 return this.http.post(`api/block/image/${imageSpec.toStringEncoded()}/flatten`, null, {
85 return this.http.get('api/block/image/default_features');
88 cloneFormatVersion() {
89 return this.http.get<number>('api/block/image/clone_format_version');
94 @cdEncodeNot snapshotName: string,
95 mirrorImageSnapshot: boolean
98 snapshot_name: snapshotName,
99 mirrorImageSnapshot: mirrorImageSnapshot
101 return this.http.post(`api/block/image/${imageSpec.toStringEncoded()}/snap`, request, {
106 renameSnapshot(imageSpec: ImageSpec, snapshotName: string, @cdEncodeNot newSnapshotName: string) {
108 new_snap_name: newSnapshotName
110 return this.http.put(
111 `api/block/image/${imageSpec.toStringEncoded()}/snap/${snapshotName}`,
119 protectSnapshot(imageSpec: ImageSpec, snapshotName: string, @cdEncodeNot isProtected: boolean) {
121 is_protected: isProtected
123 return this.http.put(
124 `api/block/image/${imageSpec.toStringEncoded()}/snap/${snapshotName}`,
132 rollbackSnapshot(imageSpec: ImageSpec, snapshotName: string) {
133 return this.http.post(
134 `api/block/image/${imageSpec.toStringEncoded()}/snap/${snapshotName}/rollback`,
136 { observe: 'response' }
140 cloneSnapshot(imageSpec: ImageSpec, snapshotName: string, request: any) {
141 return this.http.post(
142 `api/block/image/${imageSpec.toStringEncoded()}/snap/${snapshotName}/clone`,
144 { observe: 'response' }
148 deleteSnapshot(imageSpec: ImageSpec, snapshotName: string) {
149 return this.http.delete(`api/block/image/${imageSpec.toStringEncoded()}/snap/${snapshotName}`, {
155 return this.http.get(`api/block/image/trash/`);
158 createNamespace(pool: string, namespace: string) {
162 return this.http.post(`api/block/pool/${pool}/namespace`, request, { observe: 'response' });
165 listNamespaces(pool: string) {
166 return this.http.get(`api/block/pool/${pool}/namespace/`);
169 deleteNamespace(pool: string, namespace: string) {
170 return this.http.delete(`api/block/pool/${pool}/namespace/${namespace}`, {
175 moveTrash(imageSpec: ImageSpec, delay: number) {
176 return this.http.post(
177 `api/block/image/${imageSpec.toStringEncoded()}/move_trash`,
179 { observe: 'response' }
183 purgeTrash(poolName: string) {
184 return this.http.post(`api/block/image/trash/purge/?pool_name=${poolName}`, null, {
189 restoreTrash(imageSpec: ImageSpec, @cdEncodeNot newImageName: string) {
190 return this.http.post(
191 `api/block/image/trash/${imageSpec.toStringEncoded()}/restore`,
192 { new_image_name: newImageName },
193 { observe: 'response' }
197 removeTrash(imageSpec: ImageSpec, force = false) {
198 return this.http.delete(
199 `api/block/image/trash/${imageSpec.toStringEncoded()}/?force=${force}`,
200 { observe: 'response' }