func()

in internal/system/clouddiscovery/cloud_discovery.go [155:221]


func (d *CloudDiscovery) DiscoverComputeResources(ctx context.Context, parentResource *spb.SapDiscovery_Resource, parentNetwork string, hostList []string, cp *ipb.CloudProperties) []*spb.SapDiscovery_Resource {
	log.CtxLogger(ctx).Debugw("DiscoverComputeResources called", "parent", parentResource, "hostList", hostList)
	var res []*spb.SapDiscovery_Resource
	var uris []string
	var discoverQueue []toDiscover
	var region string
	if cp.GetZone() != "" {
		region = regionFromZone(cp.GetZone())
	}
	for _, h := range hostList {
		discoverQueue = append(discoverQueue, toDiscover{
			name:    h,
			region:  region,
			network: parentNetwork,
			parent:  parentResource,
		})
	}
	for len(discoverQueue) > 0 {
		var h toDiscover
		h, discoverQueue = discoverQueue[0], discoverQueue[1:]
		if h.name == "" {
			continue
		}
		if slices.Contains(uris, h.name) {
			log.CtxLogger(ctx).Debugw("Already discovered", "h", h.name)
			// Already discovered, ignore
			continue
		}
		r, dis, err := d.discoverResource(ctx, h, cp.GetProjectId())
		if err != nil {
			log.CtxLogger(ctx).Infow("discoverResource error", "err", err, "h", h.name)
			continue
		}
		// If the parent is not an instance, and this resource is, then move the instance properties
		// virtual hostname from the parent to this resource.
		if h.parent != nil && r.ResourceKind == spb.SapDiscovery_Resource_RESOURCE_KIND_INSTANCE &&
			h.parent.ResourceKind != spb.SapDiscovery_Resource_RESOURCE_KIND_INSTANCE &&
			h.parent.GetInstanceProperties().GetVirtualHostname() != "" {
			if r.InstanceProperties == nil {
				r.InstanceProperties = &spb.SapDiscovery_Resource_InstanceProperties{}
			}
			r.InstanceProperties.VirtualHostname = h.parent.GetInstanceProperties().GetVirtualHostname()
			h.parent.InstanceProperties = nil
		}

		if h.name != r.ResourceUri {
			// Only apply a virtual hostn	ame if the name being discovered is not a URI or IP address.
			log.CtxLogger(ctx).Debugw("Checking virtual hostname", "h", h.name)
			if !addressRegex.MatchString(h.name) && !uriRegex.MatchString(h.name) {
				if r.InstanceProperties == nil {
					r.InstanceProperties = &spb.SapDiscovery_Resource_InstanceProperties{}
				}
				log.CtxLogger(ctx).Debugw("Setting virtual hostname on resource", "h", h.name, "r", r)
				r.InstanceProperties.VirtualHostname = h.name
			}
		}
		log.CtxLogger(ctx).Debugw("Adding to queue", "dis", dis, "h", h.name)
		discoverQueue = append(discoverQueue, dis...)
		res = append(res, r)
		uris = append(uris, h.name)
		if h.name != r.ResourceUri {
			uris = append(uris, r.ResourceUri)
		}
	}

	return res
}