Skip to content

Commit 96a98f9

Browse files
committed
Use a unique value as ClientToken for each RunInstances request
The purpose of ClientToken is to ensure request idempotency in case the request needs to be retried. This means the value must be unique for all unique RunInstances API calls. When building a template that references the same source multiple times (or multiple different sources), many calls to RunInstances are made. However, the same auto-generated instance name was used as "ClientToken". This changes ClientToken to be unique regardless of whether the user has supplied a unique "instance_name" for each source/build. Fixes #6
1 parent adef8db commit 96a98f9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

builder/tencentcloud/cvm/step_run_instance.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212

1313
"github.com/hashicorp/packer-plugin-sdk/multistep"
14+
"github.com/hashicorp/packer-plugin-sdk/uuid"
1415
cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
1516
)
1617

@@ -125,7 +126,11 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul
125126
req.InternetAccessible.BandwidthPackageId = &s.BandwidthPackageId
126127
}
127128
}
128-
req.InstanceName = &s.InstanceName
129+
130+
// Generate a unique ClientToken for each RunInstances request
131+
clientToken := uuid.TimeOrderedUUID()
132+
req.ClientToken = &clientToken
133+
129134
loginSettings := cvm.LoginSettings{}
130135
if password != "" {
131136
loginSettings.Password = &password
@@ -135,7 +140,7 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul
135140
}
136141
req.LoginSettings = &loginSettings
137142
req.SecurityGroupIds = []*string{&security_group_id}
138-
req.ClientToken = &s.InstanceName
143+
req.InstanceName = &s.InstanceName
139144
req.HostName = &s.HostName
140145
req.UserData = &userData
141146
req.CamRoleName = &s.CamRoleName

0 commit comments

Comments
 (0)