2020-11-20 10:30:12 -06:00
|
|
|
// simple provider a minimal provider implementation for testing
|
|
|
|
package simple
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type simple struct {
|
2021-02-18 09:13:43 -06:00
|
|
|
schema providers.GetProviderSchemaResponse
|
2020-11-20 10:30:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Provider() providers.Interface {
|
|
|
|
simpleResource := providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {
|
|
|
|
Computed: true,
|
|
|
|
Type: cty.String,
|
|
|
|
},
|
|
|
|
"value": {
|
|
|
|
Optional: true,
|
|
|
|
Type: cty.String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return simple{
|
2021-02-18 09:13:43 -06:00
|
|
|
schema: providers.GetProviderSchemaResponse{
|
2020-11-20 10:30:12 -06:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: nil,
|
|
|
|
},
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"simple_resource": simpleResource,
|
|
|
|
},
|
|
|
|
DataSources: map[string]providers.Schema{
|
|
|
|
"simple_resource": simpleResource,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (s simple) GetProviderSchema() providers.GetProviderSchemaResponse {
|
2020-11-20 10:30:12 -06:00
|
|
|
return s.schema
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (s simple) ValidateProviderConfig(req providers.ValidateProviderConfigRequest) (resp providers.ValidateProviderConfigResponse) {
|
2020-11-20 10:30:12 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (s simple) ValidateResourceConfig(req providers.ValidateResourceConfigRequest) (resp providers.ValidateResourceConfigResponse) {
|
2020-11-20 10:30:12 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:04:28 -06:00
|
|
|
func (s simple) ValidateDataResourceConfig(req providers.ValidateDataResourceConfigRequest) (resp providers.ValidateDataResourceConfigResponse) {
|
2020-11-20 10:30:12 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p simple) UpgradeResourceState(req providers.UpgradeResourceStateRequest) (resp providers.UpgradeResourceStateResponse) {
|
|
|
|
ty := p.schema.ResourceTypes[req.TypeName].Block.ImpliedType()
|
|
|
|
val, err := ctyjson.Unmarshal(req.RawStateJSON, ty)
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(err)
|
|
|
|
resp.UpgradedState = val
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
func (s simple) ConfigureProvider(providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
2020-11-20 10:30:12 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) ReadResource(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
|
|
|
|
// just return the same state we received
|
|
|
|
resp.NewState = req.PriorState
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) PlanResourceChange(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
m := req.ProposedNewState.AsValueMap()
|
|
|
|
_, ok := m["id"]
|
|
|
|
if !ok {
|
|
|
|
m["id"] = cty.UnknownVal(cty.String)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.PlannedState = cty.ObjectVal(m)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) ApplyResourceChange(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
|
|
|
|
if req.PlannedState.IsNull() {
|
|
|
|
resp.NewState = req.PlannedState
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
m := req.PlannedState.AsValueMap()
|
|
|
|
_, ok := m["id"]
|
|
|
|
if !ok {
|
|
|
|
m["id"] = cty.StringVal(time.Now().String())
|
|
|
|
}
|
|
|
|
resp.NewState = cty.ObjectVal(m)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) ImportResourceState(providers.ImportResourceStateRequest) (resp providers.ImportResourceStateResponse) {
|
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(errors.New("unsupported"))
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) ReadDataSource(req providers.ReadDataSourceRequest) (resp providers.ReadDataSourceResponse) {
|
|
|
|
m := req.Config.AsValueMap()
|
|
|
|
m["id"] = cty.StringVal("static_id")
|
|
|
|
resp.State = cty.ObjectVal(m)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s simple) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|