Skip to content

Commit

Permalink
fix: support content library item uuid for hcp
Browse files Browse the repository at this point in the history
If a content library item is created, that this item is used for the `vsphere_uuid` in HCP. Stores the `content_library_item_uuid` into state and uses this value if present. otherwise, it uses the default.

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed May 24, 2024
1 parent ec5d9c8 commit 7598c30
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
8 changes: 7 additions & 1 deletion builder/vsphere/common/hcp_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ func GetVMMetadata(vm *driver.VirtualMachineDriver, state multistep.StateBag) ma
return labels
}
if info.Config != nil {
labels["vsphere_uuid"] = info.Config.Uuid
if itemUuid, ok := state.Get("content_library_item_uuid").(string); ok {
// If the content library is used, use the content library item UUID.
labels["vsphere_uuid"] = itemUuid
} else {
// If the content library is not used, use the virtual machine UUID.
labels["vsphere_uuid"] = info.Config.Uuid
}

// VM description
if info.Config.Annotation != "" {
Expand Down
13 changes: 11 additions & 2 deletions builder/vsphere/common/step_import_to_content_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (s *StepImportToContentLibrary) Run(_ context.Context, state multistep.Stat
}
ui.Say(fmt.Sprintf("Importing %s template %s to Content Library '%s' as the item '%s' with the description '%s'...",
vmTypeLabel, s.ContentLibConfig.Name, s.ContentLibConfig.Library, s.ContentLibConfig.Name, s.ContentLibConfig.Description))

if s.ContentLibConfig.Ovf {
err = s.importOvfTemplate(vm)
} else {
Expand All @@ -161,10 +162,18 @@ func (s *StepImportToContentLibrary) Run(_ context.Context, state multistep.Stat
state.Put("destroy_vm", s.ContentLibConfig.Destroy)
}

// For HCP Packer metadata, we save the template's datastore in state.
// For HCP Packer metadata, save the content library item UUID in state.
itemUuid, err := vm.FindContentLibraryItemUUID(s.ContentLibConfig.Library, s.ContentLibConfig.Name)
if err != nil {
ui.Say(fmt.Sprintf("[TRACE] Failed to get content library item uuid: %s", err.Error()))
} else {
state.Put("content_library_item_uuid", itemUuid)
}

// For HCP Packer metadata, save the content library datastore name in state.
datastores, err := vm.FindContentLibraryTemplateDatastoreName(s.ContentLibConfig.Library)
if err != nil {
ui.Say(fmt.Sprintf("[TRACE] Failed to get Content Library datastore name: %s", err.Error()))
ui.Say(fmt.Sprintf("[TRACE] Failed to get content library datastore name: %s", err.Error()))
} else {
state.Put("content_library_datastore", datastores)
}
Expand Down
8 changes: 8 additions & 0 deletions builder/vsphere/driver/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func (d *VCenterDriver) FindContentLibraryItem(libraryId string, name string) (*
return nil, fmt.Errorf("Item %s not found", name)
}

func (d *VCenterDriver) FindContentLibraryItemUUID(libraryId string, name string) (string, error) {
item, err := d.FindContentLibraryItem(libraryId, name)
if err != nil {
return "", err
}
return item.ID, nil
}

func (d *VCenterDriver) FindContentLibraryFileDatastorePath(isoPath string) (string, error) {
log.Printf("Check if ISO path is a Content Library path")
err := d.restClient.Login(d.ctx)
Expand Down
23 changes: 23 additions & 0 deletions builder/vsphere/driver/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,29 @@ func (vm *VirtualMachineDriver) Datacenter() *object.Datacenter {
return vm.driver.datacenter
}

func (vm *VirtualMachineDriver) FindContentLibraryItemUUID(library string, name string) (string, error) {
err := vm.driver.restClient.Login(vm.driver.ctx)

Check failure on line 1372 in builder/vsphere/driver/vm.go

View workflow job for this annotation

GitHub Actions / Lint check

File is not `goimports`-ed (goimports)
if err != nil {
return "", err
}

l, err := vm.driver.FindContentLibraryByName(library)
if err != nil {
log.Printf("cannot find content library: %v", err)
vm.logout()
return "", err
}

item, err := vm.driver.FindContentLibraryItemUUID(l.library.ID, name)
if err != nil {
log.Printf("cannot find content library item: %v", err)
vm.logout()
return "", err
}

return item, nil
}

func (vm *VirtualMachineDriver) FindContentLibraryTemplateDatastoreName(library string) ([]string, error) {
err := vm.driver.restClient.Login(vm.driver.ctx)
if err != nil {
Expand Down

0 comments on commit 7598c30

Please sign in to comment.