-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42f86fd
commit e0bf0a3
Showing
7 changed files
with
293 additions
and
73 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import {eg} from "./utils/edgeGrid"; | ||
import {getProperty} from "./utils/getProperty"; | ||
|
||
eg.auth({ | ||
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'GET', | ||
}) | ||
|
||
eg.send((err, response, body) => { | ||
const {properties} = JSON.parse(body ?? '{}'); | ||
if (!properties) { | ||
const handler = async () => { | ||
try { | ||
const property = await getProperty(); | ||
if (property) { | ||
console.log('Property already exists'); | ||
process.exit(1); | ||
} | ||
} catch (e) { | ||
console.log('Error during property check:', e); | ||
process.exit(1); | ||
} | ||
const property = properties.items.find(t => t.propertyName === `${process.env.BRANCH_NAME}.cfi-fingerprint.com`); | ||
if (property) { | ||
process.exit(1) | ||
} | ||
process.exit(0) | ||
}); | ||
} | ||
|
||
handler().finally(); |
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 |
---|---|---|
@@ -1,41 +1,122 @@ | ||
import {eg} from "./utils/edgeGrid"; | ||
|
||
eg.auth({ | ||
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
productId: "Site_Accel", | ||
propertyName: `${process.env.BRANCH_NAME}.cfi-fingerprint.com`, | ||
ruleFormat: "latest" | ||
}) | ||
}) | ||
import {akamaiRequest} from "./utils/akamaiRequest"; | ||
import {getProperty} from "./utils/getProperty"; | ||
import {CI_DOMAIN} from "./utils/constants"; | ||
|
||
eg.send((err, response, body) => { | ||
if (!response) { | ||
process.exit(1) | ||
} | ||
if (response?.status >= 200 && response?.status < 300) { | ||
eg.auth({ | ||
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
const createProperty = async () => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
productId: "Site_Accel", | ||
propertyName: CI_DOMAIN, | ||
ruleFormat: "latest" | ||
}) | ||
}); | ||
const {propertyId} = await getProperty(); | ||
return propertyId; | ||
} | ||
|
||
const createEdgeHostname = async () => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
ipVersionBehavior: 'IPV4', | ||
domainPrefix: CI_DOMAIN, | ||
domainSuffix: `edgesuite.net`, | ||
productId: `Site_Accel`, | ||
}) | ||
}); | ||
|
||
return findEdgeHostname() | ||
} | ||
|
||
const createCPCode = async () => { | ||
let cpcodeId = await findCpcode(); | ||
if (!cpcodeId) { | ||
await akamaiRequest({ | ||
path: `/papi/v1/cpcodes?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
ipVersionBehavior: 'IPV4', | ||
domainPrefix: `${process.env.BRANCH_NAME}.cfi-fingerprint.com`, | ||
domainSuffix: `edgesuite.net`, | ||
cpcodeName: CI_DOMAIN, | ||
productId: `Site_Accel`, | ||
}) | ||
}), | ||
}); | ||
} | ||
|
||
eg.send((err, response, body) => { | ||
if (!response) { | ||
process.exit(1) | ||
} | ||
if (response?.status >= 200 && response?.status < 300) { | ||
process.exit(0) | ||
return findCpcode() | ||
} | ||
|
||
const findCpcode = async () => { | ||
const {body: {cpcodes: {items}}} = await akamaiRequest({ | ||
path: `/papi/v1/cpcodes?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'GET', | ||
}); | ||
|
||
const cpcode = items.find(t => t.cpcodeName === CI_DOMAIN); | ||
return cpcode?.cpcodeId; | ||
}; | ||
|
||
const patchCpcode = async (propertyId: string, cpcodeId: string) => akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/1/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
headers: { | ||
"Content-Type": "application/json-patch+json", | ||
}, | ||
body: [ | ||
{ | ||
op: "replace", | ||
path: "/rules/children/0/children/0/behaviors/0/options", | ||
value: { | ||
value: { | ||
id: Number(cpcodeId.replace('cpc_', '')), | ||
}, | ||
} | ||
process.exit(1); | ||
}); | ||
process.exit(0) | ||
} | ||
] | ||
}); | ||
|
||
const findEdgeHostname = async () => { | ||
const {body: {edgeHostnames: {items}}} = await akamaiRequest({ | ||
path: `/papi/v1/edgehostnames?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'GET', | ||
}); | ||
return items.find(t => t.domainPrefix === CI_DOMAIN); | ||
} | ||
|
||
const patchEdgeHostname = async (propertyId: string) => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/1/hostnames`, | ||
method: 'PATCH', | ||
body: JSON.stringify({ | ||
"add": [ | ||
{ | ||
"cnameType": "EDGE_HOSTNAME", | ||
"cnameFrom": CI_DOMAIN, | ||
"cnameTo": `${CI_DOMAIN}.edgesuite.net` | ||
}, | ||
] | ||
}) | ||
}) | ||
}; | ||
|
||
const handler = async () => { | ||
try { | ||
let {propertyId} = await getProperty(); | ||
if (!propertyId) { | ||
propertyId = await createProperty(); | ||
} | ||
const hostname = await findEdgeHostname(); | ||
if (!hostname) { | ||
await createEdgeHostname(); | ||
} | ||
await patchEdgeHostname(propertyId); | ||
const cpcodeId = await createCPCode(); | ||
await patchCpcode(propertyId, cpcodeId); | ||
} catch (e: any) { | ||
console.error(e) | ||
process.exit(1); | ||
} | ||
process.exit(1) | ||
}); | ||
} | ||
|
||
handler().finally(); |
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 |
---|---|---|
@@ -1,33 +1,134 @@ | ||
import {patchBody} from "../build"; | ||
import {eg} from "./utils/edgeGrid"; | ||
import {akamaiRequest} from "./utils/akamaiRequest"; | ||
import {getProperty} from "./utils/getProperty"; | ||
import {ORIGIN_DOMAIN} from "./utils/constants"; | ||
|
||
patchBody({ | ||
integrationPath: 'worker', | ||
agentPath: 'agent', | ||
resultPath: 'result', | ||
proxySecret: 'abc123', | ||
}) | ||
const getLatestVersion = async (propertyId: string) => { | ||
const {body} = await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'GET', | ||
}); | ||
|
||
import('../dist/patch-body/body.json').then(module => { | ||
const content = module.default as object; | ||
return body.versions.items[0]; | ||
}; | ||
|
||
eg.auth({ | ||
path: `/papi/v1/properties?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'GET', | ||
const createNewVersion = async (propertyId: string) => { | ||
const latestVersion = await getLatestVersion(propertyId); | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
createFromVersion: latestVersion.propertyVersion, | ||
createFromVersionEtag: latestVersion.etag, | ||
}) | ||
}); | ||
|
||
return getLatestVersion(propertyId); | ||
}; | ||
|
||
const patchOriginHostname = async (propertyId: string, version: string) => akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
headers: { | ||
"Content-Type": "application/json-patch+json", | ||
}, | ||
body: JSON.stringify([ | ||
{ | ||
op: "add", | ||
path: "/rules/behaviors/0/options/hostname", | ||
value: ORIGIN_DOMAIN, | ||
}, | ||
{ | ||
op: "replace", | ||
path: "/rules/behaviors/0/options/cacheKeyHostname", | ||
value: "ORIGIN_HOSTNAME", | ||
}, | ||
{ | ||
op: "replace", | ||
path: "/rules/behaviors/0/options/forwardHostHeader", | ||
value: "ORIGIN_HOSTNAME", | ||
} | ||
]) | ||
}); | ||
|
||
const patchRemoveFingerprintRules = async(propertyId: string, version: string) => akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
headers: { | ||
"Content-Type": "application/json-patch+json", | ||
}, | ||
body: JSON.stringify([ | ||
{ | ||
op: "remove", | ||
path: "/rules/children/6", | ||
} | ||
]) | ||
}); | ||
|
||
const patchAddFingerprintRules = async (propertyId: string, version: string, content: string) => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
headers: { | ||
"Content-Type": "application/json-patch+json", | ||
}, | ||
body: content, | ||
}) | ||
} | ||
|
||
eg.send((err, response, body) => { | ||
const {properties} = JSON.parse(body ?? '{}'); | ||
const property = properties.items.find(t => t.propertyName === `${process.env.BRANCH_NAME}.cfi-fingerprint.com`); | ||
const clearVariables = async (propertyId: string, version: string) => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/versions/${version}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
headers: { | ||
"Content-Type": "application/json-patch+json", | ||
}, | ||
body: JSON.stringify([ | ||
{ | ||
"op": "replace", | ||
"path": "/rules/variables", | ||
"value": [] | ||
} | ||
]) | ||
}) | ||
}; | ||
|
||
eg.auth({ | ||
path: `/papi/v1/properties/${property.propertyId}/versions/${property.latestVersion}/rules?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'PATCH', | ||
body: JSON.stringify(content), | ||
}); | ||
const activateVersion = async (propertyId: string, version: string) => { | ||
await akamaiRequest({ | ||
path: `/papi/v1/properties/${propertyId}/activations?contractId=${process.env.AK_CONTRACT_ID}&groupId=${process.env.AK_GROUP_ID}`, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
propertyVersion: version, | ||
network: "PRODUCTION", | ||
notifyEmails: [ | ||
"[email protected]", | ||
], | ||
acknowledgeAllWarnings: true, | ||
activationType: "ACTIVATE", | ||
|
||
eg.send((err, response, body) => { | ||
// TODO: Deploy Successful | ||
}); | ||
}); | ||
}), | ||
}) | ||
} | ||
|
||
import('../dist/patch-body/body.json').then((module) => { | ||
const content = module.default as object; | ||
|
||
const handler = async () => { | ||
try { | ||
const {propertyId} = await getProperty() | ||
const {propertyVersion} = await createNewVersion(propertyId); | ||
await patchOriginHostname(propertyId, propertyVersion); | ||
await clearVariables(propertyId, propertyVersion); | ||
try { | ||
await patchRemoveFingerprintRules(propertyId, propertyVersion); | ||
} catch (_) { | ||
// Ignore error if fingerprint rules not exists | ||
} | ||
await patchAddFingerprintRules(propertyId, propertyVersion, JSON.stringify(content)); | ||
await activateVersion(propertyId, propertyVersion); | ||
} catch (e: any) { | ||
console.error(e.error) | ||
process.exit(1) | ||
} | ||
}; | ||
handler().finally(); | ||
}) |
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,23 @@ | ||
import {eg} from "./edgeGrid"; | ||
import {AxiosResponse} from "axios"; | ||
|
||
type AkamaiResponse<ResponseBody extends any | any [] = any> = { | ||
response: AxiosResponse; | ||
body: ResponseBody; | ||
} | ||
|
||
export const akamaiRequest = async <ResponseBody = any>(authOptions: any,) => { | ||
return new Promise<AkamaiResponse<ResponseBody>>((resolve, reject) => { | ||
eg.auth(authOptions); | ||
eg.send((err, response, body) => { | ||
if (err) { | ||
reject({error: err}); | ||
} | ||
if (!err && response && response.status >= 200 && response.status < 300 && body) { | ||
resolve({body: JSON.parse(body ?? '{}'), response}); | ||
} else { | ||
reject({error: body, response}); | ||
} | ||
}); | ||
}); | ||
} |
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,2 @@ | ||
export const CI_DOMAIN = `${process.env.BRANCH_NAME}.cfi-fingerprint.com`; | ||
export const ORIGIN_DOMAIN = "origin-akamai.cfi-fingerprint.com"; |
Oops, something went wrong.