1919import os
2020import re
2121import shutil
22- import subprocess
2322import tempfile
2423import time
2524from typing import (
26- TYPE_CHECKING ,
2725 Any ,
2826 Callable ,
2927 Dict ,
3735import requests
3836
3937from datacustomcode .cmd import cmd_output
40- from datacustomcode .credentials import AuthType
4138from datacustomcode .scan import find_base_directory , get_package_type
4239
43- if TYPE_CHECKING :
44- from datacustomcode .credentials import Credentials
45-
4640DATA_CUSTOM_CODE_PATH = "services/data/v63.0/ssot/data-custom-code"
4741DATA_TRANSFORMS_PATH = "services/data/v63.0/ssot/data-transforms"
48- AUTH_PATH = "services/oauth2/token"
4942WAIT_FOR_DEPLOYMENT_TIMEOUT = 3000
5043
5144# Available compute types for Data Cloud deployments.
@@ -163,80 +156,6 @@ class AccessTokenResponse(BaseModel):
163156 instance_url : str
164157
165158
166- def _retrieve_access_token (credentials : Credentials ) -> AccessTokenResponse :
167- """Get an access token for the Salesforce API."""
168- logger .debug ("Getting oauth token..." )
169-
170- url = f"{ credentials .login_url .rstrip ('/' )} /{ AUTH_PATH .lstrip ('/' )} "
171-
172- if credentials .auth_type == AuthType .OAUTH_TOKENS :
173- data = {
174- "grant_type" : "refresh_token" ,
175- "refresh_token" : credentials .refresh_token ,
176- "client_id" : credentials .client_id ,
177- "client_secret" : credentials .client_secret ,
178- }
179- elif credentials .auth_type == AuthType .CLIENT_CREDENTIALS :
180- data = {
181- "grant_type" : "client_credentials" ,
182- "client_id" : credentials .client_id ,
183- "client_secret" : credentials .client_secret ,
184- }
185- else :
186- raise ValueError (f"Unsupported auth_type: { credentials .auth_type } " )
187-
188- response = _make_api_call (url , "POST" , data = data )
189- return AccessTokenResponse (** response )
190-
191-
192- def _retrieve_access_token_from_sf_cli (sf_cli_org : str ) -> AccessTokenResponse :
193- """Get an access token from the Salesforce CLI."""
194- try :
195- result = subprocess .run (
196- ["sf" , "org" , "display" , "--target-org" , sf_cli_org , "--json" ],
197- capture_output = True ,
198- text = True ,
199- check = True ,
200- timeout = 30 ,
201- )
202- except FileNotFoundError as exc :
203- raise RuntimeError (
204- "The 'sf' command was not found. "
205- "Please install Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli"
206- ) from exc
207- except subprocess .TimeoutExpired as exc :
208- raise RuntimeError (
209- f"'sf org display' timed out for org '{ sf_cli_org } '"
210- ) from exc
211- except subprocess .CalledProcessError as exc :
212- raise RuntimeError (
213- f"'sf org display' failed for org '{ sf_cli_org } '.\n "
214- f"Ensure the org is authenticated via 'sf org login web'.\n "
215- f"stderr: { exc .stderr .strip ()} "
216- ) from exc
217-
218- try :
219- data = json .loads (result .stdout )
220- except json .JSONDecodeError as exc :
221- raise RuntimeError (f"Failed to parse 'sf org display' output: { exc } " ) from exc
222-
223- if data .get ("status" ) != 0 :
224- raise RuntimeError (
225- f"SF CLI error for org '{ sf_cli_org } ': "
226- f"{ data .get ('message' , 'unknown error' )} "
227- )
228-
229- org_result = data .get ("result" , {})
230- access_token = org_result .get ("accessToken" )
231- instance_url = org_result .get ("instanceUrl" )
232- if not access_token or not instance_url :
233- raise RuntimeError (
234- f"'sf org display' did not return an access token or instance URL "
235- f"for org '{ sf_cli_org } '"
236- )
237- return AccessTokenResponse (access_token = access_token , instance_url = instance_url )
238-
239-
240159class CreateDeploymentResponse (BaseModel ):
241160 fileUploadUrl : str
242161
@@ -567,16 +486,11 @@ def zip(
567486def deploy_full (
568487 directory : str ,
569488 metadata : CodeExtensionMetadata ,
570- credentials : Union [ "Credentials" , AccessTokenResponse ] ,
489+ access_token : AccessTokenResponse ,
571490 docker_network : str ,
572491 callback = None ,
573492) -> AccessTokenResponse :
574493 """Deploy a data transform in the DataCloud."""
575- if isinstance (credentials , AccessTokenResponse ):
576- access_token = credentials
577- else :
578- access_token = _retrieve_access_token (credentials )
579-
580494 # prepare payload
581495 config = get_config (directory )
582496
@@ -587,7 +501,6 @@ def deploy_full(
587501 wait_for_deployment (access_token , metadata , callback )
588502
589503 # create data transform
590-
591504 if isinstance (config , DataTransformConfig ):
592505 create_data_transform (directory , access_token , metadata , config )
593506 return access_token
0 commit comments