-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compatibility layer for Terraform v0.11/v0.12+
- Loading branch information
1 parent
06e4a39
commit 3c5f7a2
Showing
4 changed files
with
289 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package tfschema | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/hashicorp/terraform/providers" | ||
) | ||
|
||
// GRPCClient implements Client interface. | ||
// This implementaion is for Terraform v0.12+. | ||
type GRPCClient struct { | ||
// provider is a provider interface of Terraform. | ||
provider providers.Interface | ||
} | ||
|
||
// getSchema is a helper function to get a schema from provider. | ||
func (c *GRPCClient) getSchema() (providers.GetSchemaResponse, error) { | ||
res := c.provider.GetSchema() | ||
if res.Diagnostics.HasErrors() { | ||
return res, fmt.Errorf("Failed to get schema from provider: %s", res.Diagnostics.Err()) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
// GetProviderSchema returns a type definiton of provider schema. | ||
func (c *GRPCClient) GetProviderSchema() (*Block, error) { | ||
res, err := c.getSchema() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
b := NewBlock(res.Provider.Block) | ||
return b, nil | ||
} | ||
|
||
// GetResourceTypeSchema returns a type definiton of resource type. | ||
func (c *GRPCClient) GetResourceTypeSchema(resourceType string) (*Block, error) { | ||
res, err := c.getSchema() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
schema, ok := res.ResourceTypes[resourceType] | ||
if !ok { | ||
return nil, fmt.Errorf("Failed to find resource type: %s", resourceType) | ||
} | ||
|
||
b := NewBlock(schema.Block) | ||
return b, nil | ||
} | ||
|
||
// GetDataSourceSchema returns a type definiton of data source. | ||
func (c *GRPCClient) GetDataSourceSchema(dataSource string) (*Block, error) { | ||
res, err := c.getSchema() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
schema, ok := res.DataSources[dataSource] | ||
if !ok { | ||
return nil, fmt.Errorf("Failed to find data source: %s", dataSource) | ||
} | ||
|
||
b := NewBlock(schema.Block) | ||
return b, nil | ||
} | ||
|
||
// ResourceTypes returns a list of resource types. | ||
func (c *GRPCClient) ResourceTypes() ([]string, error) { | ||
res, err := c.getSchema() | ||
if err != nil { | ||
return nil, 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 *GRPCClient) DataSources() ([]string, error) { | ||
res, err := c.getSchema() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keys := make([]string, 0, len(res.DataSources)) | ||
for k := range res.DataSources { | ||
keys = append(keys, k) | ||
} | ||
|
||
sort.Strings(keys) | ||
return keys, nil | ||
} | ||
|
||
// Close closes a connection and kills a process of the plugin. | ||
func (c *GRPCClient) Close() { | ||
c.provider.Close() | ||
} |
Oops, something went wrong.