From df2bcd2f8ffb55ae06fd7b6589ef87c0ccc3456c Mon Sep 17 00:00:00 2001 From: Ignacio Anaya Date: Thu, 6 Jun 2024 01:00:08 -0300 Subject: [PATCH 1/2] feat: allow env delete timeout customization --- docs/data-sources/env_aws.md | 7 +++++- docs/data-sources/env_azure.md | 12 +++++++++ docs/data-sources/env_gcp.md | 12 +++++++++ docs/data-sources/env_k8s.md | 12 +++++++++ docs/resources/env_aws.md | 11 +++++++- docs/resources/env_azure.md | 9 +++++++ docs/resources/env_gcp.md | 9 +++++++ docs/resources/env_k8s.md | 9 +++++++ .../altinitycloud_env_aws/peering/main.tf | 2 +- go.mod | 1 + go.sum | 2 ++ internal/provider/env/aws/model.go | 10 +++++--- internal/provider/env/aws/resource.go | 8 +++++- internal/provider/env/aws/schema.go | 25 +++++++++++++------ internal/provider/env/azure/model.go | 10 +++++--- internal/provider/env/azure/resource.go | 8 +++++- internal/provider/env/azure/schema.go | 7 ++++++ internal/provider/env/gcp/model.go | 10 +++++--- internal/provider/env/gcp/resource.go | 8 +++++- internal/provider/env/gcp/schema.go | 7 ++++++ internal/provider/env/k8s/model.go | 10 +++++--- internal/provider/env/k8s/resource.go | 9 ++++++- internal/provider/env/k8s/schema.go | 7 ++++++ 23 files changed, 175 insertions(+), 30 deletions(-) diff --git a/docs/data-sources/env_aws.md b/docs/data-sources/env_aws.md index 8edb112..f14a0ac 100644 --- a/docs/data-sources/env_aws.md +++ b/docs/data-sources/env_aws.md @@ -30,7 +30,7 @@ data "altinitycloud_env_aws" "current" { ### Optional -- `cloud_connect` (Boolean) `true` indicates that cloud resources are to be managed via altinity/cloud-connect and `false` means direct management (default `true`). **[IMMUTABLE]** +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) ### Read-Only @@ -40,6 +40,7 @@ data "altinitycloud_env_aws" "current" { Examples: - "10.136.0.0/21" - "172.20.0.0/21" +- `cloud_connect` (Boolean) `true` indicates that cloud resources are to be managed via altinity/cloud-connect and `false` means direct management (default `true`). **[IMMUTABLE]** - `custom_domain` (String) Custom domain. Examples: @@ -82,6 +83,10 @@ data "altinitycloud_env_aws" "current" { - ["us-east-1a", "us-east-1b"] - ["sa-east-1c", "sa-east-1d"] + +### Nested Schema for `timeouts` + + ### Nested Schema for `endpoints` diff --git a/docs/data-sources/env_azure.md b/docs/data-sources/env_azure.md index 91e5c3d..f2448b0 100644 --- a/docs/data-sources/env_azure.md +++ b/docs/data-sources/env_azure.md @@ -28,6 +28,10 @@ data "altinitycloud_env_azure" "current" { Examples: - "acme-staging" (where "acme" is your account name) +### Optional + +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) + ### Read-Only - `cidr` (String) VPC CIDR block from the private IPv4 address ranges as specified in RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). At least /21 required. **[IMMUTABLE]** @@ -77,6 +81,14 @@ data "altinitycloud_env_azure" "current" { Examples: - ["eastus-1", "eastus-2"] + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + + ### Nested Schema for `load_balancers` diff --git a/docs/data-sources/env_gcp.md b/docs/data-sources/env_gcp.md index 79d92af..74fe53b 100644 --- a/docs/data-sources/env_gcp.md +++ b/docs/data-sources/env_gcp.md @@ -28,6 +28,10 @@ data "altinitycloud_env_gcp" "current" { Examples: - "acme-staging" (where "acme" is your account name) +### Optional + +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) + ### Read-Only - `cidr` (String) VPC CIDR block from the private IPv4 address ranges as specified in RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). At least /21 required. **[IMMUTABLE]** @@ -72,6 +76,14 @@ data "altinitycloud_env_gcp" "current" { Examples: - ["us-west1a", "us-west1b"] + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + + ### Nested Schema for `load_balancers` diff --git a/docs/data-sources/env_k8s.md b/docs/data-sources/env_k8s.md index 152c958..81bfca1 100644 --- a/docs/data-sources/env_k8s.md +++ b/docs/data-sources/env_k8s.md @@ -28,6 +28,10 @@ data "altinitycloud_env_k8s" "current" { Examples: - "acme-staging" (where "acme" is your account name) +### Optional + +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) + ### Read-Only - `custom_domain` (String) Custom domain. @@ -68,6 +72,14 @@ data "altinitycloud_env_k8s" "current" { - `skip_deprovision_on_destroy` (Boolean) - `spec_revision` (Number) Spec revision + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + + ### Nested Schema for `custom_node_types` diff --git a/docs/resources/env_aws.md b/docs/resources/env_aws.md index d422359..2d9c56a 100644 --- a/docs/resources/env_aws.md +++ b/docs/resources/env_aws.md @@ -159,7 +159,7 @@ resource "altinitycloud_env_aws" "this" { } peering_connections = [ { - aws_account_id = local.aws_account_id # This only required if the VPC is it not in the same account as the environment. + aws_account_id = local.aws_account_id # This only required if the VPC is not in the same account as the environment. vpc_id = "vpc-xyz" } ] @@ -246,6 +246,7 @@ resource "aws_vpc_peering_connection_accepter" "peer" { - `peering_connections` (Attributes List) AWS environment VPC peering configuration. (see [below for nested schema](#nestedatt--peering_connections)) - `skip_deprovision_on_destroy` (Boolean) - `tags` (Attributes List) Tags to apply to AWS resources. (see [below for nested schema](#nestedatt--tags)) +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) - `zones` (List of String) Explicit list of AWS availability zones. At least 2 required. Examples: @@ -364,6 +365,14 @@ Required: - `key` (String) Name of the key - `value` (String) Value of the key + + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + ## Import Import is supported using the following syntax: diff --git a/docs/resources/env_azure.md b/docs/resources/env_azure.md index 1e44c01..4cd3bcc 100644 --- a/docs/resources/env_azure.md +++ b/docs/resources/env_azure.md @@ -115,6 +115,7 @@ resource "altinitycloud_env_azure" "azure" { - `private_link_service` (Attributes) Azure Private Link service configuration. (see [below for nested schema](#nestedatt--private_link_service)) - `skip_deprovision_on_destroy` (Boolean) - `tags` (Attributes List) Tags to apply to Azure resources. (see [below for nested schema](#nestedatt--tags)) +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) - `zones` (List of String) Explicit list of Azure availability zones. At least 2 required. Examples: @@ -212,6 +213,14 @@ Required: - `key` (String) Name of the key - `value` (String) Value of the key + + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + ## Import Import is supported using the following syntax: diff --git a/docs/resources/env_gcp.md b/docs/resources/env_gcp.md index 7883b67..1baaad0 100644 --- a/docs/resources/env_gcp.md +++ b/docs/resources/env_gcp.md @@ -121,6 +121,7 @@ resource "altinitycloud_env_gcp" "this" { - `maintenance_windows` (Attributes List) List of maintenance windows during which automatic maintenance is permitted. By default updates are applied as soon as they are available. (see [below for nested schema](#nestedatt--maintenance_windows)) - `number_of_zones` (Number) Number of zones where the environment will be available. When set, zones will be set automatically based on your cloud provider (Do not use it together with zones) - `skip_deprovision_on_destroy` (Boolean) +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) - `zones` (List of String) Explicit list of GCP zones. At least 2 required. Examples: - ["us-west1a", "us-west1b"] @@ -200,6 +201,14 @@ Optional: - `enabled` (Boolean) Set to `true` if maintenance window is enabled, `false` otherwise. (default `false`) + + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + ## Import Import is supported using the following syntax: diff --git a/docs/resources/env_k8s.md b/docs/resources/env_k8s.md index ce667d0..df8ff44 100644 --- a/docs/resources/env_k8s.md +++ b/docs/resources/env_k8s.md @@ -136,6 +136,7 @@ resource "altinitycloud_env_k8s" "this" { - `maintenance_windows` (Attributes List) List of maintenance windows during which automatic maintenance is permitted. By default updates are applied as soon as they are available. (see [below for nested schema](#nestedatt--maintenance_windows)) - `metrics` (Attributes) Metrics configuration (see [below for nested schema](#nestedatt--metrics)) - `skip_deprovision_on_destroy` (Boolean) +- `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) ### Read-Only @@ -322,6 +323,14 @@ Optional: - `retention_period_in_days` (Number) Metrics retention period in days (default `30`). + + +### Nested Schema for `timeouts` + +Optional: + +- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. + ## Import Import is supported using the following syntax: diff --git a/examples/resources/altinitycloud_env_aws/peering/main.tf b/examples/resources/altinitycloud_env_aws/peering/main.tf index f631fec..a71438d 100644 --- a/examples/resources/altinitycloud_env_aws/peering/main.tf +++ b/examples/resources/altinitycloud_env_aws/peering/main.tf @@ -29,7 +29,7 @@ resource "altinitycloud_env_aws" "this" { } peering_connections = [ { - aws_account_id = local.aws_account_id # This only required if the VPC is it not in the same account as the environment. + aws_account_id = local.aws_account_id # This only required if the VPC is not in the same account as the environment. vpc_id = "vpc-xyz" } ] diff --git a/go.mod b/go.mod index 7b0bd44..db33e7c 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( require ( github.com/MakeNowJust/heredoc/v2 v2.0.1 + github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 github.com/hashicorp/terraform-plugin-testing v1.8.0 ) diff --git a/go.sum b/go.sum index b8156d4..57a480d 100644 --- a/go.sum +++ b/go.sum @@ -122,6 +122,8 @@ github.com/hashicorp/terraform-plugin-docs v0.19.3 h1:xoxpeIuBfnoGxXY0dTajdj4GjE github.com/hashicorp/terraform-plugin-docs v0.19.3/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA= github.com/hashicorp/terraform-plugin-framework v1.9.0 h1:caLcDoxiRucNi2hk8+j3kJwkKfvHznubyFsJMWfZqKU= github.com/hashicorp/terraform-plugin-framework v1.9.0/go.mod h1:qBXLDn69kM97NNVi/MQ9qgd1uWWsVftGSnygYG1tImM= +github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1 h1:gm5b1kHgFFhaKFhm4h2TgvMUlNzFAtUqlcOWnWPm+9E= +github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1/go.mod h1:MsjL1sQ9L7wGwzJ5RjcI6FzEMdyoBnw+XK8ZnOvQOLY= github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc= github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg= github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co= diff --git a/internal/provider/env/aws/model.go b/internal/provider/env/aws/model.go index c7e4e87..9205f38 100644 --- a/internal/provider/env/aws/model.go +++ b/internal/provider/env/aws/model.go @@ -6,6 +6,7 @@ import ( common "github.com/altinity/terraform-provider-altinitycloud/internal/provider/env/common" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" sdk "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -27,10 +28,11 @@ type AWSEnvResourceModel struct { CloudConnect types.Bool `tfsdk:"cloud_connect"` MaintenanceWindows []common.MaintenanceWindowModel `tfsdk:"maintenance_windows"` - SpecRevision types.Int64 `tfsdk:"spec_revision"` - ForceDestroy types.Bool `tfsdk:"force_destroy"` - ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` - SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + SpecRevision types.Int64 `tfsdk:"spec_revision"` + ForceDestroy types.Bool `tfsdk:"force_destroy"` + ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` + SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } type LoadBalancersModel struct { diff --git a/internal/provider/env/aws/resource.go b/internal/provider/env/aws/resource.go index d7c387b..a3b2e20 100644 --- a/internal/provider/env/aws/resource.go +++ b/internal/provider/env/aws/resource.go @@ -177,8 +177,14 @@ func (r *AWSEnvResource) Delete(ctx context.Context, req resource.DeleteRequest, return } + deleteTimeout, diags := data.Timeouts.Delete(ctx, 60*time.Minute) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Polling to wait for deletion to complete - timeout := time.After(time.Duration(30) * time.Minute) + timeout := time.After(deleteTimeout) ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() diff --git a/internal/provider/env/aws/schema.go b/internal/provider/env/aws/schema.go index cfbca3d..9024788 100644 --- a/internal/provider/env/aws/schema.go +++ b/internal/provider/env/aws/schema.go @@ -6,6 +6,7 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -38,11 +39,14 @@ func (r *AWSEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "peering_connections": getPeeringConnectionsAttribute(false, true, false), "endpoints": getEndpointsAttribute(false, true, false), "tags": getTagsAttribute(false, true, false), - "cloud_connect": cloudConnectAttribute, + "cloud_connect": getCloudConnectAttribute(false, true, true), "spec_revision": common.SpecRevisionAttribute, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } @@ -66,7 +70,7 @@ func (d *AWSEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "peering_connections": getPeeringConnectionsAttribute(false, false, true), "endpoints": getEndpointsAttribute(false, false, true), "tags": getTagsAttribute(false, false, true), - "cloud_connect": cloudConnectAttribute, + "cloud_connect": getCloudConnectAttribute(false, false, true), "spec_revision": common.SpecRevisionAttribute, // these options are not used in data sources, @@ -74,6 +78,7 @@ func (d *AWSEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{}), }, } } @@ -167,13 +172,19 @@ func getEndpointsAttribute(required, optional, computed bool) rschema.ListNested } } -var cloudConnectAttribute = rschema.BoolAttribute{ - Optional: true, - Computed: true, - MarkdownDescription: common.CLOUD_CONNECT_DESCRIPTION, - Default: booldefault.StaticBool(true), +func getCloudConnectAttribute(required, optional, computed bool) rschema.BoolAttribute { + return rschema.BoolAttribute{ + Optional: optional, + Required: required, + Computed: computed, + MarkdownDescription: common.CLOUD_CONNECT_DESCRIPTION, + Default: booldefault.StaticBool(true), + } } +// Optional: true, +// Computed: true, + var endpointAttribute = rschema.NestedAttributeObject{ Attributes: map[string]rschema.Attribute{ "service_name": rschema.StringAttribute{ diff --git a/internal/provider/env/azure/model.go b/internal/provider/env/azure/model.go index fe6088a..5db30ab 100644 --- a/internal/provider/env/azure/model.go +++ b/internal/provider/env/azure/model.go @@ -5,6 +5,7 @@ import ( common "github.com/altinity/terraform-provider-altinitycloud/internal/provider/env/common" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -25,10 +26,11 @@ type AzureEnvResourceModel struct { Tags []common.KeyValueModel `tfsdk:"tags"` PrivateLinkService *PrivateLinkServiceModel `tfsdk:"private_link_service"` - SpecRevision types.Int64 `tfsdk:"spec_revision"` - ForceDestroy types.Bool `tfsdk:"force_destroy"` - ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` - SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + SpecRevision types.Int64 `tfsdk:"spec_revision"` + ForceDestroy types.Bool `tfsdk:"force_destroy"` + ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` + SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } type PrivateLinkServiceModel struct { diff --git a/internal/provider/env/azure/resource.go b/internal/provider/env/azure/resource.go index f526259..80342de 100644 --- a/internal/provider/env/azure/resource.go +++ b/internal/provider/env/azure/resource.go @@ -178,8 +178,14 @@ func (r *AzureEnvResource) Delete(ctx context.Context, req resource.DeleteReques return } + deleteTimeout, diags := data.Timeouts.Delete(ctx, 60*time.Minute) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Polling to wait for deletion to complete - timeout := time.After(time.Duration(30) * time.Minute) + timeout := time.After(deleteTimeout) ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() diff --git a/internal/provider/env/azure/schema.go b/internal/provider/env/azure/schema.go index c47eda5..d3337bf 100644 --- a/internal/provider/env/azure/schema.go +++ b/internal/provider/env/azure/schema.go @@ -6,6 +6,7 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -41,6 +42,9 @@ func (r *AzureEnvResource) Schema(ctx context.Context, req resource.SchemaReques "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } @@ -71,6 +75,9 @@ func (d *AzureEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRe "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } diff --git a/internal/provider/env/gcp/model.go b/internal/provider/env/gcp/model.go index 2216c15..b7da769 100644 --- a/internal/provider/env/gcp/model.go +++ b/internal/provider/env/gcp/model.go @@ -5,6 +5,7 @@ import ( common "github.com/altinity/terraform-provider-altinitycloud/internal/provider/env/common" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -22,10 +23,11 @@ type GCPEnvResourceModel struct { LoadBalancingStrategy types.String `tfsdk:"load_balancing_strategy"` MaintenanceWindows []common.MaintenanceWindowModel `tfsdk:"maintenance_windows"` - SpecRevision types.Int64 `tfsdk:"spec_revision"` - ForceDestroy types.Bool `tfsdk:"force_destroy"` - ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` - SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + SpecRevision types.Int64 `tfsdk:"spec_revision"` + ForceDestroy types.Bool `tfsdk:"force_destroy"` + ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` + SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } type LoadBalancersModel struct { diff --git a/internal/provider/env/gcp/resource.go b/internal/provider/env/gcp/resource.go index 7ea0f7d..dc8360c 100644 --- a/internal/provider/env/gcp/resource.go +++ b/internal/provider/env/gcp/resource.go @@ -178,8 +178,14 @@ func (r *GCPEnvResource) Delete(ctx context.Context, req resource.DeleteRequest, return } + deleteTimeout, diags := data.Timeouts.Delete(ctx, 60*time.Minute) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Polling to wait for deletion to complete - timeout := time.After(time.Duration(30) * time.Minute) + timeout := time.After(deleteTimeout) ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() diff --git a/internal/provider/env/gcp/schema.go b/internal/provider/env/gcp/schema.go index 2ee6e3a..6a0228d 100644 --- a/internal/provider/env/gcp/schema.go +++ b/internal/provider/env/gcp/schema.go @@ -6,6 +6,7 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -36,6 +37,9 @@ func (r *GCPEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } @@ -63,6 +67,9 @@ func (d *GCPEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } diff --git a/internal/provider/env/k8s/model.go b/internal/provider/env/k8s/model.go index 031a6b5..c47960c 100644 --- a/internal/provider/env/k8s/model.go +++ b/internal/provider/env/k8s/model.go @@ -5,6 +5,7 @@ import ( common "github.com/altinity/terraform-provider-altinitycloud/internal/provider/env/common" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -21,10 +22,11 @@ type K8SEnvResourceModel struct { Metrics *MetricsModel `tfsdk:"metrics"` MaintenanceWindows []common.MaintenanceWindowModel `tfsdk:"maintenance_windows"` - SpecRevision types.Int64 `tfsdk:"spec_revision"` - ForceDestroy types.Bool `tfsdk:"force_destroy"` - ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` - SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + SpecRevision types.Int64 `tfsdk:"spec_revision"` + ForceDestroy types.Bool `tfsdk:"force_destroy"` + ForceDestroyClusters types.Bool `tfsdk:"force_destroy_clusters"` + SkipDeprovisionOnDestroy types.Bool `tfsdk:"skip_deprovision_on_destroy"` + Timeouts timeouts.Value `tfsdk:"timeouts"` } type LogsModel struct { diff --git a/internal/provider/env/k8s/resource.go b/internal/provider/env/k8s/resource.go index 8a56050..1716bf6 100644 --- a/internal/provider/env/k8s/resource.go +++ b/internal/provider/env/k8s/resource.go @@ -176,10 +176,17 @@ func (r *K8SEnvResource) Delete(ctx context.Context, req resource.DeleteRequest, return } + deleteTimeout, diags := data.Timeouts.Delete(ctx, 60*time.Minute) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + // Polling to wait for deletion to complete - timeout := time.After(time.Duration(30) * time.Minute) + timeout := time.After(deleteTimeout) ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() + for { select { case <-ctx.Done(): diff --git a/internal/provider/env/k8s/schema.go b/internal/provider/env/k8s/schema.go index fdefb57..97fba79 100644 --- a/internal/provider/env/k8s/schema.go +++ b/internal/provider/env/k8s/schema.go @@ -7,6 +7,7 @@ import ( "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/float64validator" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" @@ -41,6 +42,9 @@ func (r *K8SEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } @@ -67,6 +71,9 @@ func (d *K8SEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), + "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + }), }, } } From aa6b135ba557008e6fa940c7d4c050908fc077a4 Mon Sep 17 00:00:00 2001 From: Ignacio Anaya Date: Thu, 6 Jun 2024 01:08:34 -0300 Subject: [PATCH 2/2] refactor: move timouts attr to common --- docs/data-sources/env_aws.md | 4 ++++ docs/data-sources/env_azure.md | 2 +- docs/data-sources/env_gcp.md | 2 +- docs/data-sources/env_k8s.md | 2 +- docs/resources/env_aws.md | 2 +- docs/resources/env_azure.md | 2 +- docs/resources/env_gcp.md | 2 +- docs/resources/env_k8s.md | 2 +- internal/provider/common/attributes.go | 12 ++++++++++-- internal/provider/env/aws/schema.go | 7 ++----- internal/provider/env/azure/schema.go | 9 ++------- internal/provider/env/gcp/schema.go | 9 ++------- internal/provider/env/k8s/schema.go | 9 ++------- 13 files changed, 29 insertions(+), 35 deletions(-) diff --git a/docs/data-sources/env_aws.md b/docs/data-sources/env_aws.md index f14a0ac..c9e8899 100644 --- a/docs/data-sources/env_aws.md +++ b/docs/data-sources/env_aws.md @@ -86,6 +86,10 @@ data "altinitycloud_env_aws" "current" { ### Nested Schema for `timeouts` +Optional: + +- `delete` (String) Adjust timeout for deleting the environment. + ### Nested Schema for `endpoints` diff --git a/docs/data-sources/env_azure.md b/docs/data-sources/env_azure.md index f2448b0..36e5993 100644 --- a/docs/data-sources/env_azure.md +++ b/docs/data-sources/env_azure.md @@ -86,7 +86,7 @@ data "altinitycloud_env_azure" "current" { Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. diff --git a/docs/data-sources/env_gcp.md b/docs/data-sources/env_gcp.md index 74fe53b..a65a06d 100644 --- a/docs/data-sources/env_gcp.md +++ b/docs/data-sources/env_gcp.md @@ -81,7 +81,7 @@ data "altinitycloud_env_gcp" "current" { Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. diff --git a/docs/data-sources/env_k8s.md b/docs/data-sources/env_k8s.md index 81bfca1..263267e 100644 --- a/docs/data-sources/env_k8s.md +++ b/docs/data-sources/env_k8s.md @@ -77,7 +77,7 @@ data "altinitycloud_env_k8s" "current" { Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. diff --git a/docs/resources/env_aws.md b/docs/resources/env_aws.md index 2d9c56a..ea684a7 100644 --- a/docs/resources/env_aws.md +++ b/docs/resources/env_aws.md @@ -371,7 +371,7 @@ Required: Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. ## Import diff --git a/docs/resources/env_azure.md b/docs/resources/env_azure.md index 4cd3bcc..e27a4a4 100644 --- a/docs/resources/env_azure.md +++ b/docs/resources/env_azure.md @@ -219,7 +219,7 @@ Required: Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. ## Import diff --git a/docs/resources/env_gcp.md b/docs/resources/env_gcp.md index 1baaad0..4f35ce4 100644 --- a/docs/resources/env_gcp.md +++ b/docs/resources/env_gcp.md @@ -207,7 +207,7 @@ Optional: Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. ## Import diff --git a/docs/resources/env_k8s.md b/docs/resources/env_k8s.md index df8ff44..598f905 100644 --- a/docs/resources/env_k8s.md +++ b/docs/resources/env_k8s.md @@ -329,7 +329,7 @@ Optional: Optional: -- `delete` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). Setting a timeout for a Delete operation is only applicable if changes are saved into state before the destroy operation occurs. +- `delete` (String) Adjust timeout for deleting the environment. ## Import diff --git a/internal/provider/common/attributes.go b/internal/provider/common/attributes.go index 751ed1f..ddfbd50 100644 --- a/internal/provider/common/attributes.go +++ b/internal/provider/common/attributes.go @@ -1,14 +1,15 @@ package common import ( + "context" "regexp" - "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" - "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -258,6 +259,13 @@ func GetTagsAttribute(required, optional, computed bool, description string) rsc } } +func GetTimeoutsAttribute(ctx context.Context) rschema.Attribute { + return timeouts.Attributes(ctx, timeouts.Opts{ + Delete: true, + DeleteDescription: "Adjust timeout for deleting the environment.", + }) +} + var PendingDeleteAttribute = rschema.BoolAttribute{ Required: false, Optional: false, diff --git a/internal/provider/env/aws/schema.go b/internal/provider/env/aws/schema.go index 9024788..1eaa347 100644 --- a/internal/provider/env/aws/schema.go +++ b/internal/provider/env/aws/schema.go @@ -6,7 +6,6 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" - "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -44,9 +43,7 @@ func (r *AWSEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } @@ -78,7 +75,7 @@ func (d *AWSEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{}), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } diff --git a/internal/provider/env/azure/schema.go b/internal/provider/env/azure/schema.go index d3337bf..e5cc8e1 100644 --- a/internal/provider/env/azure/schema.go +++ b/internal/provider/env/azure/schema.go @@ -6,7 +6,6 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" - "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -42,9 +41,7 @@ func (r *AzureEnvResource) Schema(ctx context.Context, req resource.SchemaReques "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } @@ -75,9 +72,7 @@ func (d *AzureEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRe "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } diff --git a/internal/provider/env/gcp/schema.go b/internal/provider/env/gcp/schema.go index 6a0228d..9bf03ad 100644 --- a/internal/provider/env/gcp/schema.go +++ b/internal/provider/env/gcp/schema.go @@ -6,7 +6,6 @@ import ( "github.com/MakeNowJust/heredoc/v2" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" - "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" dschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -37,9 +36,7 @@ func (r *GCPEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } @@ -67,9 +64,7 @@ func (d *GCPEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } diff --git a/internal/provider/env/k8s/schema.go b/internal/provider/env/k8s/schema.go index 97fba79..b50162d 100644 --- a/internal/provider/env/k8s/schema.go +++ b/internal/provider/env/k8s/schema.go @@ -7,7 +7,6 @@ import ( "github.com/altinity/terraform-provider-altinitycloud/internal/provider/common" "github.com/altinity/terraform-provider-altinitycloud/internal/provider/modifiers" "github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client" - "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" "github.com/hashicorp/terraform-plugin-framework-validators/float64validator" "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" @@ -42,9 +41,7 @@ func (r *K8SEnvResource) Schema(ctx context.Context, req resource.SchemaRequest, "force_destroy": common.GetForceDestroyAttribute(false, true, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, true, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, true, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } } @@ -71,9 +68,7 @@ func (d *K8SEnvDataSource) Schema(ctx context.Context, req datasource.SchemaRequ "force_destroy": common.GetForceDestroyAttribute(false, false, true), "force_destroy_clusters": common.GetForceDestroyClustersAttribute(false, false, true), "skip_deprovision_on_destroy": common.GetSkipProvisioningOnDestroyAttribute(false, false, true), - "timeouts": timeouts.Attributes(ctx, timeouts.Opts{ - Delete: true, - }), + "timeouts": common.GetTimeoutsAttribute(ctx), }, } }