-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SYSENG-1726: initialize terraform-plugin-framework
- Loading branch information
Mario Schäfer
authored and
anx-mschaefer
committed
Feb 12, 2024
1 parent
488e690
commit 3d01954
Showing
8 changed files
with
193 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/anexia-it/terraform-provider-anxcloud/anxcloud" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/provider" | ||
"github.com/hashicorp/terraform-plugin-framework/provider/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"go.anx.io/go-anxcloud/pkg/client" | ||
) | ||
|
||
var _ provider.Provider = &AnexiaProvider{} | ||
|
||
type AnexiaProvider struct { | ||
version string | ||
} | ||
|
||
type AnexiaProviderModel struct { | ||
Token types.String `tfsdk:"token"` | ||
} | ||
|
||
func (p *AnexiaProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { | ||
resp.TypeName = "anxcloud" | ||
resp.Version = p.version | ||
} | ||
|
||
func (p *AnexiaProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"token": schema.StringAttribute{ | ||
Optional: true, | ||
Description: "Anexia Cloud token.", | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (p *AnexiaProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { | ||
apiToken := os.Getenv("ANEXIA_TOKEN") | ||
|
||
var data AnexiaProviderModel | ||
|
||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
if data.Token.ValueString() != "" { | ||
apiToken = data.Token.ValueString() | ||
} | ||
|
||
if apiToken == "" { | ||
resp.Diagnostics.AddError("Missing Anexia Engine token", "The Anexia Engine token was neither configured via `ANEXIA_TOKEN` env var nor via provider argument") | ||
} | ||
|
||
logger := anxcloud.NewTerraformr(log.Default().Writer()) | ||
opts := []client.Option{ | ||
client.TokenFromString(apiToken), | ||
client.Logger(logger.WithName("client")), | ||
client.UserAgent(fmt.Sprintf("%s/%s (%s)", "terraform-provider-anxcloud", p.version, runtime.GOOS)), | ||
} | ||
|
||
resp.ResourceData = opts | ||
resp.DataSourceData = opts | ||
} | ||
|
||
func (p *AnexiaProvider) Resources(ctx context.Context) []func() resource.Resource { | ||
return nil | ||
} | ||
|
||
func (p *AnexiaProvider) DataSources(ctx context.Context) []func() datasource.DataSource { | ||
return nil | ||
} | ||
|
||
func New(version string) func() provider.Provider { | ||
return func() provider.Provider { | ||
return &AnexiaProvider{ | ||
version: version, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
|
||
"github.com/anexia-it/terraform-provider-anxcloud/anxcloud" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin" | ||
"github.com/anexia-it/terraform-provider-anxcloud/internal/provider" | ||
"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" | ||
) | ||
|
||
var version = "development" | ||
|
||
func main() { | ||
var debugMode bool | ||
ctx := context.Background() | ||
|
||
flag.BoolVar(&debugMode, "debuggable", false, "set to true to run the provider with support for debuggers like delve") | ||
var debug bool | ||
|
||
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve") | ||
flag.Parse() | ||
|
||
opts := &plugin.ServeOpts{ | ||
Debug: debugMode, | ||
ProviderAddr: "registry.terraform.io/hashicorp/anxcloud", | ||
ProviderFunc: anxcloud.Provider, | ||
upgradedSdkServer, err := tf5to6server.UpgradeServer( | ||
ctx, | ||
anxcloud.Provider(version).GRPCProvider, | ||
) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
plugin.Serve(opts) | ||
providers := []func() tfprotov6.ProviderServer{ | ||
providerserver.NewProtocol6(provider.New(version)()), | ||
func() tfprotov6.ProviderServer { | ||
return upgradedSdkServer | ||
}, | ||
} | ||
|
||
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var serveOpts []tf6server.ServeOpt | ||
|
||
if debug { | ||
serveOpts = append(serveOpts, tf6server.WithManagedDebug()) | ||
} | ||
|
||
err = tf6server.Serve( | ||
"registry.terraform.io/anexia-it/anxcloud", | ||
muxServer.ProviderServer, | ||
serveOpts..., | ||
) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"version": 1, | ||
"metadata": { | ||
"protocol_versions": ["6.0"] | ||
} | ||
} |