Skip to content

Commit

Permalink
Use providers.Interface instead of terraform.ResourceProvider
Browse files Browse the repository at this point in the history
In Terraform v0.12, the provider protocol version is upgraded from 4 to 5,
and its implementation uses GRPCProvider.
  • Loading branch information
minamijoyo committed May 2, 2019
1 parent 5d97605 commit ed31b5e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 61 deletions.
17 changes: 6 additions & 11 deletions command/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ func (m *Meta) completePredictResourceType() complete.Predictor {

defer client.Kill()

res := client.Resources()

resourceTypes := []string{}
for _, r := range res {
resourceTypes = append(resourceTypes, r.Name)
resourceTypes, err := client.ResourceTypes()
if err != nil {
return nil
}

return resourceTypes
Expand All @@ -49,14 +47,11 @@ func (m *Meta) completePredictDataSource() complete.Predictor {

defer client.Kill()

res := client.DataSources()

dataSources := []string{}
for _, r := range res {
dataSources = append(dataSources, r.Name)
dataSources, err := client.DataSources()
if err != nil {
return nil
}

return dataSources

})
}
9 changes: 4 additions & 5 deletions command/data_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func (c *DataListCommand) Run(args []string) int {

defer client.Kill()

res := client.DataSources()

dataSources := []string{}
for _, r := range res {
dataSources = append(dataSources, r.Name)
dataSources, err := client.DataSources()
if err != nil {
c.UI.Error(err.Error())
return 1
}

c.UI.Output(strings.Join(dataSources, "\n"))
Expand Down
9 changes: 4 additions & 5 deletions command/resource_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ func (c *ResourceListCommand) Run(args []string) int {

defer client.Kill()

res := client.Resources()

resourceTypes := []string{}
for _, r := range res {
resourceTypes = append(resourceTypes, r.Name)
resourceTypes, err := client.ResourceTypes()
if err != nil {
c.UI.Error(err.Error())
return 1
}

c.UI.Output(strings.Join(resourceTypes, "\n"))
Expand Down
90 changes: 50 additions & 40 deletions tfschema/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"

"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/plugin/discovery"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/providers"
"github.com/mitchellh/go-homedir"
)

// Client represents a tfschema Client.
type Client struct {
// provider is a resource provider of Terraform.
provider terraform.ResourceProvider
// provider is a provider interface of Terraform.
provider providers.Interface
// pluginClient is a pointer to plugin client instance.
// The type of pluginClient is
// *github.com/hashicorp/terraform/vendor/github.com/hashicorp/go-plugin.Client.
Expand All @@ -38,17 +39,17 @@ func NewClient(providerName string) (*Client, error) {

// initialize a plugin client.
pluginClient := plugin.Client(*pluginMeta)
rpcClient, err := pluginClient.Client()
gRPCClient, err := pluginClient.Client()
if err != nil {
return nil, fmt.Errorf("Failed to initialize plugin: %s", err)
}

// create a new resource provider.
raw, err := rpcClient.Dispense(plugin.ProviderPluginName)
raw, err := gRPCClient.Dispense(plugin.ProviderPluginName)
if err != nil {
return nil, fmt.Errorf("Failed to dispense plugin: %s", err)
}
provider := raw.(terraform.ResourceProvider)
provider := raw.(*plugin.GRPCProvider)

return &Client{
provider: provider,
Expand Down Expand Up @@ -120,68 +121,77 @@ func pluginDirs() ([]string, error) {

// GetProviderSchema returns a type definiton of provider schema.
func (c *Client) GetProviderSchema() (*Block, error) {
req := &terraform.ProviderSchemaRequest{
ResourceTypes: []string{},
DataSources: []string{},
res := c.provider.GetSchema()
if res.Diagnostics.HasErrors() {
return nil, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err())
}

res, err := c.provider.GetSchema(req)
if err != nil {
return nil, fmt.Errorf("Failed to get schema from provider: %s", err)
}

b := NewBlock(res.Provider)
b := NewBlock(res.Provider.Block)
return b, nil
}

// GetResourceTypeSchema returns a type definiton of resource type.
func (c *Client) GetResourceTypeSchema(resourceType string) (*Block, error) {
req := &terraform.ProviderSchemaRequest{
ResourceTypes: []string{resourceType},
DataSources: []string{},
res := c.provider.GetSchema()
if res.Diagnostics.HasErrors() {
return nil, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err())
}

res, err := c.provider.GetSchema(req)
if err != nil {
return nil, fmt.Errorf("Failed to get schema from provider: %s", err)
}

if res.ResourceTypes[resourceType] == nil {
schema, ok := res.ResourceTypes[resourceType]
if !ok {
return nil, fmt.Errorf("Failed to find resource type: %s", resourceType)
}

b := NewBlock(res.ResourceTypes[resourceType])
b := NewBlock(schema.Block)
return b, nil
}

// GetDataSourceSchema returns a type definiton of data source.
func (c *Client) GetDataSourceSchema(dataSource string) (*Block, error) {
req := &terraform.ProviderSchemaRequest{
ResourceTypes: []string{},
DataSources: []string{dataSource},
}

res, err := c.provider.GetSchema(req)
if err != nil {
return nil, fmt.Errorf("Failed to get schema from provider: %s", err)
res := c.provider.GetSchema()
if res.Diagnostics.HasErrors() {
return nil, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err())
}

if res.DataSources[dataSource] == nil {
schema, ok := res.DataSources[dataSource]
if !ok {
return nil, fmt.Errorf("Failed to find data source: %s", dataSource)
}

b := NewBlock(res.DataSources[dataSource])
b := NewBlock(schema.Block)
return b, nil
}

// Resources returns a list of resource types.
func (c *Client) Resources() []terraform.ResourceType {
return c.provider.Resources()
// ResourceTypes returns a list of resource types.
func (c *Client) ResourceTypes() ([]string, error) {
res := c.provider.GetSchema()
if res.Diagnostics.HasErrors() {
return nil, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err())
}

keys := make([]string, 0, len(res.ResourceTypes))
for k := range res.ResourceTypes {
keys = append(keys, k)
}

sort.Strings(keys)
return keys, nil
}

// DataSources returns a list of data sources.
func (c *Client) DataSources() []terraform.DataSource {
return c.provider.DataSources()
func (c *Client) DataSources() ([]string, error) {
res := c.provider.GetSchema()
if res.Diagnostics.HasErrors() {
return nil, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err())
}

keys := make([]string, 0, len(res.DataSources))
for k := range res.DataSources {
keys = append(keys, k)
}

sort.Strings(keys)
return keys, nil
}

// Kill kills a process of the plugin.
Expand Down

0 comments on commit ed31b5e

Please sign in to comment.