func()

in internal/pkg/cli/run_local.go [1133:1220]


func (h *hostDiscoverer) rdsHosts(ctx context.Context) ([]orchestrator.Host, error) {
	var hosts []orchestrator.Host

	resources, err := h.rg.GetResourcesByTags(resourcegroups.ResourceTypeRDS, map[string]string{
		deploy.AppTagKey: h.app,
		deploy.EnvTagKey: h.env,
	})
	switch {
	case err != nil:
		return nil, fmt.Errorf("get tagged resources: %w", err)
	case len(resources) == 0:
		return nil, nil
	}

	dbFilter := &rds.Filter{
		Name: aws.String("db-instance-id"),
	}
	clusterFilter := &rds.Filter{
		Name: aws.String("db-cluster-id"),
	}
	for i := range resources {
		// we don't want resources that belong to other services
		// but we do want env level services
		if wkld, ok := resources[i].Tags[deploy.ServiceTagKey]; ok && wkld != h.wkld {
			continue
		}

		arn, err := arn.Parse(resources[i].ARN)
		if err != nil {
			return nil, fmt.Errorf("invalid arn %q: %w", resources[i].ARN, err)
		}

		switch {
		case strings.HasPrefix(arn.Resource, "db:"):
			dbFilter.Values = append(dbFilter.Values, aws.String(resources[i].ARN))
		case strings.HasPrefix(arn.Resource, "cluster:"):
			clusterFilter.Values = append(clusterFilter.Values, aws.String(resources[i].ARN))
		}
	}

	if len(dbFilter.Values) > 0 {
		err = h.rds.DescribeDBInstancesPagesWithContext(ctx, &rds.DescribeDBInstancesInput{
			Filters: []*rds.Filter{dbFilter},
		}, func(out *rds.DescribeDBInstancesOutput, lastPage bool) bool {
			for _, db := range out.DBInstances {
				if db.Endpoint != nil {
					hosts = append(hosts, orchestrator.Host{
						Name: aws.StringValue(db.Endpoint.Address),
						Port: uint16(aws.Int64Value(db.Endpoint.Port)),
					})
				}
			}
			return true
		})
		if err != nil {
			return nil, fmt.Errorf("describe instances: %w", err)
		}
	}

	if len(clusterFilter.Values) > 0 {
		err = h.rds.DescribeDBClustersPagesWithContext(ctx, &rds.DescribeDBClustersInput{
			Filters: []*rds.Filter{clusterFilter},
		}, func(out *rds.DescribeDBClustersOutput, lastPage bool) bool {
			for _, db := range out.DBClusters {
				add := func(s *string) {
					if s != nil {
						hosts = append(hosts, orchestrator.Host{
							Name: aws.StringValue(s),
							Port: uint16(aws.Int64Value(db.Port)),
						})
					}
				}

				add(db.Endpoint)
				add(db.ReaderEndpoint)
				for i := range db.CustomEndpoints {
					add(db.CustomEndpoints[i])
				}
			}
			return true
		})
		if err != nil {
			return nil, fmt.Errorf("describe clusters: %w", err)
		}
	}

	return hosts, nil
}