Skip to content

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

Merged
merged 6 commits into from
Oct 11, 2018
Merged

Add ResourceNameCompleter #72

merged 6 commits into from
Oct 11, 2018

Conversation

maddieclayton
Copy link
Contributor

No description provided.

}
else
{
allProviders = client.ResourceGroups.ListResourcesAsync(parentResources[0], odataQuery);
Copy link
Member

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?

Copy link
Contributor Author

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]))
Copy link
Member

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))

Copy link
Contributor Author

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();
Copy link
Member

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

Copy link
Contributor Author

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);
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@maddieclayton maddieclayton assigned markcowl and unassigned cormacpayne Oct 8, 2018
else
{
allProviders = client.ResourceGroups.ListResourcesAsync(resourceGroupName, odataQuery);
}
Copy link
Contributor

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);

Copy link
Contributor Author

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
}
Copy link
Contributor

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>();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
}

return output;
Copy link
Contributor

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@MiYanni MiYanni dismissed markcowl’s stale review October 11, 2018 23:34

Comments have been addressed.

@MiYanni MiYanni merged commit 2c49680 into develop Oct 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants