You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a new configuration to convert things to scope (#3037)
* add the configuration but it is untested yet
* remove a temporary configuration since it is no longer needed or used in our SDKs
* various changes and fixes for assigning a path as scope
* fix some issues around usual scope
* a format
* introduce a patch especially for resourcemanager
* generalized the previous fix
* another fix for recent changes
* fix test cases
* add a new case in test project for the new scope
* update documents
* add test case to the testgen as well
* fix the base test on scope resources
* add some more clarification for generate arm resource extensions configuration
* add some description
* fix a using issue in generated samples
Copy file name to clipboardExpand all lines: docs/mgmt/readme.md
+82-1Lines changed: 82 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -650,12 +650,34 @@ and since it is a scope resource without any configuration, its parent is anythi
650
650
```csharp
651
651
public static partial class ResourcesExtension
652
652
{
653
-
public static DeploymentCollection GetDeployments(this ArmResource armResource)
653
+
public static DeploymentCollection GetDeployments(this ArmClient client, ResourceIdentifier scope)
654
654
{
655
655
/* ... */
656
656
}
657
657
}
658
658
```
659
+
If you would like to generate an extension method of the `ArmResource` class for this scope resource, you need to add this configuration `generate-arm-resource-extensions` by adding the request path of this scope resource:
And you will see this change in the generated code:
665
+
```diff
666
+
public static partial class ResourcesExtension
667
+
{
668
+
public static DeploymentCollection GetDeployments(this ArmClient client, ResourceIdentifier scope)
669
+
{
670
+
/* ... */
671
+
}
672
+
+
673
+
+ public static DeploymentCollection GetDeployments(this ArmResource armResource)
674
+
+ {
675
+
+ /* ... */
676
+
+ }
677
+
}
678
+
```
679
+
Please note this extension methods have a huge side effect: because all `Resource` class would inherit from `ArmResource`, once the user import the namespace of this SDK, they would see this `GetDeployments` method on any `Resource` instance, while in the real life, this scope resource might not be that general to be applied onto any resource coming from any RP. Please only use this configuration when it could be confirmed to support plenty of resources and has the plan to support more.
680
+
659
681
To assign specific resource types to this scope, you can use the following configuration:
660
682
661
683
```yaml
@@ -689,6 +711,65 @@ public static partial class ResourcesExtension
689
711
}
690
712
```
691
713
714
+
In some cases, we might have a resource that extends another resource from another RP. For instance this resource in `guestconfiguration` RP: `/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/providers/Microsoft.GuestConfiguration/guestConfigurationAssignments/{guestConfigurationAssignmentName}`extends another resource virtual machine in the `compute` RP.
715
+
716
+
By default, because the generator will never find the `VirtualMachineResource` when generating this SDK, the parent resource of this `VmGuestConfigurationResource` will be `ResourceGroupResource`.
717
+
```csharp
718
+
public static partial class GuestConfigurationExtensions
719
+
{
720
+
public static VmGuestConfigurationCollection GetVmGuestConfigurations(this ResourceGroupResource resourceGroup, string vmName)
721
+
{
722
+
/* ... */
723
+
}
724
+
}
725
+
```
726
+
727
+
To show the relationship between resources across different RPs, we could convert it into a scope resource by using the following configuration:
This configuration registers the listed request paths as resources the generator could recognize even if they might not exist in the current context. After applying this configuration, the generated code will have the following changes:
733
+
```diff
734
+
public static partial class GuestConfigurationExtensions
735
+
{
736
+
- public static VmGuestConfigurationCollection GetVmGuestConfigurations(this ResourceGroupResource resourceGroup, string vmName)
737
+
- {
738
+
- /* ... */
739
+
- }
740
+
+ public static VmGuestConfigurationCollection GetVmGuestConfigurations(this ArmClient client, ResourceIdentifier scope)
741
+
+ {
742
+
+ if (!scope.ResourceType.Equals("Microsoft.Compute/virtualMachines"))
743
+
+ throw new InvalidOperationException(string.Format("Invalid resource type {0} expected Microsoft.Compute/virtualMachines", scope.ResourceType));
744
+
+ /* ... */
745
+
+ }
746
+
}
747
+
```
748
+
749
+
This configuration works fine with the configuration introduced above:
public static partial class GuestConfigurationExtensions
757
+
{
758
+
public static VmGuestConfigurationCollection GetVmGuestConfigurations(this ArmClient client, ResourceIdentifier scope)
759
+
{
760
+
if (!scope.ResourceType.Equals("Microsoft.Compute/virtualMachines"))
761
+
throw new InvalidOperationException(string.Format("Invalid resource type {0} expected Microsoft.Compute/virtualMachines", scope.ResourceType));
762
+
/* ... */
763
+
}
764
+
+
765
+
+ public static VmGuestConfigurationCollection GetVmGuestConfigurations(this ArmResource armResource)
766
+
+ {
767
+
+ /* ... */
768
+
+ }
769
+
}
770
+
```
771
+
Please note this `ArmResource` extension method has the same side effect as explained above: this extension is only meant to be got or created under the virtual machine resource, but the extension method will let you see this method on any resource instance because they all inherit from `ArmResource`.
772
+
692
773
### SDK polishing configurations
693
774
694
775
During the SDK review, we would like to make some polish to our generated SDK according to the review comments, for instance, changing names of types, properties, making type of properties more specific, etc. To achieve these, you will need the SDK polishing configurations.
// my parent is myself? Only tenant has this attribute, return empty
47
48
returnEnumerable.Empty<MgmtTypeProvider>();
48
49
}
50
+
// if the scope of this request path is parameterized, and the direct parent path we get from the resource list is parent of the scope, we return the scope as its parent since the scope here is a child
51
+
// if the request path is a "by id" path, its scope is the same as itself, therefore this condition here is nullified and should be skipped
.Concat(newList<RequestPath>{RequestPath.ResourceGroup,RequestPath.Subscription,RequestPath.ManagementGroup})// When generating management group in management.json, the path is /providers/Microsoft.Management/managementGroups/{groupId} while RequestPath.ManagementGroup is /providers/Microsoft.Management/managementGroups/{managementGroupId}. We pick the first one.
0 commit comments