Skip to content

Commit 234d828

Browse files
author
Mattia Moretti
authored
formatter to fix regex issue with Compartments (#261)
* trying with formatter * cleanup * seems to work * some clean * external formatter * further cleaning * better naming convention * Ready * versioning
1 parent 7108090 commit 234d828

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "oci-metrics-datasource",
33
"private": true,
4-
"version": "5.0.4",
4+
"version": "5.1.0",
55
"description": "Oracle Cloud Infrastructure Metrics Data Source for Grafana",
66
"scripts": {
77
"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",

src/datasource.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ import {
2929
} from "./types";
3030
import QueryModel from './query_model';
3131

32-
3332
export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSourceOptions> {
3433
private jsonData: any;
34+
ocidCompartmentStore: Record<string, string> ={}
3535

3636
constructor(instanceSettings: DataSourceInstanceSettings<OCIDataSourceOptions>) {
3737
super(instanceSettings);
@@ -51,6 +51,17 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
5151
return true;
5252
}
5353

54+
compartmentFormatter = (value: string): string => {
55+
// if (typeof value === 'string') {
56+
// return value;
57+
// }
58+
if (this.ocidCompartmentStore[value] || this.isVariable(value)) {
59+
return this.ocidCompartmentStore[value]
60+
} else {
61+
return value
62+
}
63+
};
64+
5465
/**
5566
* Override to apply template variables
5667
*
@@ -62,7 +73,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
6273

6374
query.region = templateSrv.replace(query.region, scopedVars);
6475
query.tenancy = templateSrv.replace(query.tenancy, scopedVars);
65-
query.compartment = templateSrv.replace(query.compartment, scopedVars);
76+
query.compartment = templateSrv.replace(query.compartment, scopedVars, this.compartmentFormatter);
6677
query.namespace = templateSrv.replace(query.namespace, scopedVars);
6778
query.resourcegroup = templateSrv.replace(query.resourcegroup, scopedVars);
6879
query.metric = templateSrv.replace(query.metric, scopedVars);
@@ -77,7 +88,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
7788
query.tenancy = templateSrv.replace(query.tenancy, scopedVars);
7889
}
7990
if (query.compartment) {
80-
query.compartment = templateSrv.replace(query.compartment, scopedVars);
91+
query.compartment = templateSrv.replace(query.compartment, scopedVars, this.compartmentFormatter);
8192
}
8293
if (query.resourcegroup) {
8394
query.resourcegroup = templateSrv.replace(query.resourcegroup, scopedVars);
@@ -107,7 +118,6 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
107118
}, {} as T);
108119
}
109120

110-
111121
// // **************************** Template variable helpers ****************************
112122

113123
// /**
@@ -119,7 +129,6 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
119129

120130
async metricFindQuery?(query: any, options?: any): Promise<MetricFindValue[]> {
121131
const templateSrv = getTemplateSrv();
122-
// const tmode = this.getJsonData().tenancymode;
123132

124133
const tenancyQuery = query.match(tenanciesQueryRegex);
125134
if (tenancyQuery) {
@@ -151,12 +160,14 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
151160
const tenancy = templateSrv.replace(compartmentQuery[1]);
152161
const compartments = await this.getCompartments(tenancy);
153162
return compartments.map(n => {
154-
return { text: n.name, value: n.ocid };
163+
this.ocidCompartmentStore[n.name]=n.ocid;
164+
return { text: n.name, value: n.name };
155165
});
156166
} else {
157167
const compartments = await this.getCompartments(DEFAULT_TENANCY);
158168
return compartments.map(n => {
159-
return { text: n.name, value: n.ocid };
169+
this.ocidCompartmentStore[n.name]=n.ocid;
170+
return { text: n.name, value: n.name };
160171
});
161172
}
162173
}
@@ -167,15 +178,15 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
167178
if (this.jsonData.tenancymode === "multitenancy") {
168179
const tenancy = templateSrv.replace(namespaceQuery[1]);
169180
const region = templateSrv.replace(namespaceQuery[2]);
170-
const compartment = templateSrv.replace(namespaceQuery[3]);
181+
const compartment = templateSrv.replace(namespaceQuery[3], undefined, this.compartmentFormatter);
171182
const namespaces = await this.getNamespacesWithMetricNames(tenancy, compartment, region);
172183
return namespaces.map(n => {
173184
return { text: n.namespace, value: n.namespace };
174185
});
175186
} else {
176187
const tenancy = DEFAULT_TENANCY;
177188
const region = templateSrv.replace(namespaceQuery[1]);
178-
const compartment = templateSrv.replace(namespaceQuery[2]);
189+
const compartment = templateSrv.replace(namespaceQuery[2], undefined, this.compartmentFormatter);
179190
const namespaces = await this.getNamespacesWithMetricNames(tenancy, compartment, region);
180191
return namespaces.map(n => {
181192
return { text: n.namespace, value: n.namespace };
@@ -188,7 +199,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
188199
if (this.jsonData.tenancymode === "multitenancy") {
189200
const tenancy = templateSrv.replace(resourcegroupQuery[1]);
190201
const region = templateSrv.replace(resourcegroupQuery[2]);
191-
const compartment = templateSrv.replace(resourcegroupQuery[3]);
202+
const compartment = templateSrv.replace(resourcegroupQuery[3], undefined, this.compartmentFormatter);
192203
const namespace = templateSrv.replace(resourcegroupQuery[4]);
193204
const resource_group = await this.getResourceGroupsWithMetricNames(tenancy, compartment, region, namespace);
194205
return resource_group.map(n => {
@@ -197,7 +208,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
197208
} else {
198209
const tenancy = DEFAULT_TENANCY;
199210
const region = templateSrv.replace(resourcegroupQuery[1]);
200-
const compartment = templateSrv.replace(resourcegroupQuery[2]);
211+
const compartment = templateSrv.replace(resourcegroupQuery[2], undefined, this.compartmentFormatter);
201212
const namespace = templateSrv.replace(resourcegroupQuery[3]);
202213
const resource_group = await this.getResourceGroupsWithMetricNames(tenancy, compartment, region, namespace);
203214
return resource_group.map(n => {
@@ -211,7 +222,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
211222
if (this.jsonData.tenancymode === "multitenancy") {
212223
const tenancy = templateSrv.replace(metricQuery[1]);
213224
const region = templateSrv.replace(metricQuery[2]);
214-
const compartment = templateSrv.replace(metricQuery[3]);
225+
const compartment = templateSrv.replace(metricQuery[3], undefined, this.compartmentFormatter);
215226
const namespace = templateSrv.replace(metricQuery[4]);
216227
// const resourcegroup = templateSrv.replace(metricQuery[4]);
217228
const metric_names = await this.getResourceGroupsWithMetricNames(tenancy, compartment, region, namespace);
@@ -223,7 +234,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
223234
} else {
224235
const tenancy = DEFAULT_TENANCY;
225236
const region = templateSrv.replace(metricQuery[1]);
226-
const compartment = templateSrv.replace(metricQuery[2]);
237+
const compartment = templateSrv.replace(metricQuery[2], undefined, this.compartmentFormatter);
227238
const namespace = templateSrv.replace(metricQuery[3]);
228239
// const resource_group = templateSrv.replace(metricQuery[4]);
229240
const metric_names = await this.getResourceGroupsWithMetricNames(tenancy, compartment, region, namespace);
@@ -240,7 +251,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
240251
if (this.jsonData.tenancymode === "multitenancy") {
241252
const tenancy = templateSrv.replace(dimensionsQuery[1]);
242253
const region = templateSrv.replace(dimensionsQuery[2]);
243-
const compartment = templateSrv.replace(dimensionsQuery[3]);
254+
const compartment = templateSrv.replace(dimensionsQuery[3], undefined, this.compartmentFormatter);
244255
const namespace = templateSrv.replace(dimensionsQuery[4]);
245256
const metric = templateSrv.replace(dimensionsQuery[5]);
246257
const dimension_values = await this.getDimensions(tenancy, compartment, region, namespace, metric);
@@ -252,7 +263,7 @@ export class OCIDataSource extends DataSourceWithBackend<OCIQuery, OCIDataSource
252263
} else {
253264
const tenancy = DEFAULT_TENANCY;
254265
const region = templateSrv.replace(dimensionsQuery[1]);
255-
const compartment = templateSrv.replace(dimensionsQuery[2]);
266+
const compartment = templateSrv.replace(dimensionsQuery[2], undefined, this.compartmentFormatter);
256267
const namespace = templateSrv.replace(dimensionsQuery[3]);
257268
const metric = templateSrv.replace(dimensionsQuery[4]);
258269
const dimension_values = await this.getDimensions(tenancy, compartment, region, namespace, metric);

src/plugin.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
{ "name": "UPL", "url": "https://oss.oracle.com/licenses/upl" }
2525
],
26-
"version": "5.0.4",
27-
"updated": "2024-02-20",
26+
"version": "5.1.0",
27+
"updated": "2024-03-07",
2828
"screenshots":[
2929
{
3030
"name":"OCI Metrics Dashboard",

0 commit comments

Comments
 (0)