Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/edgio_provider/resources/tls_certs_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (r *TLSCertsResource) Create(ctx context.Context, req resource.CreateReques
tlsRes = *res
}

if tlsRes.Status == "failed" {
resp.Diagnostics.AddError("Error Uploading TLS Cert", fmt.Sprintf("Error: %s", tlsRes.ActivationError))
return
}

newState := utility.ConvertTlsCertsToModel(&tlsRes)
newState.PrivateKey = plan.PrivateKey
diags = resp.State.Set(ctx, &newState)
Expand Down
51 changes: 51 additions & 0 deletions internal/edgio_provider/resources/tls_certs_resource_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources_test

import (
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -249,3 +250,53 @@ func TestTLSCertsResource_UploadUpdate(t *testing.T) {

mockClient.AssertExpectations(t)
}

func TestTLSCertsResource_StateFailed(t *testing.T) {
mockClient := new(edgio_api.MockEdgioClient)

fixedTime := time.Date(2024, 10, 2, 10, 0, 0, 0, time.UTC)

uploadedCert := &dtos.TLSCertResponse{
ID: "cert-456",
EnvironmentID: "env-123",
Expiration: fixedTime.Add(365 * 24 * time.Hour).Format(time.RFC3339),
Status: "failed",
ActivationError: "Certificate upload failed",
PrimaryCert: "-----BEGIN CERTIFICATE-----1",
IntermediateCert: "-----BEGIN CERTIFICATE-----2",
Generated: false,
Serial: "0987654321",
CommonName: "example.org",
AlternativeNames: []string{"www.example.org"},
CreatedAt: fixedTime.Format(time.RFC3339),
UpdatedAt: fixedTime.Format(time.RFC3339),
}

mockClient.On("UploadTlsCert", mock.AnythingOfType("dtos.UploadTlsCertRequest")).Return(uploadedCert, nil)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"edgio": providerserver.NewProtocol6WithError(edgio_provider.NewMockedProvider(mockClient)),
},
Steps: []resource.TestStep{
// Test uploading a TLS cert with a failed status
{
Config: `
provider "edgio" {
client_id = "mock-client-id"
client_secret = "mock-client-secret"
}

resource "edgio_tls_cert" "uploaded" {
environment_id = "env-123"
primary_cert = "-----BEGIN CERTIFICATE-----1"
intermediate_cert = "-----BEGIN CERTIFICATE-----2"
private_key = "-----BEGIN PRIVATE KEY-----3"
}`,
ExpectError: regexp.MustCompile("Error: Certificate upload failed"),
},
},
})

mockClient.AssertExpectations(t)
}