|
| 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