저번에 올린 pr에 원철 멘토님께서 리뷰를 달아주셨다.
멘토님께서 남겨주신 리뷰를 따라 예외 처리를 해보자.
수정 사항 : 해당 값이 없더라도 패닉이 발생하지 않도록 예외 처리 해주기
panic 다시보기
=== RUN TestAccNcloudLoadBalancerSSLCertificateBasic
panic: runtime error: index out of range [0] with length 0
goroutine 263 [running]:
github.com/terraform-providers/terraform-provider-ncloud/internal/service/classicloadbalancer.resourceNcloudLoadBalancerSSLCertificateCreate(0x0?, {0x187cda0?, 0xc00054d9c0})
C:/Users/jpark/Desktop/terraform-provider-ncloud/internal/service/classicloadbalancer/load_balancer_ssl_certificate.go:76 +0x205
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).create(0x1bdf800?, {0x1bdf800?, 0xc0004fef90?}, 0xd?, {0x187cda0?, 0xc00054d9c0?})
C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/resource.go:695 +0x178
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc000533b20, {0x1bdf800, 0xc0004fef90}, 0xc00048a4e0, 0xc0009fe800, {0x187cda0, 0xc00054d9c0})
C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/resource.go:837 +0xa85
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc0006fc6a8, {0x1bdf800?, 0xc0004feed0?}, 0xc00027a5f0)
C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk/v2@v2.24.1/helper/schema/grpc_provider.go:1021 +0xe8d
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc00034a5a0, {0x1bdf800?, 0xc0004fe6c0?}, 0xc0009f80e0)
C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.1/tfprotov5/tf5server/server.go:818 +0x574
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1a0bc60?, 0xc00034a5a0}, {0x1bdf800, 0xc0004fe6c0}, 0xc0009f8070, 0x0)
C:/Users/jpark/go/pkg/mod/github.com/hashicorp/terraform-plugin-go@v0.14.1/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001245a0, {0x1be35d8, 0xc000476000}, 0xc00060c7e0, 0xc0001451d0, 0x223c640, 0x0)
C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1336 +0xd33
google.golang.org/grpc.(*Server).handleStream(0xc0001245a0, {0x1be35d8, 0xc000476000}, 0xc00060c7e0, 0x0)
C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1704 +0xa36
google.golang.org/grpc.(*Server).serveStreams.func1.2()
C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:965 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
C:/Users/jpark/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:963 +0x28a
FAIL command-line-arguments 1.055s
FAIL
load_balancer_ssl_certificate.go의 76번째 줄에서 패닉이 발생하므로 해당 부분이 처리가 되기 전에 예외 처리해주기
수정 전 코드
func resourceNcloudLoadBalancerSSLCertificateCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*conn.ProviderConfig).Client
config := meta.(*conn.ProviderConfig)
if config.SupportVPC {
return NotSupportVpc("resource `ncloud_load_balancer_ssl_certificate`")
}
reqParams, err := buildCreateLoadBalancerSSLCertificateParams(d)
if err != nil {
LogErrorResponse("AddLoadBalancerSslCertificate", err, reqParams)
return err
}
LogCommonRequest("AddLoadBalancerSslCertificate", reqParams)
resp, err := client.Loadbalancer.V2Api.AddLoadBalancerSslCertificate(reqParams)
if err != nil {
LogErrorResponse("AddLoadBalancerSslCertificate", err, reqParams)
return err
}
LogCommonResponse("AddLoadBalancerSslCertificate", GetCommonResponse(resp))
cert := resp.SslCertificateList[0]
d.SetId(*cert.CertificateName)
return resourceNcloudLoadBalancerSSLCertificateRead(d, meta)
}
수정한 코드
func resourceNcloudLoadBalancerSSLCertificateCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*conn.ProviderConfig).Client
config := meta.(*conn.ProviderConfig)
if config.SupportVPC {
return NotSupportVpc("resource `ncloud_load_balancer_ssl_certificate`")
}
reqParams, err := buildCreateLoadBalancerSSLCertificateParams(d)
if err != nil {
LogErrorResponse("AddLoadBalancerSslCertificate", err, reqParams)
return err
}
LogCommonRequest("AddLoadBalancerSslCertificate", reqParams)
resp, err := client.Loadbalancer.V2Api.AddLoadBalancerSslCertificate(reqParams)
if err != nil {
LogErrorResponse("AddLoadBalancerSslCertificate", err, reqParams)
return err
}
LogCommonResponse("AddLoadBalancerSslCertificate", GetCommonResponse(resp))
if len(resp.SslCertificateList) == 0 {
return fmt.Errorf("no SSL certificate found in the API response")
}
cert := resp.SslCertificateList[0]
d.SetId(*cert.CertificateName)
return resourceNcloudLoadBalancerSSLCertificateRead(d, meta)
}
실행해보기
Debug 결과
$ TF_ACC=1 go test ./internal/service/classicloadbalancer/load_balancer_ssl_certificate_test.go -run=TestAccNcloudLoadBalancerSSLCertificateBasic -v
=== RUN TestAccNcloudLoadBalancerSSLCertificateBasic
load_balancer_ssl_certificate_test.go:85: Step 1/2 error: Error running apply: exit status 1
Error: no SSL certificate found in the API response
with ncloud_load_balancer_ssl_certificate.cert,
on terraform_plugin_test.tf line 3, in resource "ncloud_load_balancer_ssl_certificate" "cert":
3: resource "ncloud_load_balancer_ssl_certificate" "cert" {
--- FAIL: TestAccNcloudLoadBalancerSSLCertificateBasic (1.42s)
FAIL
FAIL command-line-arguments 1.516s
FAIL
PR 추가하기
기존 변경 내역
새롭게 추가한 pr
pr이 추가됨에 따라 pr 제목과 내용도 수정했다.
내 기록들… (힘들었다.. git은 어려워…)
알게 된 것 : merge 되기 전이라면 보낸 pr에 추가해서 pr을 계속 수정할 수 있다…!!
'개발 > Terraform on NaverCloud' 카테고리의 다른 글
SourcePipelineProject 살펴보기 (1) (0) | 2023.08.21 |
---|---|
이슈 선정 2 : SourcePipelineProject (0) | 2023.08.21 |
오픈 소스 기여하기 : panic 원인 파악하기 (0) | 2023.08.01 |
오픈 소스 기여하기 : terraform-provider-ncloud 이슈 선정 (0) | 2023.08.01 |
Terraform으로 NAVER Cloud Server 만들기 (0) | 2023.07.25 |