Best Python code snippet using lisa_python
setup-vpn.py
Source:setup-vpn.py  
...170        request.add_header('Authorization', token)171        request.get_method = lambda: 'GET'172        apiResponse = json.loads(urllib2.urlopen(request).read())173        public_ip_name = apiResponse['properties']['ipConfigurations'][0]['properties']['publicIPAddress']['id'].split('/')[-1]174        return self._get_public_ip_address(public_ip_name)175    def _get_public_ip_address(self, public_ip_name):176        url = 'https://management.azure.com/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Network/publicIPAddresses/{}?api-version=2018-02-01'.format(self.subId, self.rgname, public_ip_name)177        request = urllib2.Request(url)178        token = "Bearer " + self.bearer_token179        request.add_header('Authorization', token)180        request.get_method = lambda: 'GET'181        apiResponse = json.loads(urllib2.urlopen(request).read())182        return apiResponse['properties']['ipAddress']183if __name__ == '__main__':184    # Retrieve all the params passed by the ARM template to the staging VM185    params = {}186    with open('/opt/data.txt','r') as f:187        words = f.read().split()188        for word in words:189            (key, val) = word.split(':')...vm_instance_manager_resource_helper.py
Source:vm_instance_manager_resource_helper.py  
...177            }178        """179        compute_data = {180            'keypair': '',181            'public_ip_address': self._get_public_ip_address(instance),182            'az': zone_info.get('zone', ''),            # zone_name183            'instance_id': instance.get('id'),184            'instance_name': instance.get('name', ''),185            'instance_state': instance.get('status'),186            'instance_type': self._get_instance_type(instance),187            'account': zone_info.get('project_id', ''),188            'image': self._get_images(instance, disks),189            'launched_at': instance.get('creationTimestamp'),190            'tags': self._get_tags_only_string_values(instance)191        }192        return Compute(compute_data)193    def _get_custom_image_type(self, instance, zone_info, instance_types):194        machine = instance.get('machineType', '')195        _machine = machine[machine.rfind('/')+1:]196        custom_image_type = self.instance_conn.get_machine_type(zone_info.get('zone'), _machine)197        instance_types.append(custom_image_type)198        cpu = custom_image_type.get('guestCpus', 0)199        memory = round(float((custom_image_type.get('memoryMb', 0)) / 1024), 2)200        return cpu, memory201    @staticmethod202    def _get_tags_only_string_values(instance):203        tags = {}204        for k, v in instance.get('tags', {}).items():205            if isinstance(v, str):206                tags.update({k: v})207        return tags208    @staticmethod209    def _get_images(instance, disks):210        image = ''211        name = instance.get('name', '')212        for disk in disks:213            if name == disk.get('name', ''):214                _image = disk.get('sourceImage', '')215                image = _image[_image.rfind('/')+1:]216                break217        return image218    @staticmethod219    def _get_instance_type(instance):220        machine_type = instance.get('machineType', '')221        machine_split = machine_type.split('/')222        return machine_split[-1]223    @staticmethod224    def _get_core_and_memory(instance, instance_types):225        machine_type = instance.get('machineType', '')226        _machine = machine_type[machine_type.rfind('/') + 1:]227        cpu = 0228        memory = 0229        for i_type in instance_types:230            if i_type.get('selfLink', '') == machine_type or i_type.get('name', '') == _machine:231                cpu = i_type.get('guestCpus')232                memory = round(float((i_type.get('memoryMb', 0)) / 1024), 2)233                break234        return cpu, memory235    @staticmethod236    def _get_primary_ip_address(instance):237        primary_ip_address = ''238        networks = instance.get('networkInterfaces', [])239        for i, v in enumerate(networks):240            if i == 0:241                primary_ip_address = v.get('networkIP', '')242                break243        return primary_ip_address244    @staticmethod245    def _get_public_ip_address(instance):246        public_ip_address = ''247        networks = instance.get('networkInterfaces', [])248        for i, v in enumerate(networks):249            if i == 0:250                access_configs = v.get('accessConfigs', [])251                for access_config in access_configs:252                    nat_ip = access_config.get('natIP', '')253                    if nat_ip != '':254                        public_ip_address = nat_ip255                        break256                break257        return public_ip_address258    @staticmethod259    def _get_ip_addresses(instance):...vm_instance_manager.py
Source:vm_instance_manager.py  
...171            }172        '''173        compute_data = {174            'keypair': '',175            'public_ip_address': self._get_public_ip_address(instance),176            'az': zone_info.get('zone', ''),            # zone_name177            'instance_id': instance.get('id'),178            'instance_name': instance.get('name', ''),179            'instance_state': instance.get('status'),180            'instance_type': self._get_instance_type(instance),181            'account': zone_info.get('project_id', ''),182            'image': self._get_images(instance, disks),183            'launched_at': instance.get('creationTimestamp'),184            'tags': self._get_tags_only_string_values(instance)185        }186        return Compute(compute_data)187    def get_custom_image_type(self, instance, zone_info, instance_types):188        machine = instance.get('machineType', '')189        _machine = machine[machine.rfind('/')+1:]190        custom_image_type = self.google_connector.get_machine_type(zone_info.get('zone'), _machine)191        instance_types.append(custom_image_type)192        cpu = custom_image_type.get('guestCpus', 0)193        memory = round(float((custom_image_type.get('memoryMb', 0)) / 1024), 2)194        return cpu, memory195    @staticmethod196    def _get_tags_only_string_values(instance):197        tags = {}198        for k, v in instance.get('tags', {}).items():199            if isinstance(v, str):200                tags.update({k:v})201        return tags202    @staticmethod203    def _get_images(instance, disks):204        image = ''205        name = instance.get('name', '')206        for disk in disks:207            if name == disk.get('name', ''):208                _image = disk.get('sourceImage', '')209                image = _image[_image.rfind('/')+1:]210                break211        return image212    @staticmethod213    def _get_instance_type(instance):214        machine_type = instance.get('machineType', '')215        machine_split = machine_type.split('/')216        return machine_split[-1]217    @staticmethod218    def _get_core_and_memory(instance, instance_types):219        machine_type = instance.get('machineType', '')220        _machine = machine_type[machine_type.rfind('/') + 1:]221        cpu = 0222        memory = 0223        for i_type in instance_types:224            if i_type.get('selfLink', '') == machine_type or i_type.get('name', '') == _machine:225                cpu = i_type.get('guestCpus')226                memory = round(float((i_type.get('memoryMb', 0)) / 1024), 2)227                break228        return cpu, memory229    @staticmethod230    def _get_primary_ip_address(instance):231        primary_ip_address = ''232        networks = instance.get('networkInterfaces', [])233        for i, v in enumerate(networks):234            if i == 0:235                primary_ip_address = v.get('networkIP', '')236                break237        return primary_ip_address238    @staticmethod239    def _get_public_ip_address(instance):240        public_ip_address = ''241        networks = instance.get('networkInterfaces', [])242        for i, v in enumerate(networks):243            if i == 0:244                access_configs = v.get('accessConfigs', [])245                for access_config in access_configs:246                    nat_ip = access_config.get('natIP', '')247                    if nat_ip != '':248                        public_ip_address = nat_ip249                        break250                break251        return public_ip_address252    @staticmethod253    def _get_ip_addresses(instance):...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!!
