github.com/minio/console@v1.4.1/web-app/src/screens/Console/Buckets/ListBuckets/AddBucket/addBucketThunks.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import { getBytes } from "../../../../../common/utils";
    18  import { createAsyncThunk } from "@reduxjs/toolkit";
    19  import { AppState } from "../../../../../store";
    20  import { api } from "../../../../../api";
    21  import {
    22    MakeBucketRequest,
    23    ObjectRetentionMode,
    24    ObjectRetentionUnit,
    25  } from "../../../../../api/consoleApi";
    26  
    27  export const addBucketAsync = createAsyncThunk(
    28    "buckets/addBucketAsync",
    29    async (_, { getState, rejectWithValue, dispatch }) => {
    30      const state = getState() as AppState;
    31  
    32      const bucketName = state.addBucket.name;
    33      const versioningEnabled = state.addBucket.versioningEnabled;
    34      const lockingEnabled = state.addBucket.lockingEnabled;
    35      const quotaEnabled = state.addBucket.quotaEnabled;
    36      const quotaSize = state.addBucket.quotaSize;
    37      const quotaUnit = state.addBucket.quotaUnit;
    38      const retentionEnabled = state.addBucket.retentionEnabled;
    39      const retentionMode = state.addBucket.retentionMode;
    40      const retentionUnit = state.addBucket.retentionUnit;
    41      const retentionValidity = state.addBucket.retentionValidity;
    42      const distributedSetup = state.system.distributedSetup;
    43      const siteReplicationInfo = state.system.siteReplicationInfo;
    44      const excludeFolders = state.addBucket.excludeFolders;
    45      const excludedPrefixes = state.addBucket.excludedPrefixes;
    46  
    47      let request: MakeBucketRequest = {
    48        name: bucketName,
    49        versioning: {
    50          enabled:
    51            distributedSetup && !siteReplicationInfo.enabled
    52              ? versioningEnabled
    53              : false,
    54          excludePrefixes:
    55            distributedSetup && !siteReplicationInfo.enabled && !lockingEnabled
    56              ? excludedPrefixes.split(",").filter((item) => item.trim() !== "")
    57              : [],
    58          excludeFolders:
    59            distributedSetup && !siteReplicationInfo.enabled && !lockingEnabled
    60              ? excludeFolders
    61              : false,
    62        },
    63        locking: distributedSetup ? lockingEnabled : false,
    64      };
    65  
    66      if (distributedSetup) {
    67        if (quotaEnabled) {
    68          const amount = getBytes(quotaSize, quotaUnit, true);
    69          request.quota = {
    70            enabled: true,
    71            quota_type: "hard",
    72            amount: parseInt(amount),
    73          };
    74        }
    75  
    76        if (retentionEnabled) {
    77          request.retention = {
    78            mode: retentionMode as ObjectRetentionMode,
    79            unit: retentionUnit as ObjectRetentionUnit,
    80            validity: retentionValidity,
    81          };
    82        }
    83      }
    84  
    85      return api.buckets.makeBucket(request);
    86    },
    87  );