From adef8db78efaa7a9ca48c64db981031b0713450d Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Tue, 22 Oct 2024 13:09:58 +0300 Subject: [PATCH 1/2] Log the instance ID as soon as it's been created --- builder/tencentcloud/cvm/step_run_instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/tencentcloud/cvm/step_run_instance.go b/builder/tencentcloud/cvm/step_run_instance.go index 943eabf6..aee81854 100644 --- a/builder/tencentcloud/cvm/step_run_instance.go +++ b/builder/tencentcloud/cvm/step_run_instance.go @@ -173,7 +173,7 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul } s.instanceId = *resp.Response.InstanceIdSet[0] - Message(state, "Waiting for instance ready", "") + Message(state, fmt.Sprintf("Instance %s created, waiting for instance ready", s.instanceId), "") err = WaitForInstance(ctx, client, s.instanceId, "RUNNING", 1800) if err != nil { From 96a98f9cc9b44c7e5a142da556224ac91f1ba234 Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Tue, 22 Oct 2024 13:10:05 +0300 Subject: [PATCH 2/2] 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 https://github.com/hashicorp/packer-plugin-tencentcloud/issues/6 --- builder/tencentcloud/cvm/step_run_instance.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/builder/tencentcloud/cvm/step_run_instance.go b/builder/tencentcloud/cvm/step_run_instance.go index aee81854..f79afefb 100644 --- a/builder/tencentcloud/cvm/step_run_instance.go +++ b/builder/tencentcloud/cvm/step_run_instance.go @@ -11,6 +11,7 @@ import ( "log" "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/uuid" cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" ) @@ -125,7 +126,11 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul req.InternetAccessible.BandwidthPackageId = &s.BandwidthPackageId } } - req.InstanceName = &s.InstanceName + + // Generate a unique ClientToken for each RunInstances request + clientToken := uuid.TimeOrderedUUID() + req.ClientToken = &clientToken + loginSettings := cvm.LoginSettings{} if password != "" { loginSettings.Password = &password @@ -135,7 +140,7 @@ func (s *stepRunInstance) Run(ctx context.Context, state multistep.StateBag) mul } req.LoginSettings = &loginSettings req.SecurityGroupIds = []*string{&security_group_id} - req.ClientToken = &s.InstanceName + req.InstanceName = &s.InstanceName req.HostName = &s.HostName req.UserData = &userData req.CamRoleName = &s.CamRoleName