From 01296af37296941f7776fd4bfdd2246c1826780c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Hansen?= Date: Sun, 3 Sep 2023 17:55:49 +0200 Subject: [PATCH 1/2] Add support for `force_https` on handlers. --- internal/provider/machine_resource.go | 22 ++++++++++++++++------ pkg/apiv1/machines.go | 10 ++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/internal/provider/machine_resource.go b/internal/provider/machine_resource.go index f324666..5014c43 100644 --- a/internal/provider/machine_resource.go +++ b/internal/provider/machine_resource.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" @@ -39,8 +40,9 @@ func (r *flyMachineResource) Configure(_ context.Context, req resource.Configure } type TfPort struct { - Port types.Int64 `tfsdk:"port"` - Handlers []types.String `tfsdk:"handlers"` + Port types.Int64 `tfsdk:"port"` + Handlers []types.String `tfsdk:"handlers"` + ForceHttps bool `tfsdk:"force_https"` } type TfService struct { @@ -195,6 +197,12 @@ func (r *flyMachineResource) Schema(_ context.Context, _ resource.SchemaRequest, Optional: true, ElementType: types.StringType, }, + "force_https": schema.BoolAttribute{ + MarkdownDescription: "Automatically redirect to HTTPS on \"http\" handler", + Computed: true, + Optional: true, + Default: booldefault.StaticBool(false), + }, }, }, }, @@ -223,8 +231,9 @@ func TfServicesToServices(input []TfService) []apiv1.Service { handlers = append(handlers, k.ValueString()) } ports = append(ports, apiv1.Port{ - Port: j.Port.ValueInt64(), - Handlers: handlers, + Port: j.Port.ValueInt64(), + Handlers: handlers, + ForceHttps: j.ForceHttps, }) } services = append(services, apiv1.Service{ @@ -246,8 +255,9 @@ func ServicesToTfServices(input []apiv1.Service) []TfService { handlers = append(handlers, types.StringValue(k)) } tfports = append(tfports, TfPort{ - Port: types.Int64Value(j.Port), - Handlers: handlers, + Port: types.Int64Value(j.Port), + Handlers: handlers, + ForceHttps: j.ForceHttps, }) } tfservices = append(tfservices, TfService{ diff --git a/pkg/apiv1/machines.go b/pkg/apiv1/machines.go index 9470c14..eeeece1 100644 --- a/pkg/apiv1/machines.go +++ b/pkg/apiv1/machines.go @@ -3,10 +3,11 @@ package apiv1 import ( "errors" "fmt" - "github.com/Khan/genqlient/graphql" - hreq "github.com/imroc/req/v3" "net/http" "time" + + "github.com/Khan/genqlient/graphql" + hreq "github.com/imroc/req/v3" ) var NonceHeader = "fly-machine-lease-nonce" @@ -25,8 +26,9 @@ type MachineMount struct { } type Port struct { - Port int64 `json:"port"` - Handlers []string `json:"handlers"` + Port int64 `json:"port"` + Handlers []string `json:"handlers"` + ForceHttps bool `json:"force_https"` } type Service struct { From f9ffcd9f77c49accfa78d1e70f02376fb2d744d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Hansen?= Date: Sun, 3 Sep 2023 17:55:59 +0200 Subject: [PATCH 2/2] Add test to ensure `force_https` is set. --- internal/provider/machine_resource_test.go | 54 +++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/internal/provider/machine_resource_test.go b/internal/provider/machine_resource_test.go index 1816026..30490f0 100644 --- a/internal/provider/machine_resource_test.go +++ b/internal/provider/machine_resource_test.go @@ -2,10 +2,11 @@ package provider import ( "fmt" - "github.com/hashicorp/terraform-plugin-testing/helper/acctest" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" "os" "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) var app = os.Getenv("FLY_TF_TEST_APP") @@ -309,3 +310,52 @@ resource "fly_machine" "testMachine" { } `, app, region) } + +func TestAccFlyMachineWithForceHttps(t *testing.T) { + t.Parallel() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + PreCheck: func() { testAccPreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testFlyMachineResourceWithForceHttps("true"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("fly_machine.testMachine", "services.0.ports.1.force_https", "true"), + ), + }, + { + Config: testFlyMachineResourceWithForceHttps("false"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("fly_machine.testMachine", "services.0.ports.1.force_https", "false"), + ), + }, + }, + }) +} + +func testFlyMachineResourceWithForceHttps(forceHttps string) string { + return providerConfig() + fmt.Sprintf(` +resource "fly_machine" "testMachine" { + app = "%s" + region = "%s" + image = "nginx:latest" + services = [ + { + ports = [ + { + port = 443 + handlers = ["tls", "http"] + }, + { + port = 80 + handlers = ["http"] + force_https = "%s" + } + ] + "protocol" : "tcp", + "internal_port" : 80 + } + ] +} +`, app, region, forceHttps) +}