1 package mocks
2
3 import (
4 "encoding/json"
5 "slices"
6 "strings"
7
8 "edge-infra.dev/pkg/edge/api/graph/model"
9 )
10
11 var (
12 defaultSummaryResponse = SummaryResponse{
13 OperatorInterventionRoleMappings: []model.OiRoleMapping{
14 {
15 Role: "role1",
16 Privileges: []*model.Privilege{
17 {Name: "priv1"},
18 {Name: "priv2"},
19 },
20 },
21 {
22 Role: "role2",
23 Privileges: []*model.Privilege{
24 {Name: "priv3"},
25 },
26 },
27 {
28 Role: "role3",
29 Privileges: []*model.Privilege{
30 {Name: "priv4"},
31 },
32 },
33 },
34 OperatorInterventionRules: []model.Rule{
35 {
36 Privilege: &model.Privilege{Name: "priv1"},
37 Commands: []*model.Command{
38 {Name: "command1"},
39 {Name: "command2"},
40 },
41 },
42 {
43 Privilege: &model.Privilege{Name: "priv2"},
44 Commands: []*model.Command{
45 {Name: "command3"},
46 },
47 },
48 {
49 Privilege: &model.Privilege{Name: "priv3"},
50 Commands: []*model.Command{
51 {Name: "command4"},
52 },
53 },
54 {
55 Privilege: &model.Privilege{Name: "priv4"},
56 Commands: []*model.Command{
57 {Name: "command5"},
58 {Name: "command6"},
59 },
60 },
61 },
62 }
63 )
64
65 type SummaryResponse struct {
66 OperatorInterventionRoleMappings []model.OiRoleMapping `graphql:"operatorInterventionRoleMappings" json:"operatorInterventionRoleMappings"`
67 OperatorInterventionRules []model.Rule `graphql:"operatorInterventionRules" json:"operatorInterventionRules"`
68 }
69
70 func Summary(body []byte) (interface{}, bool, error) {
71 if !strings.Contains(string(body), "operatorInterventionRoleMappings(roles: $roles){role,privileges{name}},operatorInterventionRules{privilege{name},commands{name}}") {
72 return nil, false, nil
73 }
74
75 var in struct {
76 Query string
77 Variables struct {
78 Roles []string
79 }
80 }
81 err := json.Unmarshal(body, &in)
82 if err != nil {
83 return nil, true, err
84 }
85
86 var newMappings []model.OiRoleMapping
87 for _, mapping := range defaultSummaryResponse.OperatorInterventionRoleMappings {
88 if slices.Contains(in.Variables.Roles, mapping.Role.String()) {
89 newMappings = append(newMappings, mapping)
90 }
91 }
92 return &SummaryResponse{
93 OperatorInterventionRoleMappings: newMappings,
94 OperatorInterventionRules: defaultSummaryResponse.OperatorInterventionRules,
95 }, true, nil
96 }
97
View as plain text