-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ui): resolved telegraf bucket dropdown bug and undefined token issue #17363
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,17 @@ import {normalize} from 'normalizr' | |
import {client} from 'src/utils/api' | ||
import {ScraperTargetRequest, PermissionResource} from '@influxdata/influx' | ||
import {createAuthorization} from 'src/authorizations/apis' | ||
import {postWrite as apiPostWrite, postLabel as apiPostLabel} from 'src/client' | ||
import {postWrite as apiPostWrite} from 'src/client' | ||
|
||
// Schemas | ||
import {authSchema} from 'src/schemas' | ||
import {telegrafSchema} from 'src/schemas/telegrafs' | ||
|
||
// Utils | ||
import {createNewPlugin} from 'src/dataLoaders/utils/pluginConfigs' | ||
import {addLabelDefaults} from 'src/labels/utils' | ||
import {getDataLoaders, getSteps} from 'src/dataLoaders/selectors' | ||
import {getBucketByName} from 'src/buckets/selectors' | ||
import {getByID} from 'src/resources/selectors' | ||
import {getOrg} from 'src/organizations/selectors' | ||
|
||
// Constants | ||
|
@@ -37,13 +38,12 @@ import { | |
import { | ||
GetState, | ||
RemoteDataState, | ||
LabelProperties, | ||
Authorization, | ||
AuthEntities, | ||
ResourceType, | ||
TelegrafEntities, | ||
Telegraf, | ||
} from 'src/types' | ||
import {ILabel} from '@influxdata/influx' | ||
import { | ||
WritePrecision, | ||
TelegrafRequest, | ||
|
@@ -57,9 +57,10 @@ import {addTelegraf, editTelegraf} from 'src/telegrafs/actions/creators' | |
import {addAuthorization} from 'src/authorizations/actions/creators' | ||
import {notify} from 'src/shared/actions/notifications' | ||
import { | ||
readWriteCardinalityLimitReached, | ||
TelegrafConfigCreationError, | ||
TelegrafConfigCreationSuccess, | ||
readWriteCardinalityLimitReached, | ||
TokenCreationError, | ||
} from 'src/shared/copy/notifications' | ||
|
||
const DEFAULT_COLLECTION_INTERVAL = 10000 | ||
|
@@ -401,6 +402,61 @@ export const createOrUpdateTelegrafConfigAsync = () => async ( | |
createTelegraf(dispatch, getState, plugins) | ||
} | ||
|
||
export const generateTelegrafToken = (configID: string) => async ( | ||
dispatch, | ||
getState: GetState | ||
) => { | ||
try { | ||
const state = getState() | ||
const orgID = getOrg(state).id | ||
const telegraf = getByID<Telegraf>(state, ResourceType.Telegrafs, configID) | ||
const bucketName = get(telegraf, 'metadata.buckets[0]', '') | ||
if (bucketName === '') { | ||
throw new Error( | ||
'A token cannot be generated without a corresponding bucket' | ||
) | ||
} | ||
const bucket = getBucketByName(state, bucketName) | ||
|
||
const permissions = [ | ||
{ | ||
action: Permission.ActionEnum.Write, | ||
resource: { | ||
type: PermissionResource.TypeEnum.Buckets, | ||
id: bucket.id, | ||
orgID, | ||
}, | ||
}, | ||
{ | ||
action: Permission.ActionEnum.Read, | ||
resource: { | ||
type: PermissionResource.TypeEnum.Telegrafs, | ||
id: configID, | ||
orgID, | ||
}, | ||
}, | ||
] | ||
|
||
const token = { | ||
name: `${telegraf.name} token`, | ||
orgID, | ||
description: `WRITE ${bucketName} bucket / READ ${ | ||
telegraf.name | ||
} telegraf config`, | ||
permissions, | ||
} | ||
|
||
// create token | ||
const createdToken = await createAuthorization(token) | ||
|
||
// add token to data loader state | ||
dispatch(setToken(createdToken.token)) | ||
} catch (error) { | ||
console.error(error) | ||
dispatch(notify(TokenCreationError)) | ||
} | ||
} | ||
|
||
const createTelegraf = async (dispatch, getState: GetState, plugins) => { | ||
try { | ||
const state = getState() | ||
|
@@ -461,40 +517,8 @@ const createTelegraf = async (dispatch, getState: GetState, plugins) => { | |
// add token to authorizations state | ||
dispatch(addAuthorization(normAuth)) | ||
|
||
// create token label | ||
const properties = { | ||
color: '#FFFFFF', | ||
description: `token for telegraf config: ${telegrafConfigName}`, | ||
tokenID: createdToken.id, | ||
} as LabelProperties // hack to make compiler work | ||
|
||
const resp = await apiPostLabel({ | ||
data: { | ||
orgID: org.id, | ||
name: `@influxdata.token-${new Date().getTime()}`, // fix for https://github.com/influxdata/influxdb/issues/15730 | ||
properties, | ||
}, | ||
}) | ||
|
||
if (resp.status !== 201) { | ||
throw new Error(resp.data.message) | ||
} | ||
|
||
const createdLabel = addLabelDefaults(resp.data.label) | ||
|
||
// add label to telegraf config | ||
const label = await client.telegrafConfigs.addLabel( | ||
tc.id, | ||
createdLabel as ILabel | ||
) | ||
|
||
const config = { | ||
...tc, | ||
labels: [label], | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
|
||
const normTelegraf = normalize<Telegraf, TelegrafEntities, string>( | ||
config, | ||
tc, | ||
telegrafSchema | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Libraries | ||
import React, {FC} from 'react' | ||
import {connect} from 'react-redux' | ||
import { | ||
Button, | ||
ComponentColor, | ||
ComponentSize, | ||
ComponentStatus, | ||
IconFont, | ||
} from '@influxdata/clockface' | ||
|
||
// Decorator | ||
import {Notification} from 'src/types' | ||
|
||
// Components | ||
import FancyScrollbar from 'src/shared/components/fancy_scrollbar/FancyScrollbar' | ||
import CopyButton from 'src/shared/components/CopyButton' | ||
|
||
// Actions | ||
import {generateTelegrafToken} from 'src/dataLoaders/actions/dataLoaders' | ||
|
||
export interface Props { | ||
configID: string | ||
token: string | ||
onCopyText?: (text: string, status: boolean) => Notification | ||
testID?: string | ||
label: string | ||
} | ||
|
||
interface DispatchProps { | ||
onGenerateTelegrafToken: typeof generateTelegrafToken | ||
} | ||
|
||
const TokenCodeSnippet: FC<Props & DispatchProps> = ({ | ||
token, | ||
onCopyText, | ||
testID, | ||
label = 'Code Snippet', | ||
}) => { | ||
const handleRefreshClick = () => { | ||
const {configID, onGenerateTelegrafToken} = this.props | ||
onGenerateTelegrafToken(configID) | ||
} | ||
|
||
return ( | ||
<div className="code-snippet" data-testid={testID}> | ||
<FancyScrollbar | ||
autoHide={false} | ||
autoHeight={true} | ||
maxHeight={400} | ||
className="code-snippet--scroll" | ||
> | ||
<div className="code-snippet--text"> | ||
<pre> | ||
export INFLUX_TOKEN=<i>{token || '<INFLUX_TOKEN>'}</i> | ||
</pre> | ||
</div> | ||
</FancyScrollbar> | ||
<div className="code-snippet--footer"> | ||
<div> | ||
<CopyButton | ||
textToCopy={token} | ||
onCopyText={onCopyText} | ||
contentName="Script" | ||
/> | ||
<Button | ||
size={ComponentSize.ExtraSmall} | ||
status={ | ||
token === '' ? ComponentStatus.Default : ComponentStatus.Disabled | ||
} | ||
text="Generate New Token" | ||
titleText="Generate New Token" | ||
icon={IconFont.Refresh} | ||
color={ComponentColor.Success} | ||
onClick={handleRefreshClick} | ||
className="new-token--btn" | ||
/> | ||
</div> | ||
<label className="code-snippet--label">{label}</label> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
const mdtp: DispatchProps = { | ||
onGenerateTelegrafToken: generateTelegrafToken, | ||
} | ||
|
||
export default connect<{}, DispatchProps>( | ||
null, | ||
mdtp | ||
)(TokenCodeSnippet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should handle no bucket name being on the telegraf config.