From a968af9a792f926e378e7d39967760e835d115d2 Mon Sep 17 00:00:00 2001 From: Ben Pearce Date: Tue, 2 Jul 2024 17:27:01 +1000 Subject: [PATCH] introduced new protocol v6 server and muxing server to old provider --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- go.mod | 4 ++- go.sum | 4 +++ main.go | 38 +++++++++++++++++---- octopusdeploy/providerv6.go | 63 +++++++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 octopusdeploy/providerv6.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a158ec25e..5a66b158a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,5 +15,5 @@ jobs: fetch-depth: 0 - uses: actions/setup-go@v3 with: - go-version: '1.21' + go-version: '1.22' - run: go build ./... \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0dd84ff1a..5b4335a90 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-go@v3 with: - go-version: '1.21' + go-version: '1.22' - uses: crazy-max/ghaction-import-gpg@v5 id: import_gpg with: diff --git a/go.mod b/go.mod index 6e343be42..2ebc4126e 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,10 @@ require ( github.com/gruntwork-io/terratest v0.41.11 github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637 github.com/hashicorp/terraform-plugin-docs v0.13.0 + github.com/hashicorp/terraform-plugin-framework v1.9.0 + github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-log v0.9.0 + github.com/hashicorp/terraform-plugin-mux v0.16.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 github.com/stretchr/testify v1.9.0 golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea @@ -83,7 +86,6 @@ require ( github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.21.0 // indirect github.com/hashicorp/terraform-json v0.22.1 // indirect - github.com/hashicorp/terraform-plugin-go v0.23.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 diff --git a/go.sum b/go.sum index 08d3650b3..f7c0110eb 100644 --- a/go.sum +++ b/go.sum @@ -250,10 +250,14 @@ github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7 github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A= github.com/hashicorp/terraform-plugin-docs v0.13.0 h1:6e+VIWsVGb6jYJewfzq2ok2smPzZrt1Wlm9koLeKazY= github.com/hashicorp/terraform-plugin-docs v0.13.0/go.mod h1:W0oCmHAjIlTHBbvtppWHe8fLfZ2BznQbuv8+UD8OucQ= +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-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co= github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ= github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0= github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow= +github.com/hashicorp/terraform-plugin-mux v0.16.0 h1:RCzXHGDYwUwwqfYYWJKBFaS3fQsWn/ZECEiW7p2023I= +github.com/hashicorp/terraform-plugin-mux v0.16.0/go.mod h1:PF79mAsPc8CpusXPfEVa4X8PtkB+ngWoiUClMrNZlYo= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE= github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= diff --git a/main.go b/main.go index 63a681dc9..9cd759050 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,15 @@ package main import ( + "context" "flag" "github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy" - "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" + "github.com/hashicorp/terraform-plugin-framework/providerserver" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server" + "github.com/hashicorp/terraform-plugin-mux/tf5to6server" + "github.com/hashicorp/terraform-plugin-mux/tf6muxserver" + "log" ) //go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs @@ -13,14 +19,34 @@ func main() { flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve") flag.Parse() - opts := &plugin.ServeOpts{ - ProviderFunc: octopusdeploy.Provider, + ctx := context.Background() + + upgradedSdkServer, err := tf5to6server.UpgradeServer( + ctx, + octopusdeploy.Provider().GRPCProvider) + if err != nil { + log.Fatal(err) + } + + providers := []func() tfprotov6.ProviderServer{ + providerserver.NewProtocol6(octopusdeploy.NewOctopusDeployProviderV6()), + func() tfprotov6.ProviderServer { + return upgradedSdkServer + }, } + muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...) + + opts := []tf6server.ServeOpt{} + + var providerName = "registry.terraform.io/OctopusDeployLabs/octopusdeploy" if debugMode { - opts.Debug = true - opts.ProviderAddr = "octopus.com/com/octopusdeploy" + opts = append(opts, tf6server.WithManagedDebug()) + providerName = "octopus.com/com/octopusdeploy" } - plugin.Serve(opts) + err = tf6server.Serve(providerName, muxServer.ProviderServer, opts...) + if err != nil { + log.Fatal(err) + } } diff --git a/octopusdeploy/providerv6.go b/octopusdeploy/providerv6.go new file mode 100644 index 000000000..1d22d2b6b --- /dev/null +++ b/octopusdeploy/providerv6.go @@ -0,0 +1,63 @@ +package octopusdeploy + +import ( + "context" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/provider/schema" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + + "github.com/hashicorp/terraform-plugin-framework/provider" +) + +type octopusDeployProviderV6 struct { + Address types.String `tfsdk:"address"` + ApiKey types.String `tfsdk:"api_key"` + SpaceID types.String `tfsdk:"space_id"` +} + +var _ provider.Provider = (*octopusDeployProviderV6)(nil) +var _ provider.ProviderWithMetaSchema = (*octopusDeployProviderV6)(nil) + +func NewOctopusDeployProviderV6() *octopusDeployProviderV6 { + return &octopusDeployProviderV6{} +} + +func (p *octopusDeployProviderV6) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { +} + +func (p *octopusDeployProviderV6) MetaSchema(ctx context.Context, request provider.MetaSchemaRequest, response *provider.MetaSchemaResponse) { + +} + +func (p *octopusDeployProviderV6) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { +} + +func (p *octopusDeployProviderV6) DataSources(ctx context.Context) []func() datasource.DataSource { + return []func() datasource.DataSource{} +} + +func (p *octopusDeployProviderV6) Resources(ctx context.Context) []func() resource.Resource { + return []func() resource.Resource{} +} + +func (p *octopusDeployProviderV6) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "address": schema.StringAttribute{ + Optional: false, + Required: true, + Description: "The endpoint of the Octopus REST API", + }, + "api_key": schema.StringAttribute{ + Optional: false, + Required: true, + Description: "The API key to use with the Octopus REST API", + }, + "space_id": schema.StringAttribute{ + Optional: true, + Description: "The space ID to target", + }, + }, + } +}