Skip to content

Commit

Permalink
add Project support
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxtof committed Aug 5, 2023
1 parent 3a39640 commit abfef57
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
21 changes: 9 additions & 12 deletions .github/workflows/test-plugin-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ on:
workflow_dispatch:
inputs:
logs:
description: 'Set 1 to activate full logs'
description: "Set 1 to activate full logs"
required: false
default: '0'
default: "0"

jobs:
plugin-build:
Expand All @@ -22,28 +22,27 @@ jobs:
- name: Setup `golang`
uses: actions/setup-go@v4
with:
go-version: '>=1.19.0'
go-version: ">=1.19.0"

- name: Build packer plugin
run: |
cd $GITHUB_WORKSPACE
make dev
- uses: actions/upload-artifact@v3
with:
name: packer-plugin-nutanix
path: ~/.packer.d/plugins/packer-plugin-nutanix
path: ~/.config/packer/plugins/packer-plugin-nutanix
retention-days: 7

- name: build test list
id: test-list
run: echo "list=$(ls test/e2e | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT


e2e:
name: E2E test
needs: plugin-build

strategy:
matrix:
test: ${{fromJSON(needs.plugin-build.outputs.test-list)}}
Expand All @@ -52,7 +51,6 @@ jobs:
defaults:
run:
working-directory: test/e2e/${{ matrix.test}}


steps:
- name: Checkout Repository
Expand All @@ -74,9 +72,9 @@ jobs:

- name: Install plugin
run: |
mkdir -p ~/.packer.d/plugins/
cp /tmp/packer-plugin-nutanix ~/.packer.d/plugins/packer-plugin-nutanix
chmod 755 ~/.packer.d/plugins/packer-plugin-nutanix
mkdir -p ~/.config/packer/plugins/
cp /tmp/packer-plugin-nutanix ~/.config/packer/plugins/packer-plugin-nutanix
chmod 755 ~/.config/packer/plugins/packer-plugin-nutanix
- name: Run `packer init`
id: init
Expand Down Expand Up @@ -108,4 +106,3 @@ jobs:
else
exit 1
fi
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ build:
@go build -o ${BINARY}

dev: build
@mkdir -p ~/.packer.d/plugins/
@mv ${BINARY} ~/.packer.d/plugins/${BINARY}
@mkdir -p ~/.config/packer/plugins
@mv ${BINARY} ~/.config/packer/plugins/${BINARY}

test:
@go test -race -count $(COUNT) $(TEST) -timeout=3m
Expand Down
1 change: 1 addition & 0 deletions builder/nutanix/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type VmConfig struct {
MemoryMB int64 `mapstructure:"memory_mb" json:"memory_mb" required:"false"`
UserData string `mapstructure:"user_data" json:"user_data" required:"false"`
VMCategories []Category `mapstructure:"vm_categories" required:"false"`
Project string `mapstructure:"project" required:"false"`
}

func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
Expand Down
4 changes: 4 additions & 0 deletions builder/nutanix/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ type nutanixImage struct {
image v3.ImageIntentResponse
}

func findProjectByName(conn *v3.Client, name string) (*v3.Project, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllProject(filter)
if err != nil {
return nil, err
}
entities := resp.Entities

found := make([]*v3.Project, 0)
for _, v := range entities {
if v.Status.Name == name {
found = append(found, &v3.Project{
Status: v.Status,
Spec: v.Spec,
Metadata: v.Metadata,
APIVersion: v.APIVersion,
})
}
}

if len(found) > 1 {
return nil, fmt.Errorf("your query returned more than one result")
}

if len(found) == 0 {
return nil, fmt.Errorf("did not find project with name %s", name)
}

return found[0], nil
}

func findClusterByName(conn *v3.Client, name string) (*v3.ClusterIntentResponse, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllCluster(filter)
Expand Down Expand Up @@ -117,6 +148,7 @@ func findSubnetByName(conn *v3.Client, name string) (*v3.SubnetIntentResponse, e

return found[0], nil
}

func sourceImageExists(conn *v3.Client, name string, uri string) (*v3.ImageIntentResponse, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllImage(filter)
Expand Down Expand Up @@ -437,6 +469,18 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig) (*v3.VMIntentInput, error) {
req.Metadata.Categories = c
}

if vm.Project != "" {
project, err := findProjectByName(conn, vm.Project)
if err != nil {
return nil, fmt.Errorf("error while findProjectByName, %s", err.Error())
}

req.Metadata.ProjectReference = &v3.Reference{
Kind: StringPtr("project"),
UUID: project.Metadata.UUID,
}
}

return req, nil

}
Expand Down
3 changes: 3 additions & 0 deletions builder/nutanix/step_build_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ func (s *stepBuildVM) Run(ctx context.Context, state multistep.StateBag) multist

ui.Say("Creating Packer Builder virtual machine...")

// Create VM Spec
vmRequest, err := d.CreateRequest(config.VmConfig)
if err != nil {
ui.Error("Error creating virtual machine request: " + err.Error())
state.Put("error", err)
return multistep.ActionHalt
}

// Create VM
vmInstance, err := d.Create(vmRequest)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions docs/builders/nutanix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ These parameters allow to define information about platform and temporary VM use
- `boot_type` (string) - Type of boot used on the temporary VM ("legacy" or "uefi").
- `ip_wait_timeout` (duration string | ex: "0h42m0s") - Amount of time to wait for VM's IP, similar to 'ssh_timeout'. Defaults to 15m (15 minutes). See the Golang [ParseDuration](https://golang.org/pkg/time/#ParseDuration) documentation for full details.
- `vm_categories` ([]Category) - Assign Categories to the vm.
- `project` (string) - Assign Project to the vm.


## Output configuration
Expand Down
2 changes: 2 additions & 0 deletions example/source.nutanix.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ source "nutanix" "centos" {
value = "Dev"
}

// project = "myproject"

image_name = "centos-packer-image"
image_export = false
force_deregister = true
Expand Down

0 comments on commit abfef57

Please sign in to comment.