Fix endpoint type conversions (#15240)

* Fix endpoint type conversions

* Update reducer

* Add token to slack endpoint

* Add test for http endpoint
pull/15244/head
Deniz Kusefoglu 2019-09-23 18:17:56 -07:00 committed by GitHub
parent 9f0d9f6ecb
commit b3a806df6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 13 deletions

View File

@ -76,6 +76,25 @@ describe('Notification Endpoints', () => {
cy.contains('Pagerduty')
})
cy.getByTestID('endpoint--dropdown-item http').click()
cy.getByTestID('endpoint--dropdown--button').within(() => {
cy.contains('HTTP')
})
})
cy.getByTestID('http-url')
.clear()
.type('http.url.us')
.should('have.value', 'http.url.us')
cy.getByTestID('endpoint-change--dropdown')
.click()
.within(() => {
cy.getByTestID('endpoint--dropdown--button').within(() => {
cy.contains('HTTP')
})
cy.getByTestID('endpoint--dropdown-item slack').click()
cy.getByTestID('endpoint--dropdown--button').within(() => {
@ -88,6 +107,11 @@ describe('Notification Endpoints', () => {
.type('slack.url.us')
.should('have.value', 'slack.url.us')
cy.getByTestID('slack-token')
.clear()
.type('tokenzzzzzz')
.should('have.value', 'tokenzzzzzz')
cy.getByTestID('endpoint-save--button').click()
cy.getByTestID(`endpoint-card ${name}`).should('exist')

View File

@ -69,6 +69,7 @@ const EndpointOptionsHTTP: FC<Props> = ({
value={url}
onChange={onChange}
required={true}
testID="http-url"
/>
</FormElement>
</Grid.Column>
@ -81,6 +82,7 @@ const EndpointOptionsHTTP: FC<Props> = ({
value={token}
onChange={onChange}
type={InputType.Password}
testID="http-bearer-token"
/>
</FormElement>
</Grid.Column>
@ -98,6 +100,7 @@ const EndpointOptionsHTTP: FC<Props> = ({
? InputType.Password
: InputType.Text
}
testID="http-username"
/>
</FormElement>
</Grid.Column>
@ -108,6 +111,7 @@ const EndpointOptionsHTTP: FC<Props> = ({
value={password}
type={InputType.Password}
onChange={onChange}
testID="http-password"
/>
</FormElement>
</Grid.Column>

View File

@ -2,7 +2,14 @@
import React, {FC, ChangeEvent} from 'react'
// Components
import {Input, FormElement, Panel, Grid, Columns} from '@influxdata/clockface'
import {
Input,
FormElement,
Panel,
Grid,
Columns,
InputType,
} from '@influxdata/clockface'
interface Props {
url: string
@ -10,7 +17,7 @@ interface Props {
onChange: (e: ChangeEvent<HTMLInputElement>) => void
}
const EndpointOptionsSlack: FC<Props> = ({url, onChange}) => {
const EndpointOptionsSlack: FC<Props> = ({url, token, onChange}) => {
return (
<Panel>
<Panel.Header>
@ -20,7 +27,7 @@ const EndpointOptionsSlack: FC<Props> = ({url, onChange}) => {
<Grid>
<Grid.Row>
<Grid.Column widthXS={Columns.Twelve}>
<FormElement label="Slack Incoming Webhook URL">
<FormElement label="Incoming Webhook URL">
<Input
name="url"
value={url}
@ -28,6 +35,15 @@ const EndpointOptionsSlack: FC<Props> = ({url, onChange}) => {
onChange={onChange}
/>
</FormElement>
<FormElement label="Token">
<Input
name="token"
value={token}
testID="slack-token"
onChange={onChange}
type={InputType.Password}
/>
</FormElement>
</Grid.Column>
</Grid.Row>
</Grid>

View File

@ -1,6 +1,9 @@
import {omit} from 'lodash'
// Types
import {NotificationEndpoint} from 'src/types'
import {DEFAULT_ENDPOINT_URLS} from 'src/alerting/constants'
import {NotificationEndpointBase} from 'src/client'
export type Action =
| {type: 'UPDATE_ENDPOINT'; endpoint: NotificationEndpoint}
@ -9,7 +12,10 @@ export type Action =
export type EndpointState = NotificationEndpoint
export const reducer = (state: EndpointState, action: Action) => {
export const reducer = (
state: EndpointState,
action: Action
): EndpointState => {
switch (action.type) {
case 'UPDATE_ENDPOINT': {
const {endpoint} = action
@ -18,25 +24,46 @@ export const reducer = (state: EndpointState, action: Action) => {
case 'UPDATE_ENDPOINT_TYPE': {
const {endpoint} = action
if (state.type != endpoint.type) {
const baseProps: NotificationEndpointBase = omit(endpoint, [
'url',
'token',
'username',
'password',
'method',
'authMethod',
'contentTemplate',
'headers',
'clientURL',
'routingKey',
])
switch (endpoint.type) {
case 'pagerduty':
return {
...state,
...endpoint,
...baseProps,
type: 'pagerduty',
clientURL: `${location.origin}/orgs/${
endpoint.orgID
baseProps.orgID
}/alert-history`,
routingKey: '',
}
default:
case 'http':
return {
...state,
...endpoint,
url: DEFAULT_ENDPOINT_URLS[endpoint.type],
...baseProps,
type: 'http',
method: 'POST',
authMethod: 'none',
url: DEFAULT_ENDPOINT_URLS.http,
}
case 'slack':
return {
...baseProps,
type: 'slack',
url: DEFAULT_ENDPOINT_URLS.slack,
token: '',
}
}
}
return {...state, ...endpoint}
return state
}
case 'DELETE_ENDPOINT': {
return state

View File

@ -98,6 +98,7 @@ export {
SlackNotificationEndpoint,
HTTPNotificationEndpoint,
NotificationEndpointUpdate,
NotificationEndpointBase,
} from '../client'
import {Check, Threshold, HTTPNotificationEndpoint} from '../client'