diff --git a/portal-ui/src/screens/Console/NotificationEndpoints/CustomForms/EditConfiguration.tsx b/portal-ui/src/screens/Console/NotificationEndpoints/CustomForms/EditConfiguration.tsx index 5df74f018..c9d2b9cc3 100644 --- a/portal-ui/src/screens/Console/NotificationEndpoints/CustomForms/EditConfiguration.tsx +++ b/portal-ui/src/screens/Console/NotificationEndpoints/CustomForms/EditConfiguration.tsx @@ -38,6 +38,7 @@ import { IElementValue, } from "../../Configurations/types"; import { ErrorResponseHandler } from "../../../../common/types"; +import ResetConfigurationModal from "./ResetConfigurationModal"; const styles = (theme: Theme) => createStyles({ @@ -84,6 +85,8 @@ const EditConfiguration = ({ const [saving, setSaving] = useState(false); const [loadingConfig, setLoadingConfig] = useState(true); const [configValues, setConfigValues] = useState([]); + const [resetConfigurationOpen, setResetConfigurationOpen] = + useState(false); //Effects useEffect(() => { const configId = get(selectedConfiguration, "configuration_id", false); @@ -147,8 +150,21 @@ const EditConfiguration = ({ [setValueObj] ); + const continueReset = (restart: boolean) => { + setResetConfigurationOpen(false); + serverNeedsRestart(restart); + }; + return ( + {resetConfigurationOpen && ( + + )} +
{loadingConfig && ( @@ -165,6 +181,17 @@ const EditConfiguration = ({ /> + +     + + + + ); +}; + +const mapDispatchToProps = { + setErrorSnackMessage, +}; + +const connector = connect(null, mapDispatchToProps); + +export default withStyles(styles)(connector(ResetConfigurationModal)); diff --git a/restapi/admin_config.go b/restapi/admin_config.go index 3ae9be7d1..b09c5b380 100644 --- a/restapi/admin_config.go +++ b/restapi/admin_config.go @@ -55,6 +55,15 @@ func registerConfigHandlers(api *operations.ConsoleAPI) { } return admin_api.NewSetConfigOK().WithPayload(resp) }) + // Reset Configuration + api.AdminAPIResetConfigHandler = admin_api.ResetConfigHandlerFunc(func(params admin_api.ResetConfigParams, session *models.Principal) middleware.Responder { + resp, err := resetConfigResponse(session, params.Name) + if err != nil { + return admin_api.NewResetConfigDefault(int(err.Code)).WithPayload(err) + } + return admin_api.NewResetConfigOK().WithPayload(resp) + }) + } // listConfig gets all configurations' names and their descriptions @@ -198,3 +207,29 @@ func setConfigResponse(session *models.Principal, name string, configRequest *mo } return &models.SetConfigResponse{Restart: needsRestart}, nil } + +func resetConfig(ctx context.Context, client MinioAdmin, configName *string) (err error) { + err = client.delConfigKV(ctx, *configName) + return err +} + +// resetConfigResponse implements resetConfig() to be used by handler +func resetConfigResponse(session *models.Principal, configName string) (*models.SetConfigResponse, *models.Error) { + mAdmin, err := NewMinioAdminClient(session) + if err != nil { + return nil, prepareError(err) + } + // create a MinIO Admin Client interface implementation + // defining the client to be used + adminClient := AdminClient{Client: mAdmin} + + ctx := context.Background() + + err = resetConfig(ctx, adminClient, &configName) + + if err != nil { + return nil, prepareError(err) + } + + return &models.SetConfigResponse{Restart: true}, nil +} diff --git a/restapi/admin_config_test.go b/restapi/admin_config_test.go index a14be1b56..12720cf1e 100644 --- a/restapi/admin_config_test.go +++ b/restapi/admin_config_test.go @@ -50,6 +50,7 @@ const ( var minioHelpConfigKVMock func(subSys, key string, envOnly bool) (madmin.Help, error) var minioGetConfigKVMock func(key string) ([]byte, error) var minioSetConfigKVMock func(kv string) (restart bool, err error) +var minioDelConfigKVMock func(name string) (err error) // mock function helpConfigKV() func (ac adminClientMock) helpConfigKV(ctx context.Context, subSys, key string, envOnly bool) (madmin.Help, error) { @@ -66,6 +67,10 @@ func (ac adminClientMock) setConfigKV(ctx context.Context, kv string) (restart b return minioSetConfigKVMock(kv) } +func (ac adminClientMock) delConfigKV(ctx context.Context, name string) (err error) { + return minioDelConfigKVMock(name) +} + func TestListConfig(t *testing.T) { assert := assert.New(t) adminClient := adminClientMock{} @@ -165,6 +170,34 @@ func TestSetConfig(t *testing.T) { } +func TestDelConfig(t *testing.T) { + assert := assert.New(t) + adminClient := adminClientMock{} + function := "resetConfig()" + // mock function response from setConfig() + minioDelConfigKVMock = func(name string) (err error) { + return nil + } + configName := "region" + + ctx := context.Background() + // Test-1 : resetConfig() resets a config with the config name + err := resetConfig(ctx, adminClient, &configName) + if err != nil { + t.Errorf("Failed on %s:, error occurred: %s", function, err.Error()) + } + + // Test-2 : resetConfig() returns error, handle properly + minioDelConfigKVMock = func(name string) (err error) { + return errors.New("error") + } + + err = resetConfig(ctx, adminClient, &configName) + if assert.Error(err) { + assert.Equal("error", err.Error()) + } +} + func Test_buildConfig(t *testing.T) { type args struct { configName *string diff --git a/restapi/client-admin.go b/restapi/client-admin.go index 5bd1c3258..56dd52595 100644 --- a/restapi/client-admin.go +++ b/restapi/client-admin.go @@ -84,6 +84,7 @@ type MinioAdmin interface { getConfigKV(ctx context.Context, key string) ([]byte, error) helpConfigKV(ctx context.Context, subSys, key string, envOnly bool) (madmin.Help, error) setConfigKV(ctx context.Context, kv string) (restart bool, err error) + delConfigKV(ctx context.Context, kv string) (err error) serviceRestart(ctx context.Context) error serverInfo(ctx context.Context) (madmin.InfoMessage, error) startProfiling(ctx context.Context, profiler madmin.ProfilerType) ([]madmin.StartProfilingResult, error) @@ -233,6 +234,11 @@ func (ac AdminClient) setConfigKV(ctx context.Context, kv string) (restart bool, return ac.Client.SetConfigKV(ctx, kv) } +// implements madmin.DelConfigKV() +func (ac AdminClient) delConfigKV(ctx context.Context, kv string) (err error) { + return ac.Client.DelConfigKV(ctx, kv) +} + // implements madmin.ServiceRestart() func (ac AdminClient) serviceRestart(ctx context.Context) (err error) { return ac.Client.ServiceRestart(ctx) diff --git a/restapi/embedded_spec.go b/restapi/embedded_spec.go index 9aa0b7fef..412b00599 100644 --- a/restapi/embedded_spec.go +++ b/restapi/embedded_spec.go @@ -2099,6 +2099,37 @@ func init() { } } }, + "/configs/{name}/reset": { + "get": { + "tags": [ + "AdminAPI" + ], + "summary": "Configuration reset", + "operationId": "ResetConfig", + "parameters": [ + { + "type": "string", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/setConfigResponse" + } + }, + "default": { + "description": "Generic error response.", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + }, "/group": { "get": { "tags": [ @@ -7796,6 +7827,37 @@ func init() { } } }, + "/configs/{name}/reset": { + "get": { + "tags": [ + "AdminAPI" + ], + "summary": "Configuration reset", + "operationId": "ResetConfig", + "parameters": [ + { + "type": "string", + "name": "name", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/setConfigResponse" + } + }, + "default": { + "description": "Generic error response.", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } + }, "/group": { "get": { "tags": [ diff --git a/restapi/operations/admin_api/reset_config.go b/restapi/operations/admin_api/reset_config.go new file mode 100644 index 000000000..c6f0a90af --- /dev/null +++ b/restapi/operations/admin_api/reset_config.go @@ -0,0 +1,88 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2021 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 . +// + +package admin_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime/middleware" + + "github.com/minio/console/models" +) + +// ResetConfigHandlerFunc turns a function with the right signature into a reset config handler +type ResetConfigHandlerFunc func(ResetConfigParams, *models.Principal) middleware.Responder + +// Handle executing the request and returning a response +func (fn ResetConfigHandlerFunc) Handle(params ResetConfigParams, principal *models.Principal) middleware.Responder { + return fn(params, principal) +} + +// ResetConfigHandler interface for that can handle valid reset config params +type ResetConfigHandler interface { + Handle(ResetConfigParams, *models.Principal) middleware.Responder +} + +// NewResetConfig creates a new http.Handler for the reset config operation +func NewResetConfig(ctx *middleware.Context, handler ResetConfigHandler) *ResetConfig { + return &ResetConfig{Context: ctx, Handler: handler} +} + +/* ResetConfig swagger:route GET /configs/{name}/reset AdminAPI resetConfig + +Configuration reset + +*/ +type ResetConfig struct { + Context *middleware.Context + Handler ResetConfigHandler +} + +func (o *ResetConfig) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + route, rCtx, _ := o.Context.RouteInfo(r) + if rCtx != nil { + *r = *rCtx + } + var Params = NewResetConfigParams() + uprinc, aCtx, err := o.Context.Authorize(r, route) + if err != nil { + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + if aCtx != nil { + *r = *aCtx + } + var principal *models.Principal + if uprinc != nil { + principal = uprinc.(*models.Principal) // this is really a models.Principal, I promise + } + + if err := o.Context.BindValidRequest(r, route, &Params); err != nil { // bind params + o.Context.Respond(rw, r, route.Produces, route, err) + return + } + + res := o.Handler.Handle(Params, principal) // actually handle the request + o.Context.Respond(rw, r, route.Produces, route, res) + +} diff --git a/restapi/operations/admin_api/reset_config_parameters.go b/restapi/operations/admin_api/reset_config_parameters.go new file mode 100644 index 000000000..065087898 --- /dev/null +++ b/restapi/operations/admin_api/reset_config_parameters.go @@ -0,0 +1,88 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2021 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 . +// + +package admin_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/errors" + "github.com/go-openapi/runtime/middleware" + "github.com/go-openapi/strfmt" +) + +// NewResetConfigParams creates a new ResetConfigParams object +// +// There are no default values defined in the spec. +func NewResetConfigParams() ResetConfigParams { + + return ResetConfigParams{} +} + +// ResetConfigParams contains all the bound params for the reset config operation +// typically these are obtained from a http.Request +// +// swagger:parameters ResetConfig +type ResetConfigParams struct { + + // HTTP Request Object + HTTPRequest *http.Request `json:"-"` + + /* + Required: true + In: path + */ + Name string +} + +// BindRequest both binds and validates a request, it assumes that complex things implement a Validatable(strfmt.Registry) error interface +// for simple values it will use straight method calls. +// +// To ensure default values, the struct must have been initialized with NewResetConfigParams() beforehand. +func (o *ResetConfigParams) BindRequest(r *http.Request, route *middleware.MatchedRoute) error { + var res []error + + o.HTTPRequest = r + + rName, rhkName, _ := route.Params.GetOK("name") + if err := o.bindName(rName, rhkName, route.Formats); err != nil { + res = append(res, err) + } + if len(res) > 0 { + return errors.CompositeValidationError(res...) + } + return nil +} + +// bindName binds and validates parameter Name from path. +func (o *ResetConfigParams) bindName(rawData []string, hasKey bool, formats strfmt.Registry) error { + var raw string + if len(rawData) > 0 { + raw = rawData[len(rawData)-1] + } + + // Required: true + // Parameter is provided by construction from the route + o.Name = raw + + return nil +} diff --git a/restapi/operations/admin_api/reset_config_responses.go b/restapi/operations/admin_api/reset_config_responses.go new file mode 100644 index 000000000..8548d2fa5 --- /dev/null +++ b/restapi/operations/admin_api/reset_config_responses.go @@ -0,0 +1,133 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2021 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 . +// + +package admin_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the swagger generate command + +import ( + "net/http" + + "github.com/go-openapi/runtime" + + "github.com/minio/console/models" +) + +// ResetConfigOKCode is the HTTP code returned for type ResetConfigOK +const ResetConfigOKCode int = 200 + +/*ResetConfigOK A successful response. + +swagger:response resetConfigOK +*/ +type ResetConfigOK struct { + + /* + In: Body + */ + Payload *models.SetConfigResponse `json:"body,omitempty"` +} + +// NewResetConfigOK creates ResetConfigOK with default headers values +func NewResetConfigOK() *ResetConfigOK { + + return &ResetConfigOK{} +} + +// WithPayload adds the payload to the reset config o k response +func (o *ResetConfigOK) WithPayload(payload *models.SetConfigResponse) *ResetConfigOK { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the reset config o k response +func (o *ResetConfigOK) SetPayload(payload *models.SetConfigResponse) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *ResetConfigOK) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(200) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} + +/*ResetConfigDefault Generic error response. + +swagger:response resetConfigDefault +*/ +type ResetConfigDefault struct { + _statusCode int + + /* + In: Body + */ + Payload *models.Error `json:"body,omitempty"` +} + +// NewResetConfigDefault creates ResetConfigDefault with default headers values +func NewResetConfigDefault(code int) *ResetConfigDefault { + if code <= 0 { + code = 500 + } + + return &ResetConfigDefault{ + _statusCode: code, + } +} + +// WithStatusCode adds the status to the reset config default response +func (o *ResetConfigDefault) WithStatusCode(code int) *ResetConfigDefault { + o._statusCode = code + return o +} + +// SetStatusCode sets the status to the reset config default response +func (o *ResetConfigDefault) SetStatusCode(code int) { + o._statusCode = code +} + +// WithPayload adds the payload to the reset config default response +func (o *ResetConfigDefault) WithPayload(payload *models.Error) *ResetConfigDefault { + o.Payload = payload + return o +} + +// SetPayload sets the payload to the reset config default response +func (o *ResetConfigDefault) SetPayload(payload *models.Error) { + o.Payload = payload +} + +// WriteResponse to the client +func (o *ResetConfigDefault) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.WriteHeader(o._statusCode) + if o.Payload != nil { + payload := o.Payload + if err := producer.Produce(rw, payload); err != nil { + panic(err) // let the recovery middleware deal with this + } + } +} diff --git a/restapi/operations/admin_api/reset_config_urlbuilder.go b/restapi/operations/admin_api/reset_config_urlbuilder.go new file mode 100644 index 000000000..09bc06ecd --- /dev/null +++ b/restapi/operations/admin_api/reset_config_urlbuilder.go @@ -0,0 +1,116 @@ +// Code generated by go-swagger; DO NOT EDIT. + +// This file is part of MinIO Console Server +// Copyright (c) 2021 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 . +// + +package admin_api + +// This file was generated by the swagger tool. +// Editing this file might prove futile when you re-run the generate command + +import ( + "errors" + "net/url" + golangswaggerpaths "path" + "strings" +) + +// ResetConfigURL generates an URL for the reset config operation +type ResetConfigURL struct { + Name string + + _basePath string + // avoid unkeyed usage + _ struct{} +} + +// WithBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *ResetConfigURL) WithBasePath(bp string) *ResetConfigURL { + o.SetBasePath(bp) + return o +} + +// SetBasePath sets the base path for this url builder, only required when it's different from the +// base path specified in the swagger spec. +// When the value of the base path is an empty string +func (o *ResetConfigURL) SetBasePath(bp string) { + o._basePath = bp +} + +// Build a url path and query string +func (o *ResetConfigURL) Build() (*url.URL, error) { + var _result url.URL + + var _path = "/configs/{name}/reset" + + name := o.Name + if name != "" { + _path = strings.Replace(_path, "{name}", name, -1) + } else { + return nil, errors.New("name is required on ResetConfigURL") + } + + _basePath := o._basePath + if _basePath == "" { + _basePath = "/api/v1" + } + _result.Path = golangswaggerpaths.Join(_basePath, _path) + + return &_result, nil +} + +// Must is a helper function to panic when the url builder returns an error +func (o *ResetConfigURL) Must(u *url.URL, err error) *url.URL { + if err != nil { + panic(err) + } + if u == nil { + panic("url can't be nil") + } + return u +} + +// String returns the string representation of the path with query string +func (o *ResetConfigURL) String() string { + return o.Must(o.Build()).String() +} + +// BuildFull builds a full url with scheme, host, path and query string +func (o *ResetConfigURL) BuildFull(scheme, host string) (*url.URL, error) { + if scheme == "" { + return nil, errors.New("scheme is required for a full url on ResetConfigURL") + } + if host == "" { + return nil, errors.New("host is required for a full url on ResetConfigURL") + } + + base, err := o.Build() + if err != nil { + return nil, err + } + + base.Scheme = scheme + base.Host = host + return base, nil +} + +// StringFull returns the string representation of a complete url +func (o *ResetConfigURL) StringFull(scheme, host string) string { + return o.Must(o.BuildFull(scheme, host)).String() +} diff --git a/restapi/operations/console_api.go b/restapi/operations/console_api.go index 540fea8d9..946f56ab5 100644 --- a/restapi/operations/console_api.go +++ b/restapi/operations/console_api.go @@ -311,6 +311,9 @@ func NewConsoleAPI(spec *loads.Document) *ConsoleAPI { AdminAPIRemoveUserHandler: admin_api.RemoveUserHandlerFunc(func(params admin_api.RemoveUserParams, principal *models.Principal) middleware.Responder { return middleware.NotImplemented("operation admin_api.RemoveUser has not yet been implemented") }), + AdminAPIResetConfigHandler: admin_api.ResetConfigHandlerFunc(func(params admin_api.ResetConfigParams, principal *models.Principal) middleware.Responder { + return middleware.NotImplemented("operation admin_api.ResetConfig has not yet been implemented") + }), AdminAPIRestartServiceHandler: admin_api.RestartServiceHandlerFunc(func(params admin_api.RestartServiceParams, principal *models.Principal) middleware.Responder { return middleware.NotImplemented("operation admin_api.RestartService has not yet been implemented") }), @@ -581,6 +584,8 @@ type ConsoleAPI struct { AdminAPIRemovePolicyHandler admin_api.RemovePolicyHandler // AdminAPIRemoveUserHandler sets the operation handler for the remove user operation AdminAPIRemoveUserHandler admin_api.RemoveUserHandler + // AdminAPIResetConfigHandler sets the operation handler for the reset config operation + AdminAPIResetConfigHandler admin_api.ResetConfigHandler // AdminAPIRestartServiceHandler sets the operation handler for the restart service operation AdminAPIRestartServiceHandler admin_api.RestartServiceHandler // UserAPISessionCheckHandler sets the operation handler for the session check operation @@ -948,6 +953,9 @@ func (o *ConsoleAPI) Validate() error { if o.AdminAPIRemoveUserHandler == nil { unregistered = append(unregistered, "admin_api.RemoveUserHandler") } + if o.AdminAPIResetConfigHandler == nil { + unregistered = append(unregistered, "admin_api.ResetConfigHandler") + } if o.AdminAPIRestartServiceHandler == nil { unregistered = append(unregistered, "admin_api.RestartServiceHandler") } @@ -1429,6 +1437,10 @@ func (o *ConsoleAPI) initHandlerCache() { o.handlers["DELETE"] = make(map[string]http.Handler) } o.handlers["DELETE"]["/user"] = admin_api.NewRemoveUser(o.context, o.AdminAPIRemoveUserHandler) + if o.handlers["GET"] == nil { + o.handlers["GET"] = make(map[string]http.Handler) + } + o.handlers["GET"]["/configs/{name}/reset"] = admin_api.NewResetConfig(o.context, o.AdminAPIResetConfigHandler) if o.handlers["POST"] == nil { o.handlers["POST"] = make(map[string]http.Handler) } diff --git a/swagger-console.yml b/swagger-console.yml index 2a4f89b2a..15eac23bb 100644 --- a/swagger-console.yml +++ b/swagger-console.yml @@ -1932,6 +1932,27 @@ paths: tags: - AdminAPI + /configs/{name}/reset: + get: + summary: Configuration reset + operationId: ResetConfig + parameters: + - name: name + in: path + required: true + type: string + responses: + 200: + description: A successful response. + schema: + $ref: "#/definitions/setConfigResponse" + default: + description: Generic error response. + schema: + $ref: "#/definitions/error" + tags: + - AdminAPI + /service/restart: post: summary: Restart Service @@ -2643,7 +2664,7 @@ definitions: name: type: array items: - type: string + type: string entityType: $ref: "#/definitions/policyEntity" entityName: @@ -3893,7 +3914,6 @@ definitions: items: $ref: "#/definitions/iamPolicyStatement" - iamPolicyStatement: type: object properties: