Skip to content

Commit

Permalink
feat: support for the apac region (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzraho authored Oct 22, 2024
1 parent d9d930c commit dbd90d1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
20 changes: 16 additions & 4 deletions lib/AdobeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const {
REQUEST_ID_HEADER,
MIN_LIST_COUNT_HINT,
REGEX_PATTERN_MATCH_KEY,
MAX_TTL_SECONDS
MAX_TTL_SECONDS,
ALLOWED_STAGE_REGION
} = require('./constants')

/* *********************************** typedefs *********************************** */
Expand Down Expand Up @@ -146,9 +147,10 @@ class AdobeState {
* @private
* @param {string} namespace the namespace for the Adobe State Store
* @param {string} apikey the apikey for the Adobe State Store
* @param {string} env the Adobe environment (AIO_CLI_ENV)
* @param {('amer'|'apac'|'emea')} [region] the region for the Adobe State Store. defaults to 'amer'
*/
constructor (namespace, apikey, region) {
constructor (namespace, apikey, env, region) {
/** @private */
this.namespace = namespace
/** @private */
Expand All @@ -158,7 +160,7 @@ class AdobeState {
/** @private */
this.region = region
/** @private */
this.endpoint = this.getRegionalEndpoint(ENDPOINTS[getCliEnv()], region)
this.endpoint = this.getRegionalEndpoint(ENDPOINTS[env], region)
/** @private */
this.fetchRetry = new HttpExponentialBackoff()
}
Expand Down Expand Up @@ -228,6 +230,16 @@ class AdobeState {
const cloned = utils.withHiddenFields(credentials, ['apikey'])
logger.debug(`init AdobeState with ${JSON.stringify(cloned, null, 2)}`)

const env = getCliEnv()

if (env === 'stage' &&
credentials.region && credentials.region !== ALLOWED_STAGE_REGION) {
logAndThrow(new codes.ERROR_BAD_ARGUMENT({
messageValues: `AIO_CLI_ENV=stage only supports the ${ALLOWED_STAGE_REGION} region.`,
sdkDetails: cloned
}))
}

if (!credentials.region) {
credentials.region = ALLOWED_REGIONS.at(0) // first item is the default
}
Expand All @@ -253,7 +265,7 @@ class AdobeState {
}))
}

return new AdobeState(credentials.namespace, credentials.apikey, credentials.region)
return new AdobeState(credentials.namespace, credentials.apikey, env, credentials.region)
}

/* **************************** ADOBE STATE STORE OPERATORS ***************************** */
Expand Down
7 changes: 5 additions & 2 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const ENDPOINT_PROD_INTERNAL = 'https://storage-state-<region>.app-builder.int.a
const ENDPOINT_STAGE = 'https://storage-state-<region>.stg.app-builder.adp.adobe.io'
const ENDPOINT_STAGE_INTERNAL = 'https://storage-state-<region>.stg.app-builder.adp.adobe.io'

// prod
const ALLOWED_REGIONS = [ // first region is the default region
'amer',
'emea'
// soon to come: 'apac'
'emea',
'apac'
]
const ALLOWED_STAGE_REGION = 'amer' // STAGE only supports one region

// can be overwritten by env
const {
Expand Down Expand Up @@ -55,6 +57,7 @@ const REQUEST_ID_HEADER = 'x-request-id'

module.exports = {
ALLOWED_REGIONS,
ALLOWED_STAGE_REGION,
ENDPOINTS,
CUSTOM_ENDPOINT,
MAX_KEY_SIZE,
Expand Down
50 changes: 47 additions & 3 deletions test/AdobeState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,61 @@ describe('private methods', () => {
expect(url).toEqual(`https://storage-state-amer.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
})

test('custom stage, emea', async () => {
test('env=stage', async () => {
jest.resetModules()
const env = STAGE_ENV
mockCLIEnv.mockReturnValue(env)

// need to instantiate a new store, when env changes
const customAdobeState = require('../lib/AdobeState').AdobeState
const store = await customAdobeState.init({ ...fakeCredentials })
const url = store.createRequestUrl()
expect(url).toEqual(`https://storage-state-amer.stg.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
})

test('env=stage && region!=amer', async () => {
jest.resetModules()
const region = 'emea'
const env = STAGE_ENV
mockCLIEnv.mockReturnValue(env)

// need to instantiate a new store, when env changes
const customAdobeState = require('../lib/AdobeState').AdobeState
await expect(customAdobeState.init({ ...fakeCredentials, region: 'emea' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.')
await expect(customAdobeState.init({ ...fakeCredentials, region: 'apac' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.')
await expect(customAdobeState.init({ ...fakeCredentials, region: 'amerr' })).rejects.toThrow('[AdobeStateLib:ERROR_BAD_ARGUMENT] AIO_CLI_ENV=stage only supports the amer region.')
})

test('region=amer', async () => {
jest.resetModules()
const region = 'amer'

// need to instantiate a new store, when env changes
const customAdobeState = require('../lib/AdobeState').AdobeState
const store = await customAdobeState.init({ ...fakeCredentials, region })
const url = store.createRequestUrl()
expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
})

test('region=emea', async () => {
jest.resetModules()
const region = 'emea'

// need to instantiate a new store, when env changes
const customAdobeState = require('../lib/AdobeState').AdobeState
const store = await customAdobeState.init({ ...fakeCredentials, region })
const url = store.createRequestUrl()
expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
})

test('region=apac', async () => {
jest.resetModules()
const region = 'apac'

// need to instantiate a new store, when env changes
const customAdobeState = require('../lib/AdobeState').AdobeState
const store = await customAdobeState.init({ ...fakeCredentials, region })
const url = store.createRequestUrl()
expect(url).toEqual(`https://storage-state-${region}.stg.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
expect(url).toEqual(`https://storage-state-${region}.app-builder.adp.adobe.io/containers/${fakeCredentials.namespace}`)
})

test('custom AIO_STATE_ENDPOINT', async () => {
Expand Down

0 comments on commit dbd90d1

Please sign in to comment.