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

add missing route fields to data source #23

Merged
merged 7 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
89 changes: 88 additions & 1 deletion example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
pomerium = {
source = "pomerium/pomerium"
version = "0.0.1"
version = "0.0.2"
}
}
}
Expand Down Expand Up @@ -36,6 +36,7 @@ resource "pomerium_settings" "settings" {
api_key = "key"
url = "http://localhost"
}
timeout_idle = "5m"
}

resource "pomerium_policy" "test_policy" {
Expand Down Expand Up @@ -67,6 +68,92 @@ resource "pomerium_key_pair" "test_key_pair" {
key = file("test.host-key.pem")
}

# Example route with prefix matching
resource "pomerium_route" "prefix_route" {
name = "prefix-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://prefix.localhost.pomerium.io"
to = ["https://target-service.internal"]
prefix = "/api/"
prefix_rewrite = "/v1/"
policies = [pomerium_policy.test_policy.id]

timeout = "30s"
idle_timeout = "5m"

set_request_headers = {
"X-Custom-Header" = "custom-value"
}
remove_request_headers = ["Referer"]
set_response_headers = {
"Strict-Transport-Security" = "max-age=31536000"
}

allow_websockets = true
preserve_host_header = true
pass_identity_headers = true
}

# Example route with path matching
resource "pomerium_route" "path_route" {
name = "path-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://path.localhost.pomerium.io"
to = ["https://path-service.internal"]
path = "/exact/path/match"

tls_skip_verify = true
tls_upstream_server_name = "internal-name"
tls_downstream_server_name = "external-name"
}

# Example route with regex matching and rewriting
resource "pomerium_route" "regex_route" {
name = "regex-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://regex.localhost.pomerium.io"
to = ["https://regex-service.internal"]
regex = "^/users/([0-9]+)/profile$"
regex_rewrite_pattern = "^/users/([0-9]+)/profile$"
regex_rewrite_substitution = "/api/v1/profiles/$1"
regex_priority_order = 100
}

# Example route with host rewriting
resource "pomerium_route" "host_route" {
name = "host-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://host.localhost.pomerium.io"
to = ["https://host-service.internal"]
host_rewrite = "internal-host"
host_path_regex_rewrite_pattern = "^/service/([^/]+)(/.*)$"
host_path_regex_rewrite_substitution = "$1.internal$2"
}

# Example route with OAuth/OIDC configuration
resource "pomerium_route" "oauth_route" {
name = "oauth-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://oauth.localhost.pomerium.io"
to = ["https://protected-service.internal"]

idp_client_id = "custom-client-id"
idp_client_secret = "custom-client-secret"
show_error_details = true
}

# Example route with Kubernetes integration
resource "pomerium_route" "kubernetes_route" {
name = "kubernetes-route"
namespace_id = pomerium_namespace.test_namespace.id
from = "https://k8s.localhost.pomerium.io"
to = ["https://kubernetes-service.internal"]

kubernetes_service_account_token = "eyJhbGciOiJS..."
allow_spdy = true
tls_upstream_allow_renegotiation = true
}

# Data source examples
data "pomerium_namespaces" "all_namespaces" {}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/hashicorp/go-set/v3 v3.0.0 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 h1:v3DapR8gsp3EM8fKMh6up9cJUFQ2iRaFsYLP8UJnCco=
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0/go.mod h1:c3PnGE9pHBDfdEVG9t1S1C9ia5LW+gkFR0CygXlM8ak=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 h1:O9QqGoYDzQT7lwTXUsZEtgabeWW96zUBh47Smn2lkFA=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
Expand Down
22 changes: 11 additions & 11 deletions internal/provider/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"
"reflect"
"time"

"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -50,7 +50,6 @@ func FromStringMap(m map[string]string) types.Map {

// ToStringList converts a types.List to Settings_StringList and handles diagnostics internally
func ToStringList(ctx context.Context, dst **pb.Settings_StringList, list types.List, diagnostics *diag.Diagnostics) {
// Handle null list case first
if list.IsNull() {
*dst = nil
return
Expand Down Expand Up @@ -90,25 +89,26 @@ func ToStringSlice(ctx context.Context, dst *[]string, list types.List, diagnost
}

// ToDuration converts a types.String containing a duration to a durationpb.Duration and handles diagnostics internally
func ToDuration(dst **durationpb.Duration, src types.String, field string, diagnostics *diag.Diagnostics) {
if src.IsNull() {
func ToDuration(dst **durationpb.Duration, src timetypes.GoDuration, diagnostics *diag.Diagnostics) {
if src.IsNull() || src.IsUnknown() {
*dst = nil
return
}

if d, err := time.ParseDuration(src.ValueString()); err == nil {
*dst = durationpb.New(d)
} else {
diagnostics.AddError("invalid "+field, err.Error())
d, diags := src.ValueGoDuration()
diagnostics.Append(diags...)
if diagnostics.HasError() {
return
}
*dst = durationpb.New(d)
}

// FromDuration converts a durationpb.Duration to a types.String
func FromDuration(d *durationpb.Duration) types.String {
func FromDuration(d *durationpb.Duration) timetypes.GoDuration {
if d == nil {
return types.StringNull()
return timetypes.NewGoDurationNull()
}
return types.StringValue(d.AsDuration().String())
return timetypes.NewGoDurationValue(d.AsDuration())
}

// GoStructToPB converts a Go struct to a protobuf Struct.
Expand Down
59 changes: 55 additions & 4 deletions internal/provider/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -58,22 +59,22 @@ func TestFromDurationP(t *testing.T) {
tests := []struct {
name string
input *durationpb.Duration
expected types.String
expected timetypes.GoDuration
}{
{
name: "nil duration",
input: nil,
expected: types.StringNull(),
expected: timetypes.NewGoDurationNull(),
},
{
name: "zero duration",
input: durationpb.New(0),
expected: types.StringValue("0s"),
expected: timetypes.NewGoDurationValueFromStringMust("0s"),
},
{
name: "normal duration",
input: durationpb.New(time.Hour + time.Minute),
expected: types.StringValue("1h1m0s"),
expected: timetypes.NewGoDurationValueFromStringMust("1h1m0s"),
},
}

Expand All @@ -85,6 +86,56 @@ func TestFromDurationP(t *testing.T) {
}
}

func TestToDuration(t *testing.T) {
tests := []struct {
name string
input timetypes.GoDuration
expected *durationpb.Duration
expectError bool
}{
{
name: "null duration",
input: timetypes.NewGoDurationNull(),
expected: nil,
},
{
name: "unknown duration",
input: timetypes.NewGoDurationUnknown(),
expected: nil,
},
{
name: "zero duration",
input: timetypes.NewGoDurationValueFromStringMust("0s"),
expected: durationpb.New(0),
},
{
name: "normal duration",
input: timetypes.NewGoDurationValueFromStringMust("1h1m0s"),
expected: durationpb.New(time.Hour + time.Minute),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result *durationpb.Duration
diagnostics := diag.Diagnostics{}
provider.ToDuration(&result, tt.input, &diagnostics)

if tt.expectError {
assert.True(t, diagnostics.HasError())
return
}

assert.False(t, diagnostics.HasError())
if tt.expected == nil {
assert.Nil(t, result)
} else {
assert.Equal(t, tt.expected.AsDuration(), result.AsDuration())
}
})
}
}

func TestToStringList(t *testing.T) {
ctx := context.Background()
tests := []struct {
Expand Down
21 changes: 19 additions & 2 deletions internal/provider/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -73,6 +74,7 @@ func (r *RouteResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"stat_name": schema.StringAttribute{
Description: "Name of the stat.",
Optional: true,
Computed: true,
},
"prefix": schema.StringAttribute{
Description: "Prefix.",
Expand Down Expand Up @@ -121,10 +123,14 @@ func (r *RouteResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"timeout": schema.StringAttribute{
Description: "Timeout.",
Optional: true,
CustomType: timetypes.GoDurationType{},
Computed: true,
},
"idle_timeout": schema.StringAttribute{
Description: "Idle timeout.",
Optional: true,
CustomType: timetypes.GoDurationType{},
Computed: true,
},
"allow_websockets": schema.BoolAttribute{
Description: "Allow websockets.",
Expand Down Expand Up @@ -188,6 +194,7 @@ func (r *RouteResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"show_error_details": schema.BoolAttribute{
Description: "Show error details.",
Optional: true,
Computed: true,
},
},
}
Expand Down Expand Up @@ -234,7 +241,11 @@ func (r *RouteResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

plan.ID = types.StringValue(respRoute.Route.Id)
diags = ConvertRouteFromPB(&plan, respRoute.Route)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}

tflog.Trace(ctx, "Created a route", map[string]interface{}{
"id": plan.ID.ValueString(),
Expand Down Expand Up @@ -283,14 +294,20 @@ func (r *RouteResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

_, err := r.client.RouteService.SetRoute(ctx, &pb.SetRouteRequest{
respRoute, err := r.client.RouteService.SetRoute(ctx, &pb.SetRouteRequest{
Route: pbRoute,
})
if err != nil {
resp.Diagnostics.AddError("set route", err.Error())
return
}

diags = ConvertRouteFromPB(&plan, respRoute.Route)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}

Expand Down
Loading