forked from kubermatic/kubermatic
/
fake_provider.go
121 lines (98 loc) · 4.34 KB
/
fake_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"errors"
"net/http"
"time"
kubermaticapiv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/provider"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
NoExistingFakeProject = "NoExistingFakeProject"
NoExistingFakeProjectID = "NoExistingFakeProject-ID"
ForbiddenFakeProject = "ForbiddenFakeProject"
ForbiddenFakeProjectID = "ForbiddenFakeProject-ID"
ExistingFakeProject = "ExistingFakeProject"
ExistingFakeProjectID = "ExistingFakeProject-ID"
ImpersonatedClientErrorMsg = "forbidden"
)
type FakePrivilegedProjectProvider struct {
}
type FakeProjectProvider struct {
}
func NewFakeProjectProvider() *FakeProjectProvider {
return &FakeProjectProvider{}
}
func NewFakePrivilegedProjectProvider() *FakePrivilegedProjectProvider {
return &FakePrivilegedProjectProvider{}
}
func (f *FakeProjectProvider) New(user *kubermaticapiv1.User, name string, labels map[string]string) (*kubermaticapiv1.Project, error) {
return nil, errors.New("not implemented")
}
// Delete deletes the given project as the given user
//
// Note:
// Before deletion project's status.phase is set to ProjectTerminating
func (f *FakeProjectProvider) Delete(userInfo *provider.UserInfo, projectInternalName string) error {
return errors.New("not implemented")
}
// Get returns the project with the given name
func (f *FakeProjectProvider) Get(userInfo *provider.UserInfo, projectInternalName string, options *provider.ProjectGetOptions) (*kubermaticapiv1.Project, error) {
if NoExistingFakeProjectID == projectInternalName || ForbiddenFakeProjectID == projectInternalName {
return nil, createError(http.StatusForbidden, ImpersonatedClientErrorMsg)
}
return GenProject(ExistingFakeProject, kubermaticapiv1.ProjectActive, DefaultCreationTimestamp().Add(2*time.Minute)), nil
}
// Update update an existing project and returns it
func (f *FakeProjectProvider) Update(userInfo *provider.UserInfo, newProject *kubermaticapiv1.Project) (*kubermaticapiv1.Project, error) {
return nil, errors.New("not implemented")
}
// List gets a list of projects, by default it returns all resources.
// If you want to filter the result please set ProjectListOptions
//
// Note that the list is taken from the cache
func (f *FakeProjectProvider) List(options *provider.ProjectListOptions) ([]*kubermaticapiv1.Project, error) {
return nil, errors.New("not implemented")
}
// GetUnsecured returns the project with the given name
// This function is unsafe in a sense that it uses privileged account to get project with the given name
func (f *FakePrivilegedProjectProvider) GetUnsecured(projectInternalName string, options *provider.ProjectGetOptions) (*kubermaticapiv1.Project, error) {
if NoExistingFakeProjectID == projectInternalName {
return nil, createError(http.StatusNotFound, "")
}
if ForbiddenFakeProjectID == projectInternalName {
return nil, createError(http.StatusForbidden, "")
}
return nil, nil
}
// DeleteUnsecured deletes any given project
// This function is unsafe in a sense that it uses privileged account to delete project with the given name
func (f *FakePrivilegedProjectProvider) DeleteUnsecured(projectInternalName string) error {
return nil
}
// UpdateUnsecured update an existing project and returns it
// This function is unsafe in a sense that it uses privileged account to update project
func (f *FakePrivilegedProjectProvider) UpdateUnsecured(project *kubermaticapiv1.Project) (*kubermaticapiv1.Project, error) {
return project, nil
}
func createError(status int32, message string) error {
return &kerrors.StatusError{ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: status,
Reason: metav1.StatusReasonBadRequest,
Message: message,
}}
}