-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(xo-web/settings/config): cloud backup (#6917)
- Loading branch information
Showing
18 changed files
with
256 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/xo-web/src/xo-app/settings/config/xo-cloud-config/backup-xo-config-modal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import _ from 'intl' | ||
import BaseComponent from 'base-component' | ||
import React from 'react' | ||
import { Password } from 'form' | ||
|
||
class BackupXoConfigModal extends BaseComponent { | ||
get value() { | ||
return { | ||
passphrase: this.state.passphrase, | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<label>{_('xoCloudConfigEnterPassphrase')}</label> | ||
<Password autoFocus onChange={this.linkState('passphrase')} value={this.state.passphrase} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default BackupXoConfigModal |
149 changes: 149 additions & 0 deletions
149
packages/xo-web/src/xo-app/settings/config/xo-cloud-config/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import _ from 'intl' | ||
import ActionButton from 'action-button' | ||
import addSubscriptions from 'add-subscriptions' | ||
import decorate from 'apply-decorators' | ||
import Icon from 'icon' | ||
import React from 'react' | ||
import { confirm } from 'modal' | ||
import { getApiApplianceInfo, subscribeCloudXoConfig, subscribeCloudXoConfigBackups } from 'xo' | ||
import { groupBy, sortBy } from 'lodash' | ||
import { injectState, provideState } from 'reaclette' | ||
import { SelectXoCloudConfig } from 'select-objects' | ||
|
||
import BackupXoConfigModal from './backup-xo-config-modal' | ||
import RestoreXoConfigModal from './restore-xo-config-modal' | ||
|
||
const CloudConfig = decorate([ | ||
addSubscriptions({ | ||
cloudXoConfig: subscribeCloudXoConfig, | ||
cloudXoConfigBackups: subscribeCloudXoConfigBackups, | ||
}), | ||
provideState({ | ||
initialState: () => ({ config: undefined }), | ||
effects: { | ||
downloadCloudXoConfig: | ||
() => | ||
({ config, isConfigDefined }) => { | ||
if (isConfigDefined) { | ||
window.open(config.content_href, '_blank') | ||
} | ||
}, | ||
restoreCloudXoConfig: | ||
() => | ||
async ({ config, isConfigDefined }) => { | ||
if (isConfigDefined) { | ||
const { passphrase } = await confirm({ | ||
icon: 'backup', | ||
title: _('xoConfigCloudBackup'), | ||
body: <RestoreXoConfigModal />, | ||
}) | ||
|
||
const resp = await fetch(`./rest/v0/cloud/xo-config/backups/${config.id}/actions/import?sync`, { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
passphrase, | ||
}), | ||
}) | ||
if (!resp.ok) { | ||
throw new Error(resp.statusText) | ||
} | ||
return { | ||
config: undefined, | ||
} | ||
} | ||
}, | ||
onChangeCloudXoConfig: (_, config) => ({ | ||
config, | ||
}), | ||
toggleEnableCloudXoConfig: | ||
() => | ||
async (state, { cloudXoConfig }) => { | ||
let passphrase | ||
if (!cloudXoConfig?.enabled) { | ||
const params = await confirm({ | ||
icon: 'backup', | ||
title: _('xoConfigCloudBackup'), | ||
body: <BackupXoConfigModal />, | ||
}) | ||
passphrase = params.passphrase | ||
} | ||
|
||
const resp = await fetch('./rest/v0/cloud/xo-config', { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
enabled: !cloudXoConfig?.enabled, | ||
passphrase, | ||
}), | ||
}) | ||
if (!resp.ok) { | ||
throw new Error(resp.statusText) | ||
} | ||
subscribeCloudXoConfig.forceRefresh() | ||
}, | ||
}, | ||
computed: { | ||
applianceId: async () => { | ||
const { id } = await getApiApplianceInfo() | ||
return id | ||
}, | ||
groupedConfigs: ({ applianceId, sortedConfigs }) => | ||
sortBy(groupBy(sortedConfigs, 'xoaId'), config => (config[0].xoaId === applianceId ? -1 : 1)), | ||
isConfigDefined: ({ config }) => config != null, | ||
sortedConfigs: (_, { cloudXoConfigBackups }) => | ||
cloudXoConfigBackups?.sort((config, nextConfig) => config.createdAt - nextConfig.createdAt), | ||
}, | ||
}), | ||
injectState, | ||
({ effects, state, cloudXoConfig }) => ( | ||
<div> | ||
<div className='mb-1'> | ||
<h2> | ||
<Icon icon='backup' /> {_('manageXoConfigCloudBackup')} | ||
</h2> | ||
<em> | ||
<Icon icon='info' /> {_('xoConfigCloudBackupTips')} | ||
</em> | ||
<br /> | ||
<ActionButton | ||
btnStyle={cloudXoConfig?.enabled ? 'warning' : 'primary'} | ||
handler={effects.toggleEnableCloudXoConfig} | ||
icon='backup' | ||
> | ||
{cloudXoConfig?.enabled ? _('disable') : _('enable')} | ||
</ActionButton> | ||
</div> | ||
<div> | ||
<h2> | ||
<Icon icon='xo-cloud-config' /> {_('backedUpXoConfigs')} | ||
</h2> | ||
<SelectXoCloudConfig onChange={effects.onChangeCloudXoConfig} value={state.config} /> | ||
<div className='mt-1'> | ||
<ActionButton | ||
handler={effects.restoreCloudXoConfig} | ||
btnStyle='warning' | ||
icon='upload' | ||
disabled={!state.isConfigDefined} | ||
> | ||
{_('restore')} | ||
</ActionButton>{' '} | ||
<ActionButton | ||
btnStyle='primary' | ||
icon='download' | ||
handler={effects.downloadCloudXoConfig} | ||
disabled={!state.isConfigDefined} | ||
> | ||
{_('download')} | ||
</ActionButton> | ||
</div> | ||
</div> | ||
</div> | ||
), | ||
]) | ||
|
||
export default CloudConfig |
Oops, something went wrong.