chore: move template installing functions to utils

pull/18833/head
Bucky Schwarz 2020-07-02 11:48:07 -07:00 committed by Bucky Schwarz
parent 897094f0ca
commit 42fd2a99cb
2 changed files with 45 additions and 21 deletions

View File

@ -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<Props> {
)
this.setState({currentTemplate}, () => {
this.applyTemplates(
this.reviewTemplateResources(
this.props.org.id,
getTemplateNameFromGithubUrl(currentTemplate)
)
@ -148,24 +143,13 @@ class UnconnectedTemplatesIndex extends Component<Props> {
)
}
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<Props> {
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 => {

View File

@ -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)
}