Skip to content

Commit

Permalink
Merge pull request #140 from microsoft/52b-friendlynameaddconfig
Browse files Browse the repository at this point in the history
Added configuration sample and fixed possible NullReferenceException
  • Loading branch information
SvenAelterman authored Sep 26, 2022
2 parents ff1d4a6 + 7a4bbae commit 687b821
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/api/Services/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ internal IList<StorageAccountAndContainers> StorageAccounts(string principalId,
/// <returns>Cached value or new value if not cached.</returns>
internal StorageAccountProperties GetStorageAccountProperties()
{
if (String.IsNullOrWhiteSpace(Configuration.StorageAccountPropertiesCacheKey)) { return null; }
var obj = GetCacheValue<StorageAccountProperties>(Configuration.StorageAccountPropertiesCacheKey);
if (obj is null) { obj = new StorageAccountProperties(); }
return obj;

}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/api/Services/ResourceGraphOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public string GetAccountResourceId(string storageAccountName)
/// <returns>Tag value</returns>
public string GetAccountResourceTagValue(string storageAccountName, string tagName)
{
if (String.IsNullOrWhiteSpace(tagName))
{
log.LogWarning("No tagname argument was provided. Please check configuration.");
return null;
}
var tags = GetGraphStorageAccountQueryResponse(storageAccountName, "tags");
if (tags is null)
{
Expand Down
13 changes: 8 additions & 5 deletions src/api/Services/RoleOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ internal IList<StorageAccountAndContainers> GetAccessibleContainersForPrincipal(
if (!m.Success)
continue; // No Match, move to next one

// There will always be a storage account name if there was a Regex match
string storageAccountName = m.Groups["accountName"].Value;
// There will always be a storage account name if there was a Regex match
string storageAccountName = m.Groups["accountName"].Value;

// Find an existing entry for this storage account in the result set
StorageAccountAndContainers fsr = results
Expand All @@ -128,12 +128,14 @@ internal IList<StorageAccountAndContainers> GetAccessibleContainersForPrincipal(
if (fsr == null)
{
// Check for cached storage account properties for the storage account name
var val = storageAccountProperties.Value.FirstOrDefault(x => x.StorageAccountName == storageAccountName);
var val = storageAccountProperties?.Value.FirstOrDefault(x => x.StorageAccountName == storageAccountName);

// Check for the friendly name in the cache. If it doesn't exist, get it from Azure and add it to the cache.
var fname = (val is null) ? String.Empty : val.FriendlyName;
var fname = val?.FriendlyName;
if (val is null)
{
// Get the friendly name from Azure
// If there is no friendly name tag specified in settings configuration, this will end up displaying the storage account name instead using the property definition
fname = rgo.GetAccountResourceTagValue(storageAccountName, Configuration.StorageAccountFriendlyTagNameKey);
// Save back into cache
storageAccountProperties.Value.Add(new StorageAccount
Expand Down Expand Up @@ -173,7 +175,8 @@ internal IList<StorageAccountAndContainers> GetAccessibleContainersForPrincipal(
}

// Update the account properties cache
_cache.SetStorageAccountProperties(storageAccountProperties);
if (storageAccountProperties is not null)
_cache.SetStorageAccountProperties(storageAccountProperties);

return results;
}
Expand Down
1 change: 1 addition & 0 deletions src/api/sample.local.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"AZURE_CLIENT_ID": "4d3f...",
"AZURE_CLIENT_SECRET": "A1B2...",
"FILESYSTEMS_API_KEY": "somerandomkey",
"STORAGE_FRIENDLY_TAG_NAME": "azsf-friendly-name",
"CacheConnection": "<endpoint>.redis.cache.windows.net:6380,password=<key>,ssl=True,abortConnect=False"
},
"Host": {
Expand Down

0 comments on commit 687b821

Please sign in to comment.