Last active
November 20, 2020 21:26
-
-
Save AndresRodH/dd58134a96f286bb06c3367952c4c001 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const createInstanceMachine = ({subdomain, userName}) => Machine({ | |
id: 'createInstance', | |
initial: 'checkingSubdomain', | |
context: { | |
subdomain, | |
userName, | |
group: `${subdomain}.coolfirecore.io`, | |
errors: [], | |
successfulJobs: [], | |
}, | |
states: { | |
checkingSubdomain: { | |
invoke: { | |
src: 'isSubdomainAvailable', | |
onDone: 'initializingServices', | |
onError: { | |
target: 'failure', | |
actions: assign({ | |
errors: (context, event) => [ | |
{ code: InstanceMachineErrorCode.SUBDOMAIN_UNAVAILABLE, originalError: fromError(event.data) }, | |
...context.errors, | |
], | |
}), | |
}, | |
}, | |
}, | |
initializingServices: { | |
type: 'parallel', | |
onDone: [{ target: 'failure', cond: 'someJobsFailed', actions: 'cleanupJobs' }, { target: 'helm' }], | |
states: { | |
cognito: { | |
initial: 'creatingGroup', | |
states: { | |
creatingGroup: { | |
invoke: { | |
src: 'createGroup', | |
onDone: 'addingUserToGroup', | |
onError: { | |
target: 'completed', | |
actions: assign({ | |
errors: (context, event) => [ | |
{ | |
code: InstanceMachineErrorCode.COGNITO_GROUP_CREATION_FAILED, | |
originalError: fromError(event.data), | |
}, | |
...context.errors, | |
], | |
}), | |
}, | |
}, | |
}, | |
addingUserToGroup: { | |
invoke: { | |
src: 'addUserToGroup', | |
onDone: { | |
target: 'completed', | |
actions: assign({ | |
successfulJobs: (context) => ['cognito', ...context.successfulJobs], | |
}), | |
}, | |
onError: { | |
target: 'completed', | |
actions: [ | |
'deleteGroup', | |
assign({ | |
errors: (context, event) => [ | |
{ | |
code: InstanceMachineErrorCode.USER_NOT_ADDED_TO_COGNITO_GROUP, | |
originalError: fromError(event.data), | |
}, | |
...context.errors, | |
], | |
}), | |
], | |
}, | |
}, | |
}, | |
completed: { | |
type: 'final', | |
}, | |
}, | |
}, | |
route53: { | |
initial: 'registeringSubdomain', | |
states: { | |
registeringSubdomain: { | |
invoke: { | |
src: 'registerSubdomain', | |
onDone: { | |
target: 'completed', | |
actions: assign({ | |
successfulJobs: (context) => ['route53', ...context.successfulJobs], | |
}), | |
}, | |
onError: { | |
target: 'completed', | |
actions: assign({ | |
errors: (context, event) => [ | |
{ | |
code: InstanceMachineErrorCode.SUBDOMAIN_REGISTRATION_FAILED, | |
originalError: fromError(event.data), | |
}, | |
...context.errors, | |
], | |
}), | |
}, | |
}, | |
}, | |
completed: { | |
type: 'final', | |
}, | |
}, | |
}, | |
mongo: { | |
initial: 'settingUpMongoDb', | |
states: { | |
settingUpMongoDb: { | |
invoke: { | |
src: 'setUpMongoDb', | |
onDone: { | |
target: 'completed', | |
actions: assign({ | |
successfulJobs: (context) => ['mongodb', ...context.successfulJobs], | |
dbPassword: (_, event) => event.data, | |
}), | |
}, | |
onError: { | |
target: 'completed', | |
actions: assign({ | |
errors: (context, event) => [ | |
{ | |
code: InstanceMachineErrorCode.MONGODB_SETUP_FAILED, | |
originalError: fromError(event.data), | |
}, | |
...context.errors, | |
], | |
}), | |
}, | |
}, | |
}, | |
completed: { | |
type: 'final' | |
}, | |
}, | |
}, | |
}, | |
}, | |
helm: { | |
invoke: { | |
src: 'helmInstall', | |
onDone: { | |
target: 'success', | |
actions: 'setupStatusCake' | |
}, | |
onError: { | |
target: 'failure', | |
actions: [ | |
'cleanupJobs', | |
assign({ | |
errors: (context, event) => [ | |
{ | |
code: InstanceMachineErrorCode.HELM_INSTALL_FAILED, | |
originalError: event.data | |
? event.data.cause | |
? fromError({ | |
...event.data, | |
cause: event.data.cause ? event.data.cause.message : undefined, | |
}) | |
: // a string may come from the service if the db password was somehow not generated | |
fromError(event.data) | |
: null, | |
}, | |
...context.errors, | |
], | |
})], | |
}, | |
}, | |
}, | |
success: { type: 'final' }, | |
failure: { type: 'final' }, | |
}, | |
}, | |
{ | |
guards: { | |
someJobsFailed: (context) => context.errors.length > 0, | |
}, | |
}); | |
const fromError = (error) => (typeof error === 'string' ? error : typeof error === 'object' ? { ...error } : null); | |
const InstanceMachineErrorCode = { | |
SUBDOMAIN_UNAVAILABLE: 'SUBDOMAIN_UNAVAILABLE', | |
SUBDOMAIN_REGISTRATION_FAILED: 'SUBDOMAIN_REGISTRATION_FAILED', | |
COGNITO_GROUP_CREATION_FAILED: 'COGNITO_GROUP_CREATION_FAILED', | |
USER_NOT_ADDED_TO_COGNITO_GROUP: 'USER_NOT_ADDED_TO_COGNITO_GROUP', | |
MONGODB_SETUP_FAILED: 'MONGODB_SETUP_FAILED', | |
HELM_INSTALL_FAILED: 'HELM_INSTALL_FAILED', | |
INSTANCE_DELETE_FAILED: 'INSTANCE_DELETE_FAILED', | |
} | |
createInstanceMachine({ | |
subdomain: "someday", | |
username: "xish" | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment