Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure status optional for name #82

Merged
merged 7 commits into from
Dec 2, 2024
Merged
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
91 changes: 46 additions & 45 deletions cloud/resource_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,14 @@ func resourcePulsarCluster() *schema.Resource {
// Auto generate the name, so we don't need to check the diff.
return nil
}
if diff.HasChanges([]string{"organization", "name", "instance_name", "location", "pool_member_name"}...) {
if diff.HasChanges([]string{"organization", "name", "instance_name", "location", "pool_member_name", "release_channel"}...) {
return fmt.Errorf("ERROR_UPDATE_PULSAR_CLUSTER: " +
"The pulsar cluster organization, name, instance_name, location, pool_member_name does not support updates, please recreate it")
}
return nil
},
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
organizationCluster := strings.Split(d.Id(), "/")
_ = d.Set("organization", organizationCluster[0])
_ = d.Set("name", organizationCluster[1])
err := resourcePulsarClusterRead(ctx, d, meta)
if err.HasError() {
return nil, fmt.Errorf("import %q: %s", d.Id(), err[0].Summary)
Expand Down Expand Up @@ -404,7 +401,15 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
if displayName != "" {
pulsarCluster.Spec.DisplayName = displayName
}
if pulsarInstance.Spec.Type == "serverless" {
if pulsarInstance.IsServerless() {
if computeUnit != 0.5 {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_PULSAR_CLUSTER: " +
"compute_unit must be 0.5 for serverless instance"))
}
if brokerReplicas != 2 {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_PULSAR_CLUSTER: " +
"broker_replicas must be 2 for serverless instance"))
}
pulsarCluster.Annotations = map[string]string{
"cloud.streamnative.io/type": "serverless",
}
Expand All @@ -421,10 +426,6 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
if !ursaEnabled && !pulsarInstance.IsServerless() {
pulsarCluster.Spec.BookKeeper = bookkeeper
}
if displayName == "" && name == "" {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_PULSAR_CLUSTER: " +
"either name or display_name must be provided"))
}
if pool_member_name != "" {
pulsarCluster.Spec.PoolMemberRef = cloudv1alpha1.PoolMemberReference{
Name: pool_member_name,
Expand All @@ -442,7 +443,7 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
}
}
}
if pulsarInstance.Spec.Type == cloudv1alpha1.PulsarInstanceTypeStandard {
if pulsarInstance.Spec.Type != cloudv1alpha1.PulsarInstanceTypeServerless && !pulsarInstance.IsUsingUrsaEngine() {
getPulsarClusterChanged(ctx, pulsarCluster, d)
}
pc, err := clientSet.CloudV1alpha1().PulsarClusters(namespace).Create(ctx, pulsarCluster, metav1.CreateOptions{
Expand All @@ -451,8 +452,7 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_CREATE_PULSAR_CLUSTER: %w", err))
}
d.Set("name", pc.Name)
d.Set("display_name", pc.Spec.DisplayName)
d.SetId(fmt.Sprintf("%s/%s", pc.Namespace, pc.Name))
if pc.Status.Conditions != nil {
ready := false
for _, condition := range pc.Status.Conditions {
Expand Down Expand Up @@ -484,36 +484,23 @@ func resourcePulsarClusterCreate(ctx context.Context, d *schema.ResourceData, me
func resourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
namespace := d.Get("organization").(string)
name := d.Get("name").(string)
displayName := d.Get("display_name").(string)
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_PULSAR_CLUSTER: %w", err))
}
var pulsarCluster *cloudv1alpha1.PulsarCluster
if name != "" {
pulsarCluster, err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
}
} else {
pulsarClusters, err := clientSet.CloudV1alpha1().PulsarClusters(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_LIST_PULSAR_CLUSTER: %w", err))
}
for _, cluster := range pulsarClusters.Items {
if cluster.Spec.DisplayName == displayName {
pulsarCluster = &cluster
break
}
}
if pulsarCluster == nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: "+
"the pulsar cluster with display_name %s does not exist", displayName))
if name == "" {
organizationCluster := strings.Split(d.Id(), "/")
name = organizationCluster[1]
namespace = organizationCluster[0]
}
pulsarCluster, err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
}
_ = d.Set("ready", "False")
if pulsarCluster.Status.Conditions != nil {
Expand Down Expand Up @@ -610,10 +597,10 @@ func resourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta

func resourcePulsarClusterUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
serverless := d.Get("type")
if serverless == cloudv1alpha1.PulsarInstanceTypeServerless {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_PULSAR_CLUSTER: "+
"updating a cluster under instance of type '%s' is no longer allowed",
cloudv1alpha1.PulsarInstanceTypeServerless))
displayNameChanged := d.HasChange("display_name")
if !displayNameChanged && serverless == string(cloudv1alpha1.PulsarInstanceTypeServerless) {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_PULSAR_CLUSTER: " +
"only display_name can be updated for serverless instance"))
}
if d.HasChange("organization") {
return diag.FromErr(fmt.Errorf("ERROR_UPDATE_PULSAR_CLUSTER: " +
Expand Down Expand Up @@ -641,10 +628,20 @@ func resourcePulsarClusterUpdate(ctx context.Context, d *schema.ResourceData, me
}
namespace := d.Get("organization").(string)
name := d.Get("name").(string)
if d.Get("type") == cloudv1alpha1.PulsarInstanceTypeServerless {
organizationCluster := strings.Split(d.Id(), "/")
namespace = organizationCluster[0]
name = organizationCluster[1]
}
clientSet, err := getClientSet(getFactoryFromMeta(meta))
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_READ_PULSAR_CLUSTER: %w", err))
}
if name == "" {
organizationCluster := strings.Split(d.Id(), "/")
name = organizationCluster[1]
namespace = organizationCluster[0]
}
pulsarCluster, err := clientSet.CloudV1alpha1().PulsarClusters(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return diag.FromErr(fmt.Errorf("ERROR_READ_PULSAR_CLUSTER: %w", err))
Expand Down Expand Up @@ -672,10 +669,14 @@ func resourcePulsarClusterUpdate(ctx context.Context, d *schema.ResourceData, me
int64(storageUnit*8*1024*1024*1024), resource.DecimalSI)
}
changed := getPulsarClusterChanged(ctx, pulsarCluster, d)
if displayNameChanged {
displayName := d.Get("display_name").(string)
pulsarCluster.Spec.DisplayName = displayName
}
if d.HasChange("bookie_replicas") ||
d.HasChange("broker_replicas") ||
d.HasChange("compute_unit") ||
d.HasChange("storage_unit") || changed {
d.HasChange("storage_unit") || changed || displayNameChanged {
_, err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Update(ctx, pulsarCluster, metav1.UpdateOptions{
FieldManager: "terraform-update",
})
Expand Down Expand Up @@ -708,11 +709,11 @@ func resourcePulsarClusterDelete(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(fmt.Errorf("ERROR_INIT_CLIENT_ON_DELETE_PULSAR_CLUSTER: %w", err))
}
namespace := d.Get("organization").(string)
t := d.Get("type")
name := d.Get("name").(string)
if t == cloudv1alpha1.PulsarInstanceTypeServerless {
id := strings.Split(d.Id(), "/")
name = id[1]
if name == "" {
organizationCluster := strings.Split(d.Id(), "/")
name = organizationCluster[1]
namespace = organizationCluster[0]
}
err = clientSet.CloudV1alpha1().PulsarClusters(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion examples/pulsarclusters/hosted.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ resource "streamnative_pulsar_cluster" "test-cluster-1" {
}
}

locals {
depends_on = [streamnative_pulsar_cluster.test-cluster-1]
nsCluster = split("/", streamnative_pulsar_cluster.test-cluster-1.id)
}

data "streamnative_pulsar_cluster" "test-cluster-1" {
depends_on = [streamnative_pulsar_cluster.test-cluster-1]
organization = streamnative_pulsar_cluster.test-cluster-1.organization
name = streamnative_pulsar_cluster.test-cluster-1.name
name = local.nsCluster[1]
}

output "pulsar_cluster_hosted" {
Expand Down
8 changes: 6 additions & 2 deletions examples/serverless/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,17 @@ resource "streamnative_pulsar_cluster" "test-serverless" {
display_name = "test-serverless"
instance_name = streamnative_pulsar_instance.test-serverless.name
location = "us-east-2"
release_channel = "rapid"
}

locals {
depends_on = [streamnative_pulsar_cluster.test-serverless]
nsCluster = split("/", streamnative_pulsar_cluster.test-serverless.id)
}

data "streamnative_pulsar_cluster" "test-serverless" {
depends_on = [streamnative_pulsar_cluster.test-serverless]
organization = streamnative_pulsar_cluster.test-serverless.organization
name = streamnative_pulsar_cluster.test-serverless.name
name = local.nsCluster[1]
}

output "pulsar_cluster_serverless" {
Expand Down