Ejemplo n.º 1
0
    def list_vms(self, params, resource_group_name):
        azure_vm_connector: AzureVMConnector = self.locator.get_connector('AzureVMConnector')
        azure_vm_connector.set_connect(params['secret_data'])

        vm_manager: AzureVmManager = AzureVmManager(params, azure_vm_connector=azure_vm_connector)
        vms = vm_manager.list_vms(resource_group_name)
        region_name = params['secret_data'].get('region_name')

        if region_name:
            return [vm for vm in vms if vm.location == region_name]

        return vms
Ejemplo n.º 2
0
    def list_all_resources(self, params):
        servers = []
        errors = []

        azure_vm_connector: AzureVMConnector = self.locator.get_connector(
            'AzureVMConnector')
        azure_vm_connector.set_connect(params['secret_data'])

        resource_group = params['resource_group']
        resource_group_name = params['resource_group'].name
        subscription = params['secret_data'].get('subscription_id')

        # call all managers
        vm_manager: AzureVmManager = AzureVmManager(
            params, azure_vm_connector=azure_vm_connector)
        disk_manager: AzureDiskManager = AzureDiskManager(
            params, azure_vm_connector=azure_vm_connector)

        load_balancer_manager: AzureLoadBalancerManager = \
            AzureLoadBalancerManager(params, azure_vm_connector=azure_vm_connector)

        network_security_group_manager: AzureNetworkSecurityGroupManager = \
            AzureNetworkSecurityGroupManager(params, azure_vm_connector=azure_vm_connector)

        nic_manager: AzureNICManager = AzureNICManager(
            params, azure_vm_connector=azure_vm_connector)
        resource_group_manager: AzureResourceGroupManager(
            params, azure_vm_connector=azure_vm_connector)
        # vmss_manager: AzureVMScaleSetManager = AzureVMScaleSetManager(params, azure_vm_connector=azure_vm_connector)
        vnet_manager: AzureVNetManager = AzureVNetManager(
            params, azure_vm_connector=azure_vm_connector)

        meta_manager: MetadataManager = MetadataManager()

        vms = params['vms']

        load_balancers = list(
            azure_vm_connector.list_load_balancers(resource_group_name))
        network_security_groups = list(
            azure_vm_connector.list_network_security_groups(
                resource_group_name))
        network_interfaces = list(
            azure_vm_connector.list_network_interfaces(resource_group_name))
        list_disks = list(azure_vm_connector.list_disk())
        public_ip_addresses = list(
            azure_vm_connector.list_public_ip_address(resource_group_name))
        virtual_networks = list(
            azure_vm_connector.list_virtual_network(resource_group_name))
        # vmss = list(azure_vm_connector.list_virtual_machine_scale_sets(resource_group_name))

        # if vmss:
        #     for scale_set in vmss:
        #         print(scale_set.name)
        #         scale_set_vms = list(azure_vm_connector.list_scale_set_vms(resource_group_name, scale_set.name))
        #         pprint.pprint(scale_set_vms)
        #         for ss in scale_set_vms:
        #             vms.append(ss)
        #         vms.append(scale_set_vms)

        subscription_info = azure_vm_connector.get_subscription_info(
            subscription)

        # tenants_info = azure_vm_connector.list_tenants()

        subscription_data = {
            'subscription_id': subscription_info.subscription_id,
            'subscription_name': subscription_info.display_name,
            'tenant_id': subscription_info.tenant_id
        }

        vm_sizes = []

        for vm in vms:
            try:
                disk_vos = disk_manager.get_disk_info(vm, list_disks)
                nic_vos, primary_ip = nic_manager.get_nic_info(
                    vm, network_interfaces, public_ip_addresses,
                    virtual_networks)

                server_data = vm_manager.get_vm_info(vm, resource_group,
                                                     subscription,
                                                     network_security_groups,
                                                     vm_sizes, primary_ip)

                if load_balancers is not None:
                    lb_vos = load_balancer_manager.get_load_balancer_info(
                        vm, load_balancers, public_ip_addresses)

                nsg_vos = network_security_group_manager.get_network_security_group_info(
                    vm, network_security_groups, network_interfaces)

                nic_name = vm.network_profile.network_interfaces[0].id.split(
                    '/')[-1]

                if nic_name is not None:
                    # vnet_data, subnet_data = vnet_manager.get_vnet_info(nic_name, network_interfaces, virtual_networks)
                    vnet_subnet_dict = vnet_manager.get_vnet_subnet_info(
                        nic_name, network_interfaces, virtual_networks)

                    if vnet_subnet_dict.get('vnet_info'):
                        vnet_data = vnet_subnet_dict['vnet_info']
                    else:
                        vnet_data = None

                    if vnet_subnet_dict.get('subnet_info'):
                        subnet_data = vnet_subnet_dict['subnet_info']
                    else:
                        subnet_data = None

                server_data.update({
                    'disks': disk_vos,
                    'nics': nic_vos,
                    'tags': self.get_tags(vm.tags)
                })

                server_data['data'].update({
                    'load_balancer':
                    lb_vos,
                    'security_group':
                    nsg_vos,
                    'vnet':
                    vnet_data,
                    'subnet':
                    subnet_data,
                    'subscription':
                    Subscription(subscription_data, strict=False),
                    'azure_monitor':
                    Monitor(
                        {
                            'resource_id':
                            f'subscriptions/{subscription}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/virtualMachines/{server_data["name"]}'
                        },
                        strict=False)
                })

                server_data['data']['compute']['account'] = subscription_data[
                    'subscription_name']
                server_data.update({
                    '_metadata':
                    meta_manager.get_metadata(),
                    'reference':
                    ReferenceModel({
                        'resource_id':
                        server_data['data']['compute']['instance_id'],
                        'external_link':
                        f"https://portal.azure.com/#@.onmicrosoft.com/resource/subscriptions/{subscription}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/virtualMachines/{server_data['data']['compute']['instance_name']}/overview"
                    }),
                    'account':
                    subscription_data['subscription_id'],
                    'instance_type':
                    server_data['data']['compute']['instance_type'],
                    'launched_at':
                    datetime_to_iso8601(
                        server_data['data']['compute']['launched_at'])
                })
                server_resource = Server(server_data, strict=False)
                servers.append(
                    ServerResourceResponse({'resource': server_resource}))
            except Exception as e:
                _LOGGER.error(f'[list_instances] [{vm.id}] {e}')

                if type(e) is dict:
                    error_resource_response = ErrorResourceResponse(
                        {'message': json.dumps(e)})
                else:
                    error_resource_response = ErrorResourceResponse({
                        'message':
                        str(e),
                        'resource': {
                            'resource_id': vm.id
                        }
                    })

                errors.append(error_resource_response)

        return servers, errors
Ejemplo n.º 3
0
    def list_all_resources(self, params):
        server_vos = []
        azure_vm_connector: AzureVMConnector = self.locator.get_connector('AzureVMConnector')
        azure_vm_connector.set_connect(params['secret_data'])

        resource_group = params['resource_group']
        resource_group_name = params['resource_group'].name
        subscription = params['secret_data'].get('subscription_id')

        # call all managers
        vm_manager: AzureVmManager = AzureVmManager(params, azure_vm_connector=azure_vm_connector)
        disk_manager: AzureDiskManager = AzureDiskManager(params, azure_vm_connector=azure_vm_connector)

        load_balancer_manager: AzureLoadBalancerManager = \
            AzureLoadBalancerManager(params, azure_vm_connector=azure_vm_connector)

        network_security_group_manager: AzureNetworkSecurityGroupManager = \
            AzureNetworkSecurityGroupManager(params, azure_vm_connector=azure_vm_connector)

        nic_manager: AzureNICManager = AzureNICManager(params, azure_vm_connector=azure_vm_connector)
        resource_group_manager: AzureResourceGroupManager(params, azure_vm_connector=azure_vm_connector)
        # vmss_manager: AzureVMScaleSetManager = AzureVMScaleSetManager(params, azure_vm_connector=azure_vm_connector)
        vnet_manager: AzureVNetManager = AzureVNetManager(params, azure_vm_connector=azure_vm_connector)

        meta_manager: MetadataManager = MetadataManager()

        vms = params['vms']

        load_balancers = list(azure_vm_connector.list_load_balancers(resource_group_name))
        network_security_groups = list(azure_vm_connector.list_network_security_groups(resource_group_name))
        network_interfaces = list(azure_vm_connector.list_network_interfaces(resource_group_name))
        list_disks = list(azure_vm_connector.list_disk())
        public_ip_addresses = list(azure_vm_connector.list_public_ip_address(resource_group_name))
        virtual_networks = list(azure_vm_connector.list_virtual_network(resource_group_name))
        # vmss = list(azure_vm_connector.list_virtual_machine_scale_sets(resource_group_name))

        # if vmss:
        #     for scale_set in vmss:
        #         print(scale_set.name)
        #         scale_set_vms = list(azure_vm_connector.list_scale_set_vms(resource_group_name, scale_set.name))
        #         pprint.pprint(scale_set_vms)
        #         for ss in scale_set_vms:
        #             vms.append(ss)
        #         vms.append(scale_set_vms)

        subscription_info = azure_vm_connector.get_subscription_info(subscription)

        # tenants_info = azure_vm_connector.list_tenants()

        subscription_data = {
            'subscription_id': subscription_info.subscription_id,
            'subscription_name': subscription_info.display_name,
            'tenant_id': subscription_info.tenant_id
        }

        vm_sizes = []

        for vm in vms:
            disk_vos = disk_manager.get_disk_info(vm, list_disks)

            nic_vos, primary_ip = nic_manager.get_nic_info(vm, network_interfaces, public_ip_addresses,
                                                           virtual_networks)

            server_data = vm_manager.get_vm_info(vm, resource_group, subscription, network_security_groups,
                                                 vm_sizes, primary_ip)

            lb_vos = load_balancer_manager.get_load_balancer_info(vm, load_balancers, public_ip_addresses)

            nsg_vos = network_security_group_manager.get_network_security_group_info(vm, network_security_groups,
                                                                                     network_interfaces)

            nic_name = vm.network_profile.network_interfaces[0].id.split('/')[-1]
            vnet_data, subnet_data = vnet_manager.get_vnet_subnet_info(nic_name, network_interfaces, virtual_networks)

            server_data.update({
                'disks': disk_vos,
                'nics': nic_vos
            })

            server_data['data'].update({
                'load_balancer': lb_vos,
                'security_group': nsg_vos,
                'vnet': vnet_data,
                'subnet': subnet_data,
                'subscription': Subscription(subscription_data, strict=False)
            })

            server_data['data']['compute']['account'] = subscription_data['subscription_name']

            server_data.update({
                '_metadata': meta_manager.get_metadata(),
                'reference': ReferenceModel({
                    'resource_id': server_data['data']['compute']['instance_id'],
                    'external_link': f"https://portal.azure.com/#@.onmicrosoft.com/resource/subscriptions/{subscription}/resourceGroups/{resource_group_name}/providers/Microsoft.Compute/virtualMachines/{server_data['data']['compute']['instance_name']}/overview"
                })
            })

            server_vos.append(Server(server_data, strict=False))

        return server_vos