1// Copyright 2017 Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package perf
16
17import (
18	"encoding/json"
19
20	istio_mixer_v1 "istio.io/api/mixer/v1"
21	attr "istio.io/istio/mixer/pkg/attribute"
22	"istio.io/pkg/attribute"
23)
24
25// Request interface is the common interface for all different types of requests.
26type Request interface {
27	// getRequestProto returns one API request proto.
28	getRequestProto() interface{}
29}
30
31// BasicReport is an implementation of Request that is used to explicitly specify a Report request.
32type BasicReport struct {
33	RequestProto istio_mixer_v1.ReportRequest `json:"requestProto,omitempty"`
34}
35
36var _ Request = &BasicReport{}
37
38// BasicCheck is an implementation of Request that is specified declaratively by the author.
39type BasicCheck struct {
40	RequestProto istio_mixer_v1.CheckRequest `json:"requestProto,omitempty"`
41}
42
43var _ Request = &BasicCheck{}
44
45// BuildBasicReport builds a BasicReport Request by creating a Report API request.
46func BuildBasicReport(attributes map[string]interface{}) BasicReport {
47	requestBag := attribute.GetMutableBag(nil)
48	for k, v := range attributes {
49		switch v := v.(type) {
50		case map[string]string:
51			requestBag.Set(k, attribute.WrapStringMap(v))
52		default:
53			requestBag.Set(k, v)
54		}
55	}
56
57	var attrProto istio_mixer_v1.CompressedAttributes
58	attr.ToProto(requestBag, &attrProto, nil, 0)
59
60	br := BasicReport{
61		RequestProto: istio_mixer_v1.ReportRequest{
62			Attributes: []istio_mixer_v1.CompressedAttributes{attrProto},
63		},
64	}
65	return br
66}
67
68func (r BasicReport) getRequestProto() interface{} {
69	return interface{}(&r.RequestProto)
70}
71
72// MarshalJSON marshals the report as JSON.
73func (r BasicReport) MarshalJSON() ([]byte, error) {
74	m := make(map[string]json.RawMessage, 2)
75
76	var err error
77	m["type"], _ = json.Marshal("basicReport")
78	m["requestProto"], err = json.Marshal(r.RequestProto)
79	if err != nil {
80		return nil, err
81	}
82	return json.Marshal(m)
83}
84
85// BuildBasicCheck builds a BasicReport Request by creating a Check API request.
86func BuildBasicCheck(attributes map[string]interface{}, quotas map[string]istio_mixer_v1.CheckRequest_QuotaParams) BasicCheck {
87	requestBag := attribute.GetMutableBag(nil)
88	for k, v := range attributes {
89		switch v := v.(type) {
90		case map[string]string:
91			requestBag.Set(k, attribute.WrapStringMap(v))
92		default:
93			requestBag.Set(k, v)
94		}
95	}
96
97	var attrProto istio_mixer_v1.CompressedAttributes
98	attr.ToProto(requestBag, &attrProto, nil, 0)
99
100	c := BasicCheck{
101		RequestProto: istio_mixer_v1.CheckRequest{
102			Attributes: attrProto,
103			Quotas:     quotas,
104		},
105	}
106	return c
107}
108
109func (c BasicCheck) getRequestProto() interface{} {
110	return interface{}(&c.RequestProto)
111}
112
113// MarshalJSON marshals the report as JSON.
114func (c BasicCheck) MarshalJSON() ([]byte, error) {
115	m := make(map[string]json.RawMessage, 3)
116
117	var err error
118	m["type"], _ = json.Marshal("basicCheck")
119	m["requestProto"], err = json.Marshal(c.RequestProto)
120	if err != nil {
121		return nil, err
122	}
123
124	return json.Marshal(m)
125}
126