Skip to content

Commit

Permalink
Implement text attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasBoulharts authored and Sispheor committed Aug 19, 2021
1 parent a76ddd1 commit 1f1ffe9
Show file tree
Hide file tree
Showing 51 changed files with 1,333 additions and 653 deletions.
6 changes: 6 additions & 0 deletions docs/manual/resource_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Example of resource group:
- Openshift projects
- Openstack tenants

Resource groups also have text attributes to provide some additional information.

### Resource

A resource is an instance of a `resource group` definition.
Expand Down Expand Up @@ -128,6 +130,7 @@ We link as well the pending instance(given by `squest.instance.id`) to this reso
vm_name: "test-vm"
vm_vcpu: 4
vm_memory: 16
desc: "My description"

tasks:
- name: Print info sent by Squest
Expand All @@ -151,6 +154,9 @@ We link as well the pending instance(given by `squest.instance.id`) to this reso
value: "{{ vm_vcpu }}"
- name: "Memory"
value: "{{ vm_memory }}"
text_attributes:
- name: "Description"
value: "{{ desc }}"
force_basic_auth: yes
status_code: 201
body_format: json
Expand Down
23 changes: 21 additions & 2 deletions playbook_examples/add_resource_in_resource_group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

vars:
squest_api: "http://127.0.0.1:8000/api/"
resource_group_vm_id: 8
resource_group_name: "OCP Worker node"
squest: # this would be the sent data from squest as extra vars
request:
instance:
Expand All @@ -17,6 +17,7 @@
vm_name: "test-vm"
vm_vcpu: 4
vm_memory: 16
desc: "My description"

tasks:
- name: Print info sent by Squest
Expand All @@ -26,6 +27,21 @@
# -----------------------
# PLACE HERE ALL THE MAGIC TO CREATE THE RESOURCE
# -----------------------
- name: Get resource group id
uri:
url: "{{ squest_api }}resource_tracker/resource_group/"
user: "admin"
password: "admin"
method: GET
force_basic_auth: yes
status_code: 200
body_format: json
register: output

- name: Setting host facts using complex arguments
set_fact:
resource_group_vm_id: "{{ output.json | selectattr('name', 'equalto', resource_group_name ) | map(attribute='id') | list | first }}"

- name: Create a resource in squest
uri:
url: "{{ squest_api }}resource_tracker/resource_group/{{ resource_group_vm_id }}/resources/"
Expand All @@ -40,6 +56,9 @@
value: "{{ vm_vcpu }}"
- name: "Memory"
value: "{{ vm_memory }}"
text_attributes:
- name: "Comments"
value: "{{ desc }}"
force_basic_auth: yes
status_code: 201
body_format: json
body_format: json
1 change: 0 additions & 1 deletion profiles/forms/billing_group_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

class BillingGroupForm(forms.ModelForm):
name = forms.CharField(label="Name",
required=True,
widget=forms.TextInput(attrs={'class': 'form-control'})
)

Expand Down
1 change: 0 additions & 1 deletion profiles/forms/group_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def __init__(self, *args, **kwargs):

class GroupForm(forms.ModelForm):
name = forms.CharField(label="Name",
required=True,
widget=forms.TextInput(attrs={'class': 'form-control'})
)

Expand Down
64 changes: 56 additions & 8 deletions resource_tracker/api/serializers/resource_group_serializer.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
from rest_framework import serializers
from rest_framework.relations import PrimaryKeyRelatedField

from resource_tracker.models import ResourceGroup, Resource, ResourceAttribute, ResourceGroupAttributeDefinition
from resource_tracker.models import ResourceGroup, Resource, ResourceAttribute, ResourceTextAttribute, \
ResourceGroupAttributeDefinition, ResourceGroupTextAttributeDefinition
from service_catalog.models import Instance


class ResourceGroupAttributeDefinitionSerializer(serializers.ModelSerializer):

class Meta:
model = ResourceGroupAttributeDefinition
fields = ["id", "name"]


class ResourceGroupTextAttributeDefinitionSerializer(serializers.ModelSerializer):
class Meta:
model = ResourceGroupTextAttributeDefinition
fields = ["id", "name"]


class ResourceAttributeSerializer(serializers.ModelSerializer):
class Meta:
model = ResourceAttribute
fields = ["name", "value"]

name = serializers.SerializerMethodField(method_name="get_attribute_name")

@staticmethod
def get_attribute_name(resource_attribute):
return resource_attribute.attribute_type.name


class ResourceTextAttributeSerializer(serializers.ModelSerializer):
class Meta:
model = ResourceAttribute
model = ResourceTextAttribute
fields = ["name", "value"]

name = serializers.SerializerMethodField(method_name="get_text_attribute_name")

class ResourceSerializer(serializers.ModelSerializer):
@staticmethod
def get_text_attribute_name(resource_attribute):
return resource_attribute.text_attribute_type.name

attributes = ResourceAttributeSerializer(many=True)

class ResourceSerializer(serializers.ModelSerializer):
class Meta:
model = Resource
fields = ["id", "name", "service_catalog_instance", "attributes"]
fields = ["id", "name", "service_catalog_instance", "attributes", "text_attributes"]

attributes = ResourceAttributeSerializer(many=True)
text_attributes = ResourceTextAttributeSerializer(many=True)

class ResourceGroupSerializer(serializers.ModelSerializer):

class ResourceGroupSerializer(serializers.ModelSerializer):
resources = ResourceSerializer(many=True, read_only=True)

class Meta:
Expand All @@ -58,10 +74,25 @@ def validate_name(self, value):
f"group '{resource_group.name}'")


class ResourceCreateSerializer(serializers.Serializer):
class TextAttributeCreateSerializer(serializers.Serializer):
name = serializers.CharField(max_length=100)
value = serializers.CharField(max_length=500)

def validate_name(self, value):
"""Chek that this name is one the the text attribute"""
resource_group = self.context.get('resource_group')
try:
ResourceGroupTextAttributeDefinition.objects.get(name=value, resource_group_definition=resource_group)
return value
except ResourceGroupTextAttributeDefinition.DoesNotExist:
raise serializers.ValidationError(f"'{value}' is not a valid text attribute of the resource "
f"group '{resource_group.name}'")


class ResourceCreateSerializer(serializers.Serializer):
name = serializers.CharField(max_length=100)
attributes = AttributeCreateSerializer(many=True)
text_attributes = TextAttributeCreateSerializer(many=True)
service_catalog_instance = PrimaryKeyRelatedField(queryset=Instance.objects.all(),
allow_null=True)

Expand All @@ -75,6 +106,16 @@ def validate_attributes(self, attributes):
raise serializers.ValidationError(f"Duplicate attribute '{t[0][1]}'")
return attributes

def validate_text_attributes(self, text_attributes):
seen = set()
for text_attribute in text_attributes:
t = tuple(text_attribute.items())
if t[0][1] not in seen:
seen.add(t[0][1])
else:
raise serializers.ValidationError(f"Duplicate text attribute '{t[0][1]}'")
return text_attributes

def validate_name(self, value):
resource_group = self.context.get('resource_group')
try:
Expand All @@ -98,4 +139,11 @@ def create(self, validated_data):
ResourceAttribute.objects.create(value=attribute.pop('value'),
resource=new_resource,
attribute_type=attribute_type)
text_attributes = validated_data.pop('text_attributes')
for text_attribute in text_attributes:
text_attribute_type = ResourceGroupTextAttributeDefinition.objects.get(name=text_attribute.pop('name'),
resource_group_definition=resource_group)
ResourceTextAttribute.objects.create(value=text_attribute.pop('value'),
resource=new_resource,
text_attribute_type=text_attribute_type)
return new_resource
178 changes: 0 additions & 178 deletions resource_tracker/forms.py

This file was deleted.

Loading

0 comments on commit 1f1ffe9

Please sign in to comment.