terraform/rpc/resource_provider.go

224 lines
4.8 KiB
Go
Raw Normal View History

2014-05-29 00:07:47 +02:00
package rpc
import (
"net/rpc"
"github.com/hashicorp/terraform/terraform"
)
2014-05-29 06:27:10 +02:00
// ResourceProvider is an implementation of terraform.ResourceProvider
// that communicates over RPC.
2014-05-29 00:07:47 +02:00
type ResourceProvider struct {
Client *rpc.Client
Name string
2014-05-29 00:07:47 +02:00
}
2014-06-13 07:37:46 +02:00
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
var resp ResourceProviderValidateResponse
args := ResourceProviderValidateArgs{
Config: c,
}
err := p.Client.Call(p.Name+".Validate", &args, &resp)
2014-06-13 07:37:46 +02:00
if err != nil {
return nil, []error{err}
}
var errs []error
if len(resp.Errors) > 0 {
errs = make([]error, len(resp.Errors))
for i, err := range resp.Errors {
errs[i] = err
}
}
return resp.Warnings, errs
}
2014-06-13 03:06:26 +02:00
func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
2014-05-29 00:07:47 +02:00
var resp ResourceProviderConfigureResponse
err := p.Client.Call(p.Name+".Configure", c, &resp)
2014-06-06 09:48:32 +02:00
if err != nil {
return err
}
if resp.Error != nil {
err = resp.Error
}
return err
}
2014-06-19 01:49:34 +02:00
func (p *ResourceProvider) Apply(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*terraform.ResourceState, error) {
var resp ResourceProviderApplyResponse
args := &ResourceProviderApplyArgs{
State: s,
Diff: d,
}
err := p.Client.Call(p.Name+".Apply", args, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
err = resp.Error
}
return resp.State, err
}
2014-06-06 09:48:32 +02:00
func (p *ResourceProvider) Diff(
s *terraform.ResourceState,
2014-06-13 03:06:26 +02:00
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
2014-06-06 09:48:32 +02:00
var resp ResourceProviderDiffResponse
args := &ResourceProviderDiffArgs{
State: s,
Config: c,
}
err := p.Client.Call(p.Name+".Diff", args, &resp)
2014-05-29 00:07:47 +02:00
if err != nil {
return nil, err
}
2014-05-29 00:31:09 +02:00
if resp.Error != nil {
err = resp.Error
}
2014-05-29 00:07:47 +02:00
2014-06-06 09:48:32 +02:00
return resp.Diff, err
2014-05-29 00:07:47 +02:00
}
2014-06-20 20:51:07 +02:00
func (p *ResourceProvider) Refresh(
s *terraform.ResourceState) (*terraform.ResourceState, error) {
var resp ResourceProviderRefreshResponse
err := p.Client.Call(p.Name+".Refresh", s, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
err = resp.Error
}
return resp.State, err
}
func (p *ResourceProvider) Resources() []terraform.ResourceType {
var result []terraform.ResourceType
err := p.Client.Call(p.Name+".Resources", new(interface{}), &result)
if err != nil {
// TODO: panic, log, what?
return nil
}
return result
}
2014-05-29 06:27:10 +02:00
// ResourceProviderServer is a net/rpc compatible structure for serving
// a ResourceProvider. This should not be used directly.
2014-05-29 00:07:47 +02:00
type ResourceProviderServer struct {
Provider terraform.ResourceProvider
}
type ResourceProviderConfigureResponse struct {
2014-06-06 09:48:32 +02:00
Error *BasicError
}
2014-06-19 01:49:34 +02:00
type ResourceProviderApplyArgs struct {
State *terraform.ResourceState
Diff *terraform.ResourceDiff
}
type ResourceProviderApplyResponse struct {
State *terraform.ResourceState
Error *BasicError
}
2014-06-06 09:48:32 +02:00
type ResourceProviderDiffArgs struct {
State *terraform.ResourceState
2014-06-13 03:06:26 +02:00
Config *terraform.ResourceConfig
2014-06-06 09:48:32 +02:00
}
type ResourceProviderDiffResponse struct {
Diff *terraform.ResourceDiff
Error *BasicError
2014-05-29 00:07:47 +02:00
}
2014-06-20 20:51:07 +02:00
type ResourceProviderRefreshResponse struct {
State *terraform.ResourceState
Error *BasicError
}
type ResourceProviderValidateArgs struct {
Config *terraform.ResourceConfig
}
2014-06-13 07:37:46 +02:00
type ResourceProviderValidateResponse struct {
Warnings []string
Errors []*BasicError
}
func (s *ResourceProviderServer) Validate(
args *ResourceProviderValidateArgs,
2014-06-13 07:37:46 +02:00
reply *ResourceProviderValidateResponse) error {
warns, errs := s.Provider.Validate(args.Config)
2014-06-13 07:37:46 +02:00
berrs := make([]*BasicError, len(errs))
for i, err := range errs {
berrs[i] = NewBasicError(err)
}
*reply = ResourceProviderValidateResponse{
Warnings: warns,
Errors: berrs,
}
return nil
}
2014-05-29 00:07:47 +02:00
func (s *ResourceProviderServer) Configure(
2014-06-13 03:06:26 +02:00
config *terraform.ResourceConfig,
2014-05-29 00:07:47 +02:00
reply *ResourceProviderConfigureResponse) error {
2014-06-06 09:48:32 +02:00
err := s.Provider.Configure(config)
2014-05-29 00:07:47 +02:00
*reply = ResourceProviderConfigureResponse{
2014-06-06 09:48:32 +02:00
Error: NewBasicError(err),
}
return nil
}
2014-06-19 01:49:34 +02:00
func (s *ResourceProviderServer) Apply(
args *ResourceProviderApplyArgs,
result *ResourceProviderApplyResponse) error {
state, err := s.Provider.Apply(args.State, args.Diff)
*result = ResourceProviderApplyResponse{
State: state,
Error: NewBasicError(err),
}
return nil
}
2014-06-06 09:48:32 +02:00
func (s *ResourceProviderServer) Diff(
args *ResourceProviderDiffArgs,
result *ResourceProviderDiffResponse) error {
diff, err := s.Provider.Diff(args.State, args.Config)
*result = ResourceProviderDiffResponse{
Diff: diff,
Error: NewBasicError(err),
2014-05-29 00:07:47 +02:00
}
return nil
}
2014-06-20 20:51:07 +02:00
func (s *ResourceProviderServer) Refresh(
state *terraform.ResourceState,
result *ResourceProviderRefreshResponse) error {
newState, err := s.Provider.Refresh(state)
*result = ResourceProviderRefreshResponse{
State: newState,
Error: NewBasicError(err),
}
return nil
}
func (s *ResourceProviderServer) Resources(
nothing interface{},
result *[]terraform.ResourceType) error {
*result = s.Provider.Resources()
return nil
}