Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Add support for force_https on handlers #240

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 16 additions & 6 deletions internal/provider/machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
},
},
},
},
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
54 changes: 52 additions & 2 deletions internal/provider/machine_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
10 changes: 6 additions & 4 deletions pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down