Skip to content

Commit

Permalink
feat: added timeout options on apollostudio_sub_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Apr 2, 2024
1 parent 047f331 commit 22e97dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
11 changes: 11 additions & 0 deletions docs/resources/sub_graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "apollostudio_sub_graph" "example" {

### Optional

- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
- `url` (String) The URL of the sub graph endpoint

### Read-Only
Expand All @@ -38,3 +39,13 @@ resource "apollostudio_sub_graph" "example" {
- `id` (String) The ID of the sub graph
- `revision` (String) The revision of the sub graph
- `updated_at` (String) The last update date of the sub graph

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (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).
- `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.
- `read` (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). Read operations occur during any refresh or planning operation when refresh is enabled.
- `update` (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).
53 changes: 45 additions & 8 deletions internal/provider/sub_graph_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"time"

Expand Down Expand Up @@ -33,13 +34,14 @@ type SubGraphResource struct {

// SubGraphResourceModel describes the resource data model.
type SubGraphResourceModel struct {
URL types.String `tfsdk:"url"`
Schema types.String `tfsdk:"schema"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Revision types.String `tfsdk:"revision"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
URL types.String `tfsdk:"url"`
Schema types.String `tfsdk:"schema"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
Revision types.String `tfsdk:"revision"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

func (r *SubGraphResource) Metadata(
Expand All @@ -48,7 +50,7 @@ func (r *SubGraphResource) Metadata(
resp.TypeName = req.ProviderTypeName + "_sub_graph"
}

func (r *SubGraphResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
func (r *SubGraphResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "This resource is used to manage subgraphs within the Federated Apollo schema. " +
"More information about the Apollo Federation subgraphs can be found " +
Expand Down Expand Up @@ -90,6 +92,9 @@ func (r *SubGraphResource) Schema(_ context.Context, _ resource.SchemaRequest, r
Computed: true,
},
},
Blocks: map[string]schema.Block{
"timeouts": timeouts.BlockAll(ctx),
},
}
}

Expand Down Expand Up @@ -126,6 +131,14 @@ func (r *SubGraphResource) Create(ctx context.Context, req resource.CreateReques
return
}

createTimeout, diagErr := plan.Timeouts.Create(ctx, defaultTimeout)
if diagErr.HasError() {
return
}

ctx, cancel := context.WithTimeout(ctx, createTimeout)
defer cancel()

s := plan.Schema.ValueString()
name := plan.Name.ValueString()
url := plan.URL.ValueString()
Expand Down Expand Up @@ -181,6 +194,14 @@ func (r *SubGraphResource) Read(ctx context.Context, req resource.ReadRequest, r
return
}

readTimeout, diagErr := state.Timeouts.Read(ctx, defaultTimeout)
if diagErr.HasError() {
return
}

ctx, cancel := context.WithTimeout(ctx, readTimeout)
defer cancel()

name := state.Name.ValueString()
result, err := r.client.GetSubGraph(ctx, name)

Expand Down Expand Up @@ -224,6 +245,14 @@ func (r *SubGraphResource) Update(ctx context.Context, req resource.UpdateReques
return
}

updateTimeout, diagErr := plan.Timeouts.Update(ctx, defaultTimeout)
if diagErr.HasError() {
return
}

ctx, cancel := context.WithTimeout(ctx, updateTimeout)
defer cancel()

s := plan.Schema.ValueString()
name := plan.Name.ValueString()
url := plan.URL.ValueString()
Expand Down Expand Up @@ -287,6 +316,14 @@ func (r *SubGraphResource) Delete(ctx context.Context, req resource.DeleteReques
return
}

deleteTimeout, diagErr := plan.Timeouts.Delete(ctx, defaultTimeout)
if diagErr.HasError() {
return
}

ctx, cancel := context.WithTimeout(ctx, deleteTimeout)
defer cancel()

name := plan.Name.ValueString()
err := r.client.RemoveSubGraph(ctx, name)

Expand Down

0 comments on commit 22e97dd

Please sign in to comment.