github.com/hashicorp/terraform-plugin-sdk@v1.17.2/plugin/ui_input.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"net/rpc"
     6  
     7  	"github.com/hashicorp/go-plugin"
     8  	"github.com/hashicorp/terraform-plugin-sdk/terraform"
     9  )
    10  
    11  // UIInput is an implementation of terraform.UIInput that communicates
    12  // over RPC.
    13  type UIInput struct {
    14  	Client *rpc.Client
    15  }
    16  
    17  func (i *UIInput) Input(ctx context.Context, opts *terraform.InputOpts) (string, error) {
    18  	var resp UIInputInputResponse
    19  	err := i.Client.Call("Plugin.Input", opts, &resp)
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  	if resp.Error != nil {
    24  		err = resp.Error
    25  		return "", err
    26  	}
    27  
    28  	return resp.Value, nil
    29  }
    30  
    31  type UIInputInputResponse struct {
    32  	Value string
    33  	Error *plugin.BasicError
    34  }
    35  
    36  // UIInputServer is a net/rpc compatible structure for serving
    37  // a UIInputServer. This should not be used directly.
    38  type UIInputServer struct {
    39  	UIInput terraform.UIInput
    40  }
    41  
    42  func (s *UIInputServer) Input(
    43  	opts *terraform.InputOpts,
    44  	reply *UIInputInputResponse) error {
    45  	value, err := s.UIInput.Input(context.Background(), opts)
    46  	*reply = UIInputInputResponse{
    47  		Value: value,
    48  		Error: plugin.NewBasicError(err),
    49  	}
    50  
    51  	return nil
    52  }