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
20 changes: 18 additions & 2 deletions cloud/data_source_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func dataSourcePulsarCluster() *schema.Resource {
"maintenance_window": {
Type: schema.TypeList,
Computed: true,
Description: "Maintenance window configuration for the Pulsar cluster",
Description: "Maintenance window configuration reported by the control plane for the Pulsar cluster.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"window": {
Expand Down Expand Up @@ -427,7 +427,9 @@ func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, me
_ = d.Set("release_channel", releaseChannel)
}

_ = d.Set("instance_name", pulsarInstance.Name)
if diagErr := setPulsarClusterDataSourceIdentityState(d, pulsarCluster, pulsarInstance); diagErr != nil {
return diagErr
}

// Set lakehouse_storage_enabled
if pulsarInstance.Spec.Type == cloudv1alpha1.PulsarInstanceTypeServerless {
Expand Down Expand Up @@ -500,3 +502,17 @@ func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, me
d.SetId(fmt.Sprintf("%s/%s", pulsarCluster.Namespace, pulsarCluster.Name))
return nil
}

func setPulsarClusterDataSourceIdentityState(
d *schema.ResourceData,
pulsarCluster *cloudv1alpha1.PulsarCluster,
pulsarInstance *cloudv1alpha1.PulsarInstance,
) diag.Diagnostics {
if err := d.Set("instance_name", pulsarInstance.Name); err != nil {
return diag.FromErr(fmt.Errorf("ERROR_SET_INSTANCE_NAME: %w", err))
}
if err := d.Set("location", pulsarCluster.Spec.Location); err != nil {
return diag.FromErr(fmt.Errorf("ERROR_SET_LOCATION: %w", err))
}
return nil
}
56 changes: 56 additions & 0 deletions cloud/pulsar_cluster_customize_diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 StreamNative, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cloud

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClearServerlessLakehouseStorageDiff(t *testing.T) {
resource := &schema.Resource{
Schema: map[string]*schema.Schema{
"lakehouse_storage_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
clearServerlessLakehouseStorageDiff(ctx, diff)
return nil
},
}

state := &terraform.InstanceState{
ID: "org/cluster",
Attributes: map[string]string{
"id": "org/cluster",
"lakehouse_storage_enabled": "true",
},
}
config := terraform.NewResourceConfigRaw(map[string]interface{}{
"lakehouse_storage_enabled": false,
})

instanceDiff, err := resource.SimpleDiff(context.Background(), state, config, nil)
require.NoError(t, err)
assert.Empty(t, instanceDiff.Attributes)
}
147 changes: 147 additions & 0 deletions cloud/pulsar_cluster_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package cloud

import (
"context"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -148,3 +151,147 @@ func TestSetPulsarClusterIdentityStateImportBYOC(t *testing.T) {
assert.Equal(t, "", resourceData.Get("location"))
assert.Equal(t, "pool-member-d", resourceData.Get("pool_member_name"))
}

func TestSetPulsarClusterDataSourceIdentityState(t *testing.T) {
resourceData := dataSourcePulsarCluster().TestResourceData()

cluster := &cloudv1alpha1.PulsarCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-e",
Namespace: "org-e",
},
Spec: cloudv1alpha1.PulsarClusterSpec{
Location: "us-central1",
},
}
instance := &cloudv1alpha1.PulsarInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "instance-e",
Namespace: "org-e",
},
}

diagErr := setPulsarClusterDataSourceIdentityState(resourceData, cluster, instance)
assert.Nil(t, diagErr)
assert.Equal(t, "instance-e", resourceData.Get("instance_name"))
assert.Equal(t, "us-central1", resourceData.Get("location"))
}

func TestValidateMaintenanceWindowAccepted(t *testing.T) {
expected := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "0,1",
Window: &cloudv1alpha1.Window{
StartTime: "02:00",
Duration: &metav1.Duration{Duration: 2 * time.Hour},
},
}

err := validateMaintenanceWindowAccepted(expected, expected.DeepCopy(), "UPDATE")
assert.NoError(t, err)
}

func TestValidateMaintenanceWindowAcceptedWhenDropped(t *testing.T) {
expected := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "0,1",
}

err := validateMaintenanceWindowAccepted(expected, nil, "CREATE")
assert.EqualError(t, err, "ERROR_CREATE_PULSAR_CLUSTER: maintenance_window is not enabled for this organization")
}

func TestExpandMaintenanceWindow(t *testing.T) {
duration := 2 * time.Hour

maintenanceWindow := expandMaintenanceWindow(context.Background(), []interface{}{
map[string]interface{}{
"recurrence": "0,1",
"window": []interface{}{
map[string]interface{}{
"start_time": "02:00",
"duration": "2h0m0s",
},
},
},
})

if assert.NotNil(t, maintenanceWindow) {
assert.Equal(t, "0,1", maintenanceWindow.Recurrence)
if assert.NotNil(t, maintenanceWindow.Window) {
assert.Equal(t, "02:00", maintenanceWindow.Window.StartTime)
if assert.NotNil(t, maintenanceWindow.Window.Duration) {
assert.Equal(t, duration, maintenanceWindow.Window.Duration.Duration)
}
}
}
}

func TestExpandMaintenanceWindowEmpty(t *testing.T) {
assert.Nil(t, expandMaintenanceWindow(context.Background(), nil))
assert.Nil(t, expandMaintenanceWindow(context.Background(), []interface{}{}))
}

func TestMaintenanceWindowSchemaStrictlyManaged(t *testing.T) {
resourceSchema := resourcePulsarCluster().Schema
maintenanceWindowSchema := resourceSchema["maintenance_window"]
if assert.NotNil(t, maintenanceWindowSchema) {
assert.True(t, maintenanceWindowSchema.Optional)
assert.False(t, maintenanceWindowSchema.Computed)
}

maintenanceWindowResource := maintenanceWindowSchema.Elem.(*schema.Resource)
windowSchema := maintenanceWindowResource.Schema["window"]
if assert.NotNil(t, windowSchema) {
assert.True(t, windowSchema.Optional)
assert.False(t, windowSchema.Computed)
}

windowResource := windowSchema.Elem.(*schema.Resource)
startTimeSchema := windowResource.Schema["start_time"]
if assert.NotNil(t, startTimeSchema) {
assert.True(t, startTimeSchema.Optional)
assert.False(t, startTimeSchema.Computed)
}

durationSchema := windowResource.Schema["duration"]
if assert.NotNil(t, durationSchema) {
assert.True(t, durationSchema.Optional)
assert.False(t, durationSchema.Computed)
}

recurrenceSchema := maintenanceWindowResource.Schema["recurrence"]
if assert.NotNil(t, recurrenceSchema) {
assert.True(t, recurrenceSchema.Optional)
assert.False(t, recurrenceSchema.Computed)
}
}

func TestMaintenanceWindowEqual(t *testing.T) {
expected := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "0,1",
Window: &cloudv1alpha1.Window{
StartTime: "02:00",
Duration: &metav1.Duration{Duration: 2 * time.Hour},
},
}

actual := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "0,1",
Window: &cloudv1alpha1.Window{
StartTime: "02:00",
Duration: &metav1.Duration{Duration: 2 * time.Hour},
},
}

assert.True(t, maintenanceWindowEqual(expected, actual))
}

func TestMaintenanceWindowEqualWhenDifferent(t *testing.T) {
expected := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "0,1",
}
actual := &cloudv1alpha1.MaintenanceWindow{
Recurrence: "2,3",
}

assert.False(t, maintenanceWindowEqual(expected, actual))
}
85 changes: 84 additions & 1 deletion cloud/pulsar_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ func TestPulsarClusterRemoveMaintenanceWindow(t *testing.T) {
testCheckPulsarClusterExists("streamnative_pulsar_cluster.test-pulsar-cluster"),
),
},
{
Config: testResourceDataSourcePulsarClusterWithoutConfig(
"sndev",
clusterGeneratedName,
"shared-gcp-prod",
"streamnative",
"us-central1", "rapid"),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}
Expand Down Expand Up @@ -333,6 +343,46 @@ func TestPulsarClusterNoConfigConfigDrift(t *testing.T) {
})
}

func TestPulsarClusterServerlessLakehouseStorageDrift(t *testing.T) {
var clusterGeneratedName = fmt.Sprintf("t-%d-%d", rand.Intn(1000), rand.Intn(100))
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testCheckPulsarClusterDestroy,
Steps: []resource.TestStep{
{
Config: testResourceDataSourcePulsarClusterServerlessWithoutLakehouseStorage(
"sndev",
clusterGeneratedName,
"shared-gcp-prod",
"streamnative",
"us-central1"),
Check: resource.ComposeTestCheckFunc(
testCheckPulsarClusterExists("streamnative_pulsar_cluster.test-pulsar-cluster"),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "organization", "sndev"),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "name", clusterGeneratedName),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "instance_name", clusterGeneratedName),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "location", "us-central1"),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "pool_member_name", ""),
resource.TestCheckResourceAttr("streamnative_pulsar_cluster.test-pulsar-cluster", "lakehouse_storage_enabled", "true"),
),
},
{
Config: testResourceDataSourcePulsarClusterServerlessWithoutLakehouseStorage(
"sndev",
clusterGeneratedName,
"shared-gcp-prod",
"streamnative",
"us-central1"),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}

func testCheckPulsarClusterDestroy(s *terraform.State) error {
time.Sleep(30 * time.Second)
for _, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -517,7 +567,40 @@ data "streamnative_pulsar_cluster" "test-pulsar-cluster" {
organization = streamnative_pulsar_cluster.test-pulsar-cluster.organization
name = streamnative_pulsar_cluster.test-pulsar-cluster.name
}
`, organization, name, poolName, poolNamespace, organization, name, name, location, releaseChannel)
`, organization, name, poolName, poolNamespace, organization, name, name, location, releaseChannel)
}

func testResourceDataSourcePulsarClusterServerlessWithoutLakehouseStorage(
organization,
name,
poolName,
poolNamespace,
location string,
) string {
return fmt.Sprintf(`
provider "streamnative" {
}
resource "streamnative_pulsar_instance" "test-pulsar-instance" {
organization = "%s"
name = "%s"
availability_mode = "zonal"
pool_name = "%s"
pool_namespace = "%s"
type = "serverless"
}
resource "streamnative_pulsar_cluster" "test-pulsar-cluster" {
organization = "%s"
name = "%s"
instance_name = "%s"
location = "%s"
depends_on = [streamnative_pulsar_instance.test-pulsar-instance]
}
data "streamnative_pulsar_cluster" "test-pulsar-cluster" {
depends_on = [streamnative_pulsar_cluster.test-pulsar-cluster]
organization = streamnative_pulsar_cluster.test-pulsar-cluster.organization
name = streamnative_pulsar_cluster.test-pulsar-cluster.name
}
`, organization, name, poolName, poolNamespace, organization, name, name, location)
}

func testResourceDataSourcePulsarClusterWithMaintenanceWindowUpdated(organization, name, poolName, poolNamespace, location, releaseChannel string) string {
Expand Down
Loading
Loading