Commit 301b61c6 authored by Lukas Burgey's avatar Lukas Burgey

Start tracking the states of credentials

Related: #7
parent 6fb287bf
......@@ -30,8 +30,12 @@ STATE_CHOICES = (
(FAILED, 'Failed'),
(REJECTED, 'Rejected'),
)
def questionnaire_default():
return {}
def credential_default():
return {}
......@@ -284,7 +288,6 @@ class NewDeployment(models.Model):
# we have to update them at the clients
def user_credentials_changed(self):
LOGGER.debug('User object of %s changed', self.user)
pass
# TODO implement
# Currently out of scope
......@@ -293,7 +296,6 @@ class NewDeployment(models.Model):
# Currently out of scope
# def user_credential_removed(self, key):
# # TODO implement
# raise Exception('user_credential_removed is currently out of scope and therefore not implemented')
def service_added(self, service):
......@@ -437,12 +439,19 @@ class NewDeploymentStateItem(models.Model):
service=service,
)
item.save()
# start tracking the states of the users ssh keys
for key in parent.user.ssh_keys.all():
CredentialState.get_credential_state(
key,
item,
)
LOGGER.debug(item.msg('created'))
return item
# STATE TRANSITIONS
# user: deployment requested
# user: provisioning requested
def user_deploy(self):
if self.state == REMOVAL_PENDING:
self._set_state(DEPLOYED)
......@@ -457,7 +466,7 @@ class NewDeploymentStateItem(models.Model):
publish=False, # the post response already contains the update
)
# user: removal requested
# user: deprovisioning requested
def user_remove(self):
if (
self.parent.state_target == DEPLOYED
......@@ -487,7 +496,7 @@ class NewDeploymentStateItem(models.Model):
# user: questionnaire answered
def user_answers(self, answers=None):
self.questionnaire = answers
self._set_state('deployment_pending', publish=False)
self._set_state(DEPLOYMENT_PENDING, publish=False)
self.parent.publish_to_client()
# returns None on success, or a string describing an error
......@@ -501,15 +510,22 @@ class NewDeploymentStateItem(models.Model):
# update values
if state == DEPLOYED:
self.credentials = output.get('credentials', {})
self.save()
# the client completed a deployment after the user wished to remove the deployment
if self.parent.state_target == NOT_DEPLOYED:
self.user_remove()
else:
self.credentials = output.get('credentials', {})
self.save()
elif state == NOT_DEPLOYED:
# reset credentials and questionnaire
self._reset()
self.save()
# the client completed a removal after the user wished to deploy the deployment
if self.parent.state_target == DEPLOYED:
self.user_deploy()
elif state == QUESTIONNAIRE:
self.questionnaire = output.get('questionnaire', {})
self.save()
......@@ -523,6 +539,17 @@ class NewDeploymentStateItem(models.Model):
self._set_state(state)
return None
# the credentials which are currently being deployed / removed
def get_onroute_credential_states(self):
return self.credential_states.filter(
state=self.state,
)
def set_onroute_credential_states(self, state):
for credential_state in self.get_onroute_credential_states().all():
credential_state.state = state
credential_state.save()
# resets all client sent values
def _reset(self):
self.credentials = credential_default()
......@@ -533,6 +560,7 @@ class NewDeploymentStateItem(models.Model):
return '[DSItem:{}] {}'.format(self, msg)
def _set_state(self, state, publish=True):
self.set_onroute_credential_states(state)
self.state = state
self.save()
LOGGER.debug(self.msg('state: '+self.state))
......@@ -555,14 +583,6 @@ class NewDeploymentStateItem(models.Model):
class CredentialState(models.Model):
DEPLOYED = 'deployed'
NOT_DEPLOYED = 'not_deployed'
STATE_CHOICES = (
(DEPLOYED, 'Deployed'),
(NOT_DEPLOYED, 'Not deployed'),
)
state = models.CharField(
max_length=50,
choices=STATE_CHOICES,
......@@ -581,3 +601,31 @@ class CredentialState(models.Model):
related_name='credential_states',
on_delete=models.CASCADE,
)
@classmethod
def get_credential_state(cls, credential, target):
try:
return cls.objects.get(
credential=credential,
target=target,
)
except cls.DoesNotExist:
LOGGER.debug('new credential state for %s / %s', credential, target)
new_state = cls(
credential=credential,
target=target,
state=NOT_DEPLOYED,
)
new_state.save()
return new_state
def set(self, state):
self.state = state
self.save()
LOGGER.debug('%s state changed to: %s', self, state)
def __str__(self):
return '{}@{}'.format(
self.credential,
self.target,
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment