File: instance_config.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (54 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package jdcloud

import (
	"fmt"

	"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
	"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
)

type JDCloudInstanceSpecConfig struct {
	ImageId         string              `mapstructure:"image_id"`
	InstanceName    string              `mapstructure:"instance_name"`
	InstanceType    string              `mapstructure:"instance_type"`
	ImageName       string              `mapstructure:"image_name"`
	SubnetId        string              `mapstructure:"subnet_id"`
	Comm            communicator.Config `mapstructure:",squash"`
	InstanceId      string
	ArtifactId      string
	PublicIpAddress string
	PublicIpId      string
}

func (jd *JDCloudInstanceSpecConfig) Prepare(ctx *interpolate.Context) []error {

	errs := jd.Comm.Prepare(ctx)

	if jd == nil {
		return append(errs, fmt.Errorf("[PRE-FLIGHT] Configuration appears to be empty"))
	}

	if len(jd.ImageId) == 0 {
		errs = append(errs, fmt.Errorf("[PRE-FLIGHT] 'image_id' empty"))
	}

	if len(jd.InstanceName) == 0 {
		errs = append(errs, fmt.Errorf("[PRE-FLIGHT] 'instance_name' empty"))
	}

	if len(jd.InstanceType) == 0 {
		errs = append(errs, fmt.Errorf("[PRE-FLIGHT] 'instance-type' empty"))
	}

	noPassword := len(jd.Comm.SSHPassword) == 0
	noKeys := len(jd.Comm.SSHKeyPairName) == 0 && len(jd.Comm.SSHPrivateKeyFile) == 0
	noTempKey := len(jd.Comm.SSHTemporaryKeyPairName) == 0
	if noPassword && noKeys && noTempKey {
		errs = append(errs, fmt.Errorf("[PRE-FLIGHT] Didn't detect any credentials, you have to specify either "+
			"{password} or "+
			"{key_name+local_private_key_path} or "+
			"{temporary_key_pair_name} cheers :)"))
	}

	return errs
}