Skip to content

Specific Extension Resource

Sample specification for an extension resource at multiple scopes.

Try it
main.tsp
import "@typespec/rest";
import "@typespec/versioning";
import "@azure-tools/typespec-azure-core";
import "@azure-tools/typespec-azure-resource-manager";
using Http;
using Rest;
using Versioning;
using Azure.Core;
using Azure.ResourceManager;
/** Contoso Resource Provider management API. */
@armProviderNamespace
@service(#{ title: "ContosoProviderHubClient" })
@versioned(Versions)
namespace Microsoft.ContosoProviderHub;
/** Contoso API versions */
enum Versions {
/** 2021-10-01-preview version */
@armCommonTypesVersion(Azure.ResourceManager.CommonTypes.Versions.v5)
`2021-10-01-preview`,
}
/** A ContosoProviderHub resource */
model Employee is ExtensionResource<EmployeeProperties> {
...ResourceNameParameter<Employee>;
}
/** Employee properties */
model EmployeeProperties {
/** Age of employee */
age?: int32;
/** City of employee */
city?: string;
/** Profile of employee */
@encode("base64url")
profile?: bytes;
/** The status of the last operation. */
@visibility(Lifecycle.Read)
provisioningState?: ProvisioningState;
}
/** The provisioning state of a resource. */
@lroStatus
union ProvisioningState {
ResourceProvisioningState,
/** The resource is being provisioned */
Provisioning: "Provisioning",
/** The resource is updating */
Updating: "Updating",
/** The resource is being deleted */
Deleting: "Deleting",
/** The resource create request has been accepted */
Accepted: "Accepted",
string,
}
interface Operations extends Azure.ResourceManager.Operations {}
/** Defines all the operations for an employee resource
* Scope parameter allows the same set of operations to be defined for
* different scopes (tenant, subscription, resource group, resource, managementGroup, etc.)
*/
interface EmplOps<Scope extends Azure.ResourceManager.Foundations.SimpleResource> {
get is Extension.Read<Scope, Employee>;
create is Extension.CreateOrReplaceAsync<Scope, Employee>;
update is Extension.CustomPatchSync<
Scope,
Employee,
Azure.ResourceManager.Foundations.ResourceUpdateModel<Employee, EmployeeProperties>
>;
delete is Extension.DeleteWithoutOkAsync<Scope, Employee>;
list is Extension.ListByTarget<Scope, Employee>;
move is Extension.ActionSync<Scope, Employee, MoveRequest, MoveResponse>;
}
/** Virtual resource for a virtual machine */
alias VirtualMachine = Extension.ExternalResource<
"Microsoft.Compute",
"virtualMachines",
"vmName",
NamePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,80}$",
Description = "The name of the virtual machine"
>;
alias Scaleset = Extension.ExternalResource<
"Microsoft.Compute",
"virtualMachineScaleSets",
"scaleSetName",
NamePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,80}$",
Description = "The name of the virtual machine scale set"
>;
alias VirtualMachineScaleSetVm = Extension.ExternalChildResource<
Scaleset,
"virtualMachineScaleSetVms",
"scaleSetVmName",
NamePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,80}$",
Description = "The name of the virtual machine scale set VM"
>;
alias ServiceGroup = Extension.ExternalResource<
"Microsoft.Management",
"serviceGroups",
"serviceGroupName",
NamePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,80}$",
Description = "The name of the service fabric cluster",
ParentType = "Tenant"
>;
/**
* Operations over any scope
* routed at: {scope}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface Employees extends EmplOps<Extension.ScopeParameter> {}
/**
* Tenant-level operations
* routed at: /providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface Tenants extends EmplOps<Extension.Tenant> {}
/**
* Subscription-level operations
* routed at: /subscriptions/{subscriptionId}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface Subscriptions extends EmplOps<Extension.Subscription> {}
/**
* Resource group-level operations
* routed at: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface ResourceGroups extends EmplOps<Extension.ResourceGroup> {}
/**
* Management group-level operations
* routed at: /providers/Microsoft.Management/managementGroups/{managementGroupId}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface ManagementGroups extends EmplOps<Extension.ManagementGroup> {}
/**
* Service group level operations
* routed at: /providers/Microsoft.Management/serviceGroups/{serviceGroupName}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface ServiceGroups extends EmplOps<ServiceGroup> {}
/**
* Operations over virtual machines
* routed at: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachines/{vmName}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface VirtualMachines extends EmplOps<VirtualMachine> {}
/**
* Operations over scale set VMs
* routed at: /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{scaleSetName}/virtualMachineScaleSetVms/{scaleSetVmName}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface ScaleSetVms extends EmplOps<VirtualMachineScaleSetVm> {}
/** Employee move request */
model MoveRequest {
/** The moving from location */
from: string;
/** The moving to location */
to: string;
}
/** Employee move response */
model MoveResponse {
/** The status of the move */
movingStatus: string;
}
alias GenericResourceParameters = {
...ApiVersionParameter;
...SubscriptionIdParameter;
...ResourceGroupParameter;
/** the provider namespace */
@path
@segment("providers")
@key
providerNamespace: string;
/** the resource type of the parent */
@path @key parentType: string;
/** the name of the parent resource */
@path @key parentName: string;
/** the resource type of the target resource */
@path @key resourceType: string;
/** the name of the target resource */
@path @key resourceName: string;
};
alias ParentParameters = {
...Extension.ExtensionProviderNamespace<Employee>;
...ParentKeysOf<Employee>;
};
#suppress "@azure-tools/typespec-azure-resource-manager/arm-resource-interface-requires-decorator"
interface GenericOps
extends Azure.ResourceManager.Legacy.ExtensionOperations<
GenericResourceParameters,
ParentParameters,
{
...Extension.ExtensionProviderNamespace<Employee>,
...KeysOf<Employee>,
}
> {}
/**
* Generic Resource Operations
* routed at /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{providerNamespace}/{parentType}/{parentName}/{resourceType}/{resourceName}/providers/Microsoft.ContosoProviderHub/employees
*/
@armResourceOperations
interface GenericResources {
get is GenericOps.Read<Employee>;
create is GenericOps.CreateOrUpdateAsync<Employee>;
update is GenericOps.CustomPatchSync<
Employee,
Azure.ResourceManager.Foundations.ResourceUpdateModel<Employee, EmployeeProperties>
>;
delete is GenericOps.DeleteWithoutOkAsync<Employee>;
list is GenericOps.List<Employee>;
}