Skip to content

Commit

Permalink
CRM headers only on create and delete (#2226)
Browse files Browse the repository at this point in the history
* rp update

* add headers only on create and delete calls

* pr updates and add headers for get entities

* update delete calls to include headers
  • Loading branch information
Allison Gruninger authored Jan 22, 2018
1 parent 38d282a commit 8fab5a3
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 97 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArmService } from 'app/shared/services/arm.service';
import { CdsFunctionDescriptor } from 'app/shared/resourceDescriptors';
import { errorIds } from 'app/shared/models/error-ids';
import { ErrorEvent } from 'app/shared/models/error-event';
Expand All @@ -17,6 +18,7 @@ import { TreeViewInfo } from './../../../tree-view/models/tree-view-info';
import { BroadcastService } from 'app/shared/services/broadcast.service';
import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { AfterContentInit } from '@angular/core/src/metadata/lifecycle_hooks';
import { UserService } from 'app/shared/services/user.service';

@Component({
selector: 'embedded-function-editor',
Expand All @@ -42,7 +44,9 @@ export class EmbeddedFunctionEditorComponent implements OnInit, AfterContentInit
constructor(
private _broadcastService: BroadcastService,
private _cacheService: CacheService,
private _translateService: TranslateService) {
private _translateService: TranslateService,
private _armService: ArmService,
private _userService: UserService) {

this._busyManager = new BusyStateScopeManager(this._broadcastService, 'dashboard');

Expand Down Expand Up @@ -163,21 +167,31 @@ export class EmbeddedFunctionEditorComponent implements OnInit, AfterContentInit
const result = confirm(this._translateService.instant(PortalResources.functionManage_areYouSure, { name: this._functionInfo.name }));
if (result) {
this._busyManager.setBusy();
this._cacheService.deleteArm(this.resourceId)
.subscribe(r => {
this._busyManager.clearBusy();
this._broadcastService.broadcastEvent<TreeUpdateEvent>(BroadcastEvent.TreeUpdate, {
resourceId: this.resourceId,
operation: 'remove'
const headers = this._armService.getHeaders();
this._userService.getStartupInfo()
.first()
.switchMap(info => {
headers.append('x-cds-crm-user-token', info.crmInfo.crmTokenHeaderName);
headers.append('x-cds-crm-org', info.crmInfo.crmInstanceHeaderName);
headers.append('x-cds-crm-solutionid', info.crmInfo.crmSolutionIdHeaderName);

const url = this._armService.getArmUrl(this.resourceId, this._armService.websiteApiVersion);
return this._cacheService.delete(url, headers);
})
.subscribe(r => {
this._busyManager.clearBusy();
this._broadcastService.broadcastEvent<TreeUpdateEvent>(BroadcastEvent.TreeUpdate, {
resourceId: this.resourceId,
operation: 'remove'
});
}, err => {
this._busyManager.clearBusy();
this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
message: this._translateService.instant(PortalResources.error_unableToDeleteFunction).format(this._functionInfo.name),
errorId: errorIds.embeddedEditorDeleteError,
resourceId: this.resourceId,
});
});
}, err => {
this._busyManager.clearBusy();
this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
message: this._translateService.instant(PortalResources.error_unableToDeleteFunction).format(this._functionInfo.name),
errorId: errorIds.embeddedEditorDeleteError,
resourceId: this.resourceId,
});
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArmService } from './../../shared/services/arm.service';
import { ArmEmbeddedService } from './../../shared/services/arm-embedded.service';
import { KeyCodes, LogCategories } from './../../shared/models/constants';
import { TreeViewInfo } from 'app/tree-view/models/tree-view-info';
Expand All @@ -24,6 +25,7 @@ import { UIFunctionBinding } from '../../shared/models/binding';
import { PortalService } from '../../shared/services/portal.service';
import { Observable } from 'rxjs/Observable';
import { CreateCard } from 'app/function/function-new/function-new.component';
import { UserService } from 'app/shared/services/user.service';

@Component({
selector: 'function-new-detail',
Expand Down Expand Up @@ -78,7 +80,9 @@ export class FunctionNewDetailComponent implements OnChanges {
private _aiService: AiService,
private _cacheService: CacheService,
private _functionAppService: FunctionAppService,
private _logService: LogService) {
private _logService: LogService,
private _armService: ArmService,
private _userService: UserService) {

this.isEmbedded = this._portalService.isEmbeddedFunctions;
}
Expand All @@ -89,7 +93,7 @@ export class FunctionNewDetailComponent implements OnChanges {
this.updateLanguageOptions();

if (this._portalService.isEmbeddedFunctions) {
this.getEntityOptions();
this._getEntities();
}
}
}
Expand Down Expand Up @@ -121,21 +125,6 @@ export class FunctionNewDetailComponent implements OnChanges {
}
}

getEntityOptions() {
this._getEntities()
.subscribe(r => {
const entities = (r.value.map(e => e.name)).sort();
this.entityOptions = [];
entities.forEach(entity => {
const dropDownElement: any = {
displayLabel: entity,
value: entity
};
this.entityOptions.push(dropDownElement);
});
});
}

onEntityChanged(entity: string) {
this.areInputsValid = false;
this.functionEntity = entity.toLowerCase();
Expand Down Expand Up @@ -313,8 +302,27 @@ export class FunctionNewDetailComponent implements OnChanges {

private _getEntities() {
const url = this._cdsEntitiesUrl;
return this._cacheService.get(url)
.map(r => r.json());
const headers = this._armService.getHeaders();
this._userService.getStartupInfo()
.first()
.switchMap(info => {
headers.append('x-cds-crm-user-token', info.crmInfo.crmTokenHeaderName);
headers.append('x-cds-crm-org', info.crmInfo.crmInstanceHeaderName);
headers.append('x-cds-crm-solutionid', info.crmInfo.crmSolutionIdHeaderName);

return this._cacheService.get(url, null, headers).map(r => r.json());
})
.subscribe(r => {
const entities = (r.value.map(e => e.name)).sort();
this.entityOptions = [];
entities.forEach(entity => {
const dropDownElement: any = {
displayLabel: entity,
value: entity
};
this.entityOptions.push(dropDownElement);
});
});
}

private _createFunction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArmService } from './../shared/services/arm.service';
import { ArmSiteDescriptor } from './../shared/resourceDescriptors';
import { FunctionAppContext } from './../shared/function-app-context';
import { TreeUpdateEvent, BroadcastEvent } from './../shared/models/broadcast-event';
Expand All @@ -19,6 +20,7 @@ import { Observable } from 'rxjs/Observable';
import { FunctionAppService } from 'app/shared/services/function-app.service';
import { Subscription } from 'rxjs/Subscription';
import { NavigableComponent } from '../shared/components/navigable-component';
import { UserService } from 'app/shared/services/user.service';

@Component({
selector: 'functions-list',
Expand Down Expand Up @@ -47,10 +49,12 @@ export class FunctionsListComponent extends NavigableComponent implements OnDest
private _translateService: TranslateService,
broadcastService: BroadcastService,
private _functionAppService: FunctionAppService,
private _cacheService: CacheService) {
private _cacheService: CacheService,
private _armService: ArmService,
private _userService: UserService) {
super('functions-list', broadcastService, DashboardType.FunctionsDashboard);

this.isEmbedded = this._portalService.isEmbeddedFunctions;
this.isEmbedded = this._portalService.isEmbeddedFunctions;
}

setupNavigation(): Subscription {
Expand All @@ -73,9 +77,9 @@ export class FunctionsListComponent extends NavigableComponent implements OnDest
this.runtimeVersion = tuple[1];
this.isLoading = false;
this.functions = (<FunctionNode[]>this._functionsNode.children);
this.functionsInfo = this._functionsNode.children.map((child: FunctionNode) =>{
this.functionsInfo = this._functionsNode.children.map((child: FunctionNode) => {
return child.functionInfo;
})
});
});
}

Expand All @@ -85,36 +89,36 @@ export class FunctionsListComponent extends NavigableComponent implements OnDest

templates.result.forEach((template) => {

const templateIndex = this.createCards.findIndex(finalTemplate => {
return finalTemplate.name === template.metadata.name;
});
const templateIndex = this.createCards.findIndex(finalTemplate => {
return finalTemplate.name === template.metadata.name;
});

// if the card doesn't exist, create it based off the template, else add information to the preexisting card
if (templateIndex === -1) {
this.createCards.push({
name: `${template.metadata.name}`,
value: template.id,
description: template.metadata.description,
enabledInTryMode: template.metadata.enabledInTryMode,
AADPermissions: template.metadata.AADPermissions,
languages: [`${template.metadata.language}`],
categories: template.metadata.category,
ids: [`${template.id}`],
icon: 'image/other.svg',
color: '#000000',
barcolor: '#D9D9D9',
focusable: false
});
} else {
this.createCards[templateIndex].languages.push(`${template.metadata.language}`);
this.createCards[templateIndex].categories = this.createCards[templateIndex].categories.concat(template.metadata.category);
this.createCards[templateIndex].ids.push(`${template.id}`);
}
// if the card doesn't exist, create it based off the template, else add information to the preexisting card
if (templateIndex === -1) {
this.createCards.push({
name: `${template.metadata.name}`,
value: template.id,
description: template.metadata.description,
enabledInTryMode: template.metadata.enabledInTryMode,
AADPermissions: template.metadata.AADPermissions,
languages: [`${template.metadata.language}`],
categories: template.metadata.category,
ids: [`${template.id}`],
icon: 'image/other.svg',
color: '#000000',
barcolor: '#D9D9D9',
focusable: false
});
} else {
this.createCards[templateIndex].languages.push(`${template.metadata.language}`);
this.createCards[templateIndex].categories = this.createCards[templateIndex].categories.concat(template.metadata.category);
this.createCards[templateIndex].ids.push(`${template.id}`);
}
});

// unique categories
this.createCards.forEach((template, index) => {
const categoriesDict: {[key: string]: string; } = {};
const categoriesDict: { [key: string]: string; } = {};
template.categories.forEach(category => {
categoriesDict[category] = category;
});
Expand All @@ -127,7 +131,7 @@ export class FunctionsListComponent extends NavigableComponent implements OnDest
});

this.createFunctionCard = this.createCards[0];
});
});
}

clickRow(item: FunctionNode) {
Expand Down Expand Up @@ -201,18 +205,30 @@ export class FunctionsListComponent extends NavigableComponent implements OnDest
embeddedDelete(item: FunctionNode) {
const result = confirm(this._translateService.instant(PortalResources.functionManage_areYouSure, { name: item.functionInfo.name }));
if (result) {
this._globalStateService.setBusyState();
this._cacheService.deleteArm(item.resourceId)
.subscribe(r => {
this._globalStateService.clearBusyState();
this._broadcastService.broadcastEvent<TreeUpdateEvent>(BroadcastEvent.TreeUpdate, {
resourceId: item.resourceId,
operation: 'remove'
});
}, err => {
this._globalStateService.clearBusyState();
// TODO: ellhamai - handle error
});
this._globalStateService.setBusyState();

const headers = this._armService.getHeaders();
this._userService.getStartupInfo()
.first()
.switchMap(info => {
headers.append('x-cds-crm-user-token', info.crmInfo.crmTokenHeaderName);
headers.append('x-cds-crm-org', info.crmInfo.crmInstanceHeaderName);
headers.append('x-cds-crm-solutionid', info.crmInfo.crmSolutionIdHeaderName);

const url = this._armService.getArmUrl(item.resourceId, this._armService.websiteApiVersion);
return this._cacheService.delete(url, headers);
})
.subscribe(r => {
this._globalStateService.clearBusyState();
this._broadcastService.broadcastEvent<TreeUpdateEvent>(BroadcastEvent.TreeUpdate, {
resourceId: item.resourceId,
operation: 'remove'
});

}, err => {
this._globalStateService.clearBusyState();
// TODO: ellhamai - handle error
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { UserService } from './user.service';
import { Http, Headers, Response } from '@angular/http';
import { Injectable } from '@angular/core';
import { ArmService } from './arm.service';
import { CrmInfo } from 'app/shared/models/portal';

@Injectable()
export class ArmEmbeddedService extends ArmService {
Expand All @@ -29,27 +28,12 @@ export class ArmEmbeddedService extends ArmService {
'/api/'
];

private _crmHeaders: CrmInfo;

constructor(http: Http,
userService: UserService,
aiService: AiService,
portalService: PortalService) {

super(http, userService, portalService, aiService);

userService.getStartupInfo().subscribe(info => {
this._crmHeaders = info.crmInfo;
});

}

getHeaders(etag?: string) {
const headers = super.getHeaders();
headers.append('x-cds-crm-user-token', this._crmHeaders.crmTokenHeaderName);
headers.append('x-cds-crm-org', this._crmHeaders.crmInstanceHeaderName);
headers.append('x-cds-crm-solutionid', this._crmHeaders.crmSolutionIdHeaderName);
return headers;
}

send(method: string, url: string, body?: any, etag?: string, headers?: Headers): Observable<Response> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ArmService {

constructor(private _http: Http,
_userService: UserService,
private _portalService: PortalService,
_portalService: PortalService,
protected _aiService: AiService) {

this.armUrl = _portalService.isEmbeddedFunctions ? ArmEmbeddedService.url : ArmServiceHelper.armEndpoint;
Expand All @@ -48,7 +48,7 @@ export class ArmService {

send(method: string, url: string, body?: any, etag?: string, headers?: Headers, invokeApi?: boolean) {

headers = (headers && !this._portalService.isEmbeddedFunctions) ? headers : this.getHeaders(etag);
headers = headers ? headers : this.getHeaders(etag);

if (invokeApi) {
let pathAndQuery = url.slice(this.armUrl.length);
Expand Down
Loading

0 comments on commit 8fab5a3

Please sign in to comment.