33
44package com .microsoft .azure .synapse .ml .fabric
55
6- import spray .json .DefaultJsonProtocol .StringJsonFormat
7- import spray .json .JsValue
6+ import spray .json .DefaultJsonProtocol .{ StringJsonFormat , mapFormat }
7+ import spray .json ._
88
99import java .net .{MalformedURLException , URL }
1010import java .util .UUID
1111import scala .io .Source
1212
1313object FabricClient extends RESTUtils {
14- private val PbiGlobalServiceEndpoints = Map (
15- " public" -> " https://api.powerbi.com/" ,
16- " fairfax" -> " https://api.powerbigov.us" ,
17- " mooncake" -> " https://api.powerbi.cn" ,
18- " blackforest" -> " https://app.powerbi.de" ,
19- " msit" -> " https://api.powerbi.com/" ,
20- " prod" -> " https://api.powerbi.com/" ,
21- " int3" -> " https://biazure-int-edog-redirect.analysis-df.windows.net/" ,
22- " dxt" -> " https://powerbistagingapi.analysis.windows.net/" ,
23- " edog" -> " https://biazure-int-edog-redirect.analysis-df.windows.net/" ,
24- " dev" -> " https://onebox-redirect.analysis.windows-int.net/" ,
25- " console" -> " http://localhost:5001/" ,
26- " daily" -> " https://dailyapi.powerbi.com/" )
27-
2814 private val WorkloadEndpointTypeML = " ML" ;
2915 private val WorkloadEndpointTypeLLMPlugin = " LlmPlugin"
3016 private val WorkloadEndpointTypeAutomatic = " Automatic"
3117 private val WorkloadEndpointTypeRegistry = " Registry"
3218 private val WorkloadEndpointTypeAdmin = " MLAdmin"
3319 private val ContextFilePath = " /home/trusted-service-user/.trident-context" ;
3420 private val SparkConfPath = " /opt/spark/conf/spark-defaults.conf" ;
21+ private val ClusterInfoPath = " /opt/health-agent/conf/cluster-info.json" ;
3522
3623 lazy val CapacityID : Option [String ] = getCapacityID;
3724 lazy val WorkspaceID : Option [String ] = getWorkspaceID;
3825 lazy val ArtifactID : Option [String ] = getArtifactID;
3926 lazy val PbiEnv : String = getPbiEnv;
4027 lazy val FabricContext : Map [String , String ] = getFabricContextFile;
4128 lazy val MLWorkloadHost : Option [String ] = getMLWorkloadHost;
29+ lazy val WorkspacePeEnabled : Boolean = getWorkspacePeEnabled;
4230
43- lazy val PbiSharedHost : String = getPbiSharedHost;
31+ lazy val PbiSharedHost : Option [ String ] = getPbiSharedHost;
4432 lazy val MLWorkloadEndpointML : String = getMLWorkloadEndpoint(WorkloadEndpointTypeML );
4533 lazy val MLWorkloadEndpointLLMPlugin : String = getMLWorkloadEndpoint(WorkloadEndpointTypeLLMPlugin );
4634 lazy val MLWorkloadEndpointAutomatic : String = getMLWorkloadEndpoint(WorkloadEndpointTypeAutomatic );
@@ -81,7 +69,22 @@ object FabricClient extends RESTUtils {
8169 }
8270
8371 private def getMLWorkloadHost : Option [String ] = {
84- extractSchemeAndHost(FabricContext .get(" trident.lakehouse.tokenservice.endpoint" ))
72+ if (WorkspacePeEnabled ) {
73+ getMLWorkloadPEHost
74+ } else {
75+ extractSchemeAndHost(FabricContext .get(" trident.lakehouse.tokenservice.endpoint" ))
76+ }
77+ }
78+
79+ private def getMLWorkloadPEHost : Option [String ] = {
80+ WorkspaceID .map { wsId =>
81+ val cleanedWsId = wsId.toLowerCase.replace(" -" , " " )
82+ val envMark = PbiEnv match {
83+ case " daily" | " dxt" | " msit" => s " $PbiEnv- "
84+ case _ => " "
85+ }
86+ s " https:// ${cleanedWsId}.z ${cleanedWsId.take(2 )}. ${envMark}c.fabric.microsoft.com "
87+ }
8588 }
8689
8790 private def readFabricContextFile (): Map [String , String ] = {
@@ -120,6 +123,25 @@ object FabricClient extends RESTUtils {
120123 }
121124 }
122125
126+ private def readClusterMetadata (): Map [String , String ] = {
127+ val source = Source .fromFile(ClusterInfoPath )
128+ try {
129+ val jsonString = try source.mkString finally source.close()
130+ val jsValue = jsonString.parseJson
131+ val clusterMetadataJson = jsValue.asJsObject.fields(" cluster_metadata" )
132+ clusterMetadataJson.convertTo[Map [String , String ]]
133+ } catch {
134+ case _ : Exception => Map .empty[String , String ]
135+ } finally {
136+ source.close()
137+ }
138+ }
139+
140+ private def getWorkspacePeEnabled : Boolean = {
141+ val metadata = readClusterMetadata()
142+ metadata.get(" workspace-pe-enabled" ).exists(_.equalsIgnoreCase(" true" ))
143+ }
144+
123145 private def getHeaders : Map [String , String ] = {
124146 Map (
125147 " Authorization" -> s " ${getMLWorkloadAADAuthHeader}" ,
@@ -129,10 +151,35 @@ object FabricClient extends RESTUtils {
129151 )
130152 }
131153
132- private def getPbiSharedHost : String = {
133- val clusterDetailUrl = s " ${PbiGlobalServiceEndpoints (PbiEnv )}powerbi/globalservice/v201606/clusterDetails " ;
134- val headers = getHeaders;
135- usageGet(clusterDetailUrl, headers).asJsObject.fields(" clusterUrl" ).convertTo[String ];
154+ private def getPbiSharedHost : Option [String ] = {
155+ if (WorkspacePeEnabled ) {
156+ getPEPbiSharedHost
157+ } else {
158+ val endpoint = FabricContext .get(" spark.trident.pbiHost" ) match {
159+ case Some (value) if value.nonEmpty =>
160+ value.replace(" https://" , " " ).replace(" http://" , " " )
161+ case _ =>
162+ PbiEnv match {
163+ case " edog" => " powerbiapi.analysis-df.windows.net"
164+ case " daily" => " dailyapi.fabric.microsoft.com"
165+ case " dxt" => " dxtapi.fabric.microsoft.com"
166+ case " msit" => " msitapi.fabric.microsoft.com"
167+ case _ => " api.fabric.microsoft.com"
168+ }
169+ }
170+ Some (" https://" + endpoint)
171+ }
172+ }
173+
174+ private def getPEPbiSharedHost : Option [String ] = {
175+ WorkspaceID .map { wsId =>
176+ val cleanedWsId = wsId.toLowerCase.replace(" -" , " " )
177+ val envMark = PbiEnv match {
178+ case " daily" | " dxt" | " msit" => PbiEnv
179+ case _ => " "
180+ }
181+ s " https:// ${cleanedWsId}.z ${cleanedWsId.take(2 )}.w. ${envMark}api.fabric.microsoft.com "
182+ }
136183 }
137184
138185 private def getMLWorkloadEndpoint (endpointType : String ): String = {
0 commit comments