Skip to content

Commit d0d9f55

Browse files
committed
fix: Exception hand
1 parent de8f66b commit d0d9f55

File tree

3 files changed

+116
-3
lines changed

3 files changed

+116
-3
lines changed

builder/tencentcloud/cvm/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (me *TencentCloudClient) UseVpcClient(cpf *profile.ClientProfile) *vpc.Clie
3434
}
3535

3636
me.vpcConn, _ = vpc.NewClient(me.Credential, me.Region, cpf)
37-
// me.vpcConn.WithHttpTransport(&LogRoundTripper{})
37+
me.vpcConn.WithHttpTransport(&LogRoundTripper{})
3838

3939
return me.vpcConn
4040
}
@@ -45,6 +45,7 @@ func (me *TencentCloudClient) UseCvmClient(cpf *profile.ClientProfile) *cvm.Clie
4545
}
4646

4747
me.cvmConn, _ = cvm.NewClient(me.Credential, me.Region, cpf)
48+
me.cvmConn.WithHttpTransport(&LogRoundTripper{})
4849

4950
return me.cvmConn
5051
}
@@ -56,6 +57,7 @@ func (me *TencentCloudClient) UseStsClient() *sts.Client {
5657

5758
cpf := me.ClientProfile
5859
me.stsConn, _ = sts.NewClient(me.Credential, me.Region, cpf)
60+
me.stsConn.WithHttpTransport(&LogRoundTripper{})
5961

6062
return me.stsConn
6163
}
@@ -66,6 +68,7 @@ func (me *TencentCloudClient) UseTagClient(cpf *profile.ClientProfile) *tag.Clie
6668
}
6769

6870
me.tagConn, _ = tag.NewClient(me.Credential, me.Region, cpf)
71+
me.tagConn.WithHttpTransport(&LogRoundTripper{})
6972

7073
return me.tagConn
7174
}
@@ -76,6 +79,7 @@ func (me *TencentCloudClient) UseOrgClient(cpf *profile.ClientProfile) *org.Clie
7679
}
7780

7881
me.orgConn, _ = org.NewClient(me.Credential, me.Region, cpf)
82+
me.orgConn.WithHttpTransport(&LogRoundTripper{})
7983

8084
return me.orgConn
8185
}
@@ -87,6 +91,7 @@ func (me *TencentCloudClient) UseCamClient() *cam.Client {
8791

8892
cpf := me.ClientProfile
8993
me.camConn, _ = cam.NewClient(me.Credential, me.Region, cpf)
94+
me.camConn.WithHttpTransport(&LogRoundTripper{})
9095

9196
return me.camConn
9297
}

builder/tencentcloud/cvm/step_check_source_image.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ func (s *stepCheckSourceImage) Run(ctx context.Context, state multistep.StateBag
2323
)
2424
config := state.Get("config").(*Config)
2525
client := state.Get("cvm_client").(*cvm.Client)
26-
source_image := state.Get("source_image").(*cvm.Image)
26+
27+
var source_image *cvm.Image
28+
if state.Get("source_image") != nil {
29+
source_image = state.Get("source_image").(*cvm.Image)
30+
}
2731

2832
Say(state, config.SourceImageId, "Trying to check source image")
2933

3034
req := cvm.NewDescribeImagesRequest()
3135
req.InstanceType = &config.InstanceType
3236
if config.SourceImageId != "" {
3337
req.ImageIds = []*string{&config.SourceImageId}
34-
} else if *source_image.ImageId != "" {
38+
} else if source_image != nil && *source_image.ImageId != "" {
3539
req.ImageIds = []*string{source_image.ImageId}
3640
} else {
3741
imageNameRegex, err = regexp.Compile(config.SourceImageName)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package cvm
5+
6+
import (
7+
"bytes"
8+
"encoding/json"
9+
"fmt"
10+
"io/ioutil"
11+
"log"
12+
"net/http"
13+
"time"
14+
15+
"github.com/hashicorp/packer-plugin-tencentcloud/version"
16+
)
17+
18+
type LogRoundTripper struct{}
19+
20+
func (me *LogRoundTripper) RoundTrip(request *http.Request) (response *http.Response, errRet error) {
21+
22+
var inBytes, outBytes []byte
23+
24+
var start = time.Now()
25+
26+
defer func() { me.log(inBytes, outBytes, errRet, start) }()
27+
28+
bodyReader, errRet := request.GetBody()
29+
if errRet != nil {
30+
return
31+
}
32+
33+
var headName = "X-TC-Action"
34+
var reqClientFormat = version.PluginVersion
35+
request.Header.Set("X-TC-RequestClient", reqClientFormat.String())
36+
inBytes = []byte(fmt.Sprintf("%s, request: ", request.Header[headName]))
37+
requestBody, errRet := ioutil.ReadAll(bodyReader)
38+
if errRet != nil {
39+
return
40+
}
41+
42+
inBytes = append(inBytes, requestBody...)
43+
headName = "X-TC-Region"
44+
appendMessage := []byte(fmt.Sprintf(
45+
", (host %+v, region:%+v)",
46+
request.Header["Host"],
47+
request.Header[headName],
48+
))
49+
50+
inBytes = append(inBytes, appendMessage...)
51+
response, errRet = http.DefaultTransport.RoundTrip(request)
52+
if errRet != nil {
53+
return
54+
}
55+
56+
outBytes, errRet = ioutil.ReadAll(response.Body)
57+
if errRet != nil {
58+
return
59+
}
60+
61+
response.Body = ioutil.NopCloser(bytes.NewBuffer(outBytes))
62+
return
63+
}
64+
65+
func (me *LogRoundTripper) log(in []byte, out []byte, err error, start time.Time) {
66+
var buf bytes.Buffer
67+
buf.WriteString("######")
68+
tag := "[DEBUG]"
69+
if err != nil {
70+
tag = "[CRITICAL]"
71+
}
72+
73+
buf.WriteString(tag)
74+
if len(in) > 0 {
75+
buf.WriteString("tencentcloud-sdk-go: ")
76+
buf.Write(in)
77+
}
78+
79+
if len(out) > 0 {
80+
buf.WriteString("; response:")
81+
err := json.Compact(&buf, out)
82+
if err != nil {
83+
out := bytes.Replace(out,
84+
[]byte("\n"),
85+
[]byte(""),
86+
-1)
87+
out = bytes.Replace(out,
88+
[]byte(" "),
89+
[]byte(""),
90+
-1)
91+
buf.Write(out)
92+
}
93+
}
94+
95+
if err != nil {
96+
buf.WriteString("; error:")
97+
buf.WriteString(err.Error())
98+
}
99+
100+
costFormat := fmt.Sprintf(",cost %s", time.Since(start).String())
101+
buf.WriteString(costFormat)
102+
103+
log.Println(buf.String())
104+
}

0 commit comments

Comments
 (0)