File: step_create_security_group.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (95 lines) | stat: -rw-r--r-- 2,675 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cloudstack

import (
	"context"
	"fmt"

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
	packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
	"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
	"github.com/xanzy/go-cloudstack/cloudstack"
)

type stepCreateSecurityGroup struct {
	tempSG string
}

func (s *stepCreateSecurityGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	client := state.Get("client").(*cloudstack.CloudStackClient)
	config := state.Get("config").(*Config)
	ui := state.Get("ui").(packersdk.Ui)

	if len(config.SecurityGroups) > 0 {
		state.Put("security_groups", config.SecurityGroups)
		return multistep.ActionContinue
	}

	if !config.CreateSecurityGroup {
		return multistep.ActionContinue
	}

	ui.Say("Creating temporary Security Group...")

	p := client.SecurityGroup.NewCreateSecurityGroupParams(
		fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()),
	)
	p.SetDescription("Temporary SG created by Packer")
	if config.Project != "" {
		p.SetProjectid(config.Project)
	}

	sg, err := client.SecurityGroup.CreateSecurityGroup(p)
	if err != nil {
		err := fmt.Errorf("Failed to create security group: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	s.tempSG = sg.Id
	state.Put("security_groups", []string{sg.Id})

	// Create Ingress rule
	i := client.SecurityGroup.NewAuthorizeSecurityGroupIngressParams()
	i.SetCidrlist(config.CIDRList)
	i.SetProtocol("TCP")
	i.SetSecuritygroupid(sg.Id)
	i.SetStartport(config.Comm.Port())
	i.SetEndport(config.Comm.Port())
	if config.Project != "" {
		i.SetProjectid(config.Project)
	}

	_, err = client.SecurityGroup.AuthorizeSecurityGroupIngress(i)
	if err != nil {
		err := fmt.Errorf("Failed to authorize security group ingress rule: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	return multistep.ActionContinue
}

// Cleanup any resources that may have been created during the Run phase.
func (s *stepCreateSecurityGroup) Cleanup(state multistep.StateBag) {
	client := state.Get("client").(*cloudstack.CloudStackClient)
	config := state.Get("config").(*Config)
	ui := state.Get("ui").(packersdk.Ui)

	if s.tempSG == "" {
		return
	}

	ui.Say(fmt.Sprintf("Cleanup temporary security group: %s ...", s.tempSG))
	p := client.SecurityGroup.NewDeleteSecurityGroupParams()
	p.SetId(s.tempSG)
	if config.Project != "" {
		p.SetProjectid(config.Project)
	}

	if _, err := client.SecurityGroup.DeleteSecurityGroup(p); err != nil {
		ui.Error(err.Error())
		ui.Error(fmt.Sprintf("Error deleting security group: %s. Please destroy it manually.\n", s.tempSG))
	}
}