diff --git a/ui/src/templates/containers/CommunityTemplatesIndex.tsx b/ui/src/templates/containers/CommunityTemplatesIndex.tsx index 4a46469a74..d172a2f456 100644 --- a/ui/src/templates/containers/CommunityTemplatesIndex.tsx +++ b/ui/src/templates/containers/CommunityTemplatesIndex.tsx @@ -28,14 +28,9 @@ import {pageTitleSuffixer} from 'src/shared/utils/pageTitles' import { getGithubUrlFromTemplateName, getTemplateNameFromGithubUrl, + reviewTemplate, } from 'src/templates/utils' -import { - Error as PkgError, - TemplateSummary, - postTemplatesApply, -} from 'src/client' - // Types import {AppState, Organization} from 'src/types' @@ -77,7 +72,7 @@ class UnconnectedTemplatesIndex extends Component { ) this.setState({currentTemplate}, () => { - this.applyTemplates( + this.reviewTemplateResources( this.props.org.id, getTemplateNameFromGithubUrl(currentTemplate) ) @@ -148,24 +143,13 @@ class UnconnectedTemplatesIndex extends Component { ) } - private applyTemplates = async (orgID, templateName) => { + private reviewTemplateResources = async (orgID, templateName) => { const yamlLocation = getRawYamlFromGithub(this.state.currentTemplate) + `/${templateName}.yml` - const params = { - data: { - dryRun: true, - orgID, - remotes: [{url: yamlLocation}], - }, - } try { - const resp = await postTemplatesApply(params) - if (resp.status >= 300) { - throw new Error((resp.data as PkgError).message) - } + const summary = await reviewTemplate(orgID, yamlLocation) - const summary = (resp.data as TemplateSummary).summary this.props.setActiveCommunityTemplate(summary) return summary } catch (err) { @@ -181,7 +165,7 @@ class UnconnectedTemplatesIndex extends Component { const name = getTemplateNameFromGithubUrl(this.state.currentTemplate) this.showInstallerOverlay(name) - this.applyTemplates(this.props.org.id, name) + this.reviewTemplateResources(this.props.org.id, name) } private showInstallerOverlay = templateName => { diff --git a/ui/src/templates/utils/index.ts b/ui/src/templates/utils/index.ts index ab9a4cf276..95d09d732d 100644 --- a/ui/src/templates/utils/index.ts +++ b/ui/src/templates/utils/index.ts @@ -8,6 +8,12 @@ import { Variable, } from 'src/types' +import { + Error as PkgError, + postTemplatesApply, + TemplateSummary, +} from 'src/client' + export function findIncludedsFromRelationships< T extends {id: string; type: TemplateType} >( @@ -90,3 +96,37 @@ export const getTemplateNameFromGithubUrl = (url: string): string => { export const getGithubUrlFromTemplateName = (templateName: string): string => { return `https://github.com/influxdata/community-templates/tree/master/${templateName}` } + +const applyTemplates = async (params) => { + const resp = await postTemplatesApply(params) + if (resp.status >= 300) { + throw new Error((resp.data as PkgError).message) + } + + const summary = (resp.data as TemplateSummary).summary + return summary +} + +export const reviewTemplate = async (orgID: string, templateUrl: string) => { + const params = { + data: { + dryRun: true, + orgID, + remotes: [{url: templateUrl}], + }, + } + + return applyTemplates(params) +} + +export const installTemplate = async (orgID: string, templateUrl: string) => { + const params = { + data: { + dryRun: false, + orgID, + remotes: [{url: templateUrl}], + }, + } + + return applyTemplates(params) +}