generated from hashicorp/terraform-provider-scaffolding
/
bootstrap_resource.go
153 lines (122 loc) · 3.88 KB
/
bootstrap_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package provider
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/talos-systems/talos/pkg/machinery/api/machine"
"github.com/talos-systems/talos/pkg/machinery/constants"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var _ resource.Resource = &BootstrapResource{}
func NewBootstrapResource() resource.Resource {
return &BootstrapResource{}
}
type BootstrapResource struct{}
type BootstrapResourceModel = KubeconfigDataSourceModel
func (r *BootstrapResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_bootstrap"
}
func (r *BootstrapResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Bootstrap a Talos cluster and download kubeconfig.",
Attributes: attributes,
}, nil
}
func (r *BootstrapResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *BootstrapResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
clientCert, err := tls.X509KeyPair(
[]byte(data.MachineCrt.Value),
[]byte(data.MachineKey.Value),
)
if err != nil {
resp.Diagnostics.AddError(
"Error parsing key pair",
err.Error(),
)
return
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM([]byte(data.MachineCa.Value)) {
resp.Diagnostics.AddError(
"failed to add server CA's certificate",
"",
)
return
}
tlsCredentials := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{clientCert},
RootCAs: certPool,
})
conn, err := grpc.Dial(
fmt.Sprintf("%s:%d", data.Endpoint.Value, constants.ApidPort),
grpc.WithTransportCredentials(tlsCredentials),
grpc.WithBlock(),
)
if err != nil {
resp.Diagnostics.AddError(
"Error creating gRPC connection",
err.Error(),
)
return
}
defer conn.Close()
client := machine.NewMachineServiceClient(conn)
if _, err := client.Bootstrap(ctx, &machine.BootstrapRequest{}); err != nil {
resp.Diagnostics.AddError(
"Error in bootstrap request",
err.Error(),
)
return
}
if err := kubeconfigRead(ctx, client, data); err != nil {
resp.Diagnostics.AddError(
"Error reading kubeconfig",
err.Error(),
)
return
}
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Trace(ctx, "created a Talos bootstrap resource")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *BootstrapResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *BootstrapResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *BootstrapResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *BootstrapResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *BootstrapResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *BootstrapResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}