diff --git a/changes/bug_spec_labels_api_not_including_ids b/changes/bug_spec_labels_api_not_including_ids new file mode 100644 index 0000000000..80bf4c9289 --- /dev/null +++ b/changes/bug_spec_labels_api_not_including_ids @@ -0,0 +1 @@ +- GET /api/v1/fleet/spec/labels/{name} endpoint should include the label id diff --git a/server/datastore/mysql/labels.go b/server/datastore/mysql/labels.go index d678819cf1..968ab92907 100644 --- a/server/datastore/mysql/labels.go +++ b/server/datastore/mysql/labels.go @@ -140,7 +140,7 @@ func (ds *Datastore) GetLabelSpecs(ctx context.Context) ([]*fleet.LabelSpec, err func (ds *Datastore) GetLabelSpec(ctx context.Context, name string) (*fleet.LabelSpec, error) { var specs []*fleet.LabelSpec query := ` -SELECT name, description, query, platform, label_type, label_membership_type +SELECT id, name, description, query, platform, label_type, label_membership_type FROM labels WHERE name = ? ` diff --git a/server/datastore/mysql/labels_test.go b/server/datastore/mysql/labels_test.go index 68be5b0009..ac1e2d3b09 100644 --- a/server/datastore/mysql/labels_test.go +++ b/server/datastore/mysql/labels_test.go @@ -11,6 +11,7 @@ import ( "github.com/fleetdm/fleet/v4/server/fleet" "github.com/fleetdm/fleet/v4/server/ptr" "github.com/fleetdm/fleet/v4/server/test" + "github.com/google/go-cmp/cmp" "github.com/jmoiron/sqlx" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -708,7 +709,10 @@ func testLabelsGetSpec(t *testing.T, ds *Datastore) { for _, s := range expectedSpecs { spec, err := ds.GetLabelSpec(context.Background(), s.Name) require.Nil(t, err) - assert.Equal(t, s, spec) + + require.True(t, cmp.Equal(s, spec, cmp.FilterPath(func(p cmp.Path) bool { + return p.String() == "ID" + }, cmp.Ignore()))) } } diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index f997546e70..38c091b9ca 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -3185,6 +3185,7 @@ func (s *integrationTestSuite) TestLabelSpecs() { var getResp getLabelSpecResponse s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/spec/labels/%s", url.PathEscape(name)), nil, http.StatusOK, &getResp) assert.Equal(t, name, getResp.Spec.Name) + assert.NotEqual(t, 0, getResp.Spec.ID) // get a non-existing label spec s.DoJSON("GET", "/api/latest/fleet/spec/labels/zzz", nil, http.StatusNotFound, &getResp)