func()

in internal/system/discovery.go [613:688]


func (d *Discovery) discoverReplicationSite(ctx context.Context, site *sappb.HANAReplicaSite, sid string, instanceResource *spb.SapDiscovery_Resource, instanceSubnetwork string, lbGroups []loadBalancerGroup, cp *ipb.CloudProperties) *spb.SapDiscovery_Component_ReplicationSite {
	siteRes := d.CloudDiscoveryInterface.DiscoverComputeResources(ctx, instanceResource, instanceSubnetwork, []string{site.Name}, cp)
	log.CtxLogger(ctx).Debugw("Site Resources", "site", site.Name, "resources", siteRes)
	repComp := &spb.SapDiscovery_Component{
		Resources: removeDuplicates(siteRes),
		Sid:       sid,
	}

	// Find the region of the instance that the site name corresponds to.
	for _, r := range repComp.Resources {
		if r.GetResourceKind() == spb.SapDiscovery_Resource_RESOURCE_KIND_INSTANCE {
			if r.InstanceProperties == nil {
				r.InstanceProperties = &spb.SapDiscovery_Resource_InstanceProperties{}
			}
			r.InstanceProperties.InstanceRole |= spb.SapDiscovery_Resource_InstanceProperties_INSTANCE_ROLE_DATABASE
			if r.GetInstanceProperties().GetVirtualHostname() == site.Name {
				instanceZone := clouddiscovery.ExtractFromURI(r.GetResourceUri(), "zones")
				regionParts := strings.Split(instanceZone, "-")
				instanceRegion := strings.Join([]string{regionParts[0], regionParts[1]}, "-")
				repComp.Region = instanceRegion
			}
		}
	}

	// Check if this site is part of a known load balancer group
	var lbGroup *loadBalancerGroup
resLoop:
	for _, r := range siteRes {
		if r.ResourceKind != spb.SapDiscovery_Resource_RESOURCE_KIND_INSTANCE {
			continue
		}
		for _, lb := range lbGroups {
			if slices.Contains(lb.instanceURIs, r.GetResourceUri()) {
				log.CtxLogger(ctx).Debugw("Site is part of a load balancer group", "site", site.Name, "lbGroup", lb.lbRes.GetResourceUri())
				lbGroup = &lb
				break resLoop
			}
		}
	}
	if lbGroup == nil {
		log.CtxLogger(ctx).Debugw("Site not part of existing LB group", "site", site.Name)
	}
	for _, childSite := range site.Targets {
		childRepSite := d.discoverReplicationSite(ctx, childSite, sid, instanceResource, instanceSubnetwork, lbGroups, cp)
		inLbGroup := false
		if lbGroup != nil {
			// Check if the child site is part of the same load balancer group
			for _, r := range childRepSite.Component.Resources {
				if r.ResourceKind != spb.SapDiscovery_Resource_RESOURCE_KIND_INSTANCE {
					continue
				}
				if slices.Contains(lbGroup.instanceURIs, r.GetResourceUri()) {
					log.CtxLogger(ctx).Debugw("Child site is part of the same LB group", "site", childSite.Name, "lbGroup", lbGroup.lbRes.GetResourceUri())
					inLbGroup = true
					// Child is part of the same load balancer group, therefore belongs in the same component.
					repComp.Resources = removeDuplicates(append(repComp.Resources, childRepSite.Component.Resources...))
					// Add the child site's replication sites to this component.
					repComp.ReplicationSites = append(repComp.ReplicationSites, childRepSite.Component.ReplicationSites...)
					repComp.HaHosts = append(repComp.HaHosts, lbGroup.instanceURIs...)
					break
				}
			}
		}
		if !inLbGroup {
			log.CtxLogger(ctx).Debugw("Child site not part of the same LB group", "site", childSite.Name)
			// Child is not part of the same load balancer group, therefore belongs in a separate component.
			childRepSite.SourceSite = site.Name

			repComp.ReplicationSites = append(repComp.ReplicationSites, childRepSite)
		}
	}
	log.CtxLogger(ctx).Debugw("Site", "site", site.Name, "component", repComp)
	return &spb.SapDiscovery_Component_ReplicationSite{
		Component: repComp,
	}
}