Use swagger api for delete single and multiple Service Accounts (#3199)

This commit is contained in:
Cesar N
2024-01-18 08:55:56 -08:00
committed by GitHub
parent 24742325b7
commit b9f0ccfaba
7 changed files with 125 additions and 42 deletions

View File

@@ -4431,10 +4431,7 @@ func init() {
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"type": "array", "$ref": "#/definitions/selectedSAs"
"items": {
"type": "string"
}
} }
} }
], ],
@@ -7985,6 +7982,12 @@ func init() {
} }
} }
}, },
"selectedSAs": {
"type": "array",
"items": {
"type": "string"
}
},
"selectedUsers": { "selectedUsers": {
"type": "array", "type": "array",
"items": { "items": {
@@ -13616,10 +13619,7 @@ func init() {
"in": "body", "in": "body",
"required": true, "required": true,
"schema": { "schema": {
"type": "array", "$ref": "#/definitions/selectedSAs"
"items": {
"type": "string"
}
} }
} }
], ],
@@ -17327,6 +17327,12 @@ func init() {
} }
} }
}, },
"selectedSAs": {
"type": "array",
"items": {
"type": "string"
}
},
"selectedUsers": { "selectedUsers": {
"type": "array", "type": "array",
"items": { "items": {

View File

@@ -29,6 +29,9 @@ import (
"github.com/go-openapi/errors" "github.com/go-openapi/errors"
"github.com/go-openapi/runtime" "github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/validate"
"github.com/minio/console/models"
) )
// NewDeleteMultipleServiceAccountsParams creates a new DeleteMultipleServiceAccountsParams object // NewDeleteMultipleServiceAccountsParams creates a new DeleteMultipleServiceAccountsParams object
@@ -52,7 +55,7 @@ type DeleteMultipleServiceAccountsParams struct {
Required: true Required: true
In: body In: body
*/ */
SelectedSA []string SelectedSA models.SelectedSAs
} }
// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface // BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface
@@ -66,7 +69,7 @@ func (o *DeleteMultipleServiceAccountsParams) BindRequest(r *http.Request, route
if runtime.HasBody(r) { if runtime.HasBody(r) {
defer r.Body.Close() defer r.Body.Close()
var body []string var body models.SelectedSAs
if err := route.Consumer.Consume(r.Body, &body); err != nil { if err := route.Consumer.Consume(r.Body, &body); err != nil {
if err == io.EOF { if err == io.EOF {
res = append(res, errors.Required("selectedSA", "body", "")) res = append(res, errors.Required("selectedSA", "body", ""))
@@ -74,8 +77,19 @@ func (o *DeleteMultipleServiceAccountsParams) BindRequest(r *http.Request, route
res = append(res, errors.NewParseError("selectedSA", "body", "", err)) res = append(res, errors.NewParseError("selectedSA", "body", "", err))
} }
} else { } else {
// no validation required on inline body // validate body object
o.SelectedSA = body if err := body.Validate(route.Formats); err != nil {
res = append(res, err)
}
ctx := validate.WithOperationRequest(r.Context())
if err := body.ContextValidate(ctx, route.Formats); err != nil {
res = append(res, err)
}
if len(res) == 0 {
o.SelectedSA = body
}
} }
} else { } else {
res = append(res, errors.Required("selectedSA", "body", "")) res = append(res, errors.Required("selectedSA", "body", ""))

44
models/selected_s_as.go Normal file
View File

@@ -0,0 +1,44 @@
// Code generated by go-swagger; DO NOT EDIT.
// This file is part of MinIO Console Server
// Copyright (c) 2023 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package models
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"context"
"github.com/go-openapi/strfmt"
)
// SelectedSAs selected s as
//
// swagger:model selectedSAs
type SelectedSAs []string
// Validate validates this selected s as
func (m SelectedSAs) Validate(formats strfmt.Registry) error {
return nil
}
// ContextValidate validates this selected s as based on context it is used
func (m SelectedSAs) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
return nil
}

View File

@@ -1425,9 +1425,7 @@ paths:
in: body in: body
required: true required: true
schema: schema:
type: array $ref: "#/definitions/selectedSAs"
items:
type: string
responses: responses:
204: 204:
description: A successful response. description: A successful response.
@@ -6196,3 +6194,8 @@ definitions:
format: int64 format: int64
required: required:
- exp - exp
selectedSAs:
type: array
items:
type: string

View File

@@ -1537,6 +1537,8 @@ export interface MaxShareLinkExpResponse {
exp: number; exp: number;
} }
export type SelectedSAs = string[];
export type QueryParamsType = Record<string | number, any>; export type QueryParamsType = Record<string | number, any>;
export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">; export type ResponseFormat = keyof Omit<Body, "body" | "bodyUsed">;
@@ -2177,7 +2179,7 @@ export class Api<
*/ */
downloadMultipleObjects: ( downloadMultipleObjects: (
bucketName: string, bucketName: string,
objectList: string[], objectList: SelectedUsers,
params: RequestParams = {}, params: RequestParams = {},
) => ) =>
this.request<File, ApiError>({ this.request<File, ApiError>({
@@ -2185,6 +2187,7 @@ export class Api<
method: "POST", method: "POST",
body: objectList, body: objectList,
secure: true, secure: true,
type: ContentType.Json,
...params, ...params,
}), }),
@@ -3088,7 +3091,7 @@ export class Api<
* @secure * @secure
*/ */
deleteMultipleServiceAccounts: ( deleteMultipleServiceAccounts: (
selectedSA: string[], selectedSA: SelectedSAs,
params: RequestParams = {}, params: RequestParams = {},
) => ) =>
this.request<void, ApiError>({ this.request<void, ApiError>({
@@ -3096,6 +3099,7 @@ export class Api<
method: "DELETE", method: "DELETE",
body: selectedSA, body: selectedSA,
secure: true, secure: true,
type: ContentType.Json,
...params, ...params,
}), }),

View File

@@ -14,14 +14,15 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { Fragment } from "react"; import React, { Fragment, useState } from "react";
import { ErrorResponseHandler } from "../../../common/types";
import useApi from "../Common/Hooks/useApi";
import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog"; import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog";
import { ConfirmDeleteIcon } from "mds"; import { ConfirmDeleteIcon } from "mds";
import { encodeURLString } from "../../../common/utils"; import { encodeURLString } from "../../../common/utils";
import { setErrorSnackMessage } from "../../../systemSlice"; import { setErrorSnackMessage } from "../../../systemSlice";
import { useAppDispatch } from "../../../store"; import { useAppDispatch } from "../../../store";
import { api } from "api";
import { ApiError, HttpResponse } from "api/consoleApi";
import { errorToHandler } from "api/errors";
interface IDeleteServiceAccountProps { interface IDeleteServiceAccountProps {
closeDeleteModalAndRefresh: (refresh: boolean) => void; closeDeleteModalAndRefresh: (refresh: boolean) => void;
@@ -35,22 +36,27 @@ const DeleteServiceAccount = ({
selectedServiceAccount, selectedServiceAccount,
}: IDeleteServiceAccountProps) => { }: IDeleteServiceAccountProps) => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const onDelSuccess = () => closeDeleteModalAndRefresh(true);
const onDelError = (err: ErrorResponseHandler) =>
dispatch(setErrorSnackMessage(err));
const onClose = () => closeDeleteModalAndRefresh(false); const onClose = () => closeDeleteModalAndRefresh(false);
const [deleteLoading, invokeDeleteApi] = useApi(onDelSuccess, onDelError); const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
if (!selectedServiceAccount) { if (!selectedServiceAccount) {
return null; return null;
} }
const onConfirmDelete = () => { const onConfirmDelete = () => {
invokeDeleteApi( setLoadingDelete(true);
"DELETE", api.serviceAccounts
`/api/v1/service-accounts/${encodeURLString(selectedServiceAccount)}`, .deleteServiceAccount(encodeURLString(selectedServiceAccount))
); .then((_) => {
closeDeleteModalAndRefresh(true);
})
.catch(async (res: HttpResponse<void, ApiError>) => {
const err = (await res.json()) as ApiError;
dispatch(setErrorSnackMessage(errorToHandler(err)));
closeDeleteModalAndRefresh(false);
})
.finally(() => setLoadingDelete(false));
}; };
return ( return (
@@ -59,7 +65,7 @@ const DeleteServiceAccount = ({
confirmText={"Delete"} confirmText={"Delete"}
isOpen={deleteOpen} isOpen={deleteOpen}
titleIcon={<ConfirmDeleteIcon />} titleIcon={<ConfirmDeleteIcon />}
isLoading={deleteLoading} isLoading={loadingDelete}
onConfirm={onConfirmDelete} onConfirm={onConfirmDelete}
onClose={onClose} onClose={onClose}
confirmationContent={ confirmationContent={

View File

@@ -14,13 +14,14 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { Fragment } from "react"; import React, { Fragment, useState } from "react";
import { ConfirmDeleteIcon } from "mds"; import { ConfirmDeleteIcon } from "mds";
import { ErrorResponseHandler } from "../../../common/types";
import useApi from "../../../screens/Console/Common/Hooks/useApi";
import ConfirmDialog from "../../../screens/Console/Common/ModalWrapper/ConfirmDialog"; import ConfirmDialog from "../../../screens/Console/Common/ModalWrapper/ConfirmDialog";
import { setErrorSnackMessage } from "../../../systemSlice"; import { setErrorSnackMessage } from "../../../systemSlice";
import { useAppDispatch } from "../../../store"; import { useAppDispatch } from "../../../store";
import { api } from "api";
import { ApiError, HttpResponse } from "api/consoleApi";
import { errorToHandler } from "api/errors";
interface IDeleteMultiSAsProps { interface IDeleteMultiSAsProps {
closeDeleteModalAndRefresh: (refresh: boolean) => void; closeDeleteModalAndRefresh: (refresh: boolean) => void;
@@ -34,20 +35,25 @@ const DeleteMultipleSAs = ({
selectedSAs, selectedSAs,
}: IDeleteMultiSAsProps) => { }: IDeleteMultiSAsProps) => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const onDelSuccess = () => closeDeleteModalAndRefresh(true);
const onDelError = (err: ErrorResponseHandler) =>
dispatch(setErrorSnackMessage(err));
const onClose = () => closeDeleteModalAndRefresh(false); const onClose = () => closeDeleteModalAndRefresh(false);
const [deleteLoading, invokeDeleteApi] = useApi(onDelSuccess, onDelError); const [loadingDelete, setLoadingDelete] = useState<boolean>(false);
if (!selectedSAs) { if (!selectedSAs) {
return null; return null;
} }
const onConfirmDelete = () => { const onConfirmDelete = () => {
invokeDeleteApi( setLoadingDelete(true);
"DELETE", api.serviceAccounts
`/api/v1/service-accounts/delete-multi`, .deleteMultipleServiceAccounts(selectedSAs)
selectedSAs, .then((_) => {
); closeDeleteModalAndRefresh(true);
})
.catch(async (res: HttpResponse<void, ApiError>) => {
const err = (await res.json()) as ApiError;
dispatch(setErrorSnackMessage(errorToHandler(err)));
closeDeleteModalAndRefresh(false);
})
.finally(() => setLoadingDelete(false));
}; };
return ( return (
<ConfirmDialog <ConfirmDialog
@@ -55,7 +61,7 @@ const DeleteMultipleSAs = ({
confirmText={"Delete"} confirmText={"Delete"}
isOpen={deleteOpen} isOpen={deleteOpen}
titleIcon={<ConfirmDeleteIcon />} titleIcon={<ConfirmDeleteIcon />}
isLoading={deleteLoading} isLoading={loadingDelete}
onConfirm={onConfirmDelete} onConfirm={onConfirmDelete}
onClose={onClose} onClose={onClose}
confirmationContent={ confirmationContent={