Skip to content

Commit

Permalink
SKS-1418: Support for finding virtual machine template by system labe…
Browse files Browse the repository at this point in the history
…l "sks-template-uid" or id or name
  • Loading branch information
haijianyang committed Jun 7, 2023
1 parent 56b9b5e commit cc70ea0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/service/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ package service
const (
// VMPlacementGroupDescription is the description of vm placement group.
VMPlacementGroupDescription = "This is VM placement group created by CAPE, don't delete it!"

// SKSVMTemplateUIDLabel is the label used to find the virtual machine template.
SKSVMTemplateUIDLabel = "system.cloudtower/sks-template-uid"
)
23 changes: 20 additions & 3 deletions pkg/service/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type VMService interface {
GetByName(name string) (*models.VM, error)
FindByIDs(ids []string) ([]*models.VM, error)
GetVMNics(vmID string) ([]*models.VMNic, error)
GetVMTemplate(id string) (*models.ContentLibraryVMTemplate, error)
GetVMTemplate(template string) (*models.ContentLibraryVMTemplate, error)
GetTask(id string) (*models.Task, error)
WaitTask(id string, timeout, interval time.Duration) (*models.Task, error)
GetCluster(id string) (*models.Cluster, error)
Expand Down Expand Up @@ -532,11 +532,21 @@ func (svr *TowerVMService) GetVlan(id string) (*models.Vlan, error) {
}

// GetVMTemplate searches for a virtual machine template.
func (svr *TowerVMService) GetVMTemplate(id string) (*models.ContentLibraryVMTemplate, error) {
// 1.0 or earlier versions use the template ID or name to find the virtual machine template,
// and other versions prefer to use SKSVMTemplateUIDLabel to find the virtual machine template.
func (svr *TowerVMService) GetVMTemplate(template string) (*models.ContentLibraryVMTemplate, error) {
getVMTemplatesParams := clientvmtemplate.NewGetContentLibraryVMTemplatesParams()
getVMTemplatesParams.RequestBody = &models.GetContentLibraryVMTemplatesRequestBody{
Where: &models.ContentLibraryVMTemplateWhereInput{
OR: []*models.ContentLibraryVMTemplateWhereInput{{ID: TowerString(id)}, {Name: TowerString(id)}},
OR: []*models.ContentLibraryVMTemplateWhereInput{
{ID: TowerString(template)},
{Name: TowerString(template)},
{LabelsSome: &models.LabelWhereInput{
AND: []*models.LabelWhereInput{
{Key: TowerString(SKSVMTemplateUIDLabel), Value: TowerString(template)},
},
}},
},
},
}

Expand All @@ -550,6 +560,13 @@ func (svr *TowerVMService) GetVMTemplate(id string) (*models.ContentLibraryVMTem
return nil, errors.New(VMTemplateNotFound)
}

for i := 0; i < len(vmTemplates); i++ {
// Match SKSVMTemplateUIDLabel.
if template != *vmTemplates[i].ID && template != *vmTemplates[i].Name {
return vmTemplates[i], nil
}
}

return vmTemplates[0], nil
}

Expand Down

0 comments on commit cc70ea0

Please sign in to comment.