diff --git a/builder/tencentcloud/cvm/run_config.go b/builder/tencentcloud/cvm/run_config.go index e4a45e11..66484d61 100644 --- a/builder/tencentcloud/cvm/run_config.go +++ b/builder/tencentcloud/cvm/run_config.go @@ -117,8 +117,9 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error { packerId := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()[:8]) if cf.Comm.SSHKeyPairName == "" && cf.Comm.SSHTemporaryKeyPairName == "" && cf.Comm.SSHPrivateKeyFile == "" && cf.Comm.SSHPassword == "" && cf.Comm.WinRMPassword == "" { - //tencentcloud support key pair name length max to 25 - cf.Comm.SSHTemporaryKeyPairName = packerId + // Key pair names must be unique so we can't use packerId here, instead we set a + // dummy value now and a unique name later + cf.Comm.SSHTemporaryKeyPairName = "dummy value" } errs := cf.Comm.Prepare(ctx) diff --git a/builder/tencentcloud/cvm/step_config_key_pair.go b/builder/tencentcloud/cvm/step_config_key_pair.go index 4a97cb1e..b8b8cab4 100644 --- a/builder/tencentcloud/cvm/step_config_key_pair.go +++ b/builder/tencentcloud/cvm/step_config_key_pair.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/packer-plugin-sdk/communicator" "github.com/hashicorp/packer-plugin-sdk/multistep" + "github.com/hashicorp/packer-plugin-sdk/uuid" cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" ) @@ -22,6 +23,12 @@ type stepConfigKeyPair struct { keyID string } +func CreateKeyPairName() string { + uuid := uuid.TimeOrderedUUID() + + return fmt.Sprintf("packer_%s_%s", uuid[:8], uuid[9:13]) +} + func (s *stepConfigKeyPair) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { client := state.Get("cvm_client").(*cvm.Client) @@ -51,6 +58,8 @@ func (s *stepConfigKeyPair) Run(ctx context.Context, state multistep.StateBag) m return multistep.ActionContinue } + // Set a unique value for each key pair name + s.Comm.SSHTemporaryKeyPairName = CreateKeyPairName() Say(state, s.Comm.SSHTemporaryKeyPairName, "Trying to create a new keypair") req := cvm.NewCreateKeyPairRequest()