]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform/plugin/resource_provisioner.go
f0cc341f1306fd87b377c4f1870862e922e71506
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / plugin / resource_provisioner.go
1 package plugin
2
3 import (
4 "net/rpc"
5
6 "github.com/hashicorp/go-plugin"
7 "github.com/hashicorp/terraform/configs/configschema"
8 "github.com/hashicorp/terraform/terraform"
9 )
10
11 // ResourceProvisionerPlugin is the plugin.Plugin implementation.
12 type ResourceProvisionerPlugin struct {
13 ResourceProvisioner func() terraform.ResourceProvisioner
14 }
15
16 func (p *ResourceProvisionerPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
17 return &ResourceProvisionerServer{
18 Broker: b,
19 Provisioner: p.ResourceProvisioner(),
20 }, nil
21 }
22
23 func (p *ResourceProvisionerPlugin) Client(
24 b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
25 return &ResourceProvisioner{Broker: b, Client: c}, nil
26 }
27
28 // ResourceProvisioner is an implementation of terraform.ResourceProvisioner
29 // that communicates over RPC.
30 type ResourceProvisioner struct {
31 Broker *plugin.MuxBroker
32 Client *rpc.Client
33 }
34
35 func (p *ResourceProvisioner) GetConfigSchema() (*configschema.Block, error) {
36 panic("not implemented")
37 return nil, nil
38 }
39
40 func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
41 var resp ResourceProvisionerValidateResponse
42 args := ResourceProvisionerValidateArgs{
43 Config: c,
44 }
45
46 err := p.Client.Call("Plugin.Validate", &args, &resp)
47 if err != nil {
48 return nil, []error{err}
49 }
50
51 var errs []error
52 if len(resp.Errors) > 0 {
53 errs = make([]error, len(resp.Errors))
54 for i, err := range resp.Errors {
55 errs[i] = err
56 }
57 }
58
59 return resp.Warnings, errs
60 }
61
62 func (p *ResourceProvisioner) Apply(
63 output terraform.UIOutput,
64 s *terraform.InstanceState,
65 c *terraform.ResourceConfig) error {
66 id := p.Broker.NextId()
67 go p.Broker.AcceptAndServe(id, &UIOutputServer{
68 UIOutput: output,
69 })
70
71 var resp ResourceProvisionerApplyResponse
72 args := &ResourceProvisionerApplyArgs{
73 OutputId: id,
74 State: s,
75 Config: c,
76 }
77
78 err := p.Client.Call("Plugin.Apply", args, &resp)
79 if err != nil {
80 return err
81 }
82 if resp.Error != nil {
83 err = resp.Error
84 }
85
86 return err
87 }
88
89 func (p *ResourceProvisioner) Stop() error {
90 var resp ResourceProvisionerStopResponse
91 err := p.Client.Call("Plugin.Stop", new(interface{}), &resp)
92 if err != nil {
93 return err
94 }
95 if resp.Error != nil {
96 err = resp.Error
97 }
98
99 return err
100 }
101
102 func (p *ResourceProvisioner) Close() error {
103 return p.Client.Close()
104 }
105
106 type ResourceProvisionerValidateArgs struct {
107 Config *terraform.ResourceConfig
108 }
109
110 type ResourceProvisionerValidateResponse struct {
111 Warnings []string
112 Errors []*plugin.BasicError
113 }
114
115 type ResourceProvisionerApplyArgs struct {
116 OutputId uint32
117 State *terraform.InstanceState
118 Config *terraform.ResourceConfig
119 }
120
121 type ResourceProvisionerApplyResponse struct {
122 Error *plugin.BasicError
123 }
124
125 type ResourceProvisionerStopResponse struct {
126 Error *plugin.BasicError
127 }
128
129 // ResourceProvisionerServer is a net/rpc compatible structure for serving
130 // a ResourceProvisioner. This should not be used directly.
131 type ResourceProvisionerServer struct {
132 Broker *plugin.MuxBroker
133 Provisioner terraform.ResourceProvisioner
134 }
135
136 func (s *ResourceProvisionerServer) Apply(
137 args *ResourceProvisionerApplyArgs,
138 result *ResourceProvisionerApplyResponse) error {
139 conn, err := s.Broker.Dial(args.OutputId)
140 if err != nil {
141 *result = ResourceProvisionerApplyResponse{
142 Error: plugin.NewBasicError(err),
143 }
144 return nil
145 }
146 client := rpc.NewClient(conn)
147 defer client.Close()
148
149 output := &UIOutput{Client: client}
150
151 err = s.Provisioner.Apply(output, args.State, args.Config)
152 *result = ResourceProvisionerApplyResponse{
153 Error: plugin.NewBasicError(err),
154 }
155 return nil
156 }
157
158 func (s *ResourceProvisionerServer) Validate(
159 args *ResourceProvisionerValidateArgs,
160 reply *ResourceProvisionerValidateResponse) error {
161 warns, errs := s.Provisioner.Validate(args.Config)
162 berrs := make([]*plugin.BasicError, len(errs))
163 for i, err := range errs {
164 berrs[i] = plugin.NewBasicError(err)
165 }
166 *reply = ResourceProvisionerValidateResponse{
167 Warnings: warns,
168 Errors: berrs,
169 }
170 return nil
171 }
172
173 func (s *ResourceProvisionerServer) Stop(
174 _ interface{},
175 reply *ResourceProvisionerStopResponse) error {
176 err := s.Provisioner.Stop()
177 *reply = ResourceProvisionerStopResponse{
178 Error: plugin.NewBasicError(err),
179 }
180
181 return nil
182 }