Best Python code snippet using autotest_python
machine.py
Source:machine.py
1from rest_framework import serializers2from orthos2.data.models import Machine3from .annotation import AnnotationSerializer4from .installation import InstallationSerializer5from .networkinterface import NetworkInterfaceSerializer6from .bmc import BMCSerializer7class NetworkInterfaceListingField(NetworkInterfaceSerializer):8 def to_representation(self, networkinterface):9 result = {}10 for name, field in self.fields.items():11 value = getattr(networkinterface, name)12 result[name] = {'label': field.label, 'value': value}13 return result14class InstallationListingField(InstallationSerializer):15 def to_representation(self, installation):16 result = {}17 for name, field in self.fields.items():18 value = getattr(installation, name)19 result[name] = {'label': field.label, 'value': value}20 return result21class AnnotationListingField(AnnotationSerializer):22 def to_representation(self, annotation):23 result = {}24 for name, field in self.fields.items():25 value = getattr(annotation, str(name))26 if name == 'reporter':27 if value:28 value = value.username29 else:30 value = 'unknown'31 result[name] = {'label': field.label, 'value': value}32 return result33class BMCListingField(BMCSerializer):34 def to_representation(self, bmc):35 result = {}36 for name, field in self.fields.items():37 value = getattr(bmc, str(name))38 result[name] = {'label': field.label, 'value': value}39 return result40class MachineSerializer(serializers.ModelSerializer):41 enclosure = serializers.StringRelatedField()42 system = serializers.StringRelatedField()43 architecture = serializers.StringRelatedField()44 networkinterfaces = NetworkInterfaceListingField(many=True)45 reserved_by = serializers.StringRelatedField()46 installations = InstallationListingField(many=True)47 annotations = AnnotationListingField(many=True)48 status_ipv4 = serializers.SerializerMethodField()49 status_ipv6 = serializers.SerializerMethodField()50 bmc = BMCListingField()51 class Meta:52 model = Machine53 fields = (54 'fqdn',55 'ipv4',56 'ipv6',57 'comment',58 'group',59 'serial_number',60 'product_code',61 'enclosure',62 'nda',63 'system',64 'bmc',65 'bmc_fqdn',66 'bmc_mac',67 'bmc_password',68 'bmc_username',69 'architecture',70 'networkinterfaces',71 'installations',72 'annotations',73 'status_ipv6',74 'status_ipv4',75 'status_ssh',76 'status_login',77 'reserved_by',78 'reserved_reason',79 'reserved_at',80 'reserved_until',81 'cpu_model',82 'cpu_id',83 'cpu_cores',84 'cpu_physical',85 'cpu_threads',86 'cpu_flags',87 'ram_amount',88 'serial_type',89 'serial_console_server',90 'serial_port',91 'serial_command',92 'serial_comment',93 'serial_baud_rate',94 'serial_kernel_device',95 'serial_kernel_device_num',96 'power_type',97 'power_host',98 'power_port',99 'power_comment',100 'location_room',101 'location_rack',102 'location_rack_position'103 )104 serial_type = serializers.CharField(source='serialconsole.stype.name')105 serial_console_server = serializers.CharField(source='serialconsole.console_server')106 serial_port = serializers.IntegerField(source='serialconsole.port')107 serial_command = serializers.CharField(source='serialconsole.command')108 serial_comment = serializers.CharField(source='serialconsole.comment')109 serial_baud_rate = serializers.IntegerField(source='serialconsole.baud_rate')110 serial_kernel_device = serializers.CharField(source='serialconsole.kernel_device')111 serial_kernel_device_num = serializers.IntegerField(source='serialconsole.kernel_device_num')112 power_type = serializers.CharField(source='remotepower.fence_name')113 power_host = serializers.CharField(source='remotepower.remote_power_device')114 power_port = serializers.CharField(source='remotepower.port')115 power_comment = serializers.CharField(source='remotepower.comment')116 bmc_fqdn = serializers.CharField(source='bmc.fqdn')117 bmc_mac = serializers.CharField(source='bmc.mac')118 bmc_username = serializers.CharField(source='bmc.username')119 bmc_password = serializers.SerializerMethodField()120 def get_bmc_password(self, obj):121 if hasattr(self, 'bmc') and self.bmc:122 if self.bmc_password:123 return "***"124 return "-"125 location_room = serializers.CharField(source='enclosure.location_room')126 location_rack = serializers.CharField(source='enclosure.location_rack')127 location_rack_position = serializers.CharField(source='enclosure.location_rack_position')128 group = serializers.CharField(source='group.name')129 def __init__(self, machine, *args, **kwargs):130 super(MachineSerializer, self).__init__(machine, *args, **kwargs)131 if not hasattr(machine, 'group') or not machine.group:132 self.fields.pop('group')133 @property134 def data_info(self):135 result = {}136 # copy dictionary for further manipulation137 data = self.data138 # add keys to exclude list which shouldn't be displayed if value is empty...139 exclude = []140 for key in self.fields.keys():141 if key.startswith('serial_') or key.startswith('power_'):142 exclude.append(key)143 exclude.remove('serial_type')144 exclude.remove('power_type')145 for name, field in self.fields.items():146 if name == 'ipv4':147 field.label = 'IPv4'148 if name == 'ipv6':149 field.label = 'IPv6'150 if name == 'status_ipv4':151 field.label = 'Status IPv4'152 if name == 'status_ipv6':153 field.label = 'Status IPv6'154 # TODO: Adapt this to the new implementation155 # if name == 'power_type' and data[name]:156 # data[name] = RemotePower.Type.to_str(data[name])157 # ... do not add label/values if in exclude list158 if (name in exclude) and (data[name] is None):159 continue160 result[name] = {'label': field.label, 'value': data[name]}161 return result162 def get_status_ipv4(self, obj):163 return obj.get_status_ipv4_display()164 def get_status_ipv6(self, obj):...
tasks.py
Source:tasks.py
...89 data="FFM_ORT"90 result=foreman.Foreman(hostname,port,username,password,False).search_environment(data)91 print result92@operation93def power_host(hostname, port, username, password, power_action, host_id, **kwargs):94 # setting node instance runtime property95 ctx.instance.runtime_properties['hostname'] = hostname96 #ctx.instance.runtime_properties['username'] = username97 #str=some_property1+some_property298 ctx.logger.info("Trying to connect FICO Foreman server.")...
info.py
Source:info.py
1from django.urls import re_path2from django.http import HttpResponseRedirect, JsonResponse3from orthos2.api.commands import BaseAPIView, get_machine4from orthos2.api.serializers.machine import MachineSerializer5from orthos2.api.serializers.misc import ErrorMessage, Serializer6from orthos2.data.models import Machine7class InfoCommand(BaseAPIView):8 METHOD = 'GET'9 URL = '/machine'10 ARGUMENTS = (11 ['fqdn'],12 )13 HELP_SHORT = "Retrieve information about a machine."14 HELP = """Command to get information about a machine.15Usage:16 INFO <fqdn>17Arguments:18 fqdn - FQDN or hostname of the machine.19Example:20 INFO foo.domain.tld21 """22 @staticmethod23 def get_urls():24 return [25 re_path(r'^machine$', InfoCommand.as_view(), name='machine'),26 ]27 @staticmethod28 def get_tabcompletion():29 return list(Machine.api.all().values_list('fqdn', flat=True))30 def get(self, request, *args, **kwargs):31 """Return machine information."""32 fqdn = request.GET.get('fqdn', None)33 response = {}34 try:35 result = get_machine(36 fqdn,37 redirect_to='api:machine',38 data=request.GET39 )40 if isinstance(result, Serializer):41 return result.as_json42 elif isinstance(result, HttpResponseRedirect):43 return result44 machine = result45 except Exception as e:46 return ErrorMessage(str(e)).as_json47 machine.enclosure.fetch_location(machine.pk)48 machine = MachineSerializer(machine)49 order = [50 'fqdn',51 'architecture',52 'ipv4',53 'ipv6',54 'serial_number',55 'product_code',56 'comment',57 'nda',58 None,59 'system',60 'enclosure',61 'group',62 None,63 'location_room',64 'location_rack',65 'location_rack_position',66 None,67 'reserved_by',68 'reserved_reason',69 'reserved_at',70 'reserved_until',71 None,72 'status_ipv4',73 'status_ipv6',74 'status_ssh',75 'status_login',76 None,77 'cpu_model',78 'cpu_id',79 'cpu_physical',80 'cpu_cores',81 'cpu_threads',82 'cpu_flags',83 'ram_amount',84 None,85 'serial_type',86 'serial_cscreen_server',87 'serial_console_server',88 'serial_port',89 'serial_command',90 'serial_comment',91 'serial_baud_rate',92 'serial_kernel_device',93 'serial_kernel_device_num',94 None,95 'power_type',96 'power_host',97 'power_port',98 'power_device',99 'power_comment',100 None,101 'bmc_fqdn',102 'bmc_mac',103 'bmc_username',104 'bmc_password',105 [106 'installations', [107 'distribution',108 'active',109 'partition',110 'architecture',111 'kernelversion'112 ]113 ],114 [115 'networkinterfaces', [116 'mac_address',117 'name',118 'ethernet_type',119 'driver_module',120 'primary',121 ]122 ],123 [124 'annotations', [125 'text',126 'reporter',127 'created'128 ]129 ]130 ]131 response['header'] = {'type': 'INFO', 'order': order}132 response['data'] = machine.data_info...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!