@@ -16,7 +16,10 @@ package compute
16
16
17
17
import (
18
18
"fmt"
19
+ "log"
19
20
"reflect"
21
+ "regexp"
22
+ "strings"
20
23
21
24
"github.com/GoogleCloudPlatform/terraform-google-conversion/v5/tfplan2cai/converters/google/resources/cai"
22
25
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
@@ -67,6 +70,12 @@ func GetComputeRegionTargetHttpsProxyApiObject(d tpgresource.TerraformResourceDa
67
70
} else if v , ok := d .GetOkExists ("name" ); ! tpgresource .IsEmptyValue (reflect .ValueOf (nameProp )) && (ok || ! reflect .DeepEqual (v , nameProp )) {
68
71
obj ["name" ] = nameProp
69
72
}
73
+ certificateManagerCertificatesProp , err := expandComputeRegionTargetHttpsProxyCertificateManagerCertificates (d .Get ("certificate_manager_certificates" ), d , config )
74
+ if err != nil {
75
+ return nil , err
76
+ } else if v , ok := d .GetOkExists ("certificate_manager_certificates" ); ! tpgresource .IsEmptyValue (reflect .ValueOf (certificateManagerCertificatesProp )) && (ok || ! reflect .DeepEqual (v , certificateManagerCertificatesProp )) {
77
+ obj ["certificateManagerCertificates" ] = certificateManagerCertificatesProp
78
+ }
70
79
sslCertificatesProp , err := expandComputeRegionTargetHttpsProxySslCertificates (d .Get ("ssl_certificates" ), d , config )
71
80
if err != nil {
72
81
return nil , err
@@ -92,6 +101,19 @@ func GetComputeRegionTargetHttpsProxyApiObject(d tpgresource.TerraformResourceDa
92
101
obj ["region" ] = regionProp
93
102
}
94
103
104
+ return resourceComputeRegionTargetHttpsProxyEncoder (d , config , obj )
105
+ }
106
+
107
+ func resourceComputeRegionTargetHttpsProxyEncoder (d tpgresource.TerraformResourceData , meta interface {}, obj map [string ]interface {}) (map [string ]interface {}, error ) {
108
+
109
+ if _ , ok := obj ["certificateManagerCertificates" ]; ok {
110
+ // The field certificateManagerCertificates should not be included in the API request, and it should be renamed to `sslCertificates`
111
+ // The API does not allow using both certificate manager certificates and sslCertificates. If that changes
112
+ // in the future, the encoder logic should change accordingly because this will mean that both fields are no longer mutual exclusive.
113
+ log .Printf ("[DEBUG] converting the field CertificateManagerCertificates to sslCertificates before sending the request" )
114
+ obj ["sslCertificates" ] = obj ["certificateManagerCertificates" ]
115
+ delete (obj , "certificateManagerCertificates" )
116
+ }
95
117
return obj , nil
96
118
}
97
119
@@ -103,6 +125,31 @@ func expandComputeRegionTargetHttpsProxyName(v interface{}, d tpgresource.Terraf
103
125
return v , nil
104
126
}
105
127
128
+ func expandComputeRegionTargetHttpsProxyCertificateManagerCertificates (v interface {}, d tpgresource.TerraformResourceData , config * transport_tpg.Config ) (interface {}, error ) {
129
+ if v == nil {
130
+ return nil , nil
131
+ }
132
+ l := v .([]interface {})
133
+ req := make ([]interface {}, 0 , len (l ))
134
+ for _ , raw := range l {
135
+ if raw == nil {
136
+ return nil , fmt .Errorf ("Invalid value for certificate_manager_certificates: nil" )
137
+ }
138
+ if strings .HasPrefix (raw .(string ), "//" ) || strings .HasPrefix (raw .(string ), "https://" ) {
139
+ // Any full URL will be passed to the API request (regardless of the resource type). This is to allow self_links of CertificateManagerCeritificate resources.
140
+ // If the full URL is an invalid reference, that should be handled by the API.
141
+ req = append (req , raw .(string ))
142
+ } else if reg , _ := regexp .Compile ("projects/(.*)/locations/(.*)/certificates/(.*)" ); reg .MatchString (raw .(string )) {
143
+ // If the input is the id pattern of CertificateManagerCertificate resource, a prefix will be added to construct the full URL before constructing the API request.
144
+ self_link := "https://certificatemanager.googleapis.com/v1/" + raw .(string )
145
+ req = append (req , self_link )
146
+ } else {
147
+ return nil , fmt .Errorf ("Invalid value for certificate_manager_certificates: %v is an invalid format for a certificateManagerCertificate resource" , raw .(string ))
148
+ }
149
+ }
150
+ return req , nil
151
+ }
152
+
106
153
func expandComputeRegionTargetHttpsProxySslCertificates (v interface {}, d tpgresource.TerraformResourceData , config * transport_tpg.Config ) (interface {}, error ) {
107
154
l := v .([]interface {})
108
155
req := make ([]interface {}, 0 , len (l ))
0 commit comments