Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active March 1, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkh44/8470feedec3b28e9d69c7eb8b28df41b to your computer and use it in GitHub Desktop.
Save tkh44/8470feedec3b28e9d69c7eb8b28df41b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const initialContextValue = {
error: undefined,
target: {
category: 'INFRASTRUCTURE',
type: 'Hosts',
strategy: 'RANDOM',
selectedService: undefined,
serviceTargets: [],
targets: {
Hosts: [],
Containers: [],
Applications: [],
Kubernetes: [],
Services: []
},
selectedTargets: new Set(),
targetParams: {},
sampling: {}
},
impact: {
category: 'State',
command: undefined,
args: {}
},
trigger: {
/*
type: trigger.type,
exact: date
start: trigger.start,
end: trigger.end,
activeDays: trigger.activeDays,
maxRuns: trigger.maxRuns,
timeZone: trigger.timeZone
*/
}
}
const fetchMachine = Machine(
{
id: 'createAttackForm',
initial: 'loading',
context: initialContextValue,
states: {
error: {
on: { RETRY: 'loading' }
},
loading: {
invoke: {
id: 'fetchTargets',
src: 'fetchTargets',
onDone: {
target: 'target',
actions: assign({
target: (context, event) => {
const [clients, alfi, kubernetes, services] = event.data
return {
...context.target,
targets: {
Hosts: clients,
Containers: getContainersFromClients(clients),
Applications: alfi,
Kubernetes: kubernetes,
Services: services
}
}
}
})
},
onError: {
target: 'error',
actions: assign({
error: (_, event) => event.data
})
}
},
on: {
SUCCESS: 'target',
ERROR: 'error'
}
},
target: {
initial: 'targetCategory',
on: {
NEXT: {
target: 'impact',
actions: ['updateContext']
}
},
states: {
targetCategory: {
on: {
SELECT_INFRA: 'infrastructure',
SELECT_SERVICES: 'services',
SELECT_ALFI: 'applications'
}
},
infrastructure: {
initial: 'host',
on: {
SELECT_HOSTS: '.host',
SELECT_CONTAINERS: '.container',
SELECT_KUBERNETES: '.kubernetes'
},
states: {
host: {
initial: 'random',
on: {
SET_RANDOM: {
target: '.random',
actions: 'setRandomStrategy'
},
SET_EXACT: {
target: '.exact',
actions: 'setExactStrategy'
}
},
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
},
exact: {
on: {
SELECT_TARGET: {
actions: 'selectTarget'
},
DESELECT_TARGET: {
actions: 'deselectTarget'
}
}
}
}
},
container: {
initial: 'random',
on: {
SET_RANDOM: {
target: '.random',
actions: 'setRandomStrategy'
},
SET_EXACT: {
target: '.exact',
actions: 'setExactStrategy'
}
},
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
},
exact: {
on: {
SELECT_TARGET: {
actions: 'selectTarget'
},
DESELECT_TARGET: {
actions: 'deselectTarget'
}
}
}
}
},
kubernetes: {
initial: 'random',
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
}
}
}
}
},
services: {
states: {
loading: {
invoke: {
id: 'fetchServices',
src: 'fetchServices',
onDone: {
target: 'selectService',
actions: assign({
target: (context, event) => {
const services = event.data
return {
...context.target,
targets: {
...context.target.targets,
Services: services
}
}
}
})
},
onError: {
target: 'error'
}
}
},
error: {
on: {
RETRY: 'loading'
}
},
selectService: {
on: {
SELECT_SERVICE: [
{
action: 'selectService',
target: 'host',
cond: (ctx, event) => event.service && event.service.provenance === 'PROCESSDATA'
},
{
action: 'selectService',
target: 'container',
cond: (ctx, event) => event.service && event.service.provenance === 'CONTAINER'
},
{
action: 'selectService',
target: 'kubernetes',
cond: (ctx, event) => event.service && event.service.provenance === 'KUBERNETES'
}
]
}
},
host: {
initial: 'random',
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
}
}
},
container: {
initial: 'random',
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
}
}
},
kubernetes: {
initial: 'loading',
states: {
loading: {
invoke: {
id: 'fetchServiceKubernetesTargets',
src: 'fetchServiceKubernetesTargets',
onDone: {
target: 'random',
actions: assign({
target: (context, event) => {
const kubernetesTargets = event.data
return {
...context.target,
serviceTargets: {
...context.target.targets,
Kubernetes: kubernetesTargets
}
}
}
})
},
onError: {
target: 'error'
}
}
},
error: {
on: {
RETRY: 'loading'
}
},
random: {
on: {
ADD_TARGET_PARAM: {
actions: 'addTargetParam'
},
REMOVE_TARGET_PARAM: {
actions: 'removeTargetParam'
}
}
}
}
}
}
},
applications: {
type: 'final'
}
}
},
impact: {
on: {
BACK: {
target: 'target',
actions: ['updateContext']
},
NEXT: {
target: 'trigger',
actions: ['updateContext']
},
SUBMIT: 'submitting'
},
states: {
category: {
on: {
SELECT_CATEGORY: {
target: 'command',
action: 'selectAttackCategory'
}
}
},
command: {
on: {
SELECT_COMMAND: {
target: 'args',
action: 'selectAttackCommand'
}
}
},
args: {
on: {
UPDATE_FIELD: {
action: 'updateArg'
}
}
}
}
},
trigger: {
on: {
BACK: {
target: 'impact',
actions: ['updateContext']
},
SUBMIT: {
target: 'submitting',
actions: ['updateContext']
}
}
},
submitting: {
on: {
SUCCESS: 'created',
ERROR: 'submitError'
}
},
submitError: {
on: {
SUBMIT: 'submitting',
ERROR: 'submitError'
}
},
created: {
type: 'final'
}
}
},
{
actions: {
updateContext: assign((context, event) => {
const { type, ...rest } = event
if (type) {
return merge({}, context, rest)
}
})
},
services: {
fetchTargets: () => Promise.resolve([])
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment