Setting the file. One moment. Provider · New Terraform Provider · hashicorp/agent-skills · Skills Docsassets/provider.go
Go·139 lines·4 KB
github.com/hashicorp/terraform-plugin-framework/provider/schema
"
14 "github.com/hashicorp/terraform-plugin-framework/resource"
15 "github.com/hashicorp/terraform-plugin-framework/types"
16)
17
18var _ provider.Provider = &demoProvider{}
19
20// New returns the provider factory consumed by main.go.
21func New(version string) func() provider.Provider {
22 return func() provider.Provider {
23 return &demoProvider{version: version}
24 }
25}
26
27// TODO: Rename demoProvider (and the "demo" TypeName below) after your provider.
28type demoProvider struct {
29 version string
30}
31
32type demoProviderModel struct {
33 Endpoint types.String `tfsdk:"endpoint"`
34 APIKey types.String `tfsdk:"api_key"`
35}
36
37func (p *demoProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
38 // TODO: Update this with your provider's type name. It is the prefix of
39 // every resource and data source type (e.g. "demo" -> demo_widget).
40 resp.TypeName = "demo"
41 resp.Version = p.version
42}
43
44func (p *demoProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
45 resp.Schema = schema.Schema{
46 Attributes: map[string]schema.Attribute{
47 // Authentication attributes are Optional (never Required) so
48 // environment variables can supply them; secrets are Sensitive.
49 // TODO: Replace endpoint/api_key and the DEMO_* environment
50 // variables with your API's connection settings.
51 "endpoint": schema.StringAttribute{
52 Optional: true,
53 MarkdownDescription: "API endpoint. May also be set via the `DEMO_ENDPOINT` environment variable.",
54 },
55 "api_key": schema.StringAttribute{
56 Optional: true,
57 Sensitive: true,
58 MarkdownDescription: "API key. May also be set via the `DEMO_API_KEY` environment variable.",
59 },
60 },
61 }
62}
63
64func (p *demoProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
65 var config demoProviderModel
66 resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
67 if resp.Diagnostics.HasError() {
68 return
69 }
70
71 // Values wired to other resources' outputs are unknown during planning;
72 // treating them as empty would silently mis-authenticate.
73 if config.Endpoint.IsUnknown() {
74 resp.Diagnostics.AddAttributeError(
75 path.Root("endpoint"),
76 "Unknown endpoint",
77 "endpoint depends on a value known only after apply. Set a static value or use the DEMO_ENDPOINT environment variable.",
78 )
79 }
80 if config.APIKey.IsUnknown() {
81 resp.Diagnostics.AddAttributeError(
82 path.Root("api_key"),
83 "Unknown API key",
84 "api_key depends on a value known only after apply. Set a static value or use the DEMO_API_KEY environment variable.",
85 )
86 }
87 if resp.Diagnostics.HasError() {
88 return
89 }
90
91 // Explicit configuration wins; environment variables are the fallback.
92 endpoint := config.Endpoint.ValueString()
93 if endpoint == "" {
94 endpoint = os.Getenv("DEMO_ENDPOINT")
95 }
96 apiKey := config.APIKey.ValueString()
97 if apiKey == "" {
98 apiKey = os.Getenv("DEMO_API_KEY")
99 }
100
101 if endpoint == "" {
102 resp.Diagnostics.AddAttributeError(
103 path.Root("endpoint"),
104 "Missing endpoint",
105 "Set endpoint in the provider block or export DEMO_ENDPOINT.",
106 )
107 }
108 if apiKey == "" {
109 resp.Diagnostics.AddAttributeError(
110 path.Root("api_key"),
111 "Missing API key",
112 "Set api_key in the provider block or export DEMO_API_KEY.",
113 )
114 }
115 if resp.Diagnostics.HasError() {
116 return
117 }
118
119 // TODO: Construct your API client here and hand it to resources and data
120 // sources. They receive it in their Configure methods via ProviderData.
121 //
122 // client := demoapi.NewClient(endpoint, apiKey)
123 // resp.ResourceData = client
124 // resp.DataSourceData = client
125 _ = endpoint
126 _ = apiKey
127}
128
129func (p *demoProvider) Resources(_ context.Context) []func() resource.Resource {
130 return []func() resource.Resource{
131 // TODO: Register resource constructors here, e.g. NewWidgetResource.
132 }
133}
134
135func (p *demoProvider) DataSources(_ context.Context) []func() datasource.DataSource {
136 return []func() datasource.DataSource{
137 // TODO: Register data source constructors here.
138 }
139}