/
put_role.go
93 lines (82 loc) · 2.62 KB
/
put_role.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
/*
Copyright 2020,2021 Avi Zimmerman
This file is part of kvdi.
kvdi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
kvdi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with kvdi. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"context"
"errors"
"fmt"
"net/http"
rbacv1 "github.com/tinyzimmer/kvdi/apis/rbac/v1"
"github.com/tinyzimmer/kvdi/pkg/types"
"github.com/tinyzimmer/kvdi/pkg/util/apiutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ktypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// swagger:operation PUT /api/roles/{role} Roles putRoleRequest
// ---
// summary: Update the specified role.
// description: All properties will be overwritten with those provided in the payload, even if undefined.
// parameters:
// - name: role
// in: path
// description: The role to update
// type: string
// required: true
// - in: body
// name: roleDetails
// description: The role details to update.
// schema:
// "$ref": "#/definitions/UpdateRoleRequest"
// responses:
// "200":
// "$ref": "#/responses/boolResponse"
// "400":
// "$ref": "#/responses/error"
// "403":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/error"
func (d *desktopAPI) UpdateRole(w http.ResponseWriter, r *http.Request) {
role := apiutil.GetRoleFromRequest(r)
nn := ktypes.NamespacedName{Name: role, Namespace: metav1.NamespaceAll}
vdiRole := &rbacv1.VDIRole{}
if err := d.client.Get(context.TODO(), nn, vdiRole); err != nil {
if client.IgnoreNotFound(err) == nil {
apiutil.ReturnAPINotFound(fmt.Errorf("The role '%s' doesn't exist", role), w)
return
}
apiutil.ReturnAPIError(err, w)
return
}
params := apiutil.GetRequestObject(r).(*types.UpdateRoleRequest)
if params == nil {
apiutil.ReturnAPIError(errors.New("Malformed request"), w)
return
}
vdiRole.Annotations = params.GetAnnotations()
vdiRole.Rules = params.GetRules()
if err := d.client.Update(context.TODO(), vdiRole); err != nil {
apiutil.ReturnAPIError(err, w)
return
}
apiutil.WriteOK(w)
}
// Request containing updates to a role
// swagger:parameters putRoleRequest
type swaggerUpdateRoleRequest struct {
// in:body
Body types.UpdateRoleRequest
}