diff --git a/stage/pulumi_run_recorder.go b/stage/pulumi_run_recorder.go index f6a53b3..3acb369 100644 --- a/stage/pulumi_run_recorder.go +++ b/stage/pulumi_run_recorder.go @@ -163,8 +163,50 @@ func (p *PulumiMySQLRunRecorder) findPulumiStackFromClusterFQDN(ctx context.Cont return nil } +func (p *PulumiMySQLRunRecorder) findStackFromClusterFQDN(ctx context.Context, clusterFQDN string) *PulumiResource { + // Check if the cluster FQDN matches the Presto DB pattern + if regexp.MustCompile(`.+\.ibm\.prestodb\.dev`).MatchString(clusterFQDN) { + return p.findPulumiStackFromClusterFQDN(ctx, clusterFQDN) + } + + // Check if the cluster FQDN matches the blueray pattern + if regexp.MustCompile(`.+\.cloud\.ibm\.com`).MatchString(clusterFQDN) { + return p.findBluerayStackFromClusterFQDN(ctx, clusterFQDN) + } + + // Log if the FQDN doesn't match any known pattern + log.Warn().Str("cluster_fqdn", clusterFQDN).Msg("cluster FQDN does not match any known pattern") + return nil +} + +func (p *PulumiMySQLRunRecorder) findBluerayStackFromClusterFQDN(ctx context.Context, clusterFQDN string) *PulumiResource { + // Extract cluster name as the first part of the FQDN (before the first dot) + parts := regexp.MustCompile(`\.`).Split(clusterFQDN, 2) + if len(parts) < 2 { + log.Error().Str("cluster_fqdn", clusterFQDN).Msg("failed to extract cluster name from Blueray FQDN") + return nil + } + + clusterName := parts[0] + + // Create a PulumiResource with the extracted information + resource := &PulumiResource{ + Type: PulumiResourceTypeStack, + Created: time.Now(), + } + + // Set the outputs + resource.Outputs.ClusterFQDN = clusterFQDN + resource.Outputs.ClusterName = clusterName + + log.Info().Str("cluster_name", clusterName).Str("cluster_fqdn", clusterFQDN). + Msg("extracted cluster information from Blueray FQDN") + + return resource +} + func (p *PulumiMySQLRunRecorder) Start(ctx context.Context, s *Stage) error { - stack := p.findPulumiStackFromClusterFQDN(ctx, s.States.ServerFQDN) + stack := p.findStackFromClusterFQDN(ctx, s.States.ServerFQDN) if stack == nil { log.Info().Msgf("did not find a matching Pulumi stack for %s", s.States.ServerFQDN) return nil diff --git a/stage/pulumi_run_recorder_test.go b/stage/pulumi_run_recorder_test.go new file mode 100644 index 0000000..426c31c --- /dev/null +++ b/stage/pulumi_run_recorder_test.go @@ -0,0 +1,44 @@ +package stage + +import ( + "context" + "testing" +) + +func TestFindBluerayStackFromClusterFQDN(t *testing.T) { + // Create a recorder with minimal initialization for testing + recorder := &PulumiMySQLRunRecorder{} + + // Test FQDN + testFQDN := "xlarge-b109n-yabin-eng.k9b9rz3nk2.staging.cvpc.lakehouse.test.cloud.ibm.com" + expectedClusterName := "xlarge-b109n-yabin-eng" + + // Call the function + ctx := context.Background() + resource := recorder.findBluerayStackFromClusterFQDN(ctx, testFQDN) + + // Verify resource is not nil + if resource == nil { + t.Fatalf("Expected non-nil resource, got nil") + } + + // Verify the cluster FQDN + if resource.Outputs.ClusterFQDN != testFQDN { + t.Errorf("Expected ClusterFQDN to be %q, got %q", testFQDN, resource.Outputs.ClusterFQDN) + } + + // Verify the cluster name + if resource.Outputs.ClusterName != expectedClusterName { + t.Errorf("Expected ClusterName to be %q, got %q", expectedClusterName, resource.Outputs.ClusterName) + } + + // Verify the resource type + if resource.Type != PulumiResourceTypeStack { + t.Errorf("Expected Type to be %q, got %q", PulumiResourceTypeStack, resource.Type) + } + + // Verify Created timestamp is not zero + if resource.Created.IsZero() { + t.Error("Expected Created timestamp to be non-zero") + } +}