mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
10.40.2019 4aa0285d00558ccec3e5a5f6978dfda820d866d6
Test button of BorgRepoConfig...
2 files modified
79 ■■■■■ changed files
borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/BorgRepoConfigsRest.java 18 ●●●●● patch | view | raw | blame | history
borgbutler-webapp/src/components/views/repos/RepoConfigPanel.jsx 61 ●●●●● patch | view | raw | blame | history
borgbutler-server/src/main/java/de/micromata/borgbutler/server/rest/BorgRepoConfigsRest.java
@@ -1,8 +1,12 @@
package de.micromata.borgbutler.server.rest;
import de.micromata.borgbutler.BorgCommandResult;
import de.micromata.borgbutler.BorgCommands;
import de.micromata.borgbutler.cache.ButlerCache;
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.data.Repository;
import de.micromata.borgbutler.jobs.JobResult;
import de.micromata.borgbutler.json.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -41,4 +45,18 @@
        repoConfig.copyFrom(newRepoConfig);
        ConfigurationHandler.getInstance().save();
    }
    /**
     * @param jsonRepoConfig All configuration value of the repo to check.
     * @return Result of borg (tbd.).
     */
    @POST
    @Path("check")
    @Produces(MediaType.APPLICATION_JSON)
    public String checkConfig(String jsonRepoConfig) {
        log.info("Testing repo config: " + jsonRepoConfig);
        BorgRepoConfig repoConfig = JsonUtils.fromJson(BorgRepoConfig.class, jsonRepoConfig);
        BorgCommandResult<Repository> result = BorgCommands.info(repoConfig);
        return result.getStatus() == JobResult.Status.OK ? "OK" : result.getError();
    }
}
borgbutler-webapp/src/components/views/repos/RepoConfigPanel.jsx
@@ -1,5 +1,5 @@
import React from 'react';
import {FormGroup} from 'reactstrap';
import {Alert, FormGroup} from 'reactstrap';
import {FormButton, FormField} from '../../general/forms/FormComponents';
import {getRestServiceUrl} from '../../../utilities/global';
import I18n from "../../general/translation/I18n";
@@ -15,13 +15,16 @@
        super(props);
        this.state = {
            loading: false,
            repoConfig: undefined
            repoConfig: undefined,
            testStatus: undefined,
            testResult: undefined
        };
        this.fetch = this.fetch.bind(this);
        this.setRepoValue = this.setRepoValue.bind(this);
        this.onSave = this.onSave.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onTest = this.onTest.bind(this);
    }
    componentDidMount = () => this.fetch();
@@ -80,6 +83,34 @@
        }
    }
    onTest(event) {
        this.setState({
            testStatus: 'fetching',
            testResult: undefined
        });
        fetch(getRestServiceUrl("repoConfig/check"), {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(this.state.repoConfig)
        })
            .then(response => response.text())
            .then(result => {
                //console.log("test-result: " + result);
                this.setState({
                    testStatus: result === 'OK' ? 'OK' : 'error',
                    testResult: result
                })
            })
            .catch((error) => {
                console.log("error", error);
                this.setState({
                    testStatus: 'exception'
                });
            });
    }
    async onCancel() {
        const response = this.fetch();
        if (response) await response;
@@ -93,10 +124,29 @@
        let repoError = '';
        if (this.props.repoError) {
            repoError = <ErrorAlert
                title={'Cannot access repository'}
                title={'Internal error'}
                description={'Repo not available or mis-configured (please refer the log files for more details).'}
            />
        }
        let testResult = '';
        if (!this.state.testStatus) {
            // No test available.
        } else if (this.state.testStatus === 'exception') {
            testResult = <ErrorAlert title={'Unknown error'} description={'Internal error while calling Rest API.'}/>;
        } else if (this.state.testStatus === 'OK') {
            testResult = <Alert color={'success'}>
                OK
            </Alert>;
        } else if (this.state.testStatus === 'fetching') {
            testResult = <Alert color={'warning'}>
                Testing...
            </Alert>;
        } else {
            testResult = <ErrorAlert
                title={'Error while testing repo configuration'}
                description={this.state.testResult}
            />
        }
        if (this.state.isFetching) {
            content = <React.Fragment>Loading...</React.Fragment>;
        } else if (this.state.failed) {
@@ -126,6 +176,9 @@
                        <FormButton onClick={this.onCancel}
                                    hintKey="configuration.cancel.hint"><I18n name={'common.cancel'}/>
                        </FormButton>
                        <FormButton onClick={this.onTest} disabled={this.state.testResult === 'fetching'} bsStyle="info"
                                    hint={'Tries to connect to the repo and to get info from.'}>Test
                        </FormButton>
                        <FormButton onClick={this.onSave} bsStyle="primary"
                                    hintKey="configuration.save.hint"><I18n name={'common.save'}/>
                        </FormButton>
@@ -134,7 +187,7 @@
                <LoadingOverlay active={this.state.loading}/>
            </React.Fragment>;
        }
        return <React.Fragment>{content}{repoError}</React.Fragment>;
        return <React.Fragment>{content}{testResult}{repoError}</React.Fragment>;
    }
}