From b28fba0fecd6736bad1a40640b9814b2bbf27cf5 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 1 Apr 2024 14:17:42 -0400 Subject: [PATCH] Add verbose and debug messages for Container Registry Server (#1615) --- src/code/ContainerRegistryServerAPICalls.cs | 34 +++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/code/ContainerRegistryServerAPICalls.cs b/src/code/ContainerRegistryServerAPICalls.cs index 0a7360109..5bf5872ad 100644 --- a/src/code/ContainerRegistryServerAPICalls.cs +++ b/src/code/ContainerRegistryServerAPICalls.cs @@ -301,6 +301,7 @@ private Stream InstallVersion( string packageVersion, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::InstallVersion()"); errRecord = null; string packageNameLowercase = packageName.ToLower(); string accessToken = string.Empty; @@ -371,6 +372,7 @@ private Stream InstallVersion( /// internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessToken()"); string accessToken = string.Empty; string containerRegistryAccessToken = string.Empty; string tenantID = string.Empty; @@ -387,7 +389,6 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) _cmdletPassedIn.WriteVerbose("Access token retrieved."); tenantID = repositoryCredentialInfo.SecretName; - _cmdletPassedIn.WriteVerbose($"Tenant ID: {tenantID}"); } else { @@ -437,6 +438,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) /// internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::IsContainerRegistryUnauthenticated()"); errRecord = null; string endpoint = $"{containerRegistyUrl}/v2/"; HttpResponseMessage response; @@ -463,6 +465,7 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out /// internal string GetContainerRegistryRefreshToken(string tenant, string accessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryRefreshToken()"); string content = string.Format(containerRegistryRefreshTokenTemplate, Registry, tenant, accessToken); var contentHeaders = new Collection> { new KeyValuePair("Content-Type", "application/x-www-form-urlencoded") }; string exchangeUrl = string.Format(containerRegistryOAuthExchangeUrlTemplate, Registry); @@ -480,6 +483,7 @@ internal string GetContainerRegistryRefreshToken(string tenant, string accessTok /// internal string GetContainerRegistryAccessTokenByRefreshToken(string refreshToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessTokenByRefreshToken()"); string content = string.Format(containerRegistryAccessTokenTemplate, Registry, refreshToken); var contentHeaders = new Collection> { new KeyValuePair("Content-Type", "application/x-www-form-urlencoded") }; string tokenUrl = string.Format(containerRegistryOAuthTokenUrlTemplate, Registry); @@ -501,6 +505,7 @@ internal string GetContainerRegistryAccessTokenByRefreshToken(string refreshToke /// private string GetDigestFromManifest(JObject manifest, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetDigestFromManifest()"); errRecord = null; string digest = String.Empty; @@ -544,6 +549,7 @@ private string GetDigestFromManifest(JObject manifest, out ErrorRecord errRecord /// internal JObject GetContainerRegistryRepositoryManifest(string packageName, string version, string containerRegistryAccessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryRepositoryManifest()"); // example of manifestUrl: https://psgetregistry.azurecr.io/hello-world:3.0.0 string manifestUrl = string.Format(containerRegistryManifestUrlTemplate, Registry, packageName, version); var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken); @@ -556,6 +562,7 @@ internal JObject GetContainerRegistryRepositoryManifest(string packageName, stri /// internal async Task GetContainerRegistryBlobAsync(string packageName, string digest, string containerRegistryAccessToken) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryBlobAsync()"); string blobUrl = string.Format(containerRegistryBlobDownloadUrlTemplate, Registry, packageName, digest); var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken); return await GetHttpContentResponseJObject(blobUrl, defaultHeaders); @@ -585,6 +592,7 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi * } * }] */ + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindContainerRegistryImageTags()"); string resolvedVersion = string.Equals(version, "*", StringComparison.OrdinalIgnoreCase) ? null : $"/{version}"; string findImageUrl = string.Format(containerRegistryFindImageVersionUrlTemplate, Registry, packageName, resolvedVersion); var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken); @@ -596,6 +604,7 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi /// internal Hashtable GetContainerRegistryMetadata(string packageName, string exactTagVersion, string containerRegistryAccessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryMetadata()"); Hashtable requiredVersionResponse = new Hashtable(); var foundTags = FindContainerRegistryManifest(packageName, exactTagVersion, containerRegistryAccessToken, out errRecord); @@ -712,6 +721,7 @@ internal Hashtable GetContainerRegistryMetadata(string packageName, string exact /// internal JObject FindContainerRegistryManifest(string packageName, string version, string containerRegistryAccessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindContainerRegistryManifest()"); var createManifestUrl = string.Format(containerRegistryManifestUrlTemplate, Registry, packageName, version); _cmdletPassedIn.WriteDebug($"GET manifest url: {createManifestUrl}"); @@ -724,6 +734,7 @@ internal JObject FindContainerRegistryManifest(string packageName, string versio /// internal ContainerRegistryInfo GetMetadataProperty(JObject foundTags, string packageName, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetMetadataProperty()"); errRecord = null; ContainerRegistryInfo serverPkgInfo = null; @@ -803,6 +814,7 @@ internal ContainerRegistryInfo GetMetadataProperty(JObject foundTags, string pac /// internal async Task UploadManifest(string packageName, string packageVersion, string configPath, bool isManifest, string containerRegistryAccessToken) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::UploadManifest()"); try { var createManifestUrl = string.Format(containerRegistryManifestUrlTemplate, Registry, packageName, packageVersion); @@ -817,6 +829,7 @@ internal async Task UploadManifest(string packageName, stri internal async Task GetHttpContentResponseJObject(string url, Collection> defaultHeaders) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpContentResponseJObject()"); try { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url); @@ -834,6 +847,7 @@ internal async Task GetHttpContentResponseJObject(string url, Colle /// internal JObject GetHttpResponseJObjectUsingDefaultHeaders(string url, HttpMethod method, Collection> defaultHeaders, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpResponseJObjectUsingDefaultHeaders()"); try { errRecord = null; @@ -883,6 +897,7 @@ internal JObject GetHttpResponseJObjectUsingDefaultHeaders(string url, HttpMetho /// internal JObject GetHttpResponseJObjectUsingContentHeaders(string url, HttpMethod method, string content, Collection> contentHeaders, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpResponseJObjectUsingContentHeaders()"); errRecord = null; try { @@ -1122,10 +1137,12 @@ internal bool PushNupkgContainerRegistry(string psd1OrPs1File, Hashtable dependencies, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::PushNupkgContainerRegistry()"); string fullNupkgFile = System.IO.Path.Combine(outputNupkgDir, packageName + "." + packageVersion.ToNormalizedString() + ".nupkg"); string packageNameLowercase = packageName.ToLower(); // Get access token (includes refresh tokens) + _cmdletPassedIn.WriteVerbose($"Get access token for container registry server."); var containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord); if (errRecord != null) { @@ -1133,6 +1150,7 @@ internal bool PushNupkgContainerRegistry(string psd1OrPs1File, } // Upload .nupkg + _cmdletPassedIn.WriteVerbose($"Upload .nupkg file: {fullNupkgFile}"); string nupkgDigest = UploadNupkgFile(packageNameLowercase, containerRegistryAccessToken, fullNupkgFile, out errRecord); if (errRecord != null) { @@ -1148,6 +1166,7 @@ internal bool PushNupkgContainerRegistry(string psd1OrPs1File, // Create config.json file var configFilePath = System.IO.Path.Combine(outputNupkgDir, "config.json"); + _cmdletPassedIn.WriteVerbose($"Create config.json file at path: {configFilePath}"); string configDigest = CreateConfigFile(configFilePath, out errRecord); if (errRecord != null) { @@ -1178,6 +1197,7 @@ internal bool PushNupkgContainerRegistry(string psd1OrPs1File, /// private string UploadNupkgFile(string packageNameLowercase, string containerRegistryAccessToken, string fullNupkgFile, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::UploadNupkgFile()"); _cmdletPassedIn.WriteVerbose("Start uploading blob"); string nupkgDigest = string.Empty; errRecord = null; @@ -1240,6 +1260,7 @@ private string UploadNupkgFile(string packageNameLowercase, string containerRegi /// private void CreateAndUploadEmptyFile(string outputNupkgDir, string pkgNameLower, string containerRegistryAccessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateAndUploadEmptyFile()"); _cmdletPassedIn.WriteVerbose("Create an empty file"); string emptyFileName = "empty" + Guid.NewGuid().ToString() + ".txt"; var emptyFilePath = System.IO.Path.Combine(outputNupkgDir, emptyFileName); @@ -1290,6 +1311,7 @@ private void CreateAndUploadEmptyFile(string outputNupkgDir, string pkgNameLower /// private string CreateConfigFile(string configFilePath, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateConfigFile()"); string configFileDigest = string.Empty; _cmdletPassedIn.WriteVerbose("Create the config file"); while (File.Exists(configFilePath)) @@ -1336,6 +1358,7 @@ private bool TryCreateAndUploadManifest(string fullNupkgFile, string containerRegistryAccessToken, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::TryCreateAndUploadManifest()"); errRecord = null; string packageNameLowercase = packageName.ToLower(); FileInfo nupkgFile = new FileInfo(fullNupkgFile); @@ -1377,6 +1400,7 @@ private string CreateManifestContent( ResourceType resourceType, string metadata) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateManifestContent()"); StringBuilder stringBuilder = new StringBuilder(); StringWriter stringWriter = new StringWriter(stringBuilder); JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter); @@ -1436,6 +1460,7 @@ private string CreateManifestContent( /// private string CreateDigest(string fileName, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateDigest()"); errRecord = null; string digest = string.Empty; FileInfo fileInfo = new FileInfo(fileName); @@ -1457,8 +1482,6 @@ private string CreateDigest(string fileName, out ErrorRecord errRecord) } digest = stringBuilder.ToString(); - // Write the name and hash value of the file to the console. - _cmdletPassedIn.WriteVerbose($"{fileInfo.Name}: {digest}"); } catch (IOException ex) { @@ -1490,6 +1513,7 @@ private string CreateDigest(string fileName, out ErrorRecord errRecord) /// private string CreateMetadataContent(ResourceType resourceType, Hashtable parsedMetadata, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateMetadataContent()"); errRecord = null; string jsonString = string.Empty; @@ -1533,6 +1557,7 @@ private string CreateMetadataContent(ResourceType resourceType, Hashtable parsed /// internal async Task GetStartUploadBlobLocation(string packageName, string containerRegistryAccessToken) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetStartUploadBlobLocation()"); try { var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken); @@ -1550,6 +1575,7 @@ internal async Task GetStartUploadBlobLocation(string packageName, strin /// internal async Task EndUploadBlob(string location, string filePath, string digest, bool isManifest, string containerRegistryAccessToken) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::EndUploadBlob()"); try { var endUploadUrl = string.Format(containerRegistryEndUploadTemplate, Registry, location, digest); @@ -1571,6 +1597,7 @@ internal async Task EndUploadBlob(string location, string f /// private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionType versionType, VersionRange versionRange, NuGetVersion requiredVersion, bool includePrerelease, bool getOnlyLatest, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindPackagesWithVersionHelper()"); string accessToken = string.Empty; string tenantID = string.Empty; string registryUrl = Repository.Uri.ToString(); @@ -1624,6 +1651,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp /// private SortedDictionary GetPackagesWithRequiredVersion(List allPkgVersions, VersionType versionType, VersionRange versionRange, NuGetVersion specificVersion, string packageName, bool includePrerelease, out ErrorRecord errRecord) { + _cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetPackagesWithRequiredVersion()"); errRecord = null; // we need NuGetVersion to sort versions by order, and string pkgVersionString (which is the exact tag from the server) to call GetContainerRegistryMetadata() later with exact version tag. SortedDictionary sortedPkgs = new SortedDictionary(VersionComparer.Default);