2014-06-03 23:26:31 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 19:40:40 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2018-05-24 21:53:34 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-06-03 23:26:31 +02:00
|
|
|
)
|
|
|
|
|
2018-05-08 02:18:17 +02:00
|
|
|
// mockProviderWithConfigSchema is a test helper to concisely create a mock
|
|
|
|
// provider with the given schema for its own configuration.
|
2018-08-17 21:32:35 +02:00
|
|
|
func mockProviderWithConfigSchema(schema *configschema.Block) *MockProvider {
|
|
|
|
return &MockProvider{
|
2021-02-18 16:13:43 +01:00
|
|
|
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
|
2021-01-11 21:45:50 +01:00
|
|
|
Provider: providers.Schema{Block: schema},
|
2018-05-08 02:18:17 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockProviderWithResourceTypeSchema is a test helper to concisely create a mock
|
|
|
|
// provider with a schema containing a single resource type.
|
2018-08-17 21:32:35 +02:00
|
|
|
func mockProviderWithResourceTypeSchema(name string, schema *configschema.Block) *MockProvider {
|
|
|
|
return &MockProvider{
|
2021-02-18 16:13:43 +01:00
|
|
|
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
|
2021-01-11 21:45:50 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"string": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"list": {
|
|
|
|
Type: cty.List(cty.String),
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"root": {
|
|
|
|
Type: cty.Map(cty.String),
|
|
|
|
Optional: true,
|
|
|
|
},
|
2018-05-24 21:53:34 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-11 21:45:50 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
name: providers.Schema{Block: schema},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockProviderWithProviderSchema is a test helper to create a mock provider
|
|
|
|
// from an existing ProviderSchema.
|
|
|
|
func mockProviderWithProviderSchema(providerSchema ProviderSchema) *MockProvider {
|
|
|
|
p := &MockProvider{
|
2021-02-18 16:13:43 +01:00
|
|
|
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
|
2021-01-11 21:45:50 +01:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: providerSchema.Provider,
|
2018-05-08 02:18:17 +02:00
|
|
|
},
|
2021-01-11 21:45:50 +01:00
|
|
|
ResourceTypes: map[string]providers.Schema{},
|
|
|
|
DataSources: map[string]providers.Schema{},
|
2018-05-08 02:18:17 +02:00
|
|
|
},
|
|
|
|
}
|
2021-01-11 21:45:50 +01:00
|
|
|
|
|
|
|
for name, schema := range providerSchema.ResourceTypes {
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse.ResourceTypes[name] = providers.Schema{
|
2021-01-11 21:45:50 +01:00
|
|
|
Block: schema,
|
|
|
|
Version: int64(providerSchema.ResourceTypeSchemaVersions[name]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, schema := range providerSchema.DataSources {
|
2021-02-18 16:13:43 +01:00
|
|
|
p.GetProviderSchemaResponse.DataSources[name] = providers.Schema{Block: schema}
|
2021-01-11 21:45:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-02-18 16:13:43 +01:00
|
|
|
// getProviderSchemaResponseFromProviderSchema is a test helper to convert a
|
|
|
|
// ProviderSchema to a GetProviderSchemaResponse for use when building a mock provider.
|
|
|
|
func getProviderSchemaResponseFromProviderSchema(providerSchema *ProviderSchema) *providers.GetProviderSchemaResponse {
|
|
|
|
resp := &providers.GetProviderSchemaResponse{
|
2021-01-11 21:45:50 +01:00
|
|
|
Provider: providers.Schema{Block: providerSchema.Provider},
|
|
|
|
ProviderMeta: providers.Schema{Block: providerSchema.ProviderMeta},
|
|
|
|
ResourceTypes: map[string]providers.Schema{},
|
|
|
|
DataSources: map[string]providers.Schema{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, schema := range providerSchema.ResourceTypes {
|
|
|
|
resp.ResourceTypes[name] = providers.Schema{
|
|
|
|
Block: schema,
|
|
|
|
Version: int64(providerSchema.ResourceTypeSchemaVersions[name]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, schema := range providerSchema.DataSources {
|
|
|
|
resp.DataSources[name] = providers.Schema{Block: schema}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2018-05-08 02:18:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 21:32:35 +02:00
|
|
|
// simpleMockProvider returns a MockProvider that is pre-configured
|
2018-05-10 01:08:10 +02:00
|
|
|
// with schema for its own config, for a resource type called "test_object" and
|
|
|
|
// for a data source also called "test_object".
|
|
|
|
//
|
|
|
|
// All three schemas have the same content as returned by function
|
|
|
|
// simpleTestSchema.
|
|
|
|
//
|
|
|
|
// For most reasonable uses the returned provider must be registered in a
|
|
|
|
// componentFactory under the name "test". Use simpleMockComponentFactory
|
|
|
|
// to obtain a pre-configured componentFactory containing the result of
|
|
|
|
// this function along with simpleMockProvisioner, both registered as "test".
|
|
|
|
//
|
|
|
|
// The returned provider has no other behaviors by default, but the caller may
|
|
|
|
// modify it in order to stub any other required functionality, or modify
|
|
|
|
// the default schema stored in the field GetSchemaReturn. Each new call to
|
|
|
|
// simpleTestProvider produces entirely new instances of all of the nested
|
|
|
|
// objects so that callers can mutate without affecting mock objects.
|
2018-08-17 21:32:35 +02:00
|
|
|
func simpleMockProvider() *MockProvider {
|
|
|
|
return &MockProvider{
|
2021-02-18 16:13:43 +01:00
|
|
|
GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{
|
2021-01-11 21:45:50 +01:00
|
|
|
Provider: providers.Schema{Block: simpleTestSchema()},
|
|
|
|
ResourceTypes: map[string]providers.Schema{
|
|
|
|
"test_object": providers.Schema{Block: simpleTestSchema()},
|
2018-08-17 21:32:35 +02:00
|
|
|
},
|
2021-01-11 21:45:50 +01:00
|
|
|
DataSources: map[string]providers.Schema{
|
|
|
|
"test_object": providers.Schema{Block: simpleTestSchema()},
|
2018-05-10 01:08:10 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|