Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active February 19, 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/8a3170193b99e9dc68217a5594f9b295 to your computer and use it in GitHub Desktop.
Save tkh44/8a3170193b99e9dc68217a5594f9b295 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 isValid = () => true
const targetingMachine = Machine({
id: "targeting",
initial: 'targetTypeSelection',
context: {
targetType: 'host',
targetSelection: 'parameter', // param / exact
selectedTargets: new Set(),
targetParams: {
/*[key]: new Set() */
}
},
states: {
targetTypeSelection: {
on: {
SELECT_TARGET_TYPE: {
actions: assign({ targetType: 'kubernetes', targetingStrategy: 'param' })
}
}
}
}
})
const createAttackMachine = Machine({
id: "create",
initial: "loading",
context: {
target: {
targetType: 'host',
targetingStrategy: 'random',
selectedTargets: new Set(),
targetParams: {
/*[key]: new Set() */
}
},
impact: {},
schedule: {},
retries: 0
},
on: {
OPEN_TARGET: {
target: '#target.attackTypeSelection',
cond: (context) => {
return isValid(context.target)
}
},
OPEN_IMPACT: {
target: '#impact.selectCategory',
cond: (context, event) => {
return isValid(context.target)
}
},
OPEN_SCHEDULE: {
target: '#schedule',
cond: (context, event) => {
return isValid(context.target, context.impact)
}
},
SUBMIT: {
target: 'saving',
cond: (context, event) => {
return isValid(context.target, context.impact, context.schedule)
}
}
},
states: {
error: {
on: {
RETRY: {
target: "loading",
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
},
loading: {
on: {
SUCCESS: "#target.attackTypeSelection",
ERROR: "error"
}
},
target: {
id: 'target',
on: {
SET_RANDOM: {
target: '.random',
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetingStrategy: 'random' })
}),
cond: (context, event) => context.target.targetType !== 'kubernetes'
},
SET_EXACT: {
target: '.exact',
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetingStrategy: 'exact' })
})
},
SET_TARGETING_STRATEGY: {
target: [
{
target: '#target.random',
cond: (_, event) => event.value === 'random'
},
{
target: '#target.exact',
cond: (_, event) => event.value === 'exact'
}
],
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetingStrategy: event.value })
})
}
},
states: {
attackTypeSelection: {
on: {
SELECT_KUBERNETES: {
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetType: 'kubernetes', targetingStrategy: 'exact' })
})
},
SELECT_HOST: {
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetType: 'host', targetingStrategy: 'random' })
})
},
SELECT_CONTAINER: {
actions: assign({
target: (context, event) => Object.assign(context.target,
{ targetType: 'container', targetingStrategy: 'random' })
})
}
}
},
random: {
on: {
DONE: '#impact',
ADD_TARGET_PARAM: {
actions: assign({
target: (context, event) => console.log(context, event)
})
},
REMOVE_TARGET_PARAM: {
actions: assign({
target: (context, event) => console.log(context, event)
})
}
}
},
exact: {
on: {
DONE: { target: '#impact' },
SELECT_TARGET: {
actions: assign({
target: (context, event) => console.log(context, event)
})
},
DESELECT_TARGET: {
actions: assign({
target: (context, event) => console.log(context, event)
})
}
}
}
}
},
impact: {
id: 'impact',
initial: 'selectCategory',
on: {
always: '.selectCategory',
SUBMIT: {
target: "review",
actions: assign({
impact: (_, event) => event.value
})
}
},
states: {
selectCategory: {
on: {
SELECT_CATEGORY: 'selectAttack'
}
},
selectAttack: {
on: {
SELECT_ATTACK: 'editArguments'
}
},
editArguments: {
on: {
EDIT_ARG: {
actions: assign({
target: (context, event) => console.log(context, event)
})
},
DONE: '#review'
}
}
}
},
schedule: {
id: 'schedule',
on: {
SUBMIT: {
target: "review",
actions: assign({
schedule: (_, event) => event.value
})
}
}
},
review: {
id: 'review',
on: {
SUBMIT: {
target: 'saving',
cond: (context, event) => {
return isValid(context.target, context.impact, context.schedule)
}
}
}
},
saving: {
on: {
SUCCESS: {
target: "target"
},
ERROR: {
target: "review"
}
}
}
}
},
{
actions: {
selectTarget: assign({
target: (context, event) => console.log(context, event)
}),
deselectTarget: assign({
target: (context, event) => console.log(context, event)
}),
addTargetParam: assign({
target: (context, event) => console.log(context, event)
}),
removeTargetParam: assign({
target: (context, event) => console.log(context, event)
})
},
guards: {
isHostTarget: (context) => {
return context.targetType === "HOST";
},
isContainerTarget: (context) => {
return context.targetType === "CONTAINER";
},
isKubernetesTarget: (context) => {
return context.targetType === "KUBERNETES";
},
verifyHostTarget: (context) => {
return context.targetType === "HOST";
},
verifyContainerTarget: (context) => {
return context.targetType === "CONTAINER";
},
verifyKubernetesTarget: (context) => {
return context.targetType === "KUBERNETES";
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment