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

Start tracking the states of credentials

Related: #7
parent 6fb287bf
...@@ -30,8 +30,12 @@ STATE_CHOICES = ( ...@@ -30,8 +30,12 @@ STATE_CHOICES = (
(FAILED, 'Failed'), (FAILED, 'Failed'),
(REJECTED, 'Rejected'), (REJECTED, 'Rejected'),
) )
def questionnaire_default(): def questionnaire_default():
return {} return {}
def credential_default(): def credential_default():
return {} return {}
...@@ -284,7 +288,6 @@ class NewDeployment(models.Model): ...@@ -284,7 +288,6 @@ class NewDeployment(models.Model):
# we have to update them at the clients # we have to update them at the clients
def user_credentials_changed(self): def user_credentials_changed(self):
LOGGER.debug('User object of %s changed', self.user) LOGGER.debug('User object of %s changed', self.user)
pass
# TODO implement # TODO implement
# Currently out of scope # Currently out of scope
...@@ -293,7 +296,6 @@ class NewDeployment(models.Model): ...@@ -293,7 +296,6 @@ class NewDeployment(models.Model):
# Currently out of scope # Currently out of scope
# def user_credential_removed(self, key): # def user_credential_removed(self, key):
# # TODO implement
# raise Exception('user_credential_removed is currently out of scope and therefore not implemented') # raise Exception('user_credential_removed is currently out of scope and therefore not implemented')
def service_added(self, service): def service_added(self, service):
...@@ -437,12 +439,19 @@ class NewDeploymentStateItem(models.Model): ...@@ -437,12 +439,19 @@ class NewDeploymentStateItem(models.Model):
service=service, service=service,
) )
item.save() 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')) LOGGER.debug(item.msg('created'))
return item return item
# STATE TRANSITIONS # STATE TRANSITIONS
# user: deployment requested # user: provisioning requested
def user_deploy(self): def user_deploy(self):
if self.state == REMOVAL_PENDING: if self.state == REMOVAL_PENDING:
self._set_state(DEPLOYED) self._set_state(DEPLOYED)
...@@ -457,7 +466,7 @@ class NewDeploymentStateItem(models.Model): ...@@ -457,7 +466,7 @@ class NewDeploymentStateItem(models.Model):
publish=False, # the post response already contains the update publish=False, # the post response already contains the update
) )
# user: removal requested # user: deprovisioning requested
def user_remove(self): def user_remove(self):
if ( if (
self.parent.state_target == DEPLOYED self.parent.state_target == DEPLOYED
...@@ -487,7 +496,7 @@ class NewDeploymentStateItem(models.Model): ...@@ -487,7 +496,7 @@ class NewDeploymentStateItem(models.Model):
# user: questionnaire answered # user: questionnaire answered
def user_answers(self, answers=None): def user_answers(self, answers=None):
self.questionnaire = answers self.questionnaire = answers
self._set_state('deployment_pending', publish=False) self._set_state(DEPLOYMENT_PENDING, publish=False)
self.parent.publish_to_client() self.parent.publish_to_client()
# returns None on success, or a string describing an error # returns None on success, or a string describing an error
...@@ -501,15 +510,22 @@ class NewDeploymentStateItem(models.Model): ...@@ -501,15 +510,22 @@ class NewDeploymentStateItem(models.Model):
# update values # update values
if state == DEPLOYED: 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: if self.parent.state_target == NOT_DEPLOYED:
self.user_remove() self.user_remove()
else:
self.credentials = output.get('credentials', {})
self.save()
elif state == NOT_DEPLOYED: elif state == NOT_DEPLOYED:
# reset credentials and questionnaire # reset credentials and questionnaire
self._reset() self._reset()
self.save() 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: elif state == QUESTIONNAIRE:
self.questionnaire = output.get('questionnaire', {}) self.questionnaire = output.get('questionnaire', {})
self.save() self.save()
...@@ -523,6 +539,17 @@ class NewDeploymentStateItem(models.Model): ...@@ -523,6 +539,17 @@ class NewDeploymentStateItem(models.Model):
self._set_state(state) self._set_state(state)
return None 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 # resets all client sent values
def _reset(self): def _reset(self):
self.credentials = credential_default() self.credentials = credential_default()
...@@ -533,6 +560,7 @@ class NewDeploymentStateItem(models.Model): ...@@ -533,6 +560,7 @@ class NewDeploymentStateItem(models.Model):
return '[DSItem:{}] {}'.format(self, msg) return '[DSItem:{}] {}'.format(self, msg)
def _set_state(self, state, publish=True): def _set_state(self, state, publish=True):
self.set_onroute_credential_states(state)
self.state = state self.state = state
self.save() self.save()
LOGGER.debug(self.msg('state: '+self.state)) LOGGER.debug(self.msg('state: '+self.state))
...@@ -555,14 +583,6 @@ class NewDeploymentStateItem(models.Model): ...@@ -555,14 +583,6 @@ class NewDeploymentStateItem(models.Model):
class CredentialState(models.Model): class CredentialState(models.Model):
DEPLOYED = 'deployed'
NOT_DEPLOYED = 'not_deployed'
STATE_CHOICES = (
(DEPLOYED, 'Deployed'),
(NOT_DEPLOYED, 'Not deployed'),
)
state = models.CharField( state = models.CharField(
max_length=50, max_length=50,
choices=STATE_CHOICES, choices=STATE_CHOICES,
...@@ -581,3 +601,31 @@ class CredentialState(models.Model): ...@@ -581,3 +601,31 @@ class CredentialState(models.Model):
related_name='credential_states', related_name='credential_states',
on_delete=models.CASCADE, 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