import { Injectable , Injector } from '@angular/core' ; import { ByosStorageAccounts } from 'app/site/byos/byos' ; import { Deployment , SourceControlData } from 'app/site/deployment-center/Models/deployment-data' ; import { Observable } from 'rxjs/Observable' ; import { HostingEnvironment } from '../models/arm/hosting-environment' ; import { PublishingUser } from '../models/arm/publishing-users' ; import { ARMApiVersions } from '../models/constants' ; import { HostStatus } from '../models/host-status' ; import { PublishingCredentials } from '../models/publishing-credentials' ; import { AppSettingsHelper } from '../Utilities/application-settings-helper' ; import { ConditionalHttpClient } from './../../shared/conditional-http-client' ; import { ArmSiteDescriptor } from './../../shared/resourceDescriptors' ; import { Availability } from './../../site/site-notifications/notifications' ; import { SlotsDiff } from './../models/arm//slots-diff' ; import { ApplicationSettings } from './../models/arm/application-settings' ; import { ArmArrayResult , ArmObj } from './../models/arm/arm-obj' ; import { AuthSettings } from './../models/arm/auth-settings' ; import { ConnectionStrings } from './../models/arm/connection-strings' ; import { CreateSlotRequest , PublishingCredentialPolicies , Site } from './../models/arm/site' ; import { SiteConfig } from './../models/arm/site-config' ; import { SiteExtension } from './../models/arm/site-extension' ; import { SlotConfigNames } from './../models/arm/slot-config-names' ; import { AvailableStack , OsType } from './../models/arm/stacks' ; import { HttpResult } from './../models/http-result' ; import { ArmService } from './arm.service' ; import { CacheService } from './cache.service' ; import { UserService } from './user.service' ; type Result < T > = Observable < HttpResult < T > > ; @ Injectable ( ) export class SiteService { private readonly _client : ConditionalHttpClient ; private static readonly _configSettingsToIgnore = [ 'ipSecurityRestrictions' , 'scmIpSecurityRestrictions' ] ; constructor ( userService , injector , private _cacheService ) { this . _client = new ConditionalHttpClient ( injector , _ => userService . getStartupInfo ( ) . map ( i => i . token ) ) ; } private static _removePropertiesFromSiteConfig ( siteConfig , settingsToIgnore ) { if ( ! ! siteConfig && ! ! settingsToIgnore ) { settingsToIgnore . forEach ( settingToIgnore => { delete siteConfig [ settingToIgnore ] ; } ) ; } } getSite ( resourceId , force ? ) { const getSite = this . _cacheService . getArm ( resourceId , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSite ) ; } getSlots ( resourceId , force ? ) { const siteDescriptor = new ArmSiteDescriptor ( resourceId ) ; const slotsId = `${ siteDescriptor . getSiteOnlyResourceId ( ) }/slots` ; const getSlots = this . _cacheService . getArm ( slotsId , force , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSlots ) ; } getSiteConfig ( resourceId , force ? ) { const getSiteConfig = this . _cacheService . getArm ( `${ resourceId }/config/web` , force , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => { const siteConfig = r . json ( ) ; if ( siteConfig && siteConfig . properties && siteConfig . properties . azureStorageAccounts ) { delete siteConfig . properties . azureStorageAccounts ; } return siteConfig ; } ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSiteConfig ) ; } getAppSettings ( resourceId , force ? ) { const getAppSettings = this . _cacheService . postArm ( `${ resourceId }/config/appSettings/list` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getAppSettings ) ; } getConnectionStrings ( resourceId , force ? ) { const getConnectionStrings = this . _cacheService . postArm ( `${ resourceId }/config/connectionstrings/list` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getConnectionStrings ) ; } getSlotConfigNames ( resourceId , force ? ) { const slotsConfigNamesId = `${ new ArmSiteDescriptor ( resourceId ) . getSiteOnlyResourceId ( ) }/config/slotConfigNames` ; const getSlotConfigNames = this . _cacheService . getArm ( slotsConfigNamesId , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSlotConfigNames ) ; } getAvailableStacks ( osType ? ) { const queryString = ! osType ? '' : `?osTypeSelected=${ osType }` ; const getAvailableStacks = this . _cacheService . getArm ( `/providers/Microsoft.Web/availablestacks${ queryString }` ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : null } , t => getAvailableStacks ) ; } getAvailability ( resourceId ) { const subscriptionId = new ArmSiteDescriptor ( resourceId ) . subscription ; const availabilityId = `${ resourceId }/providers/Microsoft.ResourceHealth/availabilityStatuses/current` ; const getAvailability = this . _cacheService . getArm ( availabilityId , false , ArmService . availabilityApiVersion ) . map ( r => r . json ( ) ) . catch ( ( e ) => { if ( e . status === 409 ) { return this . _cacheService . postArm ( `/subscriptions/${ subscriptionId }/providers/Microsoft.ResourceHealth/register` ) . mergeMap ( ( ) => { return this . _cacheService . getArm ( availabilityId , false , ArmService . availabilityApiVersion ) ; } ) . map ( r => r . json ( ) ) ; } return Observable . throw ( e ) ; } ) ; return this . _client . execute ( { resourceId : resourceId } , t => getAvailability ) ; } getAuthSettings ( resourceId , force ? ) { const getAuthSettings = this . _cacheService . postArm ( `${ resourceId }/config/authsettings/list` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getAuthSettings ) ; } getSiteExtensions ( resourceId ) { const getSiteExtensions = this . _cacheService . getArm ( `${ resourceId }/siteExtensions` , false ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSiteExtensions ) ; } getPublishingProfile ( resourceId ) { const getPublishingProfile = this . _cacheService . postArm ( `${ resourceId }/publishxml` , true , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => r . text ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getPublishingProfile ) ; } addOrUpdateAppSettings ( resourceId , newOrUpdatedSettings ) { const addOrUpdateAppSettings = this . _cacheService . postArm ( `${ resourceId }/config/appSettings/list` , true ) . mergeMap ( r => { const appSettingsArm = r . json ( ) as ArmObj < ApplicationSettings > ; if ( appSettingsArm && newOrUpdatedSettings ) { appSettingsArm . properties = AppSettingsHelper . mergeAppSettings ( appSettingsArm . properties , newOrUpdatedSettings ) ; return this . _cacheService . putArm ( appSettingsArm . id , null , appSettingsArm ) ; } return Observable . of ( r ) ; } ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => addOrUpdateAppSettings ) ; } createSlot ( resourceId , slotName , slotData ) { if ( ! ! slotData . properties . siteConfig ) { [ 'experiments' , 'routingRules' ] . forEach ( propertyName => { if ( slotData . properties . siteConfig . hasOwnProperty ( propertyName ) ) { slotData . properties . siteConfig [ propertyName ] = null ; } } ) ; } const payload = JSON . stringify ( slotData ) ; const newSlotId = `${ resourceId }/slots/${ slotName }` ; const createSlot = this . _cacheService . putArm ( newSlotId , ARMApiVersions . antaresApiVersion20181101 , payload ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => createSlot ) ; } getSlotDiffs ( resourceId , slotName ) { const payload = { targetSlot : slotName } ; const slotDiffsId = `${ resourceId }/slotsdiffs` ; const getSlotDiffs = this . _cacheService . postArm ( slotDiffsId , true , null , payload ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSlotDiffs ) ; } getPublishingCredentials ( resourceId , force ? ) { const getPublishingCredentials = this . _cacheService . postArm ( `${ resourceId }/config/publishingcredentials/list` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getPublishingCredentials ) ; } getSiteConfigSettingsToIgnore ( ) { return SiteService . _configSettingsToIgnore ; } updateSiteConfig ( resourceId , siteConfig , settingsToIgnore = SiteService . _configSettingsToIgnore ) { const payload = { ... siteConfig } ; SiteService . _removePropertiesFromSiteConfig ( siteConfig . properties , settingsToIgnore ) ; const putSiteConfig = this . _cacheService . putArm ( `${ resourceId }/config/web` , ARMApiVersions . antaresApiVersion20181101 , payload ) ; return this . _client . execute ( { resourceId : resourceId } , t => putSiteConfig ) ; } updateAppSettings ( resourceId , appSettings ) { const putAppSettings = this . _cacheService . putArm ( `${ resourceId }/config/appSettings` , null , appSettings ) ; return this . _client . execute ( { resourceId : resourceId } , t => putAppSettings ) ; } getAzureStorageAccounts ( resourceId , force ? ) { const getSiteConfig = this . _cacheService . postArm ( `${ resourceId }/config/azureStorageAccounts/list` , force , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSiteConfig ) ; } getHostingEnvironment ( resourceId , force ? ) { const getHostingEnvironment = this . _cacheService . getArm ( resourceId , force , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getHostingEnvironment ) ; } resetPublishProfile ( resourceId ) { const resetPublishProfile = this . _cacheService . postArm ( `${ resourceId }/newpassword` , true ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => resetPublishProfile ) ; } getPublishingUser ( ) { const getPublishingUser = this . _cacheService . getArm ( `/providers/Microsoft.Web/publishingUsers/web` , true ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : '' } , t => getPublishingUser ) ; } updatePublishingUser ( properties ) { const updatePublishingUser = this . _cacheService . putArm ( `/providers/Microsoft.Web/publishingUsers/web` , null , { properties } ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : '' } , t => updatePublishingUser ) ; } getSiteSourceControlConfig ( resourceId , force ? ) { const getSiteSourceControlConfig = this . _cacheService . getArm ( `${ resourceId }/sourcecontrols/web` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSiteSourceControlConfig ) ; } deleteSiteSourceControlConfig ( resourceId ) { const deleteSiteSourceControlConfig = this . _cacheService . deleteArm ( `${ resourceId }/sourcecontrols/web` ) ; return this . _client . execute ( { resourceId : resourceId } , t => deleteSiteSourceControlConfig ) ; } getSiteDeployments ( resourceId ) { const getSiteDeployments = this . _cacheService . getArm ( `${ resourceId }/deployments` , true ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getSiteDeployments ) ; } clearSiteConfigArmCache ( resourceId ) { this . _cacheService . clearArmIdCachePrefix ( `${ resourceId }/config/web` ) ; } fetchSiteConfigMetadata ( resourceId , force ? ) { const fetchSiteConfigMetadata = this . _cacheService . postArm ( `${ resourceId }/config/metadata/list` , force , ARMApiVersions . antaresApiVersion20181101 ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => fetchSiteConfigMetadata ) ; } getBasicPublishingCredentialsPolicies = ( resourceId ) : Result < ArmObj < PublishingCredentialPolicies > > => { const getBasicPublishingCredentialsPolicies = this . _cacheService . getArm ( `${ resourceId }/basicPublishingCredentialsPolicies` , true ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getBasicPublishingCredentialsPolicies ) ; } ; getHostStatus ( resourceId , force ? ) { const getHostStatus = this . _cacheService . getArm ( `${ resourceId }/host/default/properties/status` , force ) . map ( r => r . json ( ) ) ; return this . _client . execute ( { resourceId : resourceId } , t => getHostStatus ) ; } }