导航Hello World自定义资源提供程序示例

 

适用于:Windows Azure Pack

示例自定义资源提供程序作为用于Windows服务器开发人员工具包的 Windows Azure 包的一部分提供。 有关详细信息,请参阅 https://www.microsoft.com/en-us/download/details.aspx?id=41146

Hello World示例为租户用户界面扩展的管理员和管理门户以及所需的终结点实现提供管理门户。 作为基于网站的 MVC ASP.NET 示例,控制器提供允许访问资源提供程序资源的终结点定义。 它们还用于代表管理门户用户界面调用服务管理 API。 自定义资源提供程序管理门户管理员用户界面扩展表示为视图。

Windows Azure Pack Hello World Sample

用户界面扩展和控制器

示例用户界面为管理员的管理门户和租户的管理门户提供扩展。 Internet Explorer 提供了基于客户端 JavaScript 的用户界面。 示例项目包括:

项目

说明

Microsoft.WAP.Samples.HelloWorld.AdminExtension

管理员扩展的管理门户

Microsoft.WAP.Samples.HelloWorld.TenantExtension

租户扩展的管理门户

有关如何创建客户端用户界面的详细信息,请参阅 Windows Azure Pack 管理门户用户界面扩展

在这些项目中,服务器端 MVC 控制器用于与服务管理 API 交互。 控制器源文件为

文件

说明

HelloWorldAdminController.cs

提供对资源提供程序管理员终结点、AdminSettingsController.cs 和其他 Microsoft.WAP.Samples.HelloWorld.API 中的访问权限。

HelloWorldTenantController.cs

提供对 Microsoft.WAP.Samples.HelloWorld.API 中的 Resource Provider 租户终结点 FileShareController.cs 的访问权限。

这两个控制器都使用 Microsoft.WAP.Samples.HelloWorld.APIClient 中的 HelloWorldClient.cs 将调用通过服务管理 API 路由到Hello World自定义资源提供程序。

资源提供程序终结点实现

这四个 REST 终结点在Hello World示例项目 Microsoft.WAP.HelloWorld.API 中作为服务器端控制器实现。 有 7 个控制器表示管理员和租户终结点。 FileShareController.cs 表示租户终结点,而另一些则表示管理员终结点。

跟踪对资源提供程序终结点的用户扩展调用Client-Side

客户端管理门户用户活动导致调用其中一个资源提供程序终结点的过程如下所示。 在此示例中,租户使用租户用户界面扩展的自定义管理门户快速创建文件共享。

  1. 租户选择“快速创建文件共享”。 处理了客户端 In HelloWorldTenantController.js这会导致 REST 调用服务器端在 HelloWorldTenantController.cs 中创建FileShare

    public JsonResult CreateFileShare(string subscriptionId, FileShareModel fileShareToCreate)
        {
            this.apiClient.CreateFileShare(subscriptionId, fileShareToCreate.ToApiObject());
    
            return this.Json(fileShareToCreate);
        }
    
  2. CreateFileShare 在 HelloWorldClient.cs 中定义的 API 客户端帮助程序类中调用 CreateFileShare — Microsoft.WAP.Samples.HelloWorld.APIClient。 此函数用于在Hello World资源提供程序上生成创建文件共享租户 REST API 调用的 URI,并通过服务管理 API 进行调用。

    public void CreateFileShare(string subscriptionId, FileShare fileShareNameToCreate)
        {
            var requestUrl = this.CreateRequestUri(string.Format(CultureInfo.InvariantCulture, HelloWorldClient.FileShares, subscriptionId));
            this.Post<FileShare>(requestUrl, fileShareNameToCreate);
        }
    

    文件共享 REST 调用的相对路径在 HelloWorldClient.FileShares 中定义。 所有管理员和租户相对路径的定义位于 HelloWorldClient.cs 中。

  3. 若要生成完整的 URI,CreateRequestUri (HelloWorldClient) 还需要从 BaseEndpoint 获取的服务管理 API 终结点。

    private Uri CreateRequestUri(string relativePath, string queryString = "")
        {
            var endpoint = new Uri(this.BaseEndpoint, relativePath);
            var uriBuilder = new UriBuilder(endpoint);
            uriBuilder.Query = queryString;
            return uriBuilder.Uri;
        }
    

    BaseEndpoint 在 HelloWorldClient 类构造函数中分配,该构造函数是在 HelloWorldTenantController 类构造函数中创建的。 rdfeEndpoint 是服务管理 API 终结点。

    public HelloWorldTenantController()
        {
            // 
            var rdfeEndpoint = new Uri(AppManagementConfiguration.Instance.RdfeUnifiedManagementServiceUri);
    
            var handler = new BearerMessageProcessingHandler();
    
            this.apiClient = new HelloWorldClient(rdfeEndpoint, handler);           
        }    
    
  4. 对此的调用。在 CreateFileShare (HelloWorldClient.cs 中发布) 将 REST 调用与随附的文件共享名称请求正文一起发送到服务管理 API,该 API 将调用路由到Hello World自定义资源提供程序。

  5. hello world 自定义提供程序租户终结点在 FileShareController.cs 中接收创建文件共享调用,并执行相应的操作。

    [HttpPost]
    public void CreateFileShare(FileShare fileShare)
    {
        fileShares.Add(new FileShare
        {
            Id = fileShares.Count,
            FileServerName = fileShare.FileServerName,
            Name = fileShare.Name,
            SubscriptionId = fileShare.SubscriptionId,
            Size = fileShare.Size
        };
    }
    

注册Hello World资源提供程序

hello world 示例项目 Microsoft.WAP.Samples.HelloWorld.Setup 提供客户提供商的安装程序和注册。 它记录在部署 Windows Azure 包管理门户扩展中。 与注册自定义资源提供程序特别相关的是向 Windows Azure Pack 注册终结点。 hello world 示例使用Windows PowerShell脚本 (Register-ResourceProvider.ps1) 来注册自定义资源提供程序终结点。 这为 Windows Azure Pack 提供了通过服务管理 API 正确传递到Hello World自定义资源提供程序的 REST 调用所需的信息。 在脚本中,Windows Azure Pack PowerShell cmdlet 执行注册。 rpSettings 参数提供用于注册终结点和其他配置信息的必要终结点信息。

$rpSettings = @{
    'Name' = $rpName;
    'DisplayName' = 'Hello World';
    'InstanceDisplayName' = 'Hello World';
    'AdminForwardingAddress' = "http://$hostName/admin";
    'AdminAuthenticationMode' = 'Basic';
    'AdminAuthenticationUserName' = $userName;
    'AdminAuthenticationPassword' = $password;        
    'TenantForwardingAddress' = "http://$hostName/";
    'TenantAuthenticationMode' = 'Basic';
    'TenantAuthenticationUserName' = $userName;
    'TenantAuthenticationPassword' = $password;
    'TenantSourceUriTemplate' = '{subid}/services/helloworld/{*path}';
    'TenantTargetUriTemplate' = 'subscriptions/{subid}/{*path}';
    'NotificationForwardingAddress' = "http://$hostName/admin";
    'NotificationAuthenticationMode' = 'Basic';
    'NotificationAuthenticationUserName' = $userName;
    'NotificationAuthenticationPassword' = $password;
}

Write-Host -ForegroundColor Green "Create new resource provider '$rpName'..."
$rp = New-ResourceProvider @rpSettings
Write-Host -ForegroundColor Green "Created new resource provider '$rpName'."

另请参阅

Windows Azure Pack 自定义资源提供程序