mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Ks8/Folders: Fix status codes returned on create (#95055)
* Fix status codes returned by k8s folder handler * Add test for status code when creating duplicate folder
This commit is contained in:
parent
9d1f8fbdb0
commit
8abfcdbb78
@ -4,6 +4,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/response"
|
"github.com/grafana/grafana/pkg/api/response"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
@ -42,3 +45,24 @@ func ToFolderErrorResponse(err error) response.Response {
|
|||||||
|
|
||||||
return response.ErrOrFallback(http.StatusInternalServerError, "Folder API error", err)
|
return response.ErrOrFallback(http.StatusInternalServerError, "Folder API error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToFolderStatusError(err error) k8sErrors.StatusError {
|
||||||
|
resp := ToFolderErrorResponse(err)
|
||||||
|
|
||||||
|
normResp, ok := resp.(*response.NormalResponse)
|
||||||
|
if !ok {
|
||||||
|
return k8sErrors.StatusError{
|
||||||
|
ErrStatus: metav1.Status{
|
||||||
|
Message: "Folder API error",
|
||||||
|
Code: http.StatusInternalServerError,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return k8sErrors.StatusError{
|
||||||
|
ErrStatus: metav1.Status{
|
||||||
|
Message: normResp.ErrMessage(),
|
||||||
|
Code: int32(normResp.Status()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apiserver/pkg/registry/rest"
|
"k8s.io/apiserver/pkg/registry/rest"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/api/apierrors"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||||
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
"github.com/grafana/grafana/pkg/apimachinery/utils"
|
||||||
"github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
|
"github.com/grafana/grafana/pkg/apis/folder/v0alpha1"
|
||||||
@ -186,7 +187,8 @@ func (s *legacyStorage) Create(ctx context.Context,
|
|||||||
ParentUID: parent,
|
ParentUID: parent,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
statusErr := apierrors.ToFolderStatusError(err)
|
||||||
|
return nil, &statusErr
|
||||||
}
|
}
|
||||||
// #TODO can we directly convert instead of doing a Get? the result of the Create
|
// #TODO can we directly convert instead of doing a Get? the result of the Create
|
||||||
// has more data than the one of Get so there is more we can include in the k8s resource
|
// has more data than the one of Get so there is more we can include in the k8s resource
|
||||||
|
@ -308,6 +308,24 @@ func TestIntegrationFoldersApp(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("with dual write (unified storage, mode 1, create existing folder)", func(t *testing.T) {
|
||||||
|
doCreateDuplicateFolderTest(t, apis.NewK8sTestHelper(t, testinfra.GrafanaOpts{
|
||||||
|
AppModeProduction: true,
|
||||||
|
DisableAnonymous: true,
|
||||||
|
APIServerStorageType: "unified",
|
||||||
|
UnifiedStorageConfig: map[string]setting.UnifiedStorageConfig{
|
||||||
|
folderv0alpha1.RESOURCEGROUP: {
|
||||||
|
DualWriterMode: grafanarest.Mode1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
EnableFeatureToggles: []string{
|
||||||
|
featuremgmt.FlagGrafanaAPIServerTestingWithExperimentalAPIs,
|
||||||
|
featuremgmt.FlagNestedFolders,
|
||||||
|
featuremgmt.FlagKubernetesFolders,
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func doFolderTests(t *testing.T, helper *apis.K8sTestHelper) *apis.K8sTestHelper {
|
func doFolderTests(t *testing.T, helper *apis.K8sTestHelper) *apis.K8sTestHelper {
|
||||||
@ -474,6 +492,36 @@ func doNestedCreateTest(t *testing.T, helper *apis.K8sTestHelper) {
|
|||||||
require.Equal(t, parentCreate.Result.URL, parent.URL)
|
require.Equal(t, parentCreate.Result.URL, parent.URL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doCreateDuplicateFolderTest(t *testing.T, helper *apis.K8sTestHelper) {
|
||||||
|
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||||
|
User: helper.Org1.Admin,
|
||||||
|
GVR: gvr,
|
||||||
|
})
|
||||||
|
|
||||||
|
payload := `{
|
||||||
|
"title": "Test",
|
||||||
|
"uid": ""
|
||||||
|
}`
|
||||||
|
create := apis.DoRequest(helper, apis.RequestParams{
|
||||||
|
User: client.Args.User,
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/api/folders",
|
||||||
|
Body: []byte(payload),
|
||||||
|
}, &folder.Folder{})
|
||||||
|
require.NotNil(t, create.Result)
|
||||||
|
parentUID := create.Result.UID
|
||||||
|
require.NotEmpty(t, parentUID)
|
||||||
|
|
||||||
|
create2 := apis.DoRequest(helper, apis.RequestParams{
|
||||||
|
User: client.Args.User,
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/api/folders",
|
||||||
|
Body: []byte(payload),
|
||||||
|
}, &folder.Folder{})
|
||||||
|
require.NotEmpty(t, create2.Response)
|
||||||
|
require.Equal(t, 409, create2.Response.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntegrationFolderCreatePermissions(t *testing.T) {
|
func TestIntegrationFolderCreatePermissions(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping integration test")
|
t.Skip("skipping integration test")
|
||||||
|
Loading…
Reference in New Issue
Block a user