-
Notifications
You must be signed in to change notification settings - Fork 1
/
datasource_environment.go
227 lines (197 loc) · 6.91 KB
/
datasource_environment.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp-forge/terraform-provider-enos/internal/diags"
"github.com/hashicorp-forge/terraform-provider-enos/internal/server/datarouter"
"github.com/hashicorp-forge/terraform-provider-enos/internal/server/state"
)
type environment struct {
providerConfig *config
}
var _ datarouter.DataSource = (*environment)(nil)
type environmentStateV1 struct {
ID *tfString
PublicIPAddress *tfString
PublicIPAddresses *tfStringSlice
PublicIPV4Addresses *tfStringSlice
PublicIPV6Addresses *tfStringSlice
failureHandlers
}
var _ state.State = (*environmentStateV1)(nil)
func newEnvironment() *environment {
return &environment{
providerConfig: newProviderConfig(),
}
}
func newEnvironmentStateV1() *environmentStateV1 {
return &environmentStateV1{
ID: newTfString(),
PublicIPAddress: newTfString(),
PublicIPAddresses: newTfStringSlice(),
PublicIPV4Addresses: newTfStringSlice(),
PublicIPV6Addresses: newTfStringSlice(),
failureHandlers: failureHandlers{},
}
}
func (d *environment) Name() string {
return "enos_environment"
}
func (d *environment) Schema() *tfprotov6.Schema {
return newEnvironmentStateV1().Schema()
}
func (d *environment) SetProviderConfig(meta tftypes.Value) error {
return d.providerConfig.FromTerraform5Value(meta)
}
// ValidateDataResourceConfig is the request Terraform sends when it wants to
// validate the data source's configuration.
func (d *environment) ValidateDataResourceConfig(ctx context.Context, req tfprotov6.ValidateDataResourceConfigRequest, res *tfprotov6.ValidateDataResourceConfigResponse) {
select {
case <-ctx.Done():
res.Diagnostics = append(res.Diagnostics, ctxToDiagnostic(ctx))
return
default:
}
// unmarshal it to our known type to ensure whatever was passed in matches
// the correct schema.
newConfig := newEnvironmentStateV1()
err := unmarshal(newConfig, req.Config)
if err != nil {
res.Diagnostics = append(res.Diagnostics, diags.ErrToDiagnostic("Serialization Error", err))
}
}
// ReadDataSource is the request Terraform sends when it wants to get the latest
// state for the data source.
func (d *environment) ReadDataSource(ctx context.Context, req tfprotov6.ReadDataSourceRequest, res *tfprotov6.ReadDataSourceResponse) {
select {
case <-ctx.Done():
res.Diagnostics = append(res.Diagnostics, ctxToDiagnostic(ctx))
return
default:
}
newState := newEnvironmentStateV1()
// unmarshal and re-marshal the state to add default fields
err := unmarshal(newState, req.Config)
if err != nil {
res.Diagnostics = append(res.Diagnostics, diags.ErrToDiagnostic("Serialization Error", err))
return
}
newState.ID.Set("static")
resolver := newPublicIPResolver()
err = resolver.resolve(ctx, defaultResolvers()...)
if len(resolver.ips()) == 0 {
err = errors.Join(err, errors.New("unable to resolve public ip address"))
}
if err != nil {
res.Diagnostics = append(res.Diagnostics,
diags.ErrToDiagnostic("Resolve IP Error", fmt.Errorf("failed to resolve public IP addresses, due to: %w", err)),
)
return
}
newState.PublicIPAddress.Set(resolver.ipsStrings()[0])
newState.PublicIPAddresses.SetStrings(resolver.ipsStrings())
newState.PublicIPV4Addresses.SetStrings(resolver.v4Strings())
newState.PublicIPV6Addresses.SetStrings(resolver.v6Strings())
res.State, err = state.Marshal(newState)
if err != nil {
res.Diagnostics = append(res.Diagnostics, diags.ErrToDiagnostic("Serialization Error", err))
}
}
// Schema is the file states Terraform schema.
func (s *environmentStateV1) Schema() *tfprotov6.Schema {
return &tfprotov6.Schema{
Version: 1,
Block: &tfprotov6.SchemaBlock{
DescriptionKind: tfprotov6.StringKindMarkdown,
Description: docCaretToBacktick(`The ^enos_environment^ datasource is a datasource that we can use to pass environment specific
information into our Terraform run. As enos relies on SSH to execute the bulk of its actions, a
common problem is granting access to the host executing the Terraform run. As such, the
enos_environment resource can be used to determine our external IP addresses so that we can dynamically
generate security groups that allow only access from our end.
`),
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "id",
Type: tftypes.String,
Computed: true,
Description: datasourceStaticIDDescription,
},
{
Name: "public_ip_address",
Type: tftypes.String,
Computed: true,
Deprecated: true, // Use public_ip_addresses
},
{
Name: "public_ip_addresses",
Type: tftypes.List{
ElementType: tftypes.String,
},
Computed: true,
Description: `All public IP addresses of the host executing Terraform. NOTE: can include both ipv4 and ipv6 addresses`,
},
{
Name: "public_ipv4_addresses",
Type: tftypes.List{
ElementType: tftypes.String,
},
Computed: true,
Description: `The public IPv4 addresses of the host executing Terraform`,
},
{
Name: "public_ipv6_addresses",
Type: tftypes.List{
ElementType: tftypes.String,
},
Computed: true,
Description: "The public IPv6 addresses of the host executing Terraform",
},
},
},
}
}
// Validate validates the configuration.
func (s *environmentStateV1) Validate(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return nil
}
// FromTerraform5Value is a callback to unmarshal from the tftypes.Vault with As().
func (s *environmentStateV1) FromTerraform5Value(val tftypes.Value) error {
_, err := mapAttributesTo(val, map[string]interface{}{
"id": s.ID,
"public_ip_address": s.PublicIPAddress,
"public_ip_addresses": s.PublicIPAddresses,
"public_ipv4_addresses": s.PublicIPV4Addresses,
"public_ipv6_addresses": s.PublicIPV6Addresses,
})
return err
}
// Terraform5Type is the file state tftypes.Type.
func (s *environmentStateV1) Terraform5Type() tftypes.Type {
return tftypes.Object{AttributeTypes: map[string]tftypes.Type{
"id": s.ID.TFType(),
"public_ip_address": s.PublicIPAddress.TFType(),
"public_ip_addresses": s.PublicIPAddresses.TFType(),
"public_ipv4_addresses": s.PublicIPV4Addresses.TFType(),
"public_ipv6_addresses": s.PublicIPV6Addresses.TFType(),
}}
}
// Terraform5Value is the file state tftypes.Value.
func (s *environmentStateV1) Terraform5Value() tftypes.Value {
return tftypes.NewValue(s.Terraform5Type(), map[string]tftypes.Value{
"id": s.ID.TFValue(),
"public_ip_address": s.PublicIPAddress.TFValue(),
"public_ip_addresses": s.PublicIPAddresses.TFValue(),
"public_ipv4_addresses": s.PublicIPV4Addresses.TFValue(),
"public_ipv6_addresses": s.PublicIPV6Addresses.TFValue(),
})
}