@@ -228,6 +228,117 @@ func TestUnit_CertificateResource_Lifecycle(t *testing.T) {
228228 }
229229}
230230
231+ // TestUnit_CertificateResource_Create_DefaultsToArrayWhenPrivateKey: verifies that
232+ // when certificate_type is unset but private_key is provided, the provider sends
233+ // "array" so the API accepts the request (API otherwise defaults to "external"
234+ // and rejects the private key).
235+ func TestUnit_CertificateResource_Create_DefaultsToArrayWhenPrivateKey (t * testing.T ) {
236+ ms := testmock .NewMockServer ()
237+ defer ms .Close ()
238+ handlers .RegisterCertificateHandlers (ms .Mux )
239+
240+ r := newTestCertificateResource (t , ms )
241+ s := certificateResourceSchema (t ).Schema
242+
243+ const certPEM = "-----BEGIN CERTIFICATE-----\n MIIBarr\n -----END CERTIFICATE-----"
244+ const privKey = "-----BEGIN PRIVATE KEY-----\n MIIEarr\n -----END PRIVATE KEY-----"
245+
246+ plan := certificatePlanWith (t , "arr-cert" , certPEM , privKey , "" )
247+ createResp := & resource.CreateResponse {
248+ State : tfsdk.State {Raw : tftypes .NewValue (buildCertificateType (), nil ), Schema : s },
249+ }
250+ r .Create (context .Background (), resource.CreateRequest {Plan : plan }, createResp )
251+ if createResp .Diagnostics .HasError () {
252+ t .Fatalf ("Create returned error: %s" , createResp .Diagnostics )
253+ }
254+
255+ var got certificateModel
256+ if diags := createResp .State .Get (context .Background (), & got ); diags .HasError () {
257+ t .Fatalf ("Get state: %s" , diags )
258+ }
259+ if got .CertificateType .ValueString () != "array" {
260+ t .Errorf ("expected certificate_type=array (inferred from private_key), got %q" , got .CertificateType .ValueString ())
261+ }
262+ }
263+
264+ // TestUnit_CertificateResource_Create_ExternalWhenNoPrivateKey: verifies that when
265+ // neither certificate_type nor private_key is provided, the provider lets the API
266+ // apply its default ("external").
267+ func TestUnit_CertificateResource_Create_ExternalWhenNoPrivateKey (t * testing.T ) {
268+ ms := testmock .NewMockServer ()
269+ defer ms .Close ()
270+ handlers .RegisterCertificateHandlers (ms .Mux )
271+
272+ r := newTestCertificateResource (t , ms )
273+ s := certificateResourceSchema (t ).Schema
274+
275+ const certPEM = "-----BEGIN CERTIFICATE-----\n MIIBext\n -----END CERTIFICATE-----"
276+
277+ plan := certificatePlanWith (t , "ext-cert" , certPEM , "" , "" )
278+ createResp := & resource.CreateResponse {
279+ State : tfsdk.State {Raw : tftypes .NewValue (buildCertificateType (), nil ), Schema : s },
280+ }
281+ r .Create (context .Background (), resource.CreateRequest {Plan : plan }, createResp )
282+ if createResp .Diagnostics .HasError () {
283+ t .Fatalf ("Create returned error: %s" , createResp .Diagnostics )
284+ }
285+
286+ var got certificateModel
287+ if diags := createResp .State .Get (context .Background (), & got ); diags .HasError () {
288+ t .Fatalf ("Get state: %s" , diags )
289+ }
290+ if got .CertificateType .ValueString () != "external" {
291+ t .Errorf ("expected certificate_type=external (API default), got %q" , got .CertificateType .ValueString ())
292+ }
293+ }
294+
295+ // TestUnit_CertificateResource_Create_PreservesNullIntermediate: verifies that when
296+ // intermediate_certificate is unset in config and the API returns "", the state
297+ // remains null rather than "" (avoids "provider produced inconsistent result").
298+ func TestUnit_CertificateResource_Create_PreservesNullIntermediate (t * testing.T ) {
299+ ms := testmock .NewMockServer ()
300+ defer ms .Close ()
301+ handlers .RegisterCertificateHandlers (ms .Mux )
302+
303+ r := newTestCertificateResource (t , ms )
304+ s := certificateResourceSchema (t ).Schema
305+
306+ const certPEM = "-----BEGIN CERTIFICATE-----\n MIIBnoint\n -----END CERTIFICATE-----"
307+
308+ plan := certificatePlanWith (t , "noint-cert" , certPEM , "" , "external" )
309+ createResp := & resource.CreateResponse {
310+ State : tfsdk.State {Raw : tftypes .NewValue (buildCertificateType (), nil ), Schema : s },
311+ }
312+ r .Create (context .Background (), resource.CreateRequest {Plan : plan }, createResp )
313+ if createResp .Diagnostics .HasError () {
314+ t .Fatalf ("Create returned error: %s" , createResp .Diagnostics )
315+ }
316+
317+ var got certificateModel
318+ if diags := createResp .State .Get (context .Background (), & got ); diags .HasError () {
319+ t .Fatalf ("Get state: %s" , diags )
320+ }
321+ if ! got .IntermediateCertificate .IsNull () {
322+ t .Errorf ("expected intermediate_certificate to stay null, got %q (IsNull=%v)" ,
323+ got .IntermediateCertificate .ValueString (), got .IntermediateCertificate .IsNull ())
324+ }
325+
326+ // Read path must also preserve null on empty API response.
327+ readResp := & resource.ReadResponse {State : createResp .State }
328+ r .Read (context .Background (), resource.ReadRequest {State : createResp .State }, readResp )
329+ if readResp .Diagnostics .HasError () {
330+ t .Fatalf ("Read returned error: %s" , readResp .Diagnostics )
331+ }
332+ var afterRead certificateModel
333+ if diags := readResp .State .Get (context .Background (), & afterRead ); diags .HasError () {
334+ t .Fatalf ("Get read state: %s" , diags )
335+ }
336+ if ! afterRead .IntermediateCertificate .IsNull () {
337+ t .Errorf ("expected intermediate_certificate null after Read, got %q" ,
338+ afterRead .IntermediateCertificate .ValueString ())
339+ }
340+ }
341+
231342// TestUnit_CertificateResource_Import: seed cert in mock → import by name → verify state.
232343func TestUnit_CertificateResource_Import (t * testing.T ) {
233344 ms := testmock .NewMockServer ()
@@ -242,7 +353,7 @@ func TestUnit_CertificateResource_Import(t *testing.T) {
242353 ID : "cert-import-001" ,
243354 Name : "import-cert" ,
244355 Certificate : "-----BEGIN CERTIFICATE-----\n MIIBseed\n -----END CERTIFICATE-----" ,
245- CertificateType : "appliance " ,
356+ CertificateType : "array " ,
246357 CommonName : "flashblade.example.com" ,
247358 IssuedBy : "CN=Test CA" ,
248359 IssuedTo : "CN=flashblade.example.com" ,
@@ -273,8 +384,8 @@ func TestUnit_CertificateResource_Import(t *testing.T) {
273384 if model .Name .ValueString () != "import-cert" {
274385 t .Errorf ("expected name=import-cert, got %s" , model .Name .ValueString ())
275386 }
276- if model .CertificateType .ValueString () != "appliance " {
277- t .Errorf ("expected certificate_type=appliance , got %s" , model .CertificateType .ValueString ())
387+ if model .CertificateType .ValueString () != "array " {
388+ t .Errorf ("expected certificate_type=array , got %s" , model .CertificateType .ValueString ())
278389 }
279390 if model .CommonName .ValueString () != "flashblade.example.com" {
280391 t .Errorf ("expected common_name=flashblade.example.com, got %s" , model .CommonName .ValueString ())
0 commit comments