forked from sodafoundation/strato
/
context.go
146 lines (131 loc) · 4.98 KB
/
context.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2018 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// 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.
// This is self defined context which is stored in context.Input.data.
// It is used to transport data in the pipe line.
package context
import (
"encoding/json"
"reflect"
"github.com/emicklei/go-restful"
"github.com/golang/glog"
)
func NewAdminContext() *Context {
return &Context{
IsAdmin: true,
}
}
func NewInternalTenantContext(tenantId, userId string) *Context {
return &Context{
TenantId: tenantId,
UserId: userId,
IsAdmin: true,
}
}
func NewContextFromJson(s string) *Context {
ctx := &Context{}
err := json.Unmarshal([]byte(s), ctx)
if err != nil {
glog.Errorf("Unmarshal json to context failed, reason: %v", err)
}
return ctx
}
func GetContext(req *restful.Request) *Context {
ctx, _ := req.Attribute("context").(*Context)
if ctx == nil {
ctx = &Context{}
}
return ctx
}
func UpdateContext(req *restful.Request, param map[string]interface{}) (*Context, error) {
ctx := GetContext(req)
if param == nil || len(param) == 0 {
glog.Warning("Context parameter is empty, nothing to be updated")
return ctx, nil
}
ctxV := reflect.ValueOf(ctx).Elem()
for key, val := range param {
field := ctxV.FieldByName(key)
pv := reflect.ValueOf(val)
if field.Kind() == pv.Kind() && field.CanSet() {
field.Set(pv)
} else {
glog.Errorf("Invalid parameter %s : %v", key, val)
}
}
req.SetAttribute("context", ctx)
return ctx, nil
}
type Context struct {
AuthToken string `policy:"true" json:"auth_token"`
UserId string `policy:"true" json:"user_id"`
TenantId string `policy:"true" json:"tenant_id"`
DomainId string `policy:"true" json:"domain_id"`
UserDomainId string `policy:"true" json:"user_domain_id"`
ProjectDomainId string `policy:"true" json:"project_domain_id"`
IsAdmin bool `policy:"true" json:"is_admin"`
ReadOnly string `policy:"true" json:"read_only"`
ShowDeleted string `policy:"true" json:"show_deleted"`
RequestId string `policy:"true" json:"request_id"`
ResourceUuid string `policy:"true" json:"resource_uuid"`
Overwrite string `policy:"true" json:"overwrite"`
Roles []string `policy:"true" json:"roles"`
UserName string `policy:"true" json:"user_name"`
ProjectName string `policy:"true" json:"project_name"`
DomainName string `policy:"true" json:"domain_name"`
UserDomainName string `policy:"true" json:"user_domain_name"`
ProjectDomainName string `policy:"true" json:"project_domain_name"`
IsAdminProject bool `policy:"true" json:"is_admin_project"`
ServiceToken string `policy:"true" json:"service_token"`
ServiceUserId string `policy:"true" json:"service_user_id"`
ServiceUserName string `policy:"true" json:"service_user_name"`
ServiceUserDomainId string `policy:"true" json:"service_user_domain_id"`
ServiceUserDomainName string `policy:"true" json:"service_user_domain_name"`
ServiceProjectId string `policy:"true" json:"service_project_id"`
ServiceProjectName string `policy:"true" json:"service_project_name"`
ServiceProjectDomainId string `policy:"true" json:"service_project_domain_id"`
ServiceProjectDomainName string `policy:"true" json:"service_project_domain_name"`
ServiceRoles string `policy:"true" json:"service_roles"`
Token string `policy:"false" json:"token"`
Uri string `policy:"false" json:"uri"`
}
func (ctx *Context) ToPolicyValue() map[string]interface{} {
ctxMap := map[string]interface{}{}
t := reflect.TypeOf(ctx).Elem()
v := reflect.ValueOf(ctx).Elem()
for i := 0; i < t.NumField(); i++ {
field := v.Field(i)
name := t.Field(i).Tag.Get("json")
if t.Field(i).Tag.Get("policy") == "false" {
continue
}
if field.Kind() == reflect.String && field.String() == "" {
continue
}
if field.Kind() == reflect.Slice && field.Len() == 0 {
continue
}
if field.Kind() == reflect.Map && field.Len() == 0 {
continue
}
ctxMap[name] = field.Interface()
}
return ctxMap
}
func (ctx *Context) ToJson() string {
b, err := json.Marshal(ctx)
if err != nil {
glog.Errorf("Context convert to json failed, reason: %v", err)
}
return string(b)
}