使用 C# .NET 的 EMC Isilon OneFS REST API
使用 C# .NET 的 Isilon OneFS REST API 基本介绍
引言
本文简要介绍了如何使用 C# .NET 访问 Isilon OneFS 7.2 REST API。
背景
我正在为 EMC 的 Isilon 横向扩展 NAS 平台开发多个自动化和可视化应用程序,但找不到太多关于如何在 .NET 中访问 API 的信息。 虽然本文中的某些概念非常基础,但它们可以用作快速启动并使用 Isilon REST API 的起点。 这绝不是 API 的完整实现,只是一个简要概述。 要使用以下方法和代码,您将需要一个物理 Isilon 集群,或者您可以针对 OneFS 模拟器(虚拟 Isilon 集群)执行测试,该模拟器可以在 http://www.emc.com/products-solutions/trial-software-download/isilon.htm?PID=SWD_isilon_trialsoftware 上找到。 我用来创建此项目的环境由运行在 VMware Workstation 上的虚拟 Isilon 集群组成。
API 工作流程
API 的主要组件是身份验证/授权,它确定提供的用户凭据是否有效,以及是否授权使用 API。 两个子组件是平台和命名空间部分。 平台部分提供访问权限以查看和修改集群的配置,命名空间部分提供对集群上的文件系统和文件的访问权限,具体取决于您的访问级别。 注意:如果要使用 API 对集群进行配置更改,强烈建议您将所有请求发送到单个节点以避免配置冲突。
基本身份验证
您可以通过两种方式对 API 进行身份验证。 首先是使用基本身份验证,这意味着您必须在每个请求的 HTTP 标头中包含凭据。 每个请求都必须针对集群进行身份验证,这会产生一些开销。 以下代码示例显示了如何执行基本身份验证
string IsilonServerAddress = "https://10.20.30.160:8080";
string ResourceString = "/platform/1/dedupe/dedupe-summary";
string UserName = "*username*";
string Password = "*password*";
// Create a web request object pointing to the Isilon server and Resource String
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);
// Add the Authroization header for Basic authentication
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(UserName + ":" + Password)));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
该代码创建一个 HTTP Web Request 对象并指定集群节点的 IP 地址。 它还指定要获取的资源,在本例中为 /platform/1/dedupe/dedupe-summary,它是平台部分的一部分。 此 API 调用将检索包含有关集群重复数据删除统计信息的响应。
返回的是一个包含 JavaScript 对象表示法 (JSON) 格式的请求数据的页面,如下所示
{ "summary" : { "block_size" : 8192, "estimated_physical_blocks" : 0.0, "estimated_saved_blocks" : 0.0, "logical_blocks" : 0.0, "saved_logical_blocks" : 0.0, "total_blocks" : 18609648, "used_blocks" : 1406655.0 } }
会话身份验证
对 Isilon API 进行身份验证的第二种方法是使用会话 cookie。 在此方法中,我们改为使用 HTTP POST 命令将 JSON 身份验证对象传递给 Isilon API。 来自集群的响应将包含会话 cookie,该 cookie 可用于在一段时间内访问集群上的资源。 它还指定此 cookie 的有效时长。
// Create a web request object pointing to the Isilon server and Resource String
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);
request.Method = "POST";
request.ContentType = "application/json";
// Build the JSON authentication request object
StringBuilder builder = new StringBuilder();
builder.Append("{\r\n");
builder.Append("\"username\": \"" + UserName + "\", \r\n");
builder.Append("\"password\": \"" + Password + "\",\r\n");
builder.Append("\"services\": [\"platform\"] \r\n");
builder.Append("}");
// Write the authentication request object to the request stream
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(builder.ToString());
writer.Flush();
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// If successful, Isilon will set a Set-Cookie header on the response, which is our cookie
string cookie = response.Headers["Set-Cookie"];
response.Close();
现在我们有了 cookie,我们可以使用它来请求与前一个示例中相同的重复数据删除统计信息。 这次我们将 cookie 放在请求流上,以根据集群验证请求。 响应将是与前一个示例中返回的相同的 JSON 对象。
// Request the dedupe-summary
ResourceString = "/platform/1/dedupe/dedupe-summary";
// Create a web request object pointing to the Isilon server and Resource String
request = (HttpWebRequest)WebRequest.Create(IsilonServerAddress + ResourceString);
// Set the cookie we received onto the request object
request.Headers.Add("Cookie", cookie);
response = (HttpWebResponse)request.GetResponse();
JSON 序列化
现在我们已成功连接、验证并可以从集群请求资源,下一步是将数据序列化为 .NET 对象,以便我们在应用程序中使用。 上面的示例返回一个 Summary 类型的对象,有关详细信息,请参阅 EMC 提供的 API 参考文档。 以下是该对象的属性
要将返回序列化为 .NET 对象,我们需要定义一个具有适当数据协定定义的 .NET 类。
[DataContract]
public class DeduplicationStats
{
[DataMember(Name = "block_size")]
public int BlockSize;
[DataMember(Name = "estimated_physical_blocks")]
public int EstimatedPhysicalBlocks;
[DataMember(Name = "estimated_saved_blocks")]
public int EstimatedSavedBlocks;
[DataMember(Name = "logical_blocks")]
public int LogicalBlocks;
[DataMember(Name = "saved_logical_blocks")]
public int SavedLogicalBlocks;
[DataMember(Name = "total_blocks")]
public int TotalBlocks;
[DataMember(Name = "used_blocks")]
public int UsedBlocks;
}
我们还需要一个类定义,因为从集群返回的对象被包装到一个空白对象中。 因此,我们创建一个通用对象 (IsilonReturn),其中包含上面定义的 DeduplicationStats 对象。
[DataContract]
public class IsilonResponse
{
[DataMember(Name = "summary")]
public DeduplicationStats DedupeStats;
}
现在我们可以使用作为 System.Runtime.Serialization.Json 命名空间一部分的 DataContractJsonSerializer 对象将 JSON 对象序列化为 IsilonResponse 对象。 以下代码完成后,您可以访问该对象以获取集群重复数据删除统计信息。
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(IsilonResponse));
IsilonResponse stats = (IsilonResponse)js.ReadObject(response.GetResponseStream());
结论
这是一个简单的示例,说明如何使用 C# 和 .NET 框架连接到 Isilon 集群并针对其运行基本命令。 API 包含丰富的功能,包括配置和文件系统访问。 您可以使用 API 执行审核、配置协议、作业、池、配额和快照设置等配置任务,以及目录和文件级别的操作。
希望您发现此信息有用!
历史
2015 年 1 月:第一个版本