Skip to content
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

New Resource: azurerm_maintenance_assignment_dynamic_scope #25467

Merged
merged 16 commits into from
May 6, 2024

Conversation

ASHR4
Copy link
Contributor

@ASHR4 ASHR4 commented Mar 29, 2024

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave "+1" or "me too" comments, they generate extra noise for PR followers and do not help prioritize for review

Description

Adding support to configure dynamic scopes on maintenance configuration resources.

PR Checklist

  • I have followed the guidelines in our Contributing Documentation.
  • I have checked to ensure there aren't other open Pull Requests for the same update/change.
  • I have checked if my changes close any open issues. If so please include appropriate closing keywords below.
  • I have updated/added Documentation as required written in a helpful and kind way to assist users that may be unfamiliar with the resource / data source.
  • I have used a meaningful PR title to help maintainers and other users understand this change and help prevent duplicate work.
    For example: “resource_name_here - description of change e.g. adding property new_property_name_here

Changes to existing Resource / Data Source

  • I have added an explanation of what my changes do and why I'd like you to include them (This may be covered by linking to an issue above, but may benefit from additional explanation).
  • I have written new tests for my resource or datasource changes & updated any relevent documentation.
  • I have successfully run tests with my changes locally. If not, please provide details on testing challenges that prevented you running the tests.
  • (For changes that include a state migration only). I have manually tested the migration path between relevant versions of the provider.

Testing

  • My submission includes Test coverage as described in the Contribution Guide and the tests pass. (if this is not possible for any reason, please include details of why you did or could not add test coverage)

Change Log

Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.

  • azurerm_maintenance_assignment_dynamic_scope - support for assigning dynamic scopes on maintenance configurations

This is a (please select all that apply):

  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Related Issue(s)

Fixes #24411, fixes #23336

Note

If this PR changes meaningfully during the course of review please update the title and description as required.

@phil-bevan
Copy link

Hi Rhys, not sure if this helps but I can also reproduce the issue in the portal if I create a dynamic scope on a Subscription. However, if I apply a further filter and filter by resource group the dynamic scope creates successfully. Dynamic scopes created at the same time as the maintenance configuration also create without error I don't know why this makes a difference but hopefully gives you a clue!

@phil-bevan
Copy link

phil-bevan commented Apr 3, 2024

From further testing via Powershell it appears to you must apply a filter of some kind. Anything seems to be acceptable.

C:\Users\BevanP> New-AzConfigurationAssignment -ConfigurationAssignmentName "mydynamicscope4" -MaintenanceConfigurationId $config.Id
New-AzConfigurationAssignment : Operation returned an invalid status code 'InternalServerError'
C:\Users\BevanP> New-AzConfigurationAssignment -ConfigurationAssignmentName "mydynamicscope4" -MaintenanceConfigurationId $config.Id -FilterOsType Linux,Windows


Location                   : global
MaintenanceConfigurationId : /subscriptions/xxx/resourcegroups/rg-testing/provide
rs/microsoft.maintenance/maintenanceconfigurations/config-philtest1
ResourceId                 : /subscriptions/xxx
Id                         : /subscriptions/xxx/providers/microsoft.maintenance/config
urationassignments/mydynamicscope4
Name                       : mydynamicscope4
Type                       : microsoft.maintenance/configurationassignments
FilterOsType[0]            : Linux
FilterOsType[1]            : Windows
C:\Users\BevanP> New-AzConfigurationAssignment -ConfigurationAssignmentName "mydynamicscope4" -MaintenanceConfigurationId $config.Id -FilterLocation uksouth


Location                   : global
MaintenanceConfigurationId : /subscriptions/xxx/resourcegroups/rg-testing/provide
rs/microsoft.maintenance/maintenanceconfigurations/config-philtest1
ResourceId                 : /subscriptions/xxx
Id                         : /subscriptions/xxx/providers/microsoft.maintenance/config
urationassignments/mydynamicscope4
Name                       : mydynamicscope4
Type                       : microsoft.maintenance/configurationassignments
FilterLocation[0]          : uksouth

@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 3, 2024

Hi Rhys, not sure if this helps but I can also reproduce the issue in the portal if I create a dynamic scope on a Subscription. However, if I apply a further filter and filter by resource group the dynamic scope creates successfully. Dynamic scopes created at the same time as the maintenance configuration also create without error I don't know why this makes a difference but hopefully gives you a clue!

Hi @phil-bevan, thank you for your input! I built a dynamic scope via the Portal and AzApi by amending the filters as you mentioned.

The other error I'm seeing occurs when Terraform verifies whether there is an existing resource in Azure; specifically the 'Get' method.

@phil-bevan
Copy link

Hi @ASHR4 , Not a Go expert, but doesn't NewScopedConfigurationAssignmentID expect a ConfigurationAssignmentName rather than a MaintenanceConfigurationName?

@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 4, 2024

Hi @ASHR4 , Not a Go expert, but doesn't NewScopedConfigurationAssignmentID expect a ConfigurationAssignmentName rather than a MaintenanceConfigurationName?

Hi @phil-bevan, the other configuration assignments do something similar, however, I have a stashed commit that takes a name argument as if we require multiple dynamic scopes the configuration assignment requires a unique resource ID as they're all assigned at the subscription level

This still didn't resolve my issue. The NewScopeConfigurationAssignmentID creates a struct that can be used to build the resource ID and check if a configuration assignment already exists.

@phil-bevan
Copy link

phil-bevan commented Apr 9, 2024

@ASHR4 I think I understand why it's not working. The current code will generate a resource ID similar to
/subscriptions/XXX/resourcegroups/rg-maint-philtest/providers/microsoft.maintenance/maintenanceconfigurations/example-mc/providers/microsoft.maintenance/configurationassignments/example1

which I guess works fine for the other config assignment types, but dynamic scopes are assigned at the subscription level so you need to access them with a resource ID such as
/subscriptions/XXX/providers/microsoft.maintenance/configurationassignments/example1

I think you need to find a different method to build that resource ID. I have a working sample in C# if that helps you at all:

using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Maintenance;
using Azure.ResourceManager.Maintenance.Models;
using Azure.ResourceManager.Resources;


var cred = new InteractiveBrowserCredential();

string subId = "XXX";
string resGroupName = "rg-maint-philtest";
string maintConfigName = "example-mc";
string dynamicScopeName = "example3";
ArmClient client = new ArmClient(cred, subId);

SubscriptionResource sub = client.GetDefaultSubscription();
MaintenanceConfigurationResource mc = sub.GetResourceGroup(resGroupName).Value.GetMaintenanceConfiguration(maintConfigName);

MaintenanceConfigurationAssignmentFilter mcaf = new MaintenanceConfigurationAssignmentFilter();
mcaf.Locations.Add(AzureLocation.UKSouth);

MaintenanceConfigurationAssignmentData mcad = new MaintenanceConfigurationAssignmentData()
{
    MaintenanceConfigurationId = mc.Id,
    Filter = mcaf

};

// Create dynamic scope.  Comment out this line if you just want to check if one exists.
sub.CreateOrUpdateConfigurationAssignmentBySubscription(dynamicScopeName, mcad);

try
{
    //Check if scope exists
    var yy = sub.GetConfigurationAssignmentBySubscription(dynamicScopeName);
}
catch (Exception ex)
{
    switch (ex)
    {
        case RequestFailedException: //If dynamic scope doesn't exist we get a 404 response
            Console.WriteLine(ex.ToString());
            break;

    }
}

I ran Fiddler in the background to identify the resource IDs in use

@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 13, 2024

Hi @phil-bevan,

You were spot on, the ForSubscription methods worked. It seems as though when I had tried these methods before my validation and input were wrong causing it to throw an error, hence why I discounted it.

I'll try and work on finishing this over the weekend :)

Appreciate the help!

@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 13, 2024

image

@ASHR4 ASHR4 marked this pull request as ready for review April 13, 2024 20:47
@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 22, 2024

@katbyte unsure if this being in draft, for a while, has effected this being reviewed.

The associated issues have over 20+ votes and so I was hoping if this could be reviewed?

Let me know if this is already on your radar :)

Thanks

@jackofallops jackofallops self-assigned this Apr 22, 2024
Copy link
Member

@jackofallops jackofallops left a comment

Choose a reason for hiding this comment

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

Hi @ASHR4 - Thanks for this PR. There's some design and schema changes noted below, sorry if it looks like a lot, I've tried to cover everything in one pass rather than go in cycles, but if you can take a look at and address the changes, I think we'll be in good shape!

Thanks!

@ASHR4
Copy link
Contributor Author

ASHR4 commented Apr 23, 2024

Hi @ASHR4 - Thanks for this PR. There's some design and schema changes noted below, sorry if it looks like a lot, I've tried to cover everything in one pass rather than go in cycles, but if you can take a look at and address the changes, I think we'll be in good shape!

Thanks!

Hi @jackofallops, appreciate your time on this.

I hope this is now in better shape :)

@leewilson86

This comment was marked as off-topic.

@ASHR4 ASHR4 requested a review from jackofallops April 25, 2024 21:23
@ASHR4
Copy link
Contributor Author

ASHR4 commented May 1, 2024

Hi @jackofallops,

I requested a re-review last week, is there any chance you have any capacity for this, this week?

Thanks

Copy link
Collaborator

@katbyte katbyte left a comment

Choose a reason for hiding this comment

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

Thanks @ASHR4 - LGTM now ⛵

@katbyte katbyte merged commit 7ecf0a0 into hashicorp:main May 6, 2024
33 checks passed
katbyte added a commit that referenced this pull request May 6, 2024
@github-actions github-actions bot added this to the v3.103.0 milestone May 6, 2024
@ASHR4 ASHR4 deleted the feature/dynamic-maintenance-assignment branch May 7, 2024 12:45
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants