Skip to content

Commit

Permalink
chore: prepare custom client
Browse files Browse the repository at this point in the history
  • Loading branch information
j0g3sc committed Nov 17, 2023
1 parent 5df57aa commit 1326220
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/hashicorp/terraform-provider-scaffolding-framework
module github.com/meshcloud/terraform-provider-meshstack

go 1.19

Expand Down
5 changes: 2 additions & 3 deletions internal/provider/example_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package provider
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand All @@ -23,7 +22,7 @@ func NewExampleDataSource() datasource.DataSource {

// ExampleDataSource defines the data source implementation.
type ExampleDataSource struct {
client *http.Client
client *MeshStackProviderClient
}

// ExampleDataSourceModel describes the data source data model.
Expand Down Expand Up @@ -60,7 +59,7 @@ func (d *ExampleDataSource) Configure(ctx context.Context, req datasource.Config
return
}

client, ok := req.ProviderData.(*http.Client)
client, ok := req.ProviderData.(*MeshStackProviderClient)

if !ok {
resp.Diagnostics.AddError(
Expand Down
5 changes: 2 additions & 3 deletions internal/provider/example_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package provider
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -28,7 +27,7 @@ func NewExampleResource() resource.Resource {

// ExampleResource defines the resource implementation.
type ExampleResource struct {
client *http.Client
client *MeshStackProviderClient
}

// ExampleResourceModel describes the resource data model.
Expand Down Expand Up @@ -75,7 +74,7 @@ func (r *ExampleResource) Configure(ctx context.Context, req resource.ConfigureR
return
}

client, ok := req.ProviderData.(*http.Client)
client, ok := req.ProviderData.(*MeshStackProviderClient)

if !ok {
resp.Diagnostics.AddError(
Expand Down
39 changes: 30 additions & 9 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package provider
import (
"context"
"net/http"
"net/url"
"time"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand All @@ -27,6 +29,14 @@ type MeshStackProviderModel struct {
ApiSecret types.String `tfsdk:"apisecret"`
}

// TODO this will be an abstraction that does the login call, get a token and then use this token in the Auth header.
type MeshStackProviderClient struct {
Url *url.URL
httpClient *http.Client
apiKey string
apiSecret string
}

func (p *MeshStackProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "meshStack"
resp.Version = p.version
Expand All @@ -53,22 +63,33 @@ func (p *MeshStackProvider) Schema(ctx context.Context, req provider.SchemaReque

func (p *MeshStackProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var data MeshStackProviderModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

url, err := url.Parse(data.Endpoint.ValueString())
if err != nil {
resp.Diagnostics.AddError("Provider endpoint not valid.", "The value provided as the providers endpoint is not a valid URL.")
} else {
client := buildClient(url, data)
resp.DataSourceData = client
resp.ResourceData = client
}

if resp.Diagnostics.HasError() {
return
}
}

// TODO validate config and append errors is case there are any e.g.
// resp.Diagnostics.AddAttributeError()

// TODO create client e.g. like this:
// Example client configuration for data sources and resources
func buildClient(url *url.URL, model MeshStackProviderModel) *MeshStackProviderClient {
client := MeshStackProviderClient{
Url: url,
httpClient: &http.Client{
Timeout: time.Minute * 5,
},
apiKey: model.ApiKey.ValueString(),
apiSecret: model.ApiKey.ValueString(),
}

client := http.DefaultClient
resp.DataSourceData = client
resp.ResourceData = client
return &client
}

func (p *MeshStackProvider) Resources(ctx context.Context) []func() resource.Resource {
Expand Down
5 changes: 1 addition & 4 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
Expand All @@ -15,7 +12,7 @@ import (
// CLI command executed to create a provider server to which the CLI can
// reattach.
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"scaffolding": providerserver.NewProtocol6WithError(New("test")()),
"meshstack": providerserver.NewProtocol6WithError(New("test")()),
}

func testAccPreCheck(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-provider-scaffolding-framework/internal/provider"
"github.com/meshcloud/terraform-provider-meshstack/internal/provider"
)

// Run "go generate" to format example terraform files and generate the docs for the registry/website
Expand Down Expand Up @@ -38,8 +38,7 @@ func main() {
flag.Parse()

opts := providerserver.ServeOpts{
// TODO: Update this string with the published name of your provider.
Address: "registry.terraform.io/hashicorp/scaffolding",
Address: "registry.terraform.io/hashicorp/meshstack",
Debug: debug,
}

Expand Down

0 comments on commit 1326220

Please sign in to comment.