Skip to content

Commit 2a46719

Browse files
committed
Initial framework scaffolding
1 parent 152ca2d commit 2a46719

File tree

6 files changed

+296
-18
lines changed

6 files changed

+296
-18
lines changed

go.mod

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/auth0/terraform-provider-auth0
22

3-
go 1.22
3+
go 1.22.0
4+
5+
toolchain go1.22.5
46

57
require (
68
github.com/PuerkitoBio/rehttp v1.4.0
@@ -9,6 +11,10 @@ require (
911
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
1012
github.com/hashicorp/go-multierror v1.1.1
1113
github.com/hashicorp/terraform-plugin-docs v0.19.4
14+
github.com/hashicorp/terraform-plugin-framework v1.12.0
15+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
16+
github.com/hashicorp/terraform-plugin-go v0.24.0
17+
github.com/hashicorp/terraform-plugin-mux v0.16.0
1218
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
1319
github.com/hashicorp/terraform-plugin-testing v1.10.0
1420
github.com/stretchr/testify v1.9.0
@@ -46,7 +52,6 @@ require (
4652
github.com/hashicorp/logutils v1.0.0 // indirect
4753
github.com/hashicorp/terraform-exec v0.21.0 // indirect
4854
github.com/hashicorp/terraform-json v0.22.1 // indirect
49-
github.com/hashicorp/terraform-plugin-go v0.23.0 // indirect
5055
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
5156
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
5257
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
@@ -84,9 +89,9 @@ require (
8489
golang.org/x/text v0.17.0 // indirect
8590
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
8691
google.golang.org/appengine v1.6.8 // indirect
87-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect
88-
google.golang.org/grpc v1.64.1 // indirect
89-
google.golang.org/protobuf v1.34.1 // indirect
92+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
93+
google.golang.org/grpc v1.66.2 // indirect
94+
google.golang.org/protobuf v1.34.2 // indirect
9095
gopkg.in/yaml.v2 v2.4.0 // indirect
9196
gopkg.in/yaml.v3 v3.0.1 // indirect
9297
)

go.sum

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,16 @@ github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7
111111
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
112112
github.com/hashicorp/terraform-plugin-docs v0.19.4 h1:G3Bgo7J22OMtegIgn8Cd/CaSeyEljqjH3G39w28JK4c=
113113
github.com/hashicorp/terraform-plugin-docs v0.19.4/go.mod h1:4pLASsatTmRynVzsjEhbXZ6s7xBlUw/2Kt0zfrq8HxA=
114-
github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/125t+AAurhqphJ2Co=
115-
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
114+
github.com/hashicorp/terraform-plugin-framework v1.12.0 h1:7HKaueHPaikX5/7cbC1r9d1m12iYHY+FlNZEGxQ42CQ=
115+
github.com/hashicorp/terraform-plugin-framework v1.12.0/go.mod h1:N/IOQ2uYjW60Jp39Cp3mw7I/OpC/GfZ0385R0YibmkE=
116+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0 h1:bxZfGo9DIUoLLtHMElsu+zwqI4IsMZQBRRy4iLzZJ8E=
117+
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0/go.mod h1:wGeI02gEhj9nPANU62F2jCaHjXulejm/X+af4PdZaNo=
118+
github.com/hashicorp/terraform-plugin-go v0.24.0 h1:2WpHhginCdVhFIrWHxDEg6RBn3YaWzR2o6qUeIEat2U=
119+
github.com/hashicorp/terraform-plugin-go v0.24.0/go.mod h1:tUQ53lAsOyYSckFGEefGC5C8BAaO0ENqzFd3bQeuYQg=
116120
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
117121
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
122+
github.com/hashicorp/terraform-plugin-mux v0.16.0 h1:RCzXHGDYwUwwqfYYWJKBFaS3fQsWn/ZECEiW7p2023I=
123+
github.com/hashicorp/terraform-plugin-mux v0.16.0/go.mod h1:PF79mAsPc8CpusXPfEVa4X8PtkB+ngWoiUClMrNZlYo=
118124
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE=
119125
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg=
120126
github.com/hashicorp/terraform-plugin-testing v1.10.0 h1:2+tmRNhvnfE4Bs8rB6v58S/VpqzGC6RCh9Y8ujdn+aw=
@@ -283,14 +289,14 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
283289
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
284290
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
285291
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
286-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e h1:Elxv5MwEkCI9f5SkoL6afed6NTdxaGoAo39eANBwHL8=
287-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
288-
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
289-
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
292+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU=
293+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
294+
google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo=
295+
google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
290296
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
291297
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
292-
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
293-
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
298+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
299+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
294300
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
295301
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
296302
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/config/config.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"github.com/PuerkitoBio/rehttp"
1515
"github.com/auth0/go-auth0"
1616
"github.com/auth0/go-auth0/management"
17+
frameworkDiag "github.com/hashicorp/terraform-plugin-framework/diag"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1819
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1920
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
2021

22+
internalError "github.com/auth0/terraform-provider-auth0/internal/error"
2123
"github.com/auth0/terraform-provider-auth0/internal/mutex"
2224
)
2325

@@ -78,6 +80,28 @@ func ConfigureProvider(terraformVersion *string) schema.ConfigureContextFunc {
7880
}
7981
}
8082

83+
// ConfigureFrameworkProvider will configure the provider.Provider so that
84+
// *management.Management client and *mutex.KeyValue is stored
85+
// and passed into the subsequent resources Configure method as the ConfigureRequest.ProviderData.
86+
func ConfigureFrameworkProvider(
87+
terraformVersion, domain, clientID, clientSecret, apiToken, audience string,
88+
debug bool,
89+
) (interface{}, frameworkDiag.Diagnostics) {
90+
apiClient, err := management.New(domain,
91+
authenticationOption(clientID, clientSecret, apiToken, audience),
92+
management.WithDebug(debug),
93+
management.WithUserAgent(userAgentFramework(terraformVersion)),
94+
management.WithAuth0ClientEnvEntry(providerName, version),
95+
management.WithNoRetries(),
96+
management.WithClient(customClientWithRetries()),
97+
)
98+
if err != nil {
99+
return nil, internalError.DiagnosticsFromError(err)
100+
}
101+
102+
return New(apiClient), nil
103+
}
104+
81105
// userAgent computes the desired User-Agent header for the *management.Management client.
82106
func userAgent(terraformVersion *string) string {
83107
sdkVersion := auth0.Version
@@ -95,6 +119,22 @@ func userAgent(terraformVersion *string) string {
95119
return userAgent
96120
}
97121

122+
// userAgentFramework computes the desired User-Agent header for the
123+
// *management.Management client using the Terraform Framework.
124+
func userAgentFramework(terraformVersion string) string {
125+
sdkVersion := auth0.Version
126+
127+
userAgent := fmt.Sprintf(
128+
"%s/%s (Go-Auth0-SDK/%s; Terraform/%s)",
129+
providerName,
130+
version,
131+
sdkVersion,
132+
terraformVersion,
133+
)
134+
135+
return userAgent
136+
}
137+
98138
// authenticationOption computes the desired authentication option for the *management.Management client.
99139
func authenticationOption(clientID, clientSecret, apiToken, audience string) management.Option {
100140
ctx := context.Background()

internal/error/diag_error.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package error
2+
3+
import (
4+
"github.com/hashicorp/go-multierror"
5+
"github.com/hashicorp/terraform-plugin-framework/diag"
6+
)
7+
8+
func DiagnosticsFromError(err error) diag.Diagnostics {
9+
result := diag.Diagnostics{}
10+
if err == nil {
11+
return result
12+
}
13+
if mErr, ok := err.(*multierror.Error); ok {
14+
if len(mErr.Errors) == 0 {
15+
return result
16+
}
17+
for _, err := range mErr.Errors {
18+
result.AddError("Error", err.Error())
19+
}
20+
} else {
21+
result.AddError("Error", err.Error())
22+
}
23+
24+
return result
25+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/path"
9+
"github.com/hashicorp/terraform-plugin-framework/provider"
10+
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/resource"
12+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
13+
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
15+
16+
"github.com/auth0/terraform-provider-auth0/internal/config"
17+
)
18+
19+
type auth0Provider struct {
20+
}
21+
22+
type auth0ProviderModel struct {
23+
Domain types.String `tfsdk:"domain"`
24+
Audience types.String `tfsdk:"audience"`
25+
ClientID types.String `tfsdk:"client_id"`
26+
ClientSecret types.String `tfsdk:"client_secret"`
27+
ApiToken types.String `tfsdk:"api_token"`
28+
Debug types.Bool `tfsdk:"debug"`
29+
}
30+
31+
func (p *auth0Provider) Metadata(_ context.Context, _ provider.MetadataRequest, _ *provider.MetadataResponse) {
32+
}
33+
34+
func (p *auth0Provider) Schema(_ context.Context, _ provider.SchemaRequest, response *provider.SchemaResponse) {
35+
if response != nil {
36+
response.Schema = schema.Schema{
37+
Attributes: map[string]schema.Attribute{
38+
"domain": schema.StringAttribute{
39+
Optional: true,
40+
Description: "Your Auth0 domain name. " +
41+
"It can also be sourced from the AUTH0_DOMAIN environment variable.",
42+
MarkdownDescription: "Your Auth0 domain name. " +
43+
"It can also be sourced from the `AUTH0_DOMAIN` environment variable.",
44+
},
45+
"audience": schema.StringAttribute{
46+
Optional: true,
47+
Description: "Your Auth0 audience when using a custom domain. " +
48+
"It can also be sourced from the AUTH0_AUDIENCE environment variable.",
49+
MarkdownDescription: "Your Auth0 audience when using a custom domain. " +
50+
"It can also be sourced from the `AUTH0_AUDIENCE` environment variable.",
51+
},
52+
"client_id": schema.StringAttribute{
53+
Optional: true,
54+
Validators: []validator.String{
55+
stringvalidator.ConflictsWith(path.Expressions{
56+
path.MatchRoot("api_token"),
57+
}...),
58+
stringvalidator.AlsoRequires(path.Expressions{
59+
path.MatchRoot("client_secret"),
60+
}...),
61+
},
62+
Description: "Your Auth0 client ID. " +
63+
"It can also be sourced from the AUTH0_CLIENT_ID environment variable.",
64+
MarkdownDescription: "Your Auth0 client ID. " +
65+
"It can also be sourced from the `AUTH0_CLIENT_ID` environment variable.",
66+
},
67+
"client_secret": schema.StringAttribute{
68+
Optional: true,
69+
Validators: []validator.String{
70+
stringvalidator.ConflictsWith(path.Expressions{
71+
path.MatchRoot("api_token"),
72+
}...),
73+
stringvalidator.AlsoRequires(path.Expressions{
74+
path.MatchRoot("client_id"),
75+
}...),
76+
},
77+
Description: "Your Auth0 client secret. " +
78+
"It can also be sourced from the AUTH0_CLIENT_SECRET environment variable.",
79+
MarkdownDescription: "Your Auth0 client secret. " +
80+
"It can also be sourced from the `AUTH0_CLIENT_SECRET` environment variable.",
81+
},
82+
"api_token": schema.StringAttribute{
83+
Optional: true,
84+
Validators: []validator.String{
85+
stringvalidator.ConflictsWith(path.Expressions{
86+
path.MatchRoot("client_id"),
87+
}...),
88+
stringvalidator.ConflictsWith(path.Expressions{
89+
path.MatchRoot("client_secret"),
90+
}...),
91+
},
92+
Description: "Your Auth0 management api access token. " +
93+
"It can also be sourced from the AUTH0_API_TOKEN environment variable. " +
94+
"It can be used instead of client_id + client_secret. " +
95+
"If both are specified, api_token will be used over client_id + client_secret fields.",
96+
MarkdownDescription: "Your Auth0 [management api access token]" +
97+
"(https://auth0.com/docs/security/tokens/access-tokens/management-api-access-tokens). " +
98+
"It can also be sourced from the `AUTH0_API_TOKEN` environment variable. " +
99+
"It can be used instead of `client_id` + `client_secret`. " +
100+
"If both are specified, `api_token` will be used over `client_id` + `client_secret` fields.",
101+
},
102+
"debug": schema.BoolAttribute{
103+
Optional: true,
104+
Description: "Indicates whether to turn on debug mode.",
105+
MarkdownDescription: "Indicates whether to turn on debug mode.",
106+
},
107+
},
108+
}
109+
}
110+
}
111+
112+
func (p *auth0Provider) Configure(ctx context.Context, request provider.ConfigureRequest, response *provider.ConfigureResponse) {
113+
domain := os.Getenv("AUTH0_DOMAIN")
114+
clientID := os.Getenv("AUTH0_CLIENT_ID")
115+
clientSecret := os.Getenv("AUTH0_CLIENT_SECRET")
116+
apiToken := os.Getenv("AUTH0_API_TOKEN")
117+
audience := os.Getenv("AUTH0_AUDIENCE")
118+
debugStr := os.Getenv("AUTH0_DEBUG")
119+
120+
var debug bool
121+
switch debugStr {
122+
case "1", "true", "on":
123+
debug = true
124+
default:
125+
debug = false
126+
}
127+
128+
var data auth0ProviderModel
129+
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
130+
131+
if data.Domain.ValueString() != "" {
132+
domain = data.Domain.ValueString()
133+
}
134+
if data.ClientID.ValueString() != "" {
135+
clientID = data.ClientID.ValueString()
136+
}
137+
if data.ClientSecret.ValueString() != "" {
138+
clientSecret = data.ClientSecret.ValueString()
139+
}
140+
if data.ApiToken.ValueString() != "" {
141+
apiToken = data.ApiToken.ValueString()
142+
}
143+
if data.Audience.ValueString() != "" {
144+
audience = data.Audience.ValueString()
145+
}
146+
if !data.Debug.IsNull() && !data.Debug.IsUnknown() {
147+
debug = data.Debug.ValueBool()
148+
}
149+
150+
config, diag := config.ConfigureFrameworkProvider(request.TerraformVersion, domain, clientID, clientSecret, apiToken, audience, debug)
151+
if config != nil {
152+
response.ResourceData = config
153+
response.DataSourceData = config
154+
}
155+
156+
response.Diagnostics.Append(diag...)
157+
}
158+
159+
func (p *auth0Provider) DataSources(_ context.Context) []func() datasource.DataSource {
160+
return []func() datasource.DataSource{}
161+
}
162+
163+
func (p *auth0Provider) Resources(_ context.Context) []func() resource.Resource {
164+
return []func() resource.Resource{}
165+
}
166+
167+
// NewAuth0Provider returns a terraform Framework provider.Provider.
168+
func NewAuth0Provider() provider.Provider {
169+
return &auth0Provider{}
170+
}

0 commit comments

Comments
 (0)