-
Notifications
You must be signed in to change notification settings - Fork 159
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
Extensibility as custom phone provider deploy cli support #1038
Draft
ramya18101
wants to merge
3
commits into
master
Choose a base branch
from
DXCDT-859-Extensibility-as-custom-provider-deploy-cli-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { constants } from '../../../tools'; | ||
|
||
import { existsMustBeDir, isFile, dumpJSON, loadJSON } from '../../../utils'; | ||
import { DirectoryHandler } from '.'; | ||
import DirectoryContext from '..'; | ||
import { ParsedAsset } from '../../../types'; | ||
import { PhoneProvider } from '../../../tools/auth0/handlers/phoneProvider'; | ||
|
||
type ParsedPhoneProvider = ParsedAsset<'phoneProviders', PhoneProvider[]>; | ||
|
||
function parse(context: DirectoryContext): ParsedPhoneProvider { | ||
const phoneProvidersFolder = path.join(context.filePath, constants.PHONE_PROVIDER_DIRECTORY); | ||
if (!existsMustBeDir(phoneProvidersFolder)) return { phoneProviders: null }; // Skip | ||
|
||
const providerFile = path.join(phoneProvidersFolder, 'provider.json'); | ||
|
||
if (isFile(providerFile)) { | ||
return { | ||
phoneProviders: loadJSON(providerFile, { | ||
mappings: context.mappings, | ||
disableKeywordReplacement: context.disableKeywordReplacement, | ||
}), | ||
}; | ||
} | ||
|
||
return { phoneProviders: null }; | ||
} | ||
|
||
async function dump(context: DirectoryContext): Promise<void> { | ||
const { phoneProviders } = context.assets; | ||
if (!phoneProviders) { | ||
return; | ||
}// Skip, nothing to dump | ||
|
||
const phoneProvidersFolder = path.join(context.filePath, constants.PHONE_PROVIDER_DIRECTORY); | ||
fs.ensureDirSync(phoneProvidersFolder); | ||
|
||
const phoneProviderFile = path.join(phoneProvidersFolder, 'provider.json'); | ||
// @ts-ignore | ||
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. why using @ts-ignore? |
||
dumpJSON(phoneProviderFile, phoneProviders.map(({ created_at, updated_at, tenant, channel, credentials, ...rest }) => rest)); | ||
} | ||
|
||
const phoneProvidersHandler: DirectoryHandler<ParsedPhoneProvider> = { | ||
parse, | ||
dump, | ||
}; | ||
|
||
export default phoneProvidersHandler; |
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,36 @@ | ||
import { YAMLHandler } from '.'; | ||
import YAMLContext from '..'; | ||
import { PhoneProvider } from '../../../tools/auth0/handlers/phoneProvider'; | ||
import { ParsedAsset } from '../../../types'; | ||
|
||
type ParsedPhoneProviders = ParsedAsset<'phoneProviders', PhoneProvider[] >; | ||
|
||
async function parse(context: YAMLContext): Promise<ParsedPhoneProviders> { | ||
const { phoneProviders } = context.assets; | ||
|
||
if (!phoneProviders) return { phoneProviders: null }; | ||
|
||
console.log(phoneProviders); | ||
|
||
return { | ||
phoneProviders, | ||
}; | ||
} | ||
|
||
async function dump(context: YAMLContext): Promise<ParsedPhoneProviders> { | ||
if (!context.assets.phoneProviders) return { phoneProviders: null }; | ||
|
||
const { phoneProviders } = context.assets; | ||
|
||
return { | ||
// @ts-ignore | ||
phoneProviders: phoneProviders.map(({ created_at, updated_at, tenant, channel, credentials, ...rest }) => rest) | ||
}; | ||
} | ||
|
||
const phoneProviderHandler: YAMLHandler<ParsedPhoneProviders> = { | ||
parse, | ||
dump, | ||
}; | ||
|
||
export default phoneProviderHandler; |
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,201 @@ | ||
import { | ||
CreatePhoneProviderRequest, | ||
DeletePhoneProviderRequest, | ||
GetBrandingPhoneProviders200ResponseProvidersInner, | ||
UpdatePhoneProviderOperationRequest | ||
} from 'auth0'; | ||
import DefaultHandler, { order } from './default'; | ||
import { Assets } from '../../../types'; | ||
import log from '../../../logger'; | ||
|
||
export type PhoneProvider = GetBrandingPhoneProviders200ResponseProvidersInner; | ||
export default class PhoneProviderHandler extends DefaultHandler { | ||
existing: PhoneProvider[] | null; | ||
|
||
constructor(options: DefaultHandler) { | ||
super({ | ||
...options, | ||
type: 'phoneProviders', | ||
id: 'id', | ||
}); | ||
} | ||
|
||
objString(provider: PhoneProvider) : string { | ||
return super.objString({ name: provider.name, disabled: provider.disabled }); //Que | ||
} | ||
|
||
async getType(): Promise<PhoneProvider[] | null> { | ||
if (!this.existing) { | ||
this.existing = await this.getPhoneProviders(); | ||
} | ||
|
||
return this.existing; | ||
} | ||
|
||
async getPhoneProviders(): Promise<PhoneProvider[] | null> { | ||
const { data: response } = await this.client.branding.getAllPhoneProviders(); | ||
return response.providers ?? []; | ||
} | ||
|
||
@order('60') | ||
async processChanges(assets: Assets): Promise<void> { | ||
const { phoneProviders } = assets; | ||
|
||
// Non-existing section means themes doesn't need to be processed | ||
if (!phoneProviders) return; | ||
|
||
// Empty array means themes should be deleted | ||
if (phoneProviders.length === 0) { | ||
return this.deletePhoneProviders(); | ||
} | ||
|
||
return this.updatePhoneProviders(phoneProviders); | ||
} | ||
|
||
async deletePhoneProviders(): Promise<void> { | ||
if (!this.config('AUTH0_ALLOW_DELETE')) { | ||
return; | ||
} | ||
|
||
// if phone providers exists we need to delete it | ||
const currentProviders = await this.getPhoneProviders(); | ||
if (currentProviders === null || currentProviders.length === 0) { | ||
return; | ||
} | ||
|
||
const currentProvider = currentProviders[0]; | ||
await this.client.branding.deletePhoneProvider(<DeletePhoneProviderRequest>{ id: currentProvider.id }); | ||
// await this.client.branding.deletePhoneProvider({ 'id': currentProvider.id }); //Quee | ||
|
||
this.deleted += 1; | ||
this.didDelete(currentProvider); | ||
} | ||
|
||
async updatePhoneProviders(phoneProviders: PhoneProvider[]): Promise<void> { | ||
if (phoneProviders.length > 1) { | ||
log.warn('Currently only one phone provider is supported per tenant'); | ||
} | ||
|
||
const currentProviders = await this.getPhoneProviders(); | ||
|
||
const providerReqPayload = ((): Omit<PhoneProvider, 'id'> => { | ||
// Removing id from update and create payloads, otherwise API will error | ||
// id may be required to handle if `--export_ids=true` | ||
const payload = phoneProviders[0]; | ||
// @ts-ignore to quell non-optional id property, but we know that it's ok to delete | ||
delete payload.id; | ||
return payload; | ||
})(); | ||
|
||
if (currentProviders === null || currentProviders.length === 0) { | ||
// if provider does not exist, create it | ||
await this.client.branding.configurePhoneProvider(providerReqPayload as CreatePhoneProviderRequest); | ||
} else { | ||
const currentProvider = currentProviders[0]; | ||
// if provider exists, overwrite it | ||
this.created += 1; | ||
await this.client.branding.updatePhoneProvider({ id: currentProvider.id } as UpdatePhoneProviderOperationRequest, providerReqPayload); | ||
} | ||
|
||
this.updated += 1; | ||
this.didUpdate(phoneProviders[0]); | ||
} | ||
} | ||
|
||
const TwilioCredentialsSchema = { | ||
type: 'object', | ||
required: ['auth_token'], | ||
properties: { | ||
auth_token: { | ||
type: 'string', | ||
minLength: 1, | ||
maxLength: 255, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
|
||
const TwilioConfigurationSchema = { | ||
type: 'object', | ||
required: ['sid', 'delivery_methods'], | ||
additionalProperties: false, | ||
properties: { | ||
default_from: { | ||
type: 'string', | ||
}, | ||
mssid: { | ||
type: 'string', | ||
}, | ||
sid: { | ||
type: 'string', | ||
}, | ||
delivery_methods: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: ['text', 'voice'], | ||
}, | ||
minItems: 1, | ||
uniqueItems: true, | ||
}, | ||
}, | ||
}; | ||
|
||
const CustomCredentialsSchema = { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: {}, | ||
}; | ||
|
||
const CustomConfigurationSchema = { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['delivery_methods'], | ||
properties: { | ||
delivery_methods: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
enum: ['text', 'voice'], | ||
}, | ||
minItems: 1, | ||
uniqueItems: true, | ||
}, | ||
}, | ||
}; | ||
|
||
export const schema = { | ||
type: 'array', | ||
description: 'List of phone provider configurations', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'string', | ||
minLength: 1, | ||
maxLength: 255, | ||
}, | ||
name: { | ||
type: 'string', | ||
description: 'Name of the phone notification provider', | ||
enum: ['twilio', 'custom'], | ||
minLength: 1, | ||
maxLength: 100, | ||
}, | ||
disabled: { | ||
type: 'boolean', | ||
description: 'Whether the provider is enabled (false) or disabled (true).', | ||
defaultValue: false, | ||
}, | ||
configuration: { | ||
type: 'object', | ||
anyOf: [TwilioConfigurationSchema, CustomConfigurationSchema], | ||
}, | ||
credentials: { | ||
description: 'Provider credentials required to authenticate to the provider.', | ||
anyOf: [TwilioCredentialsSchema, CustomCredentialsSchema], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}; |
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.
why using @ts-ignore?