Skip to content

Commit e4872b5

Browse files
authored
fix: update telegraf cloning to include labels (#3943)
1 parent bcaaa44 commit e4872b5

File tree

4 files changed

+125
-8
lines changed

4 files changed

+125
-8
lines changed

cypress/e2e/shared/telegrafs.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,39 @@ describe('Collectors', () => {
201201
})
202202

203203
it('can clone a config', () => {
204+
const firstLabel = 'test_label_1'
205+
const secondLabel = 'test_label_2'
206+
204207
cy.getByTestID('resource-card').should('have.length', 1)
205-
cy.getByTestID('context-menu-telegraf').click({force: true})
208+
209+
// create two labels
210+
cy.get('button.cf-button[title="Add labels"]').click()
211+
cy.getByTestID('inline-labels--popover--dialog').should('be.visible')
212+
cy.getByTestID('inline-labels--popover-field').type(
213+
`${firstLabel}{enter}`
214+
)
215+
cy.getByTestID('overlay--container').should('be.visible')
216+
cy.getByTestID('create-label-form--submit').click()
217+
218+
cy.getByTestID('overlay--container').should('not.exist')
219+
cy.get('button.cf-button[title="Add labels"]').click()
220+
cy.getByTestID('inline-labels--popover--dialog').should('be.visible')
221+
cy.getByTestID('inline-labels--popover-field').type(
222+
`${secondLabel}{enter}`
223+
)
224+
cy.getByTestID('overlay--container').should('be.visible')
225+
cy.getByTestID('create-label-form--submit').click()
226+
227+
// ensure the two labels are present before cloning
228+
cy.getByTestID('overlay--container').should('not.exist')
229+
cy.getByTestID(`label--pill ${firstLabel}`).should('be.visible')
230+
cy.getByTestID(`label--pill ${secondLabel}`).should('be.visible')
231+
232+
// clone the telegraf
233+
cy.getByTestID('context-menu-telegraf').click()
206234
cy.getByTestID('context-clone-telegraf').click()
235+
cy.getByTestID(`label--pill ${firstLabel}`).should('have.length', 2)
236+
cy.getByTestID(`label--pill ${secondLabel}`).should('have.length', 2)
207237
cy.getByTestID('resource-card').should('have.length', 2)
208238
cy.getByTestID('collector-card--name').then(el => {
209239
expect(el[1].innerText).to.equal('New Config (clone 1)')

src/shared/copy/notifications.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,11 @@ export const telegrafUpdateFailed = (telegrafName: string): Notification => ({
984984
message: `Failed to update telegraf: "${telegrafName}"`,
985985
})
986986

987+
export const cloneTelegrafSuccess = (): Notification => ({
988+
...defaultSuccessNotification,
989+
message: `Telegraf configuration was cloned successfully`,
990+
})
991+
987992
export const addTelegrafLabelFailed = (): Notification => ({
988993
...defaultErrorNotification,
989994
message: `Failed to add label to telegraf config`,

src/telegrafs/actions/thunks.ts

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import {
1717

1818
// Constants
1919
import {
20-
telegrafGetFailed,
21-
telegrafCreateFailed,
22-
telegrafUpdateFailed,
23-
telegrafDeleteFailed,
2420
addTelegrafLabelFailed,
21+
cloneTelegrafSuccess,
22+
getTelegrafConfigFailed,
2523
removeTelegrafLabelFailed,
24+
telegrafCreateFailed,
25+
telegrafDeleteFailed,
26+
telegrafGetFailed,
27+
telegrafUpdateFailed,
2628
} from 'src/shared/copy/notifications'
2729

2830
// Utils
@@ -108,6 +110,60 @@ export const createTelegraf = (telegraf: Telegraf) => async (
108110
}
109111
}
110112

113+
const cloneTelegrafLabels = (
114+
sourceTelegraf: Telegraf,
115+
destinationTelegraf: Telegraf
116+
) => async (dispatch: Dispatch<Action>) => {
117+
try {
118+
const pendingLabels = sourceTelegraf.labels.map(labelID =>
119+
postTelegrafsLabel({
120+
telegrafID: destinationTelegraf.id,
121+
data: {labelID},
122+
})
123+
)
124+
125+
const mappedLabels = await Promise.all(pendingLabels)
126+
127+
if (
128+
mappedLabels.length &&
129+
mappedLabels.some(label => label.status !== 201)
130+
) {
131+
throw new Error(
132+
'An error occurred cloning the labels for this telegraf config'
133+
)
134+
}
135+
dispatch(notify(cloneTelegrafSuccess()))
136+
} catch {
137+
dispatch(notify(addTelegrafLabelFailed()))
138+
}
139+
}
140+
141+
export const cloneTelegraf = (telegraf: Telegraf) => async (
142+
dispatch: Dispatch<Action>
143+
) => {
144+
let clonedTelegraf
145+
146+
// Step 1: create a new telegraf
147+
try {
148+
const response = await postTelegraf({data: telegraf})
149+
150+
if (response.status !== 201) {
151+
throw new Error(response.data.message)
152+
}
153+
154+
clonedTelegraf = response.data
155+
} catch (error) {
156+
console.error(error)
157+
dispatch(notify(telegrafCreateFailed()))
158+
}
159+
160+
// Step 2: clone the labels
161+
cloneTelegrafLabels(telegraf, clonedTelegraf)(dispatch)
162+
163+
// Step 3: refresh the cloned telegraf in the UI to show the labels
164+
refreshTelegraf(clonedTelegraf)(dispatch)
165+
}
166+
111167
export const updateTelegraf = (telegraf: Telegraf) => async (
112168
dispatch: Dispatch<Action>
113169
) => {
@@ -249,3 +305,29 @@ export const getTelegraf = (telegrafConfigID: string) => async () => {
249305
throw error
250306
}
251307
}
308+
309+
// adds a telegraf with its latest properties to the state's resources
310+
export const refreshTelegraf = (telegraf: Telegraf) => async (
311+
dispatch: Dispatch<Action>
312+
) => {
313+
try {
314+
const response = await apiGetTelegraf({
315+
telegrafID: telegraf.id,
316+
headers: {Accept: 'application/json'},
317+
})
318+
319+
if (response.status !== 200) {
320+
throw new Error(response.data.message)
321+
}
322+
323+
const refreshedTelegraf = response.data
324+
const normTelegraf = normalize<Telegraf, TelegrafEntities, string>(
325+
refreshedTelegraf,
326+
telegrafSchema
327+
)
328+
dispatch(addTelegraf(normTelegraf))
329+
} catch (error) {
330+
console.error(error)
331+
dispatch(notify(getTelegrafConfigFailed()))
332+
}
333+
}

src/telegrafs/components/CollectorCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
removeTelegrafLabelAsync,
2626
} from 'src/telegrafs/actions/thunks'
2727

28-
import {createTelegraf} from 'src/telegrafs/actions/thunks'
28+
import {cloneTelegraf} from 'src/telegrafs/actions/thunks'
2929
// Selectors
3030
import {getOrg} from 'src/organizations/selectors'
3131

@@ -187,7 +187,7 @@ class CollectorRow extends PureComponent<
187187
const allTelegrafNames = Object.values(this.props.telegrafs).map(
188188
t => t.name
189189
)
190-
this.props.onCloneTelegraf({
190+
this.props.cloneTelegraf({
191191
...this.props.collector,
192192
name: incrementCloneName(allTelegrafNames, this.props.collector.name),
193193
})
@@ -204,7 +204,7 @@ const mstp = (state: AppState) => {
204204
}
205205

206206
const mdtp = {
207-
onCloneTelegraf: createTelegraf,
207+
cloneTelegraf,
208208
onAddLabel: addTelegrafLabelAsync,
209209
onRemoveLabel: removeTelegrafLabelAsync,
210210
}

0 commit comments

Comments
 (0)