Update API to support setting instances to Deprovisioning

- allow the node_state to be set to deprovisioning
- set the links that touch the instance to removing
- only allow on K8S
- only allow to be done to execution nodes
This commit is contained in:
Jeff Bradberry 2022-08-26 14:27:36 -04:00
parent 0b1891d82a
commit b6946c7e35
2 changed files with 19 additions and 1 deletions

View File

@ -4881,7 +4881,7 @@ class InstanceSerializer(BaseSerializer):
class Meta:
model = Instance
read_only_fields = ('ip_address', 'uuid', 'version', 'node_state')
read_only_fields = ('ip_address', 'uuid', 'version')
fields = (
'id',
'type',
@ -4959,6 +4959,21 @@ class InstanceSerializer(BaseSerializer):
return value
def validate_node_state(self, value):
if self.instance:
if value != self.instance.node_state:
if not settings.IS_K8S:
raise serializers.ValidationError("Can only change the state on Kubernetes or OpenShift.")
if value != Instance.States.DEPROVISIONING:
raise serializers.ValidationError("Can only change instances to the 'deprovisioning' state.")
if self.instance.node_type not in (Instance.Types.EXECUTION,):
raise serializers.ValidationError("Can only deprovision execution nodes.")
else:
if value and value != Instance.States.INSTALLED:
raise serializers.ValidationError("Can only create instances in the 'installed' state.")
return value
class InstanceHealthCheckSerializer(BaseSerializer):
class Meta:

View File

@ -384,6 +384,9 @@ class InstanceDetail(RetrieveUpdateAPIView):
r = super(InstanceDetail, self).update(request, *args, **kwargs)
if status.is_success(r.status_code):
obj = self.get_object()
if obj.node_state == models.Instance.States.DEPROVISIONING:
models.InstanceLink.objects.filter(target=obj).update(link_state=models.InstanceLink.States.REMOVING)
models.InstanceLink.objects.filter(source=obj).update(link_state=models.InstanceLink.States.REMOVING)
obj.set_capacity_value()
obj.save(update_fields=['capacity'])
r.data = serializers.InstanceSerializer(obj, context=self.get_serializer_context()).to_representation(obj)