forked from ManageIQ/manageiq-ui-classic
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a DDF component for provider credentials validation
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
app/javascript/components/async-credentials/async-provider-credentials.jsx
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,28 @@ | ||
import React from 'react'; | ||
import AsyncCredentials from './async-credentials'; | ||
|
||
const AsyncProviderCredentials = ({ ...props }) => { | ||
const asyncValidate = fields => new Promise((resolve, reject) => { | ||
API.post('/api/providers', { | ||
action: 'verify_credentials', | ||
resource: fields, | ||
}).then(({ results: [result] }) => { | ||
const { task_id, success } = result; // eslint-disable-line camelcase | ||
// The request here can either create a background task or fail | ||
return success ? API.wait_for_task(task_id) : Promise.reject(result); | ||
// The wait_for_task request can succeed with valid or invalid credentials | ||
// with the message that the task is completed successfully. Based on the | ||
// task_results we resolve() or reject() with an unknown error. | ||
// Any known errors are passed to the catch(), which will reject() with a | ||
// message describing what went wrong. | ||
}).then(result => (result.task_results ? resolve() : reject(__('Validation failed: unknown error')))) | ||
.catch(({ message }) => reject([__('Validation failed:'), message].join(' '))); | ||
}); | ||
|
||
return <AsyncCredentials asyncValidate={asyncValidate} {...props} />; | ||
}; | ||
|
||
AsyncProviderCredentials.propTypes = AsyncCredentials.propTypes; | ||
AsyncProviderCredentials.defaultProps = AsyncCredentials.defaultProps; | ||
|
||
export default AsyncProviderCredentials; |
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