-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split organization.go into multiple files
- Loading branch information
Showing
10 changed files
with
677 additions
and
564 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
orgv1 "github.com/appuio/control-api/apis/organization/v1" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
) | ||
|
||
var _ rest.Creater = &organizationStorage{} | ||
|
||
func (s *organizationStorage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) { | ||
org, ok := obj.(*orgv1.Organization) | ||
if !ok { | ||
return nil, fmt.Errorf("not an organization: %#v", obj) | ||
} | ||
err := s.authorizer.AuthorizeContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Validate Org | ||
if err := createValidation(ctx, obj); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := s.namepaces.CreateNamespace(ctx, org.ToNamespace(), options); err != nil { | ||
return nil, convertNamespaceError(err) | ||
} | ||
return org, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
orgv1 "github.com/appuio/control-api/apis/organization/v1" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apiserver/pkg/authorization/authorizer" | ||
"k8s.io/apiserver/pkg/endpoints/request" | ||
) | ||
|
||
func TestOrganizationStorage_Create(t *testing.T) { | ||
tests := map[string]struct { | ||
organizationIn *orgv1.Organization | ||
|
||
namespaceErr error | ||
|
||
authDecision authResponse | ||
|
||
organizationOut *orgv1.Organization | ||
err error | ||
}{ | ||
"GivenCreateOrg_ThenSuccess": { | ||
organizationIn: fooOrg, | ||
authDecision: authResponse{ | ||
decision: authorizer.DecisionAllow, | ||
}, | ||
organizationOut: fooOrg, | ||
}, | ||
"GivenNsExists_ThenFail": { | ||
organizationIn: fooOrg, | ||
authDecision: authResponse{ | ||
decision: authorizer.DecisionAllow, | ||
}, | ||
namespaceErr: apierrors.NewAlreadyExists(schema.GroupResource{ | ||
Resource: "namepaces", | ||
}, "foo"), | ||
err: apierrors.NewAlreadyExists(schema.GroupResource{ | ||
Group: orgv1.GroupVersion.Group, | ||
Resource: "organizations", | ||
}, "foo"), | ||
}, | ||
"GivenAuthFails_ThenFail": { | ||
organizationIn: fooOrg, | ||
authDecision: authResponse{ | ||
err: errors.New("failed"), | ||
}, | ||
err: errors.New("failed"), | ||
}, | ||
"GivenForbidden_ThenForbidden": { | ||
organizationIn: fooOrg, | ||
authDecision: authResponse{ | ||
decision: authorizer.DecisionDeny, | ||
reason: "confidential", | ||
}, | ||
err: apierrors.NewForbidden(schema.GroupResource{ | ||
Group: orgv1.GroupVersion.Group, | ||
Resource: "organizations", | ||
}, fooOrg.Name, errors.New("confidential")), | ||
}, | ||
} | ||
|
||
for n, tc := range tests { | ||
t.Run(n, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
os, mnp, mauth := newMockedOrganizationStorage(ctrl) | ||
mauth.EXPECT(). | ||
Authorize(gomock.Any(), isAuthRequest("create")). | ||
Return(tc.authDecision.decision, tc.authDecision.reason, tc.authDecision.err). | ||
Times(1) | ||
mnp.EXPECT(). | ||
CreateNamespace(gomock.Any(), gomock.Any(), gomock.Any()). | ||
Return(tc.namespaceErr). | ||
AnyTimes() | ||
|
||
nopValidate := func(ctx context.Context, obj runtime.Object) error { | ||
return nil | ||
} | ||
org, err := os.Create(request.WithRequestInfo(request.NewContext(), | ||
&request.RequestInfo{ | ||
Verb: "create", | ||
APIGroup: orgv1.GroupVersion.Group, | ||
Resource: "organizations", | ||
Name: tc.organizationIn.Name, | ||
}), | ||
tc.organizationIn, nopValidate, nil) | ||
|
||
if tc.err != nil { | ||
assert.EqualError(t, err, tc.err.Error()) | ||
return | ||
} | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.organizationOut, org) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package organization | ||
|
||
import ( | ||
"context" | ||
|
||
orgv1 "github.com/appuio/control-api/apis/organization/v1" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
) | ||
|
||
var _ rest.GracefulDeleter = &organizationStorage{} | ||
|
||
func (s *organizationStorage) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) { | ||
err := s.authorizer.AuthorizeContext(ctx) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
org, err := s.Get(ctx, name, nil) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if deleteValidation != nil { | ||
err := deleteValidation(ctx, org) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
} | ||
|
||
ns, err := s.namepaces.DeleteNamespace(ctx, name, options) | ||
return orgv1.NewOrganizationFromNS(ns), false, convertNamespaceError(err) | ||
} |
Oops, something went wrong.