[英]Openstack.Net SDK cannot access service with region
使用我們自己的硬件,我們為所有組件安裝了vanilla openstack,但是由於地區問題,我在訪問身份以外的服務時遇到了問題。 所使用的代碼如下所示,並使用我們創建的admin帳戶和admin tennant進行調用...
public static void TestAccess(string userName, string password, string projectName, string projectId)
{
try
{
Uri baseUrl = new Uri(URL_IDENTITY);
CloudIdentityWithProject projectCloudId = new CloudIdentityWithProject();
projectCloudId.Username = userName;
projectCloudId.Password = password;
projectCloudId.ProjectName = projectName;
projectCloudId.ProjectId = new ProjectId(projectId);
OpenStackIdentityProvider idProvider = new OpenStackIdentityProvider(baseUrl, projectCloudId);
UserAccess userAccess = idProvider.Authenticate(projectCloudId);
IEnumerable<ExtendedEndpoint> eps = idProvider.ListEndpoints(userAccess.Token.Id);
string reg = idProvider.DefaultRegion; // This is null
ServiceCatalog[] scs = userAccess.ServiceCatalog;
// Get the list of regions
regionList = new List<string>();
foreach (ServiceCatalog sc in scs)
{
foreach (Endpoint ep in sc.Endpoints)
{
regionList.Add(ep.Region); // This is 'regionOne' in every case
}
}
// Try stuff...
foreach(string region in regionList.Distinct())
{
// Get a list of containers
CloudFilesProvider cfp = new CloudFilesProvider(projectCloudId, idProvider);
// THIS LINE FAILS
IEnumerable<Container> listOfContainers = cfp.ListContainers(region: region);
foreach (Container ctnr in listOfContainers)
{
Console.WriteLine("Container: {0}", ctnr.Name);
}
CloudNetworksProvider cnp = new CloudNetworksProvider(identity: null, identityProvider: idProvider);
IEnumerable<CloudNetwork> networks = cnp.ListNetworks(identity: null, region: region);
foreach (CloudNetwork network in networks)
{
Console.WriteLine("Network[{0}] name: {1}", networkCount, network.Label);
Console.WriteLine("Network[{0}] Id: {1}", networkCount, network.Id);
++networkCount;
}
Console.WriteLine("{0} networks listed.", networkCount);
}
}
catch(Exception ex)
{
throw;
}
}
該代碼在調用ListContainers(region:region)時失敗,並顯示以下錯誤...“用戶無權訪問所請求的服務或區域”,好像我沒有指定區域一樣,錯誤僅是“ No region”已提供,該服務不提供與區域無關的終結點,並且沒有為用戶帳戶設置默認區域
我們目前僅訪問內部網絡,因此區域對我們而言並不重要...
還要注意的是,在致電...
CloudNetwork detail = cnp.ShowNetwork(networkGuid, "regionOne");
我可以看到的網絡返回錯誤“找不到或不存在該項目”。
幫助和建議,不勝感激。
我設法相當簡單地擴展了Openstack.Net SDK的功能。 下面的代碼將其擴展為包括用於承租人/項目操縱的各種功能...
首先,創建一個NewTenant容器,該容器將用於與Web服務之間來回傳遞數據,我已將其放置在與其他服務相同的名稱空間中...
using Newtonsoft.Json;
namespace net.openstack.Core.Domain
{
[JsonObject(MemberSerialization.OptIn)]
public class NewTenant
{
/// <summary>
/// Gets the ID for the new user.
/// <note type="warning">The value of this property is not defined. Do not use.</note>
/// </summary>
[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.Include)]
public string Id { get; private set; }
[JsonProperty("name")]
public string Name { get; private set; }
[JsonProperty("description")]
public string Description { get; private set; }
[JsonProperty("enabled")]
public bool Enabled { get; private set; }
public NewTenant(string name, string description, bool enabled = true)
{
Name = name;
Description = description;
Enabled = enabled;
}
}
}
現在我們可以創建任何新的Request類來發布數據...
using System;
using Newtonsoft.Json;
using net.openstack.Core.Domain;
namespace net.openstack.Core.Request
{
[JsonObject(MemberSerialization.OptIn)]
internal class AddTenantRequest
{
[JsonProperty("tenant")]
public NewTenant Tenant { get; private set; }
public AddTenantRequest(NewTenant tenant)
{
if (tenant == null)
throw new ArgumentNullException("tenant");
Tenant = tenant;
}
}
}
現在為請求創建Response對象,以幫助檢索數據
using net.openstack.Core.Domain;
using Newtonsoft.Json;
namespace net.openstack.Core.Response
{
[JsonObject(MemberSerialization.OptIn)]
internal class NewTenantResponse
{
[JsonProperty("tenant")]
public NewTenant NewTenant { get; private set; }
}
[JsonObject(MemberSerialization.OptIn)]
internal class TenantResponse
{
[JsonProperty("tenant")]
public Tenant Tenant { get; private set; }
}
}
現在,我們可以創建一個繼承自OpenStackIdentityProvider的類,並具有我們想要的用於Tenant / Project操作的附加功能...
using System;
using System.Net;
using JSIStudios.SimpleRESTServices.Client;
using net.openstack.Core.Domain;
using net.openstack.Core.Request;
using net.openstack.Core.Response;
namespace net.openstack.Core.Providers
{
public class ExtendedOpenStackIdentityProvider : OpenStackIdentityProvider
{
public ExtendedOpenStackIdentityProvider(Uri urlBase)
: base(urlBase)
{
}
public ExtendedOpenStackIdentityProvider(Uri urlBase, CloudIdentity identity)
: base(urlBase, identity)
{
}
public ExtendedOpenStackIdentityProvider(Uri urlBase, JSIStudios.SimpleRESTServices.Client.IRestService restService, net.openstack.Core.Caching.ICache<UserAccess> tokenCache)
: base(urlBase, restService, tokenCache)
{
}
public ExtendedOpenStackIdentityProvider(Uri urlBase, CloudIdentity identity, JSIStudios.SimpleRESTServices.Client.IRestService restService, net.openstack.Core.Caching.ICache<UserAccess> tokenCache)
: base(urlBase, identity, restService, tokenCache)
{
}
public NewTenant AddTenant(NewTenant tenant, CloudIdentity identity)
{
if (tenant == null)
throw new ArgumentNullException("tenant");
if (string.IsNullOrEmpty(tenant.Name))
throw new ArgumentException("tenant.Name cannot be null or empty");
if (tenant.Id != null)
throw new InvalidOperationException("tenant.Id must be null");
CheckIdentity(identity);
var response = ExecuteRESTRequest<NewTenantResponse>(identity, new Uri(UrlBase, "/v2.0/tenants"), HttpMethod.POST, new AddTenantRequest(tenant));
if (response == null || response.Data == null)
return null;
return response.Data.NewTenant;
}
public Tenant GetTenant(string tenantId, CloudIdentity identity)
{
if (tenantId == null)
throw new ArgumentNullException("tenantId");
CheckIdentity(identity);
var urlPath = string.Format("v2.0/tenants/{0}", tenantId);
var response = ExecuteRESTRequest<TenantResponse>(identity, new Uri(UrlBase, urlPath), HttpMethod.GET);
if (response == null || response.Data == null)
return null;
return response.Data.Tenant;
}
public bool DeleteTenant(string tenantId, CloudIdentity identity)
{
if (tenantId == null)
throw new ArgumentNullException("tenantId");
if (string.IsNullOrEmpty(tenantId))
throw new ArgumentException("tenantId cannot be empty");
CheckIdentity(identity);
var urlPath = string.Format("v2.0/tenants/{0}", tenantId);
var response = ExecuteRESTRequest(identity, new Uri(UrlBase, urlPath), HttpMethod.DELETE);
if (response != null && response.StatusCode == HttpStatusCode.NoContent)
return true;
return false;
}
public bool AddTenantUserRole(string tenantId, string userId, string roleId, CloudIdentity identity)
{
if (tenantId == null)
throw new ArgumentNullException("tenantId");
if (string.IsNullOrEmpty(tenantId))
throw new ArgumentException("tenantId cannot be empty");
if (userId == null)
throw new ArgumentNullException("userId");
if (string.IsNullOrEmpty(userId))
throw new ArgumentException("userId cannot be empty");
if (roleId == null)
throw new ArgumentNullException("roleId");
if (string.IsNullOrEmpty(roleId))
throw new ArgumentException("roleId cannot be empty");
CheckIdentity(identity);
var urlPath = string.Format("v2.0/tenants/{0}/users/{1}/roles/OS-KSADM/{2}", tenantId, userId, roleId);
var response = ExecuteRESTRequest(identity, new Uri(UrlBase, urlPath), HttpMethod.PUT);
if (response != null && response.StatusCode == HttpStatusCode.NoContent)
return true;
return false;
}
}
}
我想這個功能很快就會出現在GitHub版本中,但是如果沒有,我希望它會有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.