-
Notifications
You must be signed in to change notification settings - Fork 56
Add ResourceNameCompleter #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
add new class
} | ||
else | ||
{ | ||
allProviders = client.ResourceGroups.ListResourcesAsync(parentResources[0], odataQuery); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why just the first element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved offline - this is the ResourceGroupName
Task<IPage<GenericResource>> allProviders = null; | ||
var odataQuery = new ODataQuery<GenericResourceFilter>(r => r.ResourceType == resourceType); | ||
|
||
if (string.IsNullOrWhiteSpace(parentResources[0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use parentResources.Any(s => !string.IsNullOrWhitespace(s))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved offline - this is the ResourceGroupName
List<ResourceIdentifier> ids = new List<ResourceIdentifier>(); | ||
if (_timeout == -1) | ||
{ | ||
allProviders.Wait(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use one of the overloads that allows you to cancel the wait if it takes too long
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - this will only ever be used in the tests, and we never want to spend more than 5 minutes on this test.
IAzureContext context = AzureRmProfileProvider.Instance?.Profile?.DefaultContext; | ||
try | ||
{ | ||
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The nested if statements make this difficult to read. We should separate the code for making the client call from the code for filtering the response. This will also enable us to mock out the client response and test our filtering logic. Also, we should use System,Linq to filter responses rather than for loops and if statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
address comments
else | ||
{ | ||
allProviders = client.ResourceGroups.ListResourcesAsync(resourceGroupName, odataQuery); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommendation (declaration and initialization in the same statement):
var allProviders = string.IsNullOrWhiteSpace(resourceGroupName)
? client.Resources.ListAsync(odataQuery)
: client.ResourceGroups.ListResourcesAsync(resourceGroupName, odataQuery);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
#if DEBUG | ||
throw new InvalidOperationException(Resources.TimeOutForProviderList); | ||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block can simply be (removes code duplication and verbosity):
Note: ForEach
+ data transformation = Select
in LINQ
var timeoutDuration = _timeout == -1 ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(_timeout);
var hasNotTimedOut = allProviders.Wait(timeoutDuration);
var hasResult = allProviders.Result != null;
var isSuccessful = hasNotTimedOut && hasResult;
#if DEBUG
if (!isSuccessful)
{
throw new InvalidOperationException(!hasResult ? "Result from client.Providers is null" : Resources.TimeOutForProviderList);
}
#endif
return isSuccessful
? allProviders.Result.Select(resource => new ResourceIdentifier(resource.Id)).ToList()
: new List<ResourceIdentifier>();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
} | ||
} | ||
|
||
return output; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Entire method could be (reduces tab depth and short-circuits on excluded resources):
return ids.Where(resource => {
if(resource.ParentResource == null)
{
return true;
}
var actualParentResources = resource.ParentResource.Split('/').Where((pr, i) => i % 2 != 0).ToArray();
var parentResourcesExceptFirst = parentResources.Skip(1).ToArray();
if(actualParentResources.Count() != parentResourcesExceptFirst.Count())
{
#if DEBUG
throw new InvalidOperationException("Improper number of parent resources were given");
#endif
return true;
}
return !actualParentResources.Any((pr, i) =>
!string.IsNullOrEmpty(parentResourcesExceptFirst[i]) &&
!string.Equals(pr, parentResources[i], StringComparison.OrdinalIgnoreCase));
}.Select(resource => resource.ResourceName).ToList()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
address comments
No description provided.