Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion stage/pulumi_run_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if regexp.MustCompile(`.+\.ibm\.prestodb\.dev`).MatchString(clusterFQDN) {
if strings.HasSuffix(clusterFQDN, ".ibm.prestodb.dev") {

return p.findPulumiStackFromClusterFQDN(ctx, clusterFQDN)
}

// Check if the cluster FQDN matches the blueray pattern
if regexp.MustCompile(`.+\.cloud\.ibm\.com`).MatchString(clusterFQDN) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if we can use our internal fork?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if regexp.MustCompile(`.+\.cloud\.ibm\.com`).MatchString(clusterFQDN) {
if strings.HasSuffix(clusterFQDN, ".cloud.ibm.com") {

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you actually do not need a context.Context here

// Extract cluster name as the first part of the FQDN (before the first dot)
parts := regexp.MustCompile(`\.`).Split(clusterFQDN, 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you do not need to use regex because you just need to get the part before the dot.

Suggested change
parts := regexp.MustCompile(`\.`).Split(clusterFQDN, 2)
parts := strings.SplitN(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
Expand Down
44 changes: 44 additions & 0 deletions stage/pulumi_run_recorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package stage

import (
"context"
"testing"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can import "github.com/stretchr/testify/assert" and "github.com/stretchr/testify/require" here and use assert/require libraries below. You can check client_test.go as an example.

)

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

context can be removed from findBluerayStackFromClusterFQDN

resource := recorder.findBluerayStackFromClusterFQDN(ctx, testFQDN)

// Verify resource is not nil
if resource == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do

require.NotNil(t, resource)

t.Fatalf("Expected non-nil resource, got nil")
}

// Verify the cluster FQDN
if resource.Outputs.ClusterFQDN != testFQDN {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do

assert.Equal(t, testFQDN, resource.Outputs.ClusterFQDN)

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do

assert.False(t, resource.Created.IsZero())

t.Error("Expected Created timestamp to be non-zero")
}
}