Skip to content
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

refactor(ui): migrate authorizations CRUD to generated client #14831

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions ui/src/authorizations/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import {Dispatch} from 'react'

// API
import {client} from 'src/utils/api'
import * as authAPI from 'src/authorizations/apis'
import * as api from 'src/client'

// Actions
import {notify} from 'src/shared/actions/notifications'
Expand All @@ -20,8 +20,12 @@ import {
} from 'src/shared/copy/notifications'

// Types
import {RemoteDataState, GetState, NotificationAction} from 'src/types'
import {Authorization} from '@influxdata/influx'
import {
RemoteDataState,
GetState,
NotificationAction,
Authorization,
} from 'src/types'

export type Action =
| SetAuthorizations
Expand Down Expand Up @@ -97,7 +101,13 @@ export const getAuthorizations = () => async (
orgs: {org},
} = getState()

const authorizations = await client.authorizations.getAll(org.id)
const resp = await api.getAuthorizations({query: {orgID: org.id}})

if (resp.status !== 200) {
throw new Error(resp.data.message)
}

const {authorizations} = resp.data

dispatch(setAuthorizations(RemoteDataState.Done, authorizations))
} catch (e) {
Expand All @@ -107,10 +117,15 @@ export const getAuthorizations = () => async (
}
}

export const getAuthorization = async (authorizationID: string) => {
export const getAuthorization = async (authID: string) => {
try {
const authorization = await client.authorizations.get(authorizationID)
return authorization
const resp = await api.getAuthorization({authID})

if (resp.status !== 200) {
throw new Error(resp.data.message)
}

return resp.data
} catch (e) {
console.error(e)
}
Expand All @@ -134,7 +149,14 @@ export const updateAuthorization = (authorization: Authorization) => async (
dispatch: Dispatch<Action | NotificationAction | GetAuthorizations>
) => {
try {
await client.authorizations.update(authorization.id, authorization)
const resp = await api.patchAuthorization({
authID: authorization.id,
data: authorization,
})

if (resp.status !== 200) {
throw new Error(resp.data.message)
}

dispatch(getAuthorizations())
dispatch(notify(authorizationUpdateSuccess()))
Expand All @@ -148,7 +170,11 @@ export const deleteAuthorization = (id: string, name: string = '') => async (
dispatch: Dispatch<Action | NotificationAction>
) => {
try {
await client.authorizations.delete(id)
const resp = await api.deleteAuthorization({authID: id})

if (resp.status !== 204) {
throw new Error(resp.data.message)
}

dispatch(removeAuthorization(id))
dispatch(notify(authorizationDeleteSuccess()))
Expand Down
4 changes: 3 additions & 1 deletion ui/src/authorizations/components/AllAccessTokenOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import {allAccessPermissions} from 'src/authorizations/utils/permissions'

// Decorators
import {ErrorHandling} from 'src/shared/decorators/errors'
import {Authorization} from '@influxdata/influx'

// Types
import {Authorization} from 'src/types'

interface DispatchProps {
onCreateAuthorization: typeof createAuthorization
Expand Down
11 changes: 5 additions & 6 deletions ui/src/authorizations/components/BucketsTokenOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ import {createAuthorization} from 'src/authorizations/actions'
import {ErrorHandling} from 'src/shared/decorators/errors'

// Types
import {AppState, Bucket} from 'src/types'
import {Permission, Authorization} from '@influxdata/influx'
import {AppState, Bucket, Permission, Authorization} from 'src/types'

interface StateProps {
buckets: Bucket[]
Expand Down Expand Up @@ -236,7 +235,7 @@ class BucketsTokenOverlay extends PureComponent<Props, State> {
return buckets.find(b => b.name === bucketName)
})

return specificBucketsPermissions(writeBuckets, Permission.ActionEnum.Write)
return specificBucketsPermissions(writeBuckets, 'write')
}

private get readBucketPermissions(): Permission[] {
Expand All @@ -246,23 +245,23 @@ class BucketsTokenOverlay extends PureComponent<Props, State> {
return buckets.find(b => b.name === bucketName)
})

return specificBucketsPermissions(readBuckets, Permission.ActionEnum.Read)
return specificBucketsPermissions(readBuckets, 'read')
}

private get allReadBucketPermissions(): Permission[] {
const {
params: {orgID},
} = this.props

return allBucketsPermissions(orgID, Permission.ActionEnum.Read)
return allBucketsPermissions(orgID, 'read')
}

private get allWriteBucketPermissions(): Permission[] {
const {
params: {orgID},
} = this.props

return allBucketsPermissions(orgID, Permission.ActionEnum.Write)
return allBucketsPermissions(orgID, 'write')
}

private handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/authorizations/components/TokenList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TokenRow from 'src/authorizations/components/TokenRow'
import ViewTokenOverlay from 'src/authorizations/components/ViewTokenOverlay'

// Types
import {Authorization} from '@influxdata/influx'
import {Authorization} from 'src/types'
import {SortTypes} from 'src/shared/utils/sort'
import {ComponentSize, Sort} from '@influxdata/clockface'

Expand Down
10 changes: 5 additions & 5 deletions ui/src/authorizations/components/TokenRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import EditableName from 'src/shared/components/EditableName'

// Types
import {Authorization, AuthorizationUpdateRequest} from '@influxdata/influx'
import {Authorization} from 'src/types'
import {DEFAULT_TOKEN_DESCRIPTION} from 'src/dashboards/constants'

interface OwnProps {
Expand Down Expand Up @@ -78,15 +78,15 @@ class TokenRow extends PureComponent<Props> {

private get isTokenEnabled(): boolean {
const {auth} = this.props
return auth.status === AuthorizationUpdateRequest.StatusEnum.Active
return auth.status === 'active'
}

private changeToggle = () => {
const {auth, onUpdate} = this.props
if (auth.status === AuthorizationUpdateRequest.StatusEnum.Active) {
auth.status = AuthorizationUpdateRequest.StatusEnum.Inactive
if (auth.status === 'active') {
auth.status = 'inactive'
} else {
auth.status = AuthorizationUpdateRequest.StatusEnum.Active
auth.status = 'active'
}
onUpdate(auth)
}
Expand Down
3 changes: 1 addition & 2 deletions ui/src/authorizations/components/TokensTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import TabbedPageHeader from 'src/shared/components/tabbed_page/TabbedPageHeader
import GenerateTokenDropdown from 'src/authorizations/components/GenerateTokenDropdown'

// Types
import {Authorization} from '@influxdata/influx'
import {AppState} from 'src/types'
import {AppState, Authorization} from 'src/types'
import {SortTypes} from 'src/shared/utils/sort'

enum AuthSearchKeys {
Expand Down
6 changes: 3 additions & 3 deletions ui/src/authorizations/components/ViewTokenOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import PermissionsWidget, {
import CodeSnippet from 'src/shared/components/CodeSnippet'

// Types
import {Authorization, Permission} from '@influxdata/influx'
import {Authorization, Permission} from 'src/types'

interface Props {
auth: Authorization
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class ViewTokenOverlay extends PureComponent<Props> {
)
}

private get permissions(): {[x: string]: Permission.ActionEnum[]} {
private get permissions(): {[x: string]: Permission['action'][]} {
const p = this.props.auth.permissions.reduce((acc, {action, resource}) => {
const {type} = resource
const name = get(resource, 'name', '')
Expand All @@ -82,7 +82,7 @@ export default class ViewTokenOverlay extends PureComponent<Props> {

private itemID = (
permission: string,
action: Permission.ActionEnum
action: Permission['action']
): string => {
return `${permission}-${action}-${permission || '*'}-${permission || '*'}`
}
Expand Down
3 changes: 1 addition & 2 deletions ui/src/authorizations/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import {produce} from 'immer'

// Types
import {RemoteDataState} from 'src/types'
import {RemoteDataState, Authorization} from 'src/types'
import {Action} from 'src/authorizations/actions'
import {Authorization} from '@influxdata/influx'

const initialState = (): AuthorizationsState => ({
status: RemoteDataState.NotStarted,
Expand Down
Loading