Skip to content

Commit 5d8d7c5

Browse files
feat: enable exporting of variables using pkger (#3960)
1 parent 4fc1933 commit 5d8d7c5

File tree

8 files changed

+60
-25
lines changed

8 files changed

+60
-25
lines changed

src/dashboards/actions/thunks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {get} from 'lodash'
77
// APIs
88
import * as dashAPI from 'src/dashboards/apis'
99
import * as api from 'src/client'
10-
import {createDashboardFromPkgerTemplate} from 'src/templates/api'
10+
import {createResourceFromPkgerTemplate} from 'src/templates/api'
1111
import {createCellWithView} from 'src/cells/actions/thunks'
1212

1313
// Schemas
@@ -296,7 +296,7 @@ export const createDashboardFromTemplate = (template: api.Template) => async (
296296
try {
297297
const org = getOrg(getState())
298298

299-
await createDashboardFromPkgerTemplate(template, org.id)
299+
await createResourceFromPkgerTemplate(template, org.id)
300300

301301
const resp = await api.getDashboards({query: {orgID: org.id}})
302302

src/overlays/components/OverlayController.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import CustomApiTokenOverlay from 'src/authorizations/components/CustomApiTokenO
3737
import DisplayTokenOverlay from 'src/authorizations/components/DisplayTokenOverlay'
3838
import NewRuleOverlay from 'src/notifications/rules/components/NewRuleOverlay'
3939
import CreateSecretOverlay from 'src/secrets/components/CreateSecret/CreateSecretOverlay'
40+
import VariableImportOverlay from 'src/variables/components/VariableImportOverlay'
4041

4142
// Actions
4243
import {dismissOverlay} from 'src/overlays/actions/overlays'
@@ -121,6 +122,9 @@ export const OverlayController: FunctionComponent = () => {
121122
case 'create-variable':
122123
activeOverlay.current = <CreateVariableOverlay />
123124
break
125+
case 'import-variable':
126+
activeOverlay.current = <VariableImportOverlay />
127+
break
124128
case 'rename-variable':
125129
activeOverlay.current = <RenameVariableOverlay />
126130
break

src/templates/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const createDashboardFromTemplate = async (
109109
}
110110
}
111111

112-
export const createDashboardFromPkgerTemplate = async (
112+
export const createResourceFromPkgerTemplate = async (
113113
template: Template,
114114
orgID: string
115115
): Promise<void> => {

src/variables/actions/thunks.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {variableSchema, arrayOfVariables} from 'src/schemas/variables'
1919
// APIs
2020
import * as api from 'src/client'
2121
import {hydrateVars} from 'src/variables/utils/hydrateVars'
22-
import {createVariableFromTemplate as createVariableFromTemplateAJAX} from 'src/templates/api'
22+
import {createResourceFromPkgerTemplate} from 'src/templates/api'
2323

2424
// Utils
2525
import {
@@ -44,7 +44,6 @@ import {
4444
AppState,
4545
GetState,
4646
RemoteDataState,
47-
VariableTemplate,
4847
Label,
4948
GenVariable,
5049
Variable,
@@ -63,6 +62,7 @@ import {
6362
} from 'src/shared/utils/filterUnusedVars'
6463
import {getActiveTimeMachine} from 'src/timeMachine/selectors'
6564
import {getActiveQuery} from 'src/timeMachine/selectors'
65+
import {getVariables as getVariablesAction} from 'src/variables/actions/thunks'
6666

6767
type Action = VariableAction | EditorAction | NotifyAction
6868

@@ -267,26 +267,28 @@ export const createVariable = (
267267
}
268268
}
269269

270-
export const createVariableFromTemplate = (
271-
template: VariableTemplate
272-
) => async (dispatch: Dispatch<Action>, getState: GetState) => {
270+
export const createVariableFromTemplate = (template: api.Template) => async (
271+
dispatch: Dispatch<Action>,
272+
getState: GetState
273+
) => {
273274
try {
274275
const state = getState()
275276
const org = getOrg(state)
276-
const resp = await createVariableFromTemplateAJAX(template, org.id)
277+
// only one variable in the exported template so we can safely just index the 0th element
278+
const variableName = template[0].spec.name
279+
280+
await createResourceFromPkgerTemplate(template, org.id)
281+
282+
await getVariablesAction()(dispatch, getState)
277283

278-
const createdVar = normalize<Variable, VariableEntities, string>(
279-
resp,
280-
variableSchema
281-
)
282284
event('variable.create.from_template.success', {
283-
id: resp?.id,
284-
name: resp?.name,
285+
name: variableName,
285286
})
286-
dispatch(setVariable(resp.id, RemoteDataState.Done, createdVar))
287-
dispatch(notify(copy.createVariableSuccess(resp.name)))
287+
dispatch(notify(copy.createVariableSuccess(variableName)))
288288
} catch (error) {
289-
event('variable.create.from_template.failure', {template: template?.id})
289+
event('variable.create.from_template.failure', {
290+
template: template[0].meta.name,
291+
})
290292
console.error(error)
291293
dispatch(notify(copy.createVariableFailed(error.message)))
292294
}

src/variables/apis/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Utils
2+
import {downloadTextFile} from 'src/shared/utils/download'
3+
4+
import {postTemplatesExport} from 'src/client'
5+
6+
export const downloadVariableTemplate = async (variable): Promise<void> => {
7+
const resp = await postTemplatesExport({
8+
data: {
9+
resources: [
10+
{
11+
kind: 'Variable',
12+
id: variable.id,
13+
},
14+
],
15+
},
16+
})
17+
18+
if (resp.status === 500) {
19+
throw new Error(resp.data.message)
20+
}
21+
22+
const data = await resp.data
23+
downloadTextFile(JSON.stringify(data), variable.name, '.json', 'text/json')
24+
}

src/variables/components/VariableCard.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
removeVariableLabelAsync,
2222
} from 'src/variables/actions/thunks'
2323
import ErrorBoundary from 'src/shared/components/ErrorBoundary'
24+
import {downloadVariableTemplate} from '../apis'
2425

2526
interface OwnProps {
2627
variable: Variable
@@ -104,11 +105,8 @@ class VariableCard extends PureComponent<
104105
}
105106

106107
private handleExport = () => {
107-
const {history, variable, match} = this.props
108-
109-
history.push(
110-
`/orgs/${match.params.orgID}/settings/variables/${variable.id}/export`
111-
)
108+
const {variable} = this.props
109+
downloadVariableTemplate(variable)
112110
}
113111

114112
private handleRenameVariable = () => {

src/variables/components/VariableImportOverlay.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class VariableImportOverlay extends PureComponent<Props> {
5555
this.setState(() => ({status}))
5656

5757
private handleImportVariable = (uploadContent: string) => {
58-
const {createVariableFromTemplate, getVariables, notify} = this.props
58+
const {createVariableFromTemplate, notify} = this.props
5959

6060
let template
6161
this.updateOverlayStatus(ComponentStatus.Default)
@@ -68,7 +68,6 @@ class VariableImportOverlay extends PureComponent<Props> {
6868
}
6969

7070
createVariableFromTemplate(template)
71-
getVariables()
7271

7372
this.onDismiss()
7473
}

src/variables/components/VariablesTab.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class VariablesTab extends PureComponent<Props, State> {
7171
<AddResourceDropdown
7272
resourceName="Variable"
7373
onSelectNew={this.handleOpenCreateOverlay}
74+
onSelectImport={this.handleOpenImportOverlay}
7475
/>
7576
)
7677

@@ -123,6 +124,7 @@ class VariablesTab extends PureComponent<Props, State> {
123124
<AddResourceDropdown
124125
resourceName="Variable"
125126
onSelectNew={this.handleOpenCreateOverlay}
127+
onSelectImport={this.handleOpenImportOverlay}
126128
/>
127129
</EmptyState>
128130
)
@@ -153,6 +155,12 @@ class VariablesTab extends PureComponent<Props, State> {
153155
const {onDeleteVariable} = this.props
154156
onDeleteVariable(variable.id)
155157
}
158+
159+
private handleOpenImportOverlay = () => {
160+
const {history, match} = this.props
161+
162+
history.push(`/orgs/${match.params.orgID}/settings/variables/import`)
163+
}
156164
}
157165

158166
const mstp = (state: AppState) => {

0 commit comments

Comments
 (0)