Skip to content

Commit

Permalink
Merge pull request #595 from aziontech/dev
Browse files Browse the repository at this point in the history
DEPLOY 2024.01.22
  • Loading branch information
csfeijo authored Jan 22, 2024
2 parents 43d47b3 + 787a7c6 commit 7cd685c
Show file tree
Hide file tree
Showing 63 changed files with 2,919 additions and 183 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azion-platform-kit",
"version": "1.7.0",
"version": "1.7.1",
"private": true,
"type": "module",
"repository": {
Expand Down Expand Up @@ -63,7 +63,7 @@
"sass": "^1.68.0",
"start-server-and-test": "^2.0.0",
"tailwindcss": "^3.3.3",
"vite": "^4.4.12",
"vite": "^4.5.2",
"vitest": "^0.33.0",
"vitest-sonar-reporter": "^1.0.0"
}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/azion-documentation-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const documentationCatalog = {
edgeApplicationCacheSettings: () => openSearchResult('cache settings'),
edgeApplicationFunctions: () => openSearchResult('edge application functions instances'),
edgeApplicationDeviceGroups: () => openSearchResult('edge application device groups'),
edgeApplicationRulesEngine: () => openSearchResult('edge application rules engine'),
users: () => openSearchResult('users'),
activityHistory: () => openSearchResult('activity history'),
edgeNodes: () => openSearchResult('edge nodes'),
Expand Down
9 changes: 8 additions & 1 deletion src/router/routes/edge-application-routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ export const edgeApplicationRoutes = {
rulesEngineServices: {
listRulesEngineService: RulesEngineService.listRulesEngineService,
deleteRulesEngineService: RulesEngineService.deleteRulesEngineService,
reorderRulesEngine: RulesEngineService.reorderRulesEngine
editRulesEngineService: RulesEngineService.editRulesEngineService,
createRulesEngineService: RulesEngineService.createRulesEngineService,
loadRulesEngineService: RulesEngineService.loadRulesEngineService,
reorderRulesEngine: RulesEngineService.reorderRulesEngine,
documentationService: Helpers.documentationCatalog.edgeApplicationRulesEngine,
listOriginsService: OriginsService.listOriginsService,
listCacheSettingsService: CacheSettingsServices.listCacheSettingsService,
listEdgeApplicationFunctionsService: FunctionsService.listEdgeApplicationFunctionsService
},
clipboardWrite: Helpers.clipboardWrite
},
Expand Down
8 changes: 8 additions & 0 deletions src/router/routes/edge-firewall-routes/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Helpers from '@/helpers'
import * as DomainServices from '@/services/domains-services'
import * as EdgeFirewall from '@/services/edge-firewall-services'
import * as EdgeFirewallFunctions from '@/services/edge-firewall-functions-services'

/** @type {import('vue-router').RouteRecordRaw} */
export const edgeFirewallRoutes = {
Expand Down Expand Up @@ -55,6 +56,13 @@ export const edgeFirewallRoutes = {
createEdgeFirewallService: EdgeFirewall.createEdgeFirewallService,
loadEdgeFirewallService: EdgeFirewall.loadEdgeFirewallService,
editEdgeFirewallService: EdgeFirewall.editEdgeFirewallService,

listEdgeFunctionsService: EdgeFirewallFunctions.listEdgeFunctionsService,
listEdgeFirewallFunctionService: EdgeFirewallFunctions.listEdgeFirewallFunctionsService,
createFunctionService: EdgeFirewallFunctions.createFunctionService,
editFunctionService: EdgeFirewallFunctions.editFunctionService,
deleteFunctionService: EdgeFirewallFunctions.deleteFunctionService,
loadFunctionService: EdgeFirewallFunctions.loadFunctionService,
documentationService: Helpers.documentationCatalog.edgeFirewall,
updatedRedirect: 'list-edge-firewall'
},
Expand Down
11 changes: 11 additions & 0 deletions src/services/auth-services/get-login-method-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { AxiosHttpClientAdapter, parseHttpResponse } from '../axios/AxiosHttpClientAdapter'
import { makeAuthMethodBaseUrl } from './make-auth-method-base-url'

export const verifyLoginMethodService = async (email) => {
const emailEncoded = encodeURIComponent(email)
let httpResponse = await AxiosHttpClientAdapter.request({
url: `${makeAuthMethodBaseUrl()}/login_method?email=${emailEncoded}`,
method: 'GET'
})
return parseHttpResponse(httpResponse)
}
3 changes: 3 additions & 0 deletions src/services/auth-services/make-auth-method-base-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const makeAuthMethodBaseUrl = () => {
return 'auth'
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const listCacheSettingsService = async ({ id }) => {

const adapt = (httpResponse) => {
const parseHttpResponse = httpResponse.body.results.map((cacheSettings) => ({
id: cacheSettings.id,
id: cacheSettings.id.toString(),
name: cacheSettings.name,
browserCache: cacheSettings.browser_cache_settings,
cdnCache: cacheSettings.cdn_cache_settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ const mapFunctionData = (edgeApplicationFunction, functionData) => {
return {
id: edgeApplicationFunction.id,
name: edgeApplicationFunction.name,
languageIcon: functionData.languageIcon,
referenceCount: functionData.referenceCount,
initiatorType: functionData.initiatorType,
functionInstanced: functionData.name,
lastEditor: functionData.lastEditor,
modified: functionData.modified,
statusTag: functionData.statusTag,
version: functionData.version
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const adapt = (httpResponse) => {

const parsedFunctions = bodyResults?.map((edgeApplicationFunction) => {
return {
id: edgeApplicationFunction.id,
id: edgeApplicationFunction.id.toString(),
edgeFunctionId: edgeApplicationFunction.edge_function_id,
name: parseName(edgeApplicationFunction),
args: edgeApplicationFunction.args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const adapt = (httpResponse) => {
originKey: {
content: origin.origin_key
},
originId: origin.origin_id,
originId: origin.origin_id.toString(),
name: origin.name,
originType: originTypeFormat[origin.origin_type],
addresses: formattedListOfAddresses,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { AxiosHttpClientAdapter } from '@/services/axios/AxiosHttpClientAdapter'
import { makeEdgeApplicationBaseUrl } from '../edge-application-services/make-edge-application-base-url'
import * as Errors from '@/services/axios/errors'

export const createRulesEngineService = async (payload) => {
const { edgeApplicationId, phase } = payload
let httpResponse = await AxiosHttpClientAdapter.request({
url: `${makeEdgeApplicationBaseUrl()}/${edgeApplicationId}/rules_engine/${phase}/rules`,
method: 'POST',
body: adapt(payload)
})

return parseHttpResponse(httpResponse, edgeApplicationId)
}

const adapt = (payload) => {
return {
name: payload.name,
phase: payload.phase,
criteria: payload.criteria,
behaviors: payload.behaviors,
is_active: payload.isActive,
description: payload.description
}
}

const errorMessagesMap = {
duplicated_behaviors: `The behavior '{value}' is duplicated.`,
blank_parameter: `The '{value}' field is empty.`,
incompatible_behaviors: `The behavior '{value}' is incompatible with the others.`,
invalid_behaviors: `The behavior '{value}' is invalid.`,
invalid_behaviors_target: `The behavior '{value}' has a invalid target.`
}

/**
* Extracts the first error message from an error object.
* @param {Object} errorObject - The error object to extract the message from.
* @returns {string} The first error message found.
*/
function getFirstErrorMessage(errorObject) {
for (let key in errorObject) {
const errorValue = errorObject[key]
if (typeof errorValue === 'string') {
return errorValue
}
if (Array.isArray(errorValue)) {
return processArrayError(key, errorValue)
}
if (typeof errorValue === 'object') {
return getFirstErrorMessage(errorValue)
}
}
}

/**
* Processes an array error to extract the error message.
* @param {string} key - The key of the error in the error object.
* @param {Array} errorArray - The array containing the error details.
* @returns {string} The processed error message.
*/
function processArrayError(key, errorArray) {
const errorMessageTemplate = errorMessagesMap[key]
if (errorMessageTemplate) {
let value = errorArray[0]
if (key === 'incompatible_behaviors') {
// Extract the behavior name from the string
value = value.match(/\(u'(.*?)',/)[1].replace(/_/g, ' ')
} else {
value = value.replace(/_/g, ' ')
}
return errorMessageTemplate.replace('{value}', value)
}
return `${key}: ${errorArray[0]}`
}

/**
* @param {Object} httpResponse - The HTTP response object.
* @param {Object} httpResponse.body - The response body.
* @param {String} httpResponse.statusCode - The HTTP status code.
* @returns {string} The result message based on the status code.
* @throws {Error} If there is an error with the response.
*/
const parseHttpResponse = (httpResponse) => {
switch (httpResponse.statusCode) {
case 201:
return {
feedback: 'Your Rules Engine has been created.'
}
case 400:
const apiError = getFirstErrorMessage(httpResponse)
throw new Error(apiError).message
case 401:
throw new Errors.InvalidApiTokenError().message
case 403:
throw new Errors.PermissionError().message
case 404:
throw new Errors.NotFoundError().message
case 500:
throw new Errors.InternalServerError().message
default:
throw new Errors.UnexpectedError().message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,51 @@ import * as Errors from '@/services/axios/errors'
export const editRulesEngineService = async ({ id, payload, reorder = false }) => {
const parsedPayload = adapt(payload, reorder)

const rulesPhase =
payload.phase.content === 'Default' ? 'request' : payload.phase.content?.toLowerCase()
const rulePhase = generateRulePhase(payload) || payload.phase

let httpResponse = await AxiosHttpClientAdapter.request({
url: `${makeEdgeApplicationBaseUrl()}/${id}/rules_engine/${rulesPhase}/rules/${payload.id}`,
url: `${makeEdgeApplicationBaseUrl()}/${id}/rules_engine/${rulePhase}/rules/${payload.id}`,
method: 'PATCH',
body: parsedPayload
})

return parseHttpResponse(httpResponse)
}

/**
*
* @param {Object} payload
* @param {Object} payload.phase
* @param {string | undefined} payload.phase.content
* @returns {string}
*/
const generateRulePhase = (payload) => {
const DEFAULT_RULE_PHASE = 'Default'
return payload.phase.content === DEFAULT_RULE_PHASE
? 'request'
: payload.phase.content?.toLowerCase()
}

const adapt = (payload, reorder) => {
let parsedPaylod
let parsedPayload

if (!reorder) {
if (reorder) {
parsedPayload = {
order: parseInt(payload.order)
}
} else {
const { name, phase, behaviors, criteria, isActive, description } = payload
parsedPaylod = {
parsedPayload = {
name,
phase: phase.content,
phase: phase.content || phase,
behaviors,
criteria,
is_active: isActive,
description
}
} else {
parsedPaylod = {
order: parseInt(payload.order)
}
}

return parsedPaylod
return parsedPayload
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/services/edge-application-rules-engine-services/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { listRulesEngineService } from './list-rules-engine-service'
import { deleteRulesEngineService } from './delete-rules-engine-service'
import { editRulesEngineService } from './edit-rules-engine-service'
import { createRulesEngineService } from './create-rules-engine-service'
import { loadRulesEngineService } from './load-rules-engine-service'
import { reorderRulesEngine } from './reorder-rules-engine-service'

export {
listRulesEngineService,
createRulesEngineService,
editRulesEngineService,
loadRulesEngineService,
deleteRulesEngineService,
reorderRulesEngine
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { AxiosHttpClientAdapter, parseHttpResponse } from '@/services/axios/AxiosHttpClientAdapter'
import { makeEdgeApplicationBaseUrl } from '../edge-application-services/make-edge-application-base-url'

export const loadRulesEngineService = async ({ edgeApplicationId, id, phase }) => {
const rulesPhase = generateRulePhase(phase)

let httpResponse = await AxiosHttpClientAdapter.request({
url: `${makeEdgeApplicationBaseUrl()}/${edgeApplicationId}/rules_engine/${rulesPhase}/rules/${id}`,
method: 'GET'
})
httpResponse = adapt(httpResponse)

return parseHttpResponse(httpResponse)
}

/**
*
* @param {Object} phase
* @param {string | undefined} phase.content
* @returns {string}
*/
const generateRulePhase = (phase) => {
const DEFAULT_RULE_PHASE = 'Default'
return phase.content === DEFAULT_RULE_PHASE ? 'request' : phase.content?.toLowerCase()
}

const adapt = (httpResponse) => {
const ruleEngine = httpResponse.body.results
const parsedBody = {
id: ruleEngine.id,
name: ruleEngine.name,
phase: ruleEngine.phase,
criteria: ruleEngine.criteria,
behaviors: ruleEngine.behaviors,
isActive: ruleEngine.is_active,
order: ruleEngine.order,
description: ruleEngine.description
}

return {
body: parsedBody,
statusCode: httpResponse.statusCode
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const makeEdgeApplicationBaseUrl = () => {
const version = 'v3'
return `${version}/edge_applications`
return `v3/edge_applications`
}
Loading

0 comments on commit 7cd685c

Please sign in to comment.