Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
haijianyang committed Jan 5, 2024
1 parent bdebdaa commit 0c73761
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions pkg/service/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,21 +757,37 @@ func (svr *TowerVMService) UpsertLabel(key, value string) (*models.Label, error)
// DeleteLabel deletes a label.
// If strict is false, delete the label directly.
// If strict is true, delete the label only if no virtual machine references the label.
//
// The return value:
// 1. An empty string indicates that the label does not need to be deleted or has been deleted.
// 2.A non-empty string indicates the deleted label ID.
func (svr *TowerVMService) DeleteLabel(key, value string, strict bool) (string, error) {
getLabelParams := clientlabel.NewGetLabelsParams()
getLabelParams.RequestBody = &models.GetLabelsRequestBody{
Where: &models.LabelWhereInput{
Key: TowerString(key),
Value: TowerString(value),
},
}
getLabelResp, err := svr.Session.Label.GetLabels(getLabelParams)
if err != nil {
return "", err
}
if len(getLabelResp.Payload) == 0 {
return "", nil
}

label := getLabelResp.Payload[0]
if strict && len(label.Vms) > 0 {
return "", nil
}

deleteLabelParams := clientlabel.NewDeleteLabelParams()
deleteLabelParams.RequestBody = &models.LabelDeletionParams{
Where: &models.LabelWhereInput{
AND: []*models.LabelWhereInput{
{Key: TowerString(key), Value: TowerString(value)},
},
ID: label.ID,
},
}
if strict {
deleteLabelParams.RequestBody.Where.AND = append(
deleteLabelParams.RequestBody.Where.AND,
&models.LabelWhereInput{VMNum: TowerInt32(0)},
)
}

deleteLabelResp, err := svr.Session.Label.DeleteLabel(deleteLabelParams)
if err != nil {
Expand Down

0 comments on commit 0c73761

Please sign in to comment.