Fix creation of manual label that allowed overriding existing one (#18474)
This commit is contained in:
@@ -3780,6 +3780,11 @@ func (s *integrationTestSuite) TestLabels() {
|
||||
assert.Empty(t, createResp.Label.HostIDs)
|
||||
lbl1 := createResp.Label.Label
|
||||
|
||||
// try to create a manual label with the same name
|
||||
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: lbl1.Name, Hosts: []string{manualHosts[0].UUID}}, http.StatusConflict, &createResp)
|
||||
// try to create a dynamic label with the same name
|
||||
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: lbl1.Name, Query: "select 2"}, http.StatusConflict, &createResp)
|
||||
|
||||
// get the label
|
||||
var getResp getLabelResponse
|
||||
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d", lbl1.ID), nil, http.StatusOK, &getResp)
|
||||
@@ -3814,6 +3819,11 @@ func (s *integrationTestSuite) TestLabels() {
|
||||
assert.Empty(t, createResp.Label.HostIDs)
|
||||
manualLbl2 := createResp.Label.Label
|
||||
|
||||
// try to create a manual label with the same name
|
||||
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: manualLbl2.Name, Hosts: []string{manualHosts[0].UUID}}, http.StatusConflict, &createResp)
|
||||
// try to create a dynamic label with the same name
|
||||
s.DoJSON("POST", "/api/latest/fleet/labels", &fleet.LabelPayload{Name: manualLbl2.Name, Query: "select 2"}, http.StatusConflict, &createResp)
|
||||
|
||||
// get the label
|
||||
getResp = getLabelResponse{}
|
||||
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d", manualLbl2.ID), nil, http.StatusOK, &getResp)
|
||||
|
||||
+10
-15
@@ -73,9 +73,14 @@ func (svc *Service) NewLabel(ctx context.Context, p fleet.LabelPayload) (*fleet.
|
||||
}
|
||||
}
|
||||
|
||||
// if membership type is manual, must use ApplyLabelSpecs (as NewLabel does
|
||||
// not create label memberships), otherwise NewLabel works for dynamic
|
||||
// membership. Must resolve the host identifiers to hostname so that
|
||||
// first create the new label, which will fail if the name is not unique
|
||||
newLbl, err := svc.ds.NewLabel(ctx, label)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Next, if membership type is manual, use ApplyLabelSpecs to create label
|
||||
// memberships. Must resolve the host identifiers to hostname so that
|
||||
// ApplySpecs can be used.
|
||||
var hostIDs []uint
|
||||
if label.LabelMembershipType == fleet.LabelMembershipTypeManual {
|
||||
@@ -96,21 +101,11 @@ func (svc *Service) NewLabel(ctx context.Context, p fleet.LabelPayload) (*fleet.
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// must reload it to get the id, and the host IDs
|
||||
lblIDsByName, err := svc.ds.LabelIDsByName(ctx, []string{label.Name})
|
||||
// must reload it to get the host IDs
|
||||
label, hostIDs, err = svc.ds.Label(ctx, newLbl.ID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
label, hostIDs, err = svc.ds.Label(ctx, lblIDsByName[label.Name])
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
} else {
|
||||
newLbl, err := svc.ds.NewLabel(ctx, label)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
label = newLbl
|
||||
}
|
||||
return label, hostIDs, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user