2019-07-02 03:37:00 +02:00
|
|
|
package provider
|
2019-06-27 00:27:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-02 15:03:13 +02:00
|
|
|
"github.com/hashicorp/hcl2/hclwrite"
|
2019-07-02 00:13:58 +02:00
|
|
|
"github.com/hashicorp/terraform/plugin/discovery"
|
2019-06-27 00:27:39 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/plugin"
|
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"go.starlark.net/starlark"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProviderInstance struct {
|
|
|
|
name string
|
|
|
|
provider *plugin.GRPCProvider
|
2019-07-02 00:13:58 +02:00
|
|
|
meta discovery.PluginMeta
|
2019-06-27 00:27:39 +02:00
|
|
|
|
2019-06-27 09:48:37 +02:00
|
|
|
dataSources *MapSchemaIntance
|
|
|
|
resources *MapSchemaIntance
|
2019-06-27 00:27:39 +02:00
|
|
|
}
|
|
|
|
|
2019-07-02 00:13:58 +02:00
|
|
|
func NewProviderInstance(pm *PluginManager, name, version string) (*ProviderInstance, error) {
|
|
|
|
cli, meta := pm.Get(name, version)
|
2019-06-27 00:27:39 +02:00
|
|
|
rpc, err := cli.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpc.Dispense(plugin.ProviderPluginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := raw.(*plugin.GRPCProvider)
|
|
|
|
response := provider.GetSchema()
|
|
|
|
|
|
|
|
defer cli.Kill()
|
|
|
|
return &ProviderInstance{
|
|
|
|
name: name,
|
|
|
|
provider: provider,
|
2019-07-02 00:13:58 +02:00
|
|
|
meta: meta,
|
2019-06-27 09:48:37 +02:00
|
|
|
dataSources: NewMapSchemaInstance(name, response.DataSources),
|
|
|
|
resources: NewMapSchemaInstance(name, response.ResourceTypes),
|
2019-06-27 00:27:39 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ProviderInstance) String() string {
|
|
|
|
return fmt.Sprintf("provider(%q)", t.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ProviderInstance) Type() string {
|
|
|
|
return "provider-instance"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ProviderInstance) Freeze() {}
|
|
|
|
func (t *ProviderInstance) Truth() starlark.Bool { return true }
|
|
|
|
func (t *ProviderInstance) Hash() (uint32, error) { return 1, nil }
|
|
|
|
func (t *ProviderInstance) Name() string { return t.name }
|
2019-06-27 09:48:37 +02:00
|
|
|
func (s *ProviderInstance) Attr(name string) (starlark.Value, error) {
|
|
|
|
switch name {
|
2019-07-02 00:13:58 +02:00
|
|
|
case "version":
|
|
|
|
return starlark.String(s.meta.Version), nil
|
2019-06-27 09:48:37 +02:00
|
|
|
case "data":
|
|
|
|
return s.dataSources, nil
|
|
|
|
case "resource":
|
|
|
|
return s.resources, nil
|
2019-07-02 15:03:13 +02:00
|
|
|
case "to_hcl":
|
|
|
|
return BuiltinToHCL(s, hclwrite.NewEmptyFile()), nil
|
2019-06-27 09:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return starlark.None, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProviderInstance) AttrNames() []string {
|
|
|
|
return []string{"data", "resource"}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MapSchemaIntance struct {
|
2019-07-02 03:37:00 +02:00
|
|
|
prefix string
|
|
|
|
schemas map[string]providers.Schema
|
|
|
|
collections map[string]*ResourceCollection
|
2019-06-27 09:48:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMapSchemaInstance(prefix string, schemas map[string]providers.Schema) *MapSchemaIntance {
|
2019-07-02 03:37:00 +02:00
|
|
|
return &MapSchemaIntance{
|
|
|
|
prefix: prefix,
|
|
|
|
schemas: schemas,
|
|
|
|
collections: make(map[string]*ResourceCollection),
|
|
|
|
}
|
2019-06-27 09:48:37 +02:00
|
|
|
}
|
2019-06-27 00:27:39 +02:00
|
|
|
|
2019-06-27 09:48:37 +02:00
|
|
|
func (t *MapSchemaIntance) String() string {
|
|
|
|
return fmt.Sprintf("schemas(%q)", t.prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *MapSchemaIntance) Type() string {
|
|
|
|
return "schemas"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *MapSchemaIntance) Freeze() {}
|
|
|
|
func (t *MapSchemaIntance) Truth() starlark.Bool { return true }
|
|
|
|
func (t *MapSchemaIntance) Hash() (uint32, error) { return 1, nil }
|
|
|
|
func (t *MapSchemaIntance) Name() string { return t.prefix }
|
|
|
|
|
|
|
|
func (s *MapSchemaIntance) Attr(name string) (starlark.Value, error) {
|
2019-07-02 15:03:13 +02:00
|
|
|
if name == "to_hcl" {
|
|
|
|
return BuiltinToHCL(s, hclwrite.NewEmptyFile()), nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 09:48:37 +02:00
|
|
|
name = s.prefix + "_" + name
|
|
|
|
|
2019-07-02 03:37:00 +02:00
|
|
|
if c, ok := s.collections[name]; ok {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 09:48:37 +02:00
|
|
|
if schema, ok := s.schemas[name]; ok {
|
2019-07-02 03:37:00 +02:00
|
|
|
s.collections[name] = NewResourceCollection(name, false, schema.Block)
|
|
|
|
return s.collections[name], nil
|
2019-06-27 00:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return starlark.None, nil
|
|
|
|
}
|
|
|
|
|
2019-06-27 09:48:37 +02:00
|
|
|
func (s *MapSchemaIntance) AttrNames() []string {
|
|
|
|
names := make([]string, len(s.schemas))
|
2019-06-27 00:27:39 +02:00
|
|
|
|
|
|
|
var i int
|
2019-06-27 09:48:37 +02:00
|
|
|
for k := range s.schemas {
|
2019-06-27 00:27:39 +02:00
|
|
|
parts := strings.SplitN(k, "_", 2)
|
|
|
|
names[i] = parts[1]
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|