Coverage for ocp_resources/template.py: 0%

48 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1import json 

2 

3from ocp_resources.resource import NamespacedResource, Resource 

4 

5 

6class Template(NamespacedResource): 

7 api_group = NamespacedResource.ApiGroup.TEMPLATE_OPENSHIFT_IO 

8 singular_name = "template" 

9 

10 class Labels: 

11 BASE = f"{NamespacedResource.ApiGroup.TEMPLATE_KUBEVIRT_IO}/type=base" 

12 FLAVOR = "flavor.template.kubevirt.io" 

13 OS = "os.template.kubevirt.io" 

14 WORKLOAD = "workload.template.kubevirt.io" 

15 

16 class Workload: 

17 DESKTOP = "desktop" 

18 HIGHPERFORMANCE = "highperformance" 

19 SERVER = "server" 

20 SAPHANA = "saphana" 

21 

22 class Flavor: 

23 LARGE = "large" 

24 MEDIUM = "medium" 

25 SMALL = "small" 

26 TINY = "tiny" 

27 

28 class Annotations: 

29 DEPRECATED = f"{NamespacedResource.ApiGroup.TEMPLATE_KUBEVIRT_IO}/deprecated" 

30 PROVIDER = f"{NamespacedResource.ApiGroup.TEMPLATE_KUBEVIRT_IO}/provider" 

31 PROVIDER_SUPPORT_LEVEL = f"{NamespacedResource.ApiGroup.TEMPLATE_KUBEVIRT_IO}/provider-support-level" 

32 PROVIDER_URL = f"{NamespacedResource.ApiGroup.TEMPLATE_KUBEVIRT_IO}/provider-url" 

33 

34 class VMAnnotations: 

35 OS = f"{Resource.ApiGroup.VM_KUBEVIRT_IO}/os" 

36 FLAVOR = f"{Resource.ApiGroup.VM_KUBEVIRT_IO}/flavor" 

37 WORKLOAD = f"{Resource.ApiGroup.VM_KUBEVIRT_IO}/workload" 

38 

39 def process(self, client=None, **kwargs): 

40 client = client or self.client 

41 instance_dict = self.instance.to_dict() 

42 params = instance_dict["parameters"] 

43 # filling the template parameters with given kwargs 

44 for param in params: 

45 try: 

46 param["value"] = kwargs[param["name"]] 

47 except KeyError: 

48 continue 

49 instance_dict["parameters"] = params 

50 # namespace label - If not defined, the template is expected to belong to the same namespace as the VM. 

51 instance_namespace = instance_dict["metadata"]["namespace"] 

52 instance_dict["objects"][0]["metadata"]["labels"]["vm.kubevirt.io/template.namespace"] = instance_namespace 

53 

54 instance_json = json.dumps(instance_dict) 

55 body = json.loads(instance_json) 

56 response = client.request( 

57 method="Post", 

58 path=f"/apis/{self.api_version}/namespaces/{instance_namespace}/processedtemplates", 

59 body=body, 

60 ) 

61 return response.to_dict()["objects"] 

62 

63 @staticmethod 

64 def generate_template_labels(os, workload, flavor): 

65 return [ 

66 f"{Template.Labels.OS}/{os}", 

67 (f"{Template.Labels.WORKLOAD}/{getattr(Template.Workload, workload.upper())}"), 

68 f"{Template.Labels.FLAVOR}/{getattr(Template.Flavor, flavor.upper())}", 

69 ]