Skip to content

Commit

Permalink
support resource recreation when 404 occurs (#74)
Browse files Browse the repository at this point in the history
fixing
#70

---------

Co-authored-by: Neng Lu <[email protected]>
  • Loading branch information
freeznet and nlu90 authored Nov 15, 2024
1 parent 9b0406f commit bee665c
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cloud/data_source_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwe"
"github.com/streamnative/terraform-provider-streamnative/cloud/util"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
Expand Down Expand Up @@ -119,6 +120,10 @@ func DataSourceApiKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
}
apiKey, err := clientSet.CloudV1alpha1().APIKeys(organization).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_API_KEY: %w", err))
}
if err = d.Set("organization", apiKey.Namespace); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -120,6 +121,10 @@ func dataSourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData,
}
cloudConnection, err := clientSet.CloudV1alpha1().CloudConnections(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -131,6 +132,10 @@ func dataSourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData,
}
cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -57,6 +58,10 @@ func DataSourcePoolRead(ctx context.Context, d *schema.ResourceData, meta interf
}
pool, err := clientSet.CloudV1alpha1().Pools(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_POOL: %w", err))
}
_ = d.Set("name", pool.Name)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pool_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -69,6 +70,10 @@ func DataSourcePoolMemberRead(ctx context.Context, d *schema.ResourceData, meta
}
poolMember, err := clientSet.CloudV1alpha1().PoolMembers(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_POOL_MEMBER: %w", err))
}
_ = d.Set("name", poolMember.Name)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -261,6 +262,10 @@ func dataSourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, me
}
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")
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pulsar_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -100,6 +101,10 @@ func dataSourcePulsarGatewayRead(ctx context.Context, d *schema.ResourceData, me
}
pg, err := clientSet.CloudV1alpha1().PulsarGateways(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_GATEWAY: %w", err))
}
d.Set("access", pg.Spec.Access)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_pulsar_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -98,6 +99,10 @@ func dataSourcePulsarInstanceRead(ctx context.Context, d *schema.ResourceData, m
}
pulsarInstance, err := clientSet.CloudV1alpha1().PulsarInstances(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_INSTANCE: %w", err))
}
_ = d.Set("ready", "False")
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -76,6 +77,10 @@ func DataSourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, m
}
serviceAccount, err := clientSet.CloudV1alpha1().ServiceAccounts(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT: %w", err))
}
_ = d.Set("name", serviceAccount.Name)
Expand Down
5 changes: 5 additions & 0 deletions cloud/data_source_service_account_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cloud
import (
"context"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -81,6 +82,10 @@ func DataSourceServiceAccountBindingRead(ctx context.Context, d *schema.Resource
}
serviceAccountBinding, err := clientSet.CloudV1alpha1().ServiceAccountBindings(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT_BINDING: %w", err))
}
_ = d.Set("name", serviceAccountBinding.Name)
Expand Down
12 changes: 9 additions & 3 deletions cloud/resource_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
"github.com/streamnative/terraform-provider-streamnative/cloud/util"
"github.com/xhit/go-str2duration/v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"strings"
"time"
)

func resourceApiKey() *schema.Resource {
Expand Down Expand Up @@ -310,6 +312,10 @@ func resourceApiKeyRead(ctx context.Context, d *schema.ResourceData, m interface
}
apiKey, err := clientSet.CloudV1alpha1().APIKeys(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_API_KEY: %w", err))
}
if err = d.Set("organization", apiKey.Namespace); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cloud/resource_cloud_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -251,6 +253,10 @@ func resourceCloudConnectionRead(ctx context.Context, d *schema.ResourceData, me
}
cloudConnection, err := clientSet.CloudV1alpha1().CloudConnections(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_CONNECTION: %w", err))
}

Expand Down
5 changes: 5 additions & 0 deletions cloud/resource_cloud_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
cloudv1alpha1 "github.com/streamnative/cloud-api-server/pkg/apis/cloud/v1alpha1"
cloudclient "github.com/streamnative/cloud-api-server/pkg/client/clientset_generated/clientset"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -266,6 +267,10 @@ func resourceCloudEnvironmentRead(ctx context.Context, d *schema.ResourceData, m
}
cloudEnvironment, err := clientSet.CloudV1alpha1().CloudEnvironments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_CLOUD_ENVIRONMENT: %w", err))
}

Expand Down
8 changes: 7 additions & 1 deletion cloud/resource_pulsar_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ package cloud
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-log/tflog"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -493,6 +495,10 @@ func resourcePulsarClusterRead(ctx context.Context, d *schema.ResourceData, meta
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 {
Expand Down
4 changes: 4 additions & 0 deletions cloud/resource_pulsar_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ func resourcePulsarGatewayRead(ctx context.Context, d *schema.ResourceData, meta

pg, err := clientSet.CloudV1alpha1().PulsarGateways(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_GATEWAY: %w", err))
}
d.SetId(fmt.Sprintf("%s/%s", pg.Namespace, pg.Name))
Expand Down
6 changes: 6 additions & 0 deletions cloud/resource_pulsar_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -203,6 +205,10 @@ func resourcePulsarInstanceRead(ctx context.Context, d *schema.ResourceData, met
}
pulsarInstance, err := clientSet.CloudV1alpha1().PulsarInstances(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_INSTANCE: %w", err))
}
_ = d.Set("ready", "False")
Expand Down
6 changes: 6 additions & 0 deletions cloud/resource_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -191,6 +193,10 @@ func resourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, met
}
serviceAccount, err := clientSet.CloudV1alpha1().ServiceAccounts(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT: %w", err))
}
_ = d.Set("name", serviceAccount.Name)
Expand Down
6 changes: 6 additions & 0 deletions cloud/resource_service_account_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -175,6 +177,10 @@ func resourceServiceAccountBindingRead(ctx context.Context, d *schema.ResourceDa
}
serviceAccountBinding, err := clientSet.CloudV1alpha1().ServiceAccountBindings(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("ERROR_READ_SERVICE_ACCOUNT_BINDING: %w", err))
}
_ = d.Set("name", serviceAccountBinding.Name)
Expand Down
Loading

0 comments on commit bee665c

Please sign in to comment.