1package terraform
2
3import (
4	"fmt"
5	"log"
6
7	"github.com/hashicorp/terraform/internal/providers"
8)
9
10// Provider is an implementation of providers.Interface
11type Provider struct {
12	// Provider is the schema for the provider itself.
13	Schema providers.Schema
14
15	// DataSources maps the data source name to that data source's schema.
16	DataSources map[string]providers.Schema
17}
18
19// NewProvider returns a new terraform provider
20func NewProvider() providers.Interface {
21	return &Provider{}
22}
23
24// GetSchema returns the complete schema for the provider.
25func (p *Provider) GetProviderSchema() providers.GetProviderSchemaResponse {
26	return providers.GetProviderSchemaResponse{
27		DataSources: map[string]providers.Schema{
28			"terraform_remote_state": dataSourceRemoteStateGetSchema(),
29		},
30	}
31}
32
33// ValidateProviderConfig is used to validate the configuration values.
34func (p *Provider) ValidateProviderConfig(req providers.ValidateProviderConfigRequest) providers.ValidateProviderConfigResponse {
35	// At this moment there is nothing to configure for the terraform provider,
36	// so we will happily return without taking any action
37	var res providers.ValidateProviderConfigResponse
38	res.PreparedConfig = req.Config
39	return res
40}
41
42// ValidateDataResourceConfig is used to validate the data source configuration values.
43func (p *Provider) ValidateDataResourceConfig(req providers.ValidateDataResourceConfigRequest) providers.ValidateDataResourceConfigResponse {
44	// FIXME: move the backend configuration validate call that's currently
45	// inside the read method  into here so that we can catch provider configuration
46	// errors in terraform validate as well as during terraform plan.
47	var res providers.ValidateDataResourceConfigResponse
48
49	// This should not happen
50	if req.TypeName != "terraform_remote_state" {
51		res.Diagnostics.Append(fmt.Errorf("Error: unsupported data source %s", req.TypeName))
52		return res
53	}
54
55	diags := dataSourceRemoteStateValidate(req.Config)
56	res.Diagnostics = diags
57
58	return res
59}
60
61// Configure configures and initializes the provider.
62func (p *Provider) ConfigureProvider(providers.ConfigureProviderRequest) providers.ConfigureProviderResponse {
63	// At this moment there is nothing to configure for the terraform provider,
64	// so we will happily return without taking any action
65	var res providers.ConfigureProviderResponse
66	return res
67}
68
69// ReadDataSource returns the data source's current state.
70func (p *Provider) ReadDataSource(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
71	// call function
72	var res providers.ReadDataSourceResponse
73
74	// This should not happen
75	if req.TypeName != "terraform_remote_state" {
76		res.Diagnostics.Append(fmt.Errorf("Error: unsupported data source %s", req.TypeName))
77		return res
78	}
79
80	newState, diags := dataSourceRemoteStateRead(req.Config)
81
82	res.State = newState
83	res.Diagnostics = diags
84
85	return res
86}
87
88// Stop is called when the provider should halt any in-flight actions.
89func (p *Provider) Stop() error {
90	log.Println("[DEBUG] terraform provider cannot Stop")
91	return nil
92}
93
94// All the Resource-specific functions are below.
95// The terraform provider supplies a single data source, `terraform_remote_state`
96// and no resources.
97
98// UpgradeResourceState is called when the state loader encounters an
99// instance state whose schema version is less than the one reported by the
100// currently-used version of the corresponding provider, and the upgraded
101// result is used for any further processing.
102func (p *Provider) UpgradeResourceState(providers.UpgradeResourceStateRequest) providers.UpgradeResourceStateResponse {
103	panic("unimplemented - terraform_remote_state has no resources")
104}
105
106// ReadResource refreshes a resource and returns its current state.
107func (p *Provider) ReadResource(providers.ReadResourceRequest) providers.ReadResourceResponse {
108	panic("unimplemented - terraform_remote_state has no resources")
109}
110
111// PlanResourceChange takes the current state and proposed state of a
112// resource, and returns the planned final state.
113func (p *Provider) PlanResourceChange(providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
114	panic("unimplemented - terraform_remote_state has no resources")
115}
116
117// ApplyResourceChange takes the planned state for a resource, which may
118// yet contain unknown computed values, and applies the changes returning
119// the final state.
120func (p *Provider) ApplyResourceChange(providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
121	panic("unimplemented - terraform_remote_state has no resources")
122}
123
124// ImportResourceState requests that the given resource be imported.
125func (p *Provider) ImportResourceState(providers.ImportResourceStateRequest) providers.ImportResourceStateResponse {
126	panic("unimplemented - terraform_remote_state has no resources")
127}
128
129// ValidateResourceConfig is used to to validate the resource configuration values.
130func (p *Provider) ValidateResourceConfig(providers.ValidateResourceConfigRequest) providers.ValidateResourceConfigResponse {
131	// At this moment there is nothing to configure for the terraform provider,
132	// so we will happily return without taking any action
133	var res providers.ValidateResourceConfigResponse
134	return res
135}
136
137// Close is a noop for this provider, since it's run in-process.
138func (p *Provider) Close() error {
139	return nil
140}
141