Skip to content

Commit

Permalink
Support custom environment name (#118)
Browse files Browse the repository at this point in the history
* support custom environment name as in the Target UI, Administration > Environments.
defaults to production

* nice catch
  • Loading branch information
shandilya3 committed Jun 19, 2023
1 parent c7aa8ef commit 5eb9441
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 114 deletions.
30 changes: 10 additions & 20 deletions packages/target-decisioning-engine/src/artifactProvider.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as HttpStatus from "http-status-codes";
import {
ENVIRONMENT_PROD,
ENVIRONMENT_STAGE,
isDefined,
TelemetryProvider
Expand All @@ -10,8 +9,7 @@ import * as constants from "./constants";
import {
ARTIFACT_FORMAT_DEFAULT,
ARTIFACT_FORMAT_JSON,
CDN_BASE_PROD,
CDN_BASE_STAGE,
CDN_BASE_PATH,
SUPPORTED_ARTIFACT_MAJOR_VERSION
} from "./constants";
import Messages from "./messages";
Expand Down Expand Up @@ -400,7 +398,7 @@ describe("determineArtifactLocation", () => {
cdnEnvironment: "staging"
})
).toEqual(
`https://${CDN_BASE_STAGE}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
`https://${CDN_BASE_PATH}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
);
});

Expand All @@ -410,7 +408,7 @@ describe("determineArtifactLocation", () => {
client: "someClientId"
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
`https://${CDN_BASE_PATH}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
);
});

Expand All @@ -421,7 +419,7 @@ describe("determineArtifactLocation", () => {
artifactFormat: "bin"
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.bin`
`https://${CDN_BASE_PATH}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.bin`
);
});

Expand All @@ -432,7 +430,7 @@ describe("determineArtifactLocation", () => {
artifactFormat: "wonk"
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.${ARTIFACT_FORMAT_DEFAULT}`
`https://${CDN_BASE_PATH}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.${ARTIFACT_FORMAT_DEFAULT}`
);
});

Expand All @@ -443,26 +441,18 @@ describe("determineArtifactLocation", () => {
environment: ENVIRONMENT_STAGE
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/${ENVIRONMENT_STAGE}/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
`https://${CDN_BASE_PATH}/someClientId/${ENVIRONMENT_STAGE}/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
);
});

it("warns on invalid environment name and defaults to prod", done => {
it("warns on invalid environment name and defaults to prod", () => {
expect(
determineArtifactLocation({
client: "someClientId",
environment: "boohoo",
logger: {
debug: (prefix, message) => {
expect(message).toEqual(
Messages.INVALID_ENVIRONMENT("boohoo", ENVIRONMENT_PROD)
);
done();
}
}
environment: "1-PROD"
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/${ENVIRONMENT_PROD}/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
`https://${CDN_BASE_PATH}/someClientId/1-prod/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/rules.json`
);
});

Expand All @@ -473,7 +463,7 @@ describe("determineArtifactLocation", () => {
propertyToken: "xyz-123-abc"
})
).toEqual(
`https://${CDN_BASE_PROD}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/xyz-123-abc/rules.json`
`https://${CDN_BASE_PATH}/someClientId/production/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/xyz-123-abc/rules.json`
);
});
});
18 changes: 1 addition & 17 deletions packages/target-decisioning-engine/src/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
ENVIRONMENT_DEV,
ENVIRONMENT_PROD,
ENVIRONMENT_STAGE
} from "@adobe/target-tools";

export const DEFAULT_POLLING_INTERVAL = 300000; // five minutes (in milliseconds)
export const MINIMUM_POLLING_INTERVAL = 300000; // five minutes (in milliseconds)
export const NUM_FETCH_RETRIES = 10;
Expand All @@ -21,24 +15,14 @@ ARTIFACT_FILENAME[ARTIFACT_FORMAT_JSON] = "rules.json";

export const LOG_PREFIX = "LD";

export const CDN_BASE_PROD = "assets.adobetarget.com";
export const CDN_BASE_STAGE = "assets.staging.adobetarget.com";
export const CDN_BASE_DEV = "assets.staging.adobetarget.com";
export const CDN_BASE_PATH = "assets.adobetarget.com";

export const HTTP_HEADER_FORWARDED_FOR = "x-forwarded-for";
export const HTTP_HEADER_GEO_LATITUDE = "x-geo-latitude";
export const HTTP_HEADER_GEO_LONGITUDE = "x-geo-longitude";
export const HTTP_HEADER_GEO_COUNTRY = "x-geo-country-code";
export const HTTP_HEADER_GEO_REGION = "x-geo-region-code";
export const HTTP_HEADER_GEO_CITY = "x-geo-city";

const CDN_BASE = {};
CDN_BASE[ENVIRONMENT_PROD] = CDN_BASE_PROD;
CDN_BASE[ENVIRONMENT_STAGE] = CDN_BASE_STAGE;
CDN_BASE[ENVIRONMENT_DEV] = CDN_BASE_DEV;

export { CDN_BASE };

export const CAMPAIGN_BUCKET_SALT = "0";

// Response token keys
Expand Down
14 changes: 7 additions & 7 deletions packages/target-decisioning-engine/src/geoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import {
UNKNOWN_IP_ADDRESS
} from "@adobe/target-tools";
import { GEO_LOCATION_UPDATED } from "./events";
import { getGeoLookupPath } from "./utils";
import {
CDN_BASE_PATH,
HTTP_HEADER_FORWARDED_FOR,
HTTP_HEADER_GEO_CITY,
HTTP_HEADER_GEO_COUNTRY,
HTTP_HEADER_GEO_LATITUDE,
HTTP_HEADER_GEO_LONGITUDE,
HTTP_HEADER_GEO_REGION
HTTP_HEADER_GEO_REGION,
SUPPORTED_ARTIFACT_MAJOR_VERSION
} from "./constants";

// When ipAddress is the only geo value passed in to getOffers(), do IP-to-Geo lookup.
const GEO_LOOKUP_URL = `https://${CDN_BASE_PATH}/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/geo`;

const GEO_MAPPINGS = [
{
headerName: HTTP_HEADER_FORWARDED_FOR,
Expand Down Expand Up @@ -106,10 +110,6 @@ export function GeoProvider(config, artifact) {
) {
delete validatedGeoRequestContext.ipAddress;
}

// When ipAddress is the only geo value passed in to getOffers(), do IP-to-Geo lookup.
const geoLookupPath = getGeoLookupPath(config);

if (
geoTargetingEnabled &&
(geoRequestContext.ipAddress === UNKNOWN_IP_ADDRESS ||
Expand All @@ -126,7 +126,7 @@ export function GeoProvider(config, artifact) {
headers[HTTP_HEADER_FORWARDED_FOR] = geoRequestContext.ipAddress;
}

return fetchApi(geoLookupPath, {
return fetchApi(GEO_LOOKUP_URL, {
headers
})
.then(geoResponse =>
Expand Down
78 changes: 8 additions & 70 deletions packages/target-decisioning-engine/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {
DEFAULT_GLOBAL_MBOX,
ENVIRONMENT_PROD,
getLogger,
getMboxNames,
getViewNames,
hasRequestedViews,
Expand All @@ -14,8 +13,7 @@ import {
isPojo,
isString,
isUndefined,
parseURI,
POSSIBLE_ENVIRONMENTS
parseURI
} from "@adobe/target-tools";

import Messages from "./messages";
Expand All @@ -25,7 +23,7 @@ import {
ARTIFACT_FORMAT_DEFAULT,
ARTIFACT_FORMAT_JSON,
ARTIFACT_FORMATS,
CDN_BASE,
CDN_BASE_PATH,
REGEX_ARTIFACT_FILENAME_BINARY,
SUPPORTED_ARTIFACT_MAJOR_VERSION
} from "./constants";
Expand Down Expand Up @@ -180,60 +178,6 @@ export function cloneDeep(obj) {
return undefined;
}

/**
*
* @param {String} environmentName
* @param logger
*/
export function getValidEnvironment(environmentName, logger) {
const isValid = includes(environmentName, POSSIBLE_ENVIRONMENTS);

if (!isValid) {
getLogger(logger).debug(
Messages.INVALID_ENVIRONMENT(environmentName, ENVIRONMENT_PROD)
);
}

return isValid ? environmentName : ENVIRONMENT_PROD;
}

/**
* @param {import("../types/DecisioningConfig").DecisioningConfig} config
*/
export function getTargetEnvironment(config) {
const { environment = ENVIRONMENT_PROD } = config;

return getValidEnvironment(environment, config.logger);
}

/**
* @param {import("../types/DecisioningConfig").DecisioningConfig} config
*/
export function getCdnEnvironment(config) {
const { cdnEnvironment = ENVIRONMENT_PROD } = config;

return getValidEnvironment(cdnEnvironment, config.logger);
}

/**
* @param {import("../types/DecisioningConfig").DecisioningConfig} config
* @return {string}
*/
export function getCdnBasePath(config) {
let { cdnBasePath } = config;

if (!isDefined(cdnBasePath)) {
const cdnEnvironment = getCdnEnvironment(config);

const env = includes(cdnEnvironment, POSSIBLE_ENVIRONMENTS)
? cdnEnvironment
: ENVIRONMENT_PROD;
cdnBasePath = CDN_BASE[env];
}

return `https://${cdnBasePath}`;
}

export function getArtifactFileName(artifactFormat = ARTIFACT_FORMAT_DEFAULT) {
// eslint-disable-next-line no-param-reassign
artifactFormat = includes(artifactFormat, ARTIFACT_FORMATS)
Expand All @@ -242,16 +186,6 @@ export function getArtifactFileName(artifactFormat = ARTIFACT_FORMAT_DEFAULT) {

return ARTIFACT_FILENAME[artifactFormat];
}

/**
* @param {import("../types/DecisioningConfig").DecisioningConfig} config
* @return {string}
*/
export function getGeoLookupPath(config) {
const cdnBasePath = getCdnBasePath(config);
return `${cdnBasePath}/v${SUPPORTED_ARTIFACT_MAJOR_VERSION}/geo`;
}

/**
* @param {import("../types/DecisioningConfig").DecisioningConfig} config Options map, required
* @param {Boolean} addPropertyToken
Expand All @@ -262,10 +196,14 @@ export function determineArtifactLocation(config) {
return artifactLocation;
}

const targetEnvironment = getTargetEnvironment(config);
const cdnBaseUrl = `https://${CDN_BASE_PATH}`;

const targetEnvironment = config.environment
? config.environment.toLowerCase()
: ENVIRONMENT_PROD;

return [
getCdnBasePath(config),
cdnBaseUrl,
client,
targetEnvironment,
`v${SUPPORTED_ARTIFACT_MAJOR_VERSION}`,
Expand Down

0 comments on commit 5eb9441

Please sign in to comment.