@@ -4,49 +4,124 @@ import (
4
4
"context"
5
5
6
6
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
7
+ "github.com/hashicorp/terraform-plugin-framework/attr"
7
8
"github.com/hashicorp/terraform-plugin-framework/diag"
8
9
"github.com/hashicorp/terraform-plugin-framework/types"
9
10
"github.com/pomerium/enterprise-client-go/pb"
10
11
)
11
12
12
13
// RouteModel represents the shared model for route resources and data sources
13
14
type RouteModel struct {
14
- ID types.String `tfsdk:"id"`
15
- Name types.String `tfsdk:"name"`
16
- From types.String `tfsdk:"from"`
17
- To types.Set `tfsdk:"to"`
18
- NamespaceID types.String `tfsdk:"namespace_id"`
19
- Policies types.Set `tfsdk:"policies"`
20
- StatName types.String `tfsdk:"stat_name"`
21
- Prefix types.String `tfsdk:"prefix"`
22
- Path types.String `tfsdk:"path"`
23
- Regex types.String `tfsdk:"regex"`
24
- PrefixRewrite types.String `tfsdk:"prefix_rewrite"`
25
- RegexRewritePattern types.String `tfsdk:"regex_rewrite_pattern"`
26
- RegexRewriteSubstitution types.String `tfsdk:"regex_rewrite_substitution"`
27
- HostRewrite types.String `tfsdk:"host_rewrite"`
28
- HostRewriteHeader types.String `tfsdk:"host_rewrite_header"`
29
- HostPathRegexRewritePattern types.String `tfsdk:"host_path_regex_rewrite_pattern"`
30
- HostPathRegexRewriteSubstitution types.String `tfsdk:"host_path_regex_rewrite_substitution"`
31
- RegexPriorityOrder types.Int64 `tfsdk:"regex_priority_order"`
32
- Timeout timetypes.GoDuration `tfsdk:"timeout"`
33
- IdleTimeout timetypes.GoDuration `tfsdk:"idle_timeout"`
34
- AllowWebsockets types.Bool `tfsdk:"allow_websockets"`
35
- AllowSPDY types.Bool `tfsdk:"allow_spdy"`
36
- TLSSkipVerify types.Bool `tfsdk:"tls_skip_verify"`
37
- TLSUpstreamServerName types.String `tfsdk:"tls_upstream_server_name"`
38
- TLSDownstreamServerName types.String `tfsdk:"tls_downstream_server_name"`
39
- TLSUpstreamAllowRenegotiation types.Bool `tfsdk:"tls_upstream_allow_renegotiation"`
40
- SetRequestHeaders types.Map `tfsdk:"set_request_headers"`
41
- RemoveRequestHeaders types.Set `tfsdk:"remove_request_headers"`
42
- SetResponseHeaders types.Map `tfsdk:"set_response_headers"`
43
- PreserveHostHeader types.Bool `tfsdk:"preserve_host_header"`
44
- PassIdentityHeaders types.Bool `tfsdk:"pass_identity_headers"`
45
- KubernetesServiceAccountToken types.String `tfsdk:"kubernetes_service_account_token"`
46
- IDPClientID types.String `tfsdk:"idp_client_id"`
47
- IDPClientSecret types.String `tfsdk:"idp_client_secret"`
48
- ShowErrorDetails types.Bool `tfsdk:"show_error_details"`
49
- JWTGroupsFilter types.Object `tfsdk:"jwt_groups_filter"`
15
+ AllowSPDY types.Bool `tfsdk:"allow_spdy"`
16
+ AllowWebsockets types.Bool `tfsdk:"allow_websockets"`
17
+ Description types.String `tfsdk:"description"`
18
+ EnableGoogleCloudServerlessAuthentication types.Bool `tfsdk:"enable_google_cloud_serverless_authentication"`
19
+ From types.String `tfsdk:"from"`
20
+ HostPathRegexRewritePattern types.String `tfsdk:"host_path_regex_rewrite_pattern"`
21
+ HostPathRegexRewriteSubstitution types.String `tfsdk:"host_path_regex_rewrite_substitution"`
22
+ HostRewrite types.String `tfsdk:"host_rewrite"`
23
+ HostRewriteHeader types.String `tfsdk:"host_rewrite_header"`
24
+ ID types.String `tfsdk:"id"`
25
+ IdleTimeout timetypes.GoDuration `tfsdk:"idle_timeout"`
26
+ IDPClientID types.String `tfsdk:"idp_client_id"`
27
+ IDPClientSecret types.String `tfsdk:"idp_client_secret"`
28
+ JWTGroupsFilter types.Object `tfsdk:"jwt_groups_filter"`
29
+ JWTIssuerFormat types.Object `tfsdk:"jwt_issuer_format"`
30
+ KubernetesServiceAccountToken types.String `tfsdk:"kubernetes_service_account_token"`
31
+ KubernetesServiceAccountTokenFile types.String `tfsdk:"kubernetes_service_account_token_file"`
32
+ LogoURL types.String `tfsdk:"logo_url"`
33
+ Name types.String `tfsdk:"name"`
34
+ NamespaceID types.String `tfsdk:"namespace_id"`
35
+ PassIdentityHeaders types.Bool `tfsdk:"pass_identity_headers"`
36
+ Path types.String `tfsdk:"path"`
37
+ Policies types.Set `tfsdk:"policies"`
38
+ Prefix types.String `tfsdk:"prefix"`
39
+ PrefixRewrite types.String `tfsdk:"prefix_rewrite"`
40
+ PreserveHostHeader types.Bool `tfsdk:"preserve_host_header"`
41
+ Redirect types.Object `tfsdk:"redirect"`
42
+ Regex types.String `tfsdk:"regex"`
43
+ RegexPriorityOrder types.Int64 `tfsdk:"regex_priority_order"`
44
+ RegexRewritePattern types.String `tfsdk:"regex_rewrite_pattern"`
45
+ RegexRewriteSubstitution types.String `tfsdk:"regex_rewrite_substitution"`
46
+ RemoveRequestHeaders types.Set `tfsdk:"remove_request_headers"`
47
+ RewriteResponseHeaders types.Set `tfsdk:"rewrite_response_headers"`
48
+ SetRequestHeaders types.Map `tfsdk:"set_request_headers"`
49
+ SetResponseHeaders types.Map `tfsdk:"set_response_headers"`
50
+ ShowErrorDetails types.Bool `tfsdk:"show_error_details"`
51
+ StatName types.String `tfsdk:"stat_name"`
52
+ Timeout timetypes.GoDuration `tfsdk:"timeout"`
53
+ TLSClientKeyPairID types.String `tfsdk:"tls_client_key_pair_id"`
54
+ TLSCustomCAKeyPairID types.String `tfsdk:"tls_custom_ca_key_pair_id"`
55
+ TLSDownstreamServerName types.String `tfsdk:"tls_downstream_server_name"`
56
+ TLSSkipVerify types.Bool `tfsdk:"tls_skip_verify"`
57
+ TLSUpstreamAllowRenegotiation types.Bool `tfsdk:"tls_upstream_allow_renegotiation"`
58
+ TLSUpstreamServerName types.String `tfsdk:"tls_upstream_server_name"`
59
+ To types.Set `tfsdk:"to"`
60
+ }
61
+
62
+ var rewriteHeaderAttrTypes = map [string ]attr.Type {
63
+ "header" : types .StringType ,
64
+ "value" : types .StringType ,
65
+ "prefix" : types .StringType ,
66
+ }
67
+
68
+ // RewriteHeaderAttrTypes returns the attribute type map for rewrite headers
69
+ func RewriteHeaderAttrTypes () map [string ]attr.Type {
70
+ return rewriteHeaderAttrTypes
71
+ }
72
+
73
+ func rewriteHeadersToPB (src types.Set ) []* pb.RouteRewriteHeader {
74
+ if (src ).IsNull () {
75
+ return nil
76
+ }
77
+
78
+ headers := make ([]* pb.RouteRewriteHeader , 0 )
79
+ elements := src .Elements ()
80
+ for _ , element := range elements {
81
+ obj := element .(types.Object )
82
+ prefixAttr := obj .Attributes ()["prefix" ].(types.String )
83
+
84
+ header := & pb.RouteRewriteHeader {
85
+ Header : obj .Attributes ()["header" ].(types.String ).ValueString (),
86
+ Value : obj .Attributes ()["value" ].(types.String ).ValueString (),
87
+ }
88
+
89
+ if ! prefixAttr .IsNull () && prefixAttr .ValueString () != "" {
90
+ header .Matcher = & pb.RouteRewriteHeader_Prefix {Prefix : prefixAttr .ValueString ()}
91
+ }
92
+
93
+ headers = append (headers , header )
94
+ }
95
+ return headers
96
+ }
97
+
98
+ func rewriteHeadersFromPB (headers []* pb.RouteRewriteHeader ) types.Set {
99
+ if len (headers ) == 0 {
100
+ return types .SetNull (RewriteHeaderObjectType ())
101
+ }
102
+
103
+ elements := make ([]attr.Value , 0 , len (headers ))
104
+ for _ , header := range headers {
105
+ prefix := header .GetPrefix ()
106
+ prefixValue := types .StringNull ()
107
+ if prefix != "" {
108
+ prefixValue = types .StringValue (prefix )
109
+ }
110
+
111
+ attrs := map [string ]attr.Value {
112
+ "header" : types .StringValue (header .Header ),
113
+ "value" : types .StringValue (header .Value ),
114
+ "prefix" : prefixValue ,
115
+ }
116
+ obj , _ := types .ObjectValue (rewriteHeaderAttrTypes , attrs )
117
+ elements = append (elements , obj )
118
+ }
119
+ result , _ := types .SetValue (RewriteHeaderObjectType (), elements )
120
+ return result
121
+ }
122
+
123
+ func RewriteHeaderObjectType () attr.Type {
124
+ return types.ObjectType {AttrTypes : rewriteHeaderAttrTypes }
50
125
}
51
126
52
127
func ConvertRouteToPB (
@@ -90,14 +165,19 @@ func ConvertRouteToPB(
90
165
pbRoute .IdpClientSecret = src .IDPClientSecret .ValueStringPointer ()
91
166
pbRoute .ShowErrorDetails = src .ShowErrorDetails .ValueBool ()
92
167
JWTGroupsFilterToPB (ctx , & pbRoute .JwtGroupsFilter , src .JWTGroupsFilter , & diagnostics )
168
+ ToStringSliceFromSet (ctx , & pbRoute .To , src .To , & diagnostics )
169
+ ToStringSliceFromSet (ctx , & pbRoute .PolicyIds , src .Policies , & diagnostics )
170
+ pbRoute .TlsClientKeyPairId = src .TLSClientKeyPairID .ValueStringPointer ()
171
+ pbRoute .TlsCustomCaKeyPairId = src .TLSCustomCAKeyPairID .ValueStringPointer ()
172
+ pbRoute .Description = src .Description .ValueStringPointer ()
173
+ pbRoute .LogoUrl = src .LogoURL .ValueStringPointer ()
174
+ if ! src .EnableGoogleCloudServerlessAuthentication .IsNull () {
175
+ pbRoute .EnableGoogleCloudServerlessAuthentication = src .EnableGoogleCloudServerlessAuthentication .ValueBool ()
176
+ }
177
+ pbRoute .KubernetesServiceAccountTokenFile = src .KubernetesServiceAccountTokenFile .ValueStringPointer ()
93
178
94
- diags := src .To .ElementsAs (ctx , & pbRoute .To , false )
95
- diagnostics .Append (diags ... )
179
+ pbRoute .RewriteResponseHeaders = rewriteHeadersToPB (src .RewriteResponseHeaders )
96
180
97
- if ! src .Policies .IsNull () {
98
- diags = src .Policies .ElementsAs (ctx , & pbRoute .PolicyIds , false )
99
- diagnostics .Append (diags ... )
100
- }
101
181
return pbRoute , diagnostics
102
182
}
103
183
@@ -143,6 +223,17 @@ func ConvertRouteFromPB(
143
223
dst .IDPClientSecret = types .StringPointerValue (src .IdpClientSecret )
144
224
dst .ShowErrorDetails = types .BoolValue (src .ShowErrorDetails )
145
225
JWTGroupsFilterFromPB (& dst .JWTGroupsFilter , src .JwtGroupsFilter )
226
+ dst .TLSClientKeyPairID = types .StringPointerValue (src .TlsClientKeyPairId )
227
+ dst .TLSCustomCAKeyPairID = types .StringPointerValue (src .TlsCustomCaKeyPairId )
228
+ dst .Description = types .StringPointerValue (src .Description )
229
+ dst .LogoURL = types .StringPointerValue (src .LogoUrl )
230
+ dst .EnableGoogleCloudServerlessAuthentication = types .BoolNull ()
231
+ if src .EnableGoogleCloudServerlessAuthentication {
232
+ dst .EnableGoogleCloudServerlessAuthentication = types .BoolValue (true )
233
+ }
234
+ dst .KubernetesServiceAccountTokenFile = types .StringPointerValue (src .KubernetesServiceAccountTokenFile )
235
+
236
+ dst .RewriteResponseHeaders = rewriteHeadersFromPB (src .RewriteResponseHeaders )
146
237
147
238
return diagnostics
148
239
}
0 commit comments