Skip to content

Commit

Permalink
Add verbose and debug messages for Container Registry Server (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
anamnavi authored Apr 1, 2024
1 parent abfbf42 commit b28fba0
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/code/ContainerRegistryServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -371,6 +372,7 @@ private Stream InstallVersion(
/// </summary>
internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessToken()");
string accessToken = string.Empty;
string containerRegistryAccessToken = string.Empty;
string tenantID = string.Empty;
Expand All @@ -387,7 +389,6 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
_cmdletPassedIn.WriteVerbose("Access token retrieved.");

tenantID = repositoryCredentialInfo.SecretName;
_cmdletPassedIn.WriteVerbose($"Tenant ID: {tenantID}");
}
else
{
Expand Down Expand Up @@ -437,6 +438,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
/// </summary>
internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::IsContainerRegistryUnauthenticated()");
errRecord = null;
string endpoint = $"{containerRegistyUrl}/v2/";
HttpResponseMessage response;
Expand All @@ -463,6 +465,7 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
/// </summary>
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<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Content-Type", "application/x-www-form-urlencoded") };
string exchangeUrl = string.Format(containerRegistryOAuthExchangeUrlTemplate, Registry);
Expand All @@ -480,6 +483,7 @@ internal string GetContainerRegistryRefreshToken(string tenant, string accessTok
/// </summary>
internal string GetContainerRegistryAccessTokenByRefreshToken(string refreshToken, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessTokenByRefreshToken()");
string content = string.Format(containerRegistryAccessTokenTemplate, Registry, refreshToken);
var contentHeaders = new Collection<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Content-Type", "application/x-www-form-urlencoded") };
string tokenUrl = string.Format(containerRegistryOAuthTokenUrlTemplate, Registry);
Expand All @@ -501,6 +505,7 @@ internal string GetContainerRegistryAccessTokenByRefreshToken(string refreshToke
/// </summary>
private string GetDigestFromManifest(JObject manifest, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetDigestFromManifest()");
errRecord = null;
string digest = String.Empty;

Expand Down Expand Up @@ -544,6 +549,7 @@ private string GetDigestFromManifest(JObject manifest, out ErrorRecord errRecord
/// </summary>
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);
Expand All @@ -556,6 +562,7 @@ internal JObject GetContainerRegistryRepositoryManifest(string packageName, stri
/// </summary>
internal async Task<HttpContent> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -596,6 +604,7 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi
/// </summary>
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);
Expand Down Expand Up @@ -712,6 +721,7 @@ internal Hashtable GetContainerRegistryMetadata(string packageName, string exact
/// </summary>
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}");

Expand All @@ -724,6 +734,7 @@ internal JObject FindContainerRegistryManifest(string packageName, string versio
/// </summary>
internal ContainerRegistryInfo GetMetadataProperty(JObject foundTags, string packageName, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetMetadataProperty()");
errRecord = null;
ContainerRegistryInfo serverPkgInfo = null;

Expand Down Expand Up @@ -803,6 +814,7 @@ internal ContainerRegistryInfo GetMetadataProperty(JObject foundTags, string pac
/// </summary>
internal async Task<HttpResponseMessage> 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);
Expand All @@ -817,6 +829,7 @@ internal async Task<HttpResponseMessage> UploadManifest(string packageName, stri

internal async Task<HttpContent> GetHttpContentResponseJObject(string url, Collection<KeyValuePair<string, string>> defaultHeaders)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpContentResponseJObject()");
try
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
Expand All @@ -834,6 +847,7 @@ internal async Task<HttpContent> GetHttpContentResponseJObject(string url, Colle
/// </summary>
internal JObject GetHttpResponseJObjectUsingDefaultHeaders(string url, HttpMethod method, Collection<KeyValuePair<string, string>> defaultHeaders, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpResponseJObjectUsingDefaultHeaders()");
try
{
errRecord = null;
Expand Down Expand Up @@ -883,6 +897,7 @@ internal JObject GetHttpResponseJObjectUsingDefaultHeaders(string url, HttpMetho
/// </summary>
internal JObject GetHttpResponseJObjectUsingContentHeaders(string url, HttpMethod method, string content, Collection<KeyValuePair<string, string>> contentHeaders, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetHttpResponseJObjectUsingContentHeaders()");
errRecord = null;
try
{
Expand Down Expand Up @@ -1122,17 +1137,20 @@ 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)
{
return false;
}

// Upload .nupkg
_cmdletPassedIn.WriteVerbose($"Upload .nupkg file: {fullNupkgFile}");
string nupkgDigest = UploadNupkgFile(packageNameLowercase, containerRegistryAccessToken, fullNupkgFile, out errRecord);
if (errRecord != null)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -1178,6 +1197,7 @@ internal bool PushNupkgContainerRegistry(string psd1OrPs1File,
/// </summary>
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;
Expand Down Expand Up @@ -1240,6 +1260,7 @@ private string UploadNupkgFile(string packageNameLowercase, string containerRegi
/// </summary>
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);
Expand Down Expand Up @@ -1290,6 +1311,7 @@ private void CreateAndUploadEmptyFile(string outputNupkgDir, string pkgNameLower
/// </summary>
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))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1436,6 +1460,7 @@ private string CreateManifestContent(
/// </summary>
private string CreateDigest(string fileName, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateDigest()");
errRecord = null;
string digest = string.Empty;
FileInfo fileInfo = new FileInfo(fileName);
Expand All @@ -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)
{
Expand Down Expand Up @@ -1490,6 +1513,7 @@ private string CreateDigest(string fileName, out ErrorRecord errRecord)
/// </summary>
private string CreateMetadataContent(ResourceType resourceType, Hashtable parsedMetadata, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::CreateMetadataContent()");
errRecord = null;
string jsonString = string.Empty;

Expand Down Expand Up @@ -1533,6 +1557,7 @@ private string CreateMetadataContent(ResourceType resourceType, Hashtable parsed
/// </summary>
internal async Task<string> GetStartUploadBlobLocation(string packageName, string containerRegistryAccessToken)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetStartUploadBlobLocation()");
try
{
var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken);
Expand All @@ -1550,6 +1575,7 @@ internal async Task<string> GetStartUploadBlobLocation(string packageName, strin
/// </summary>
internal async Task<HttpResponseMessage> 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);
Expand All @@ -1571,6 +1597,7 @@ internal async Task<HttpResponseMessage> EndUploadBlob(string location, string f
/// </summary>
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();
Expand Down Expand Up @@ -1624,6 +1651,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
/// </summary>
private SortedDictionary<NuGet.Versioning.SemanticVersion, string> GetPackagesWithRequiredVersion(List<JToken> 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<NuGet.Versioning.SemanticVersion, string> sortedPkgs = new SortedDictionary<SemanticVersion, string>(VersionComparer.Default);
Expand Down

0 comments on commit b28fba0

Please sign in to comment.