Changes in posting constraints due to rescoping to OCP/K8S-only

- node_state is now read only
- node_state gets set automatically to Installed in the create view
- raise a validation error when creating on non-K8S
- allow SystemAdministrator the 'add' permission for Instances
- expose the new listener_port field
This commit is contained in:
Jeff Bradberry
2022-07-28 14:29:31 -04:00
parent 350efc12f5
commit e4518f7b13
3 changed files with 41 additions and 48 deletions

View File

@@ -4877,40 +4877,40 @@ class InstanceSerializer(BaseSerializer):
percent_capacity_remaining = serializers.SerializerMethodField() percent_capacity_remaining = serializers.SerializerMethodField()
jobs_running = serializers.IntegerField(help_text=_('Count of jobs in the running or waiting state that are targeted for this instance'), read_only=True) jobs_running = serializers.IntegerField(help_text=_('Count of jobs in the running or waiting state that are targeted for this instance'), read_only=True)
jobs_total = serializers.IntegerField(help_text=_('Count of all jobs that target this instance'), read_only=True) jobs_total = serializers.IntegerField(help_text=_('Count of all jobs that target this instance'), read_only=True)
ip_address = serializers.IPAddressField(required=False)
class Meta: class Meta:
model = Instance model = Instance
read_only_fields = ('uuid', 'version') read_only_fields = ('ip_address', 'uuid', 'version', 'node_state')
fields = ( fields = (
"id", 'id',
"type", 'type',
"url", 'url',
"related", 'related',
"summary_fields", 'summary_fields',
"uuid", 'uuid',
"hostname", 'hostname',
"created", 'created',
"modified", 'modified',
"last_seen", 'last_seen',
"last_health_check", 'last_health_check',
"errors", 'errors',
'capacity_adjustment', 'capacity_adjustment',
"version", 'version',
"capacity", 'capacity',
"consumed_capacity", 'consumed_capacity',
"percent_capacity_remaining", 'percent_capacity_remaining',
"jobs_running", 'jobs_running',
"jobs_total", 'jobs_total',
"cpu", 'cpu',
"memory", 'memory',
"cpu_capacity", 'cpu_capacity',
"mem_capacity", 'mem_capacity',
"enabled", 'enabled',
"managed_by_policy", 'managed_by_policy',
"node_type", 'node_type',
"node_state", 'node_state',
"ip_address", 'ip_address',
'listener_port',
) )
def get_related(self, obj): def get_related(self, obj):
@@ -4940,30 +4940,20 @@ class InstanceSerializer(BaseSerializer):
else: else:
return float("{0:.2f}".format(((float(obj.capacity) - float(obj.consumed_capacity)) / (float(obj.capacity))) * 100)) return float("{0:.2f}".format(((float(obj.capacity) - float(obj.consumed_capacity)) / (float(obj.capacity))) * 100))
def validate(self, data):
if not self.instance and not settings.IS_K8S:
raise serializers.ValidationError("Can only create instances on Kubernetes or OpenShift.")
return data
def validate_node_type(self, value): def validate_node_type(self, value):
# ensure that new node type is execution node-only
if not self.instance: if not self.instance:
if value not in [Instance.Types.EXECUTION, Instance.Types.HOP]: if value not in [Instance.Types.EXECUTION, Instance.Types.HOP]:
raise serializers.ValidationError('invalid node_type; can only create execution and hop nodes') raise serializers.ValidationError("Can only create execution and hop nodes.")
else: else:
if self.instance.node_type != value: if self.instance.node_type != value:
raise serializers.ValidationError('cannot change node_type') raise serializers.ValidationError("Cannot change node type.")
def validate_node_state(self, value): return value
if not self.instance:
if value not in [Instance.States.PROVISIONING, Instance.States.INSTALLED]:
raise serializers.ValidationError('net new execution node creation must be in installed or provisioning node_state')
else:
if self.instance.node_state != value and value not in [Instance.States.PROVISIONING, Instance.States.INSTALLED, Instance.States.DEPROVISIONING]:
raise serializers.ValidationError('modifying an existing instance can only be in provisioning or deprovisoning node_states')
def validate_peers(self, value):
pass
# 1- dont wanna remove links between two control plane nodes
# 2- can of worms - reversing links
def validate_instance_group(self, value):
pass
class InstanceHealthCheckSerializer(BaseSerializer): class InstanceHealthCheckSerializer(BaseSerializer):

View File

@@ -367,6 +367,9 @@ class InstanceList(ListCreateAPIView):
search_fields = ('hostname',) search_fields = ('hostname',)
ordering = ('id',) ordering = ('id',)
def perform_create(self, serializer):
serializer.save(node_state=models.Instance.States.INSTALLED)
class InstanceDetail(RetrieveUpdateAPIView): class InstanceDetail(RetrieveUpdateAPIView):

View File

@@ -579,7 +579,7 @@ class InstanceAccess(BaseAccess):
return super(InstanceAccess, self).can_unattach(obj, sub_obj, relationship, relationship, data=data) return super(InstanceAccess, self).can_unattach(obj, sub_obj, relationship, relationship, data=data)
def can_add(self, data): def can_add(self, data):
return False return self.user.is_superuser
def can_change(self, obj, data): def can_change(self, obj, data):
return False return False