1package models
2
3import (
4	"errors"
5	"time"
6
7	"github.com/grafana/grafana/pkg/components/simplejson"
8)
9
10const (
11	DS_GRAPHITE       = "graphite"
12	DS_INFLUXDB       = "influxdb"
13	DS_INFLUXDB_08    = "influxdb_08"
14	DS_ES             = "elasticsearch"
15	DS_PROMETHEUS     = "prometheus"
16	DS_MYSQL          = "mysql"
17	DS_ACCESS_DIRECT  = "direct"
18	DS_ACCESS_PROXY   = "proxy"
19	DS_ES_OPEN_DISTRO = "grafana-es-open-distro-datasource"
20	DS_ES_OPENSEARCH  = "grafana-opensearch-datasource"
21)
22
23var (
24	ErrDataSourceNotFound                = errors.New("data source not found")
25	ErrDataSourceNameExists              = errors.New("data source with the same name already exists")
26	ErrDataSourceUidExists               = errors.New("data source with the same uid already exists")
27	ErrDataSourceUpdatingOldVersion      = errors.New("trying to update old version of datasource")
28	ErrDatasourceIsReadOnly              = errors.New("data source is readonly, can only be updated from configuration")
29	ErrDataSourceAccessDenied            = errors.New("data source access denied")
30	ErrDataSourceFailedGenerateUniqueUid = errors.New("failed to generate unique datasource ID")
31	ErrDataSourceIdentifierNotSet        = errors.New("unique identifier and org id are needed to be able to get or delete a datasource")
32)
33
34type DsAccess string
35
36type DataSource struct {
37	Id      int64 `json:"id"`
38	OrgId   int64 `json:"orgId"`
39	Version int   `json:"version"`
40
41	Name              string            `json:"name"`
42	Type              string            `json:"type"`
43	Access            DsAccess          `json:"access"`
44	Url               string            `json:"url"`
45	Password          string            `json:"password"`
46	User              string            `json:"user"`
47	Database          string            `json:"database"`
48	BasicAuth         bool              `json:"basicAuth"`
49	BasicAuthUser     string            `json:"basicAuthUser"`
50	BasicAuthPassword string            `json:"basicAuthPassword"`
51	WithCredentials   bool              `json:"withCredentials"`
52	IsDefault         bool              `json:"isDefault"`
53	JsonData          *simplejson.Json  `json:"jsonData"`
54	SecureJsonData    map[string][]byte `json:"secureJsonData"`
55	ReadOnly          bool              `json:"readOnly"`
56	Uid               string            `json:"uid"`
57
58	Created time.Time `json:"created"`
59	Updated time.Time `json:"updated"`
60}
61
62// ----------------------
63// COMMANDS
64
65// Also acts as api DTO
66type AddDataSourceCommand struct {
67	Name              string            `json:"name" binding:"Required"`
68	Type              string            `json:"type" binding:"Required"`
69	Access            DsAccess          `json:"access" binding:"Required"`
70	Url               string            `json:"url"`
71	Password          string            `json:"password"`
72	Database          string            `json:"database"`
73	User              string            `json:"user"`
74	BasicAuth         bool              `json:"basicAuth"`
75	BasicAuthUser     string            `json:"basicAuthUser"`
76	BasicAuthPassword string            `json:"basicAuthPassword"`
77	WithCredentials   bool              `json:"withCredentials"`
78	IsDefault         bool              `json:"isDefault"`
79	JsonData          *simplejson.Json  `json:"jsonData"`
80	SecureJsonData    map[string]string `json:"secureJsonData"`
81	Uid               string            `json:"uid"`
82
83	OrgId                   int64             `json:"-"`
84	ReadOnly                bool              `json:"-"`
85	EncryptedSecureJsonData map[string][]byte `json:"-"`
86
87	Result *DataSource
88}
89
90// Also acts as api DTO
91type UpdateDataSourceCommand struct {
92	Name              string            `json:"name" binding:"Required"`
93	Type              string            `json:"type" binding:"Required"`
94	Access            DsAccess          `json:"access" binding:"Required"`
95	Url               string            `json:"url"`
96	Password          string            `json:"password"`
97	User              string            `json:"user"`
98	Database          string            `json:"database"`
99	BasicAuth         bool              `json:"basicAuth"`
100	BasicAuthUser     string            `json:"basicAuthUser"`
101	BasicAuthPassword string            `json:"basicAuthPassword"`
102	WithCredentials   bool              `json:"withCredentials"`
103	IsDefault         bool              `json:"isDefault"`
104	JsonData          *simplejson.Json  `json:"jsonData"`
105	SecureJsonData    map[string]string `json:"secureJsonData"`
106	Version           int               `json:"version"`
107	Uid               string            `json:"uid"`
108
109	OrgId                   int64             `json:"-"`
110	Id                      int64             `json:"-"`
111	ReadOnly                bool              `json:"-"`
112	EncryptedSecureJsonData map[string][]byte `json:"-"`
113
114	Result *DataSource
115}
116
117// DeleteDataSourceCommand will delete a DataSource based on OrgID as well as the UID (preferred), ID, or Name.
118// At least one of the UID, ID, or Name properties must be set in addition to OrgID.
119type DeleteDataSourceCommand struct {
120	ID   int64
121	UID  string
122	Name string
123
124	OrgID int64
125
126	DeletedDatasourcesCount int64
127}
128
129// ---------------------
130// QUERIES
131
132type GetDataSourcesQuery struct {
133	OrgId           int64
134	DataSourceLimit int
135	User            *SignedInUser
136	Result          []*DataSource
137}
138
139type GetDataSourcesByTypeQuery struct {
140	Type   string
141	Result []*DataSource
142}
143
144type GetDefaultDataSourceQuery struct {
145	OrgId  int64
146	User   *SignedInUser
147	Result *DataSource
148}
149
150// GetDataSourceQuery will get a DataSource based on OrgID as well as the UID (preferred), ID, or Name.
151// At least one of the UID, ID, or Name properties must be set in addition to OrgID.
152type GetDataSourceQuery struct {
153	Id   int64
154	Uid  string
155	Name string
156
157	OrgId int64
158
159	Result *DataSource
160}
161
162// ---------------------
163//  Permissions
164// ---------------------
165
166type DsPermissionType int
167
168const (
169	DsPermissionNoAccess DsPermissionType = iota
170	DsPermissionQuery
171	DsPermissionRead
172)
173
174func (p DsPermissionType) String() string {
175	names := map[int]string{
176		int(DsPermissionQuery):    "Query",
177		int(DsPermissionNoAccess): "No Access",
178	}
179	return names[int(p)]
180}
181
182type DatasourcesPermissionFilterQuery struct {
183	User        *SignedInUser
184	Datasources []*DataSource
185	Result      []*DataSource
186}
187