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