package mocks import ( "encoding/json" "slices" "strings" "edge-infra.dev/pkg/edge/api/graph/model" ) var ( defaultSummaryResponse = SummaryResponse{ OperatorInterventionRoleMappings: []model.OiRoleMapping{ { Role: "role1", Privileges: []*model.Privilege{ {Name: "priv1"}, {Name: "priv2"}, }, }, { Role: "role2", Privileges: []*model.Privilege{ {Name: "priv3"}, }, }, { Role: "role3", Privileges: []*model.Privilege{ {Name: "priv4"}, }, }, }, OperatorInterventionRules: []model.Rule{ { Privilege: &model.Privilege{Name: "priv1"}, Commands: []*model.Command{ {Name: "command1"}, {Name: "command2"}, }, }, { Privilege: &model.Privilege{Name: "priv2"}, Commands: []*model.Command{ {Name: "command3"}, }, }, { Privilege: &model.Privilege{Name: "priv3"}, Commands: []*model.Command{ {Name: "command4"}, }, }, { Privilege: &model.Privilege{Name: "priv4"}, Commands: []*model.Command{ {Name: "command5"}, {Name: "command6"}, }, }, }, } ) type SummaryResponse struct { OperatorInterventionRoleMappings []model.OiRoleMapping `graphql:"operatorInterventionRoleMappings" json:"operatorInterventionRoleMappings"` OperatorInterventionRules []model.Rule `graphql:"operatorInterventionRules" json:"operatorInterventionRules"` } func Summary(body []byte) (interface{}, bool, error) { if !strings.Contains(string(body), "operatorInterventionRoleMappings(roles: $roles){role,privileges{name}},operatorInterventionRules{privilege{name},commands{name}}") { return nil, false, nil } var in struct { Query string Variables struct { Roles []string } } err := json.Unmarshal(body, &in) if err != nil { return nil, true, err } var newMappings []model.OiRoleMapping for _, mapping := range defaultSummaryResponse.OperatorInterventionRoleMappings { if slices.Contains(in.Variables.Roles, mapping.Role.String()) { newMappings = append(newMappings, mapping) } } return &SummaryResponse{ OperatorInterventionRoleMappings: newMappings, OperatorInterventionRules: defaultSummaryResponse.OperatorInterventionRules, }, true, nil }