(self)
Fetch the complete infrastructure, generate tags for each monitored resources and store all of that
        into the infrastructure_cache. It also computes the resource `hostname` property to be used when submitting
        metrics for this mor.
Fetch the complete infrastructure, generate tags for each monitored resources and store all of that
        into the infrastructure_cache. It also computes the resource `hostname` property to be used when submitting
        metrics for this mor.
Fetch the complete infrastructure generate tags for each monitored resources and store all of that into the infrastructure_cache . It also computes the resource hostname property to be used when submitting metrics for this mor .
def refresh_infrastructure_cache(self):
        # type: () -> None
        """Fetch the complete infrastructure, generate tags for each monitored resources and store all of that
        into the infrastructure_cache. It also computes the resource `hostname` property to be used when submitting
        metrics for this mor."""
        self.log.debug("Refreshing the infrastructure cache...")
        t0 = Timer()
        infrastructure_data = self.api.get_infrastructure()
        self.gauge(
            "datadog.vsphere.refresh_infrastructure_cache.time",
            t0.total(),
            tags=self._config.base_tags,
            raw=True,
            hostname=self._hostname,
        )
        self.log.debug("Infrastructure cache refreshed in %.3f seconds.", t0.total())
        self.log.debug("Infrastructure cache: %s", infrastructure_data)

        all_tags = {}
        if self._config.should_collect_tags:
            all_tags = self.collect_tags(infrastructure_data)
        self.infrastructure_cache.set_all_tags(all_tags)

        for mor, properties in iteritems(infrastructure_data):
            if not isinstance(mor, tuple(self._config.collected_resource_types)):
                # Do nothing for the resource types we do not collect
                continue

            mor_name = to_string(properties.get("name", "unknown"))
            mor_type_str = MOR_TYPE_AS_STRING[type(mor)]
            hostname = None
            tags = []

            if isinstance(mor, vim.VirtualMachine):
                power_state = properties.get("runtime.powerState")
                if power_state != vim.VirtualMachinePowerState.poweredOn:
                    # Skipping because the VM is not powered on
                    # TODO: Sometimes VM are "poweredOn" but "disconnected" and thus have no metrics
                    self.log.debug("Skipping VM %s in state %s", mor_name, to_string(power_state))
                    continue

                # Hosts are not considered as parents of the VMs they run, we use the `runtime.host` property
                # to get the name of the ESXi host
                runtime_host = properties.get("runtime.host")
                runtime_host_props = {}  # type: InfrastructureDataItem
                if runtime_host:
                    if runtime_host in infrastructure_data:
                        runtime_host_props = infrastructure_data.get(runtime_host, {})
                    else:
                        self.log.debug("Missing runtime.host details for VM %s", mor_name)
                runtime_hostname = to_string(runtime_host_props.get("name", "unknown"))
                tags.append('vsphere_host:{}'.format(runtime_hostname))

                if self._config.use_guest_hostname:
                    hostname = properties.get("guest.hostName", mor_name)
                else:
                    hostname = mor_name
            elif isinstance(mor, vim.HostSystem):
                hostname = mor_name
            else:
                tags.append('vsphere_{}:{}'.format(mor_type_str, mor_name))

            parent = properties.get('parent')
            runtime_host = properties.get('runtime.host')
            if parent is not None:
                tags.extend(get_tags_recursively(parent, infrastructure_data, self._config))
            if runtime_host is not None:
                tags.extend(
                    get_tags_recursively(
                        runtime_host, infrastructure_data, self._config, include_only=['vsphere_cluster']
                    )
                )
            tags.append('vsphere_type:{}'.format(mor_type_str))

            # Attach tags from fetched attributes.
            tags.extend(properties.get('attributes', []))

            resource_tags = self.infrastructure_cache.get_mor_tags(mor) + tags
            if not is_resource_collected_by_filters(
                mor,
                infrastructure_data,
                self._config.resource_filters,
                resource_tags,
            ):
                # The resource does not match the specified whitelist/blacklist patterns.
                self.log.debug(
                    "Skipping resource not matched by filters. resource=`%s` tags=`%s`", mor_name, resource_tags
                )
                continue

            mor_payload = {"tags": tags}  # type: Dict[str, Any]

            if hostname:
                mor_payload['hostname'] = hostname

            self.infrastructure_cache.set_mor_props(mor, mor_payload)
def refresh_infrastructure_cache ( self ) : # type: () -> None self . log . debug ( "Refreshing the infrastructure cache..." ) t0 = Timer ( ) infrastructure_data = self . api . get_infrastructure ( ) self . gauge ( "datadog.vsphere.refresh_infrastructure_cache.time" , t0 . total ( ) , tags = self . _config . base_tags , raw = True , hostname = self . _hostname , ) self . log . debug ( "Infrastructure cache refreshed in %.3f seconds." , t0 . total ( ) ) self . log . debug ( "Infrastructure cache: %s" , infrastructure_data ) all_tags = { } if self . _config . should_collect_tags : all_tags = self . collect_tags ( infrastructure_data ) self . infrastructure_cache . set_all_tags ( all_tags ) for mor , properties in iteritems ( infrastructure_data ) : if not isinstance ( mor , tuple ( self . _config . collected_resource_types ) ) : # Do nothing for the resource types we do not collect continue mor_name = to_string ( properties . get ( "name" , "unknown" ) ) mor_type_str = MOR_TYPE_AS_STRING [ type ( mor ) ] hostname = None tags = [ ] if isinstance ( mor , vim . VirtualMachine ) : power_state = properties . get ( "runtime.powerState" ) if power_state != vim . VirtualMachinePowerState . poweredOn : # Skipping because the VM is not powered on # TODO: Sometimes VM are "poweredOn" but "disconnected" and thus have no metrics self . log . debug ( "Skipping VM %s in state %s" , mor_name , to_string ( power_state ) ) continue # Hosts are not considered as parents of the VMs they run, we use the `runtime.host` property # to get the name of the ESXi host runtime_host = properties . get ( "runtime.host" ) runtime_host_props = { } # type: InfrastructureDataItem if runtime_host : if runtime_host in infrastructure_data : runtime_host_props = infrastructure_data . get ( runtime_host , { } ) else : self . log . debug ( "Missing runtime.host details for VM %s" , mor_name ) runtime_hostname = to_string ( runtime_host_props . get ( "name" , "unknown" ) ) tags . append ( 'vsphere_host:{}' . format ( runtime_hostname ) ) if self . _config . use_guest_hostname : hostname = properties . get ( "guest.hostName" , mor_name ) else : hostname = mor_name elif isinstance ( mor , vim . HostSystem ) : hostname = mor_name else : tags . append ( 'vsphere_{}:{}' . format ( mor_type_str , mor_name ) ) parent = properties . get ( 'parent' ) runtime_host = properties . get ( 'runtime.host' ) if parent is not None : tags . extend ( get_tags_recursively ( parent , infrastructure_data , self . _config ) ) if runtime_host is not None : tags . extend ( get_tags_recursively ( runtime_host , infrastructure_data , self . _config , include_only = [ 'vsphere_cluster' ] ) ) tags . append ( 'vsphere_type:{}' . format ( mor_type_str ) ) # Attach tags from fetched attributes. tags . extend ( properties . get ( 'attributes' , [ ] ) ) resource_tags = self . infrastructure_cache . get_mor_tags ( mor ) + tags if not is_resource_collected_by_filters ( mor , infrastructure_data , self . _config . resource_filters , resource_tags , ) : # The resource does not match the specified whitelist/blacklist patterns. self . log . debug ( "Skipping resource not matched by filters. resource=`%s` tags=`%s`" , mor_name , resource_tags ) continue mor_payload = { "tags" : tags } # type: Dict[str, Any] if hostname : mor_payload [ 'hostname' ] = hostname self . infrastructure_cache . set_mor_props ( mor , mor_payload )