Skip to content

Commit

Permalink
SALTO-6164: add support for customizing elem id in the new infra (#6126)
Browse files Browse the repository at this point in the history

---------

Co-authored-by: Daniel Barros <bros.ar@gmail.com>
  • Loading branch information
edenhassid and DanielBarros1 authored Jun 27, 2024
1 parent 786d8af commit a4f1f63
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ const log = logger(module)
* 1. config is changed in place
* 2. only elemID related definitions are converted
*/
const updateElemIDDefinitions = <TCustomNameMappingOptions extends string>(
apiDefinitions: AdapterApiConfig,
export const updateElemIDDefinitions = <TCustomNameMappingOptions extends string>(
apiDefinitions: AdapterApiConfig | undefined,
): Record<string, ElemIDCustomization<TCustomNameMappingOptions>> => {
if (apiDefinitions === undefined) {
return {}
}
const { types } = apiDefinitions
if (_.isEmpty(types)) {
return {}
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-components/src/definitions/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export {
adapterConfigFromConfig,
} from './user_config'
// eslint-disable-next-line import/no-cycle
export { updateDeprecatedConfig } from './config_upgrade_utils'
export { updateDeprecatedConfig, updateElemIDDefinitions } from './config_upgrade_utils'
// eslint-disable-next-line import/no-cycle
export {
UserFetchConfig,
Expand Down
16 changes: 15 additions & 1 deletion packages/zendesk-adapter/src/adapter_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
config as configUtils,
definitions,
} from '@salto-io/adapter-components'
import { inspectValue } from '@salto-io/adapter-utils'
import ZendeskAdapter from './adapter'
import {
Credentials,
Expand Down Expand Up @@ -56,7 +57,7 @@ import { customReferenceHandlers } from './custom_references'

const log = logger(module)
const { validateCredentials } = clientUtils
const { validateClientConfig, mergeWithDefaultConfig } = definitions
const { validateClientConfig, mergeWithDefaultConfig, updateElemIDDefinitions } = definitions
const { validateDuckTypeApiDefinitionConfig } = configUtils
const { validateDefaultMissingUserFallbackConfig } = definitions

Expand Down Expand Up @@ -113,6 +114,7 @@ const isValidUser = (user: string): boolean => EMAIL_REGEX.test(user)

const adapterConfigFromConfig = (config: Readonly<InstanceElement> | undefined): ZendeskConfig => {
const configValue = config?.value ?? {}
const useNewInfra = configValue.fetch?.useNewInfra
const isGuideDisabled = config?.value.fetch.guide === undefined
DEFAULT_CONFIG.apiDefinitions.supportedTypes = isGuideDisabled
? DEFAULT_CONFIG.apiDefinitions.supportedTypes
Expand All @@ -123,6 +125,18 @@ const adapterConfigFromConfig = (config: Readonly<InstanceElement> | undefined):
) as configUtils.AdapterDuckTypeApiConfig

const fetch = mergeWithDefaultConfig(DEFAULT_CONFIG.fetch, config?.value.fetch) as ZendeskFetchConfig
if (useNewInfra === true) {
const configForNewInfra = config?.clone()
const updatedElemIDs = updateElemIDDefinitions(configForNewInfra?.value?.apiDefinitions)
if (updatedElemIDs?.elemID !== undefined) {
if (fetch.elemID !== undefined) {
log.debug('fetch.elemId is defined and is going to be merged with data from the api_definition')
}
const mergedElemIDConfig = _.merge(_.pick(fetch, 'elemID'), updatedElemIDs)
fetch.elemID = mergedElemIDConfig.elemID
log.debug(`elemId config has changes and equal to: ${inspectValue(fetch.elemID)}`)
}
}

const adapterConfig: { [K in keyof Required<ZendeskConfig>]: ZendeskConfig[K] } = {
client: configValue.client,
Expand Down
2 changes: 1 addition & 1 deletion packages/zendesk-adapter/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ export type FilterContext = {

export const validateFetchConfig = (
fetchConfigPath: string,
userFetchConfig: definitions.UserFetchConfig,
userFetchConfig: definitions.UserFetchConfig<{ customNameMappingOptions: never }>,
adapterApiConfig: configUtils.AdapterApiConfig,
): void =>
validateDuckTypeFetchConfig(
Expand Down
41 changes: 37 additions & 4 deletions packages/zendesk-adapter/src/definitions/fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/people/team/groups/{id}' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand All @@ -79,6 +80,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/people/team/roles/{id}' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand All @@ -102,6 +104,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/agent/organizations/{id}/tickets' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand Down Expand Up @@ -273,6 +276,7 @@ const createCustomizations = (): Record<
isTopLevel: true,
serviceUrl: { path: '/admin/apps-integrations/targets/targets' },
elemID: { parts: [{ fieldName: 'title' }, { fieldName: 'type' }] },
path: { pathParts: [{ parts: [{ fieldName: 'title' }, { fieldName: 'type' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand Down Expand Up @@ -356,6 +360,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'fileName' }] },
path: { pathParts: [{ parts: [{ fieldName: 'fileName' }] }] },
},
fieldCustomizations: { id: { fieldType: 'number', hide: true } },
},
Expand Down Expand Up @@ -399,6 +404,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/objects-rules/tickets/ticket-forms/edit/{id}' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
name: { hide: true },
Expand Down Expand Up @@ -490,6 +496,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'value' }], extendsParent: true, useOldFormat: true },
path: { pathParts: [{ parts: [{ fieldName: 'value' }], extendsParent: true, useOldFormat: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand Down Expand Up @@ -528,6 +535,7 @@ const createCustomizations = (): Record<
isTopLevel: true,
serviceUrl: { path: '/agent/admin/user_fields/{id}' },
elemID: { parts: [{ fieldName: 'key' }] },
path: { pathParts: [{ parts: [{ fieldName: 'key' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -554,6 +562,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'value' }], extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'value' }], extendsParent: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand Down Expand Up @@ -587,6 +596,7 @@ const createCustomizations = (): Record<
isTopLevel: true,
serviceUrl: { path: '/agent/admin/organization_fields/{id}' },
elemID: { parts: [{ fieldName: 'key' }] },
path: { pathParts: [{ parts: [{ fieldName: 'key' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -613,6 +623,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'value' }], extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'value' }], extendsParent: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -638,6 +649,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/account/brand_management/brands' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
ticket_form_ids: { omit: true },
Expand Down Expand Up @@ -714,6 +726,7 @@ const createCustomizations = (): Record<
element: {
topLevel: {
isTopLevel: true,
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand Down Expand Up @@ -787,6 +800,7 @@ const createCustomizations = (): Record<
element: {
topLevel: {
isTopLevel: true,
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand Down Expand Up @@ -822,6 +836,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'name' }, { fieldName: 'email', isReference: true }] },
path: { pathParts: [{ parts: [{ fieldName: 'name' }, { fieldName: 'email', isReference: true }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand Down Expand Up @@ -960,6 +975,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/workspaces/agent-workspace/dynamic_content' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -984,6 +1000,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'locale_id', isReference: true }], extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'locale_id', isReference: true }], extendsParent: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -1006,6 +1023,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/apps-integrations/webhooks/webhooks/{id}/details' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
meta: { omit: true },
Expand All @@ -1032,6 +1050,7 @@ const createCustomizations = (): Record<
serviceUrl: { path: '/admin/apps-integrations/apis/zendesk-api/oauth_clients' },
// note: requires oauth_global_client to be included in the config
elemID: { parts: [{ fieldName: 'client_id', isReference: true }, { fieldName: 'token' }] },
path: { pathParts: [{ parts: [{ fieldName: 'client_id', isReference: true }, { fieldName: 'token' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -1056,6 +1075,7 @@ const createCustomizations = (): Record<
isTopLevel: true,
serviceUrl: { path: '/admin/apps-integrations/apis/zendesk-api/settings/tokens/' },
elemID: { parts: [{ fieldName: 'description' }] },
path: { pathParts: [{ parts: [{ fieldName: 'description' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand Down Expand Up @@ -1085,6 +1105,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'key' }] },
path: { pathParts: [{ parts: [{ fieldName: 'key' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number' },
Expand Down Expand Up @@ -1120,6 +1141,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { extendsParent: true, parts: [{ fieldName: 'key' }] },
path: { pathParts: [{ parts: [{ fieldName: 'key' }], extendsParent: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -1144,6 +1166,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
elemID: { parts: [{ fieldName: 'value' }], extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'value' }], extendsParent: true }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand All @@ -1154,7 +1177,9 @@ const createCustomizations = (): Record<
account_features: {
requests: [{ endpoint: { path: '/api/v2/account/features' }, transformation: { root: 'features' } }],
resource: { directFetch: true },
element: { topLevel: { isTopLevel: true, singleton: true } },
element: {
topLevel: { isTopLevel: true, singleton: true },
},
},

routing_attribute: {
Expand All @@ -1177,6 +1202,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/objects-rules/rules/routing' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'string' },
Expand Down Expand Up @@ -1205,9 +1231,9 @@ const createCustomizations = (): Record<
element: {
topLevel: {
isTopLevel: true,
elemID: { extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
elemID: { parts: DEFAULT_ID_PARTS, extendsParent: true },
serviceUrl: { path: '/admin/objects-rules/rules/routing' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: { id: { hide: true, fieldType: 'string' } },
},
Expand Down Expand Up @@ -1261,6 +1287,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/knowledge/permissions/{id}' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: { id: { hide: true, fieldType: 'number' } },
},
Expand All @@ -1278,6 +1305,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/knowledge/user_segments/edit/{id}' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { hide: true, fieldType: 'number' },
Expand Down Expand Up @@ -1316,6 +1344,7 @@ const createCustomizations = (): Record<
topLevel: {
isTopLevel: true,
serviceUrl: { path: '/admin/objects-rules/rules/schedules' },
path: { pathParts: [{ parts: [{ fieldName: 'name' }] }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
Expand All @@ -1339,7 +1368,11 @@ const createCustomizations = (): Record<
},
],
element: {
topLevel: { isTopLevel: true, elemID: { extendsParent: true } },
topLevel: {
isTopLevel: true,
elemID: { parts: DEFAULT_ID_PARTS, extendsParent: true },
path: { pathParts: [{ parts: [{ fieldName: 'name' }], extendsParent: true }] },
},
fieldCustomizations: {
id: { fieldType: 'number', hide: true },
start_year: { fieldType: 'string', hide: true },
Expand Down
56 changes: 56 additions & 0 deletions packages/zendesk-adapter/test/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,62 @@ describe('adapter', () => {
})
expect(supportAddress?.value.brand_id.elemID.getFullName()).toEqual('zendesk.brand.instance.myBrand')
})
it('should generate the right elements on fetch with new infra, with elemID customization', async () => {
mockAxiosAdapter.onGet().reply(callbackResponseFunc)
const { elements } = await adapter
.operations({
credentials: new InstanceElement('config', usernamePasswordCredentialsType, {
username: 'user123',
password: 'token456',
subdomain: 'myBrand',
}),
config: new InstanceElement('config', configType, {
[FETCH_CONFIG]: {
include: [
{
type: 'group',
},
],
exclude: [],
guide: {
brands: ['.*'],
},
useNewInfra: true,
omitInactive: {
default: false,
customizations: {},
},
},
[API_DEFINITIONS_CONFIG]: {
types: {
group: {
transformation: {
idFields: ['default', 'name'],
},
},
},
},
}),
elementsSource: buildElementsSourceFromElements([]),
})
.fetch({ progressReporter: { reportProgress: () => null } })

expect(
elements
.map(e => e.elemID.getFullName())
.filter(a => a.includes('group'))
.sort(),
).toEqual([
'zendesk.group',
'zendesk.group.instance.false_Support2',
'zendesk.group.instance.false_Support4',
'zendesk.group.instance.false_Support5',
'zendesk.group.instance.true_Support',
'zendesk.groups',
'zendesk.permission_group',
'zendesk.permission_groups',
])
})
it('should not generate tags when excluded', async () => {
mockAxiosAdapter.onGet().reply(callbackResponseFunc)
const { elements } = await adapter
Expand Down

0 comments on commit a4f1f63

Please sign in to comment.