Skip to content

Commit

Permalink
adding csp oauth changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuldeep22 committed Nov 10, 2023
1 parent bf6aa26 commit fdbbe4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/partials/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ <h5>Wavefront Settings</h5>
</div>
</div>

<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-10">CSP OAuth Token</span>
</div>
<div class="gf-form max-width-24">
<i class="gf-form-label max-width-22 text-center" ng-show="ctrl.cspOAuthExists">Configured</i>
<a class="btn btn-secondary gf-form-btn" style="flex-grow:1" type="submit" ng-click="ctrl.resetCspOAuth()" ng-show="ctrl.cspOAuthExists">Reset</a>
<input type="text" class="gf-form-input max-width-24" ng-hide="ctrl.cspOAuthExists" ng-model="ctrl.current.jsonData.cspOAuthClientId"/>
<input type="text" class="gf-form-input max-width-24" ng-hide="ctrl.cspOAuthExists" ng-model="ctrl.current.jsonData.cspOAuthClientSecret"/>
<info-popover mode="right-absolute" ng-hide="ctrl.cspOAuthExists">
Paste your CSP OAuth token here. You can find and manage your tokens on your profile page in the Wavefront app
</info-popover>
</div>
</div>
qqq
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label width-10">CSP API Token</span>
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/configCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export class WavefrontConfigCtrl {
public current: any;
public wavefrontTokenExists = false;
public cspApiTokenExists = false;
public cspOAuthExists = false;

constructor() {
this.wavefrontTokenExists = (this.current.jsonData.wavefrontToken != null && this.current.jsonData.wavefrontToken !== "");
this.cspApiTokenExists = (this.current.jsonData.cspAPIToken != null && this.current.jsonData.cspAPIToken !== "");
this.cspOAuthExists = (this.current.jsonData.cspOAuthClientId != null && this.current.jsonData.cspOAuthClientSecret !== "");
}

public resetWavefrontToken() {
Expand All @@ -19,4 +21,10 @@ export class WavefrontConfigCtrl {
this.current.jsonData.cspAPIToken = "";
this.cspApiTokenExists = false;
}

public resetCspOAuth() {
this.current.jsonData.cspOAuthClientId = "";
this.current.jsonData.cspOAuthClientSecret = "";
this.cspOAuthExists = false;
}
}
25 changes: 24 additions & 1 deletion src/plugin/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const queryKeyLookbackMillis = 7 * 24 * 60 * 60 * 1000;
const CSP_API_TOKEN_URL = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize";
const CSP_OAUTH_TOKEN_URL = "https://console.cloud.vmware.com/csp/gateway/am/api/auth/authorize";

const appSecret = ""



export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSrv) {
this.url = sanitizeUrl(instanceSettings.url);

Expand All @@ -17,6 +21,9 @@ export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSr
this.backendSrv = new BackendSrvCancelledRetriesDecorator(backendSrv, $q);
this.templateSrv = templateSrv;
this.defaultRequestTimeoutSecs = 15;
const appId = instanceSettings.jsonData.cspOAuthClientId
const appSecret = instanceSettings.jsonData.cspOAuthClientSecret
const credentials = `Basic ${Buffer.from(`${appId}:${appSecret}`).toString('base64')}`;

this.requestConfigProto = {
headers: {
Expand All @@ -43,7 +50,23 @@ export function WavefrontDatasource(instanceSettings, $q, backendSrv, templateSr
} catch(e) {
console.error(e);
}
} else {
} else if (instanceSettings.jsonData.cspOAuthClientId && instanceSettings.jsonData.cspOAuthClientSecret) {
try {
fetch(CSP_OAUTH_TOKEN_URL, {
method: "POST",
body: JSON.stringify({
grant_type: "client_credentials",
}),
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
}
})
.then((response) => response.json())
.then((json) => console.log(json));
} catch(e) {
console.error(e);
}
}else {
this.requestConfigProto.withCredentials = true;
}

Expand Down

0 comments on commit fdbbe4d

Please sign in to comment.