fix: 避免创建磁盘时显示provider disable底下的存储

This commit is contained in:
ioito
2019-08-27 17:26:22 +08:00
parent 49c1ce6cba
commit dd67f68485
2 changed files with 30 additions and 4 deletions
+12
View File
@@ -395,6 +395,18 @@ func (manager *SDiskManager) ValidateCreateData(ctx context.Context, userCred mc
}
storage := storageObj.(*SStorage)
if provider := storage.GetCloudprovider(); provider != nil {
if !provider.Enabled {
return nil, httperrors.NewInputParameterError("provider %s(%s) is disabled, you need enable provider first", provider.Name, provider.Id)
}
if !utils.IsInStringArray(provider.Status, api.CLOUD_PROVIDER_VALID_STATUS) {
return nil, httperrors.NewInputParameterError("invalid provider %s(%s) status %s, require status is %s", provider.Name, provider.Id, provider.Status, api.CLOUD_PROVIDER_VALID_STATUS)
}
if !utils.IsInStringArray(provider.HealthStatus, api.CLOUD_PROVIDER_VALID_HEALTH_STATUS) {
return nil, httperrors.NewInputParameterError("invalid provider %s(%s) health status %s, require status is %s", provider.Name, provider.Id, provider.HealthStatus, api.CLOUD_PROVIDER_VALID_HEALTH_STATUS)
}
}
host := storage.GetMasterHost()
if host == nil {
return nil, httperrors.NewResourceNotFoundError("storage %s(%s) need onlne and attach host for create disk", storage.Name, storage.Id)
+18 -4
View File
@@ -1132,11 +1132,25 @@ func (manager *SStorageManager) ListItemFilter(ctx context.Context, q *sqlchemy.
if jsonutils.QueryBoolean(query, "usable", false) {
hostStorageTable := HoststorageManager.Query().SubQuery()
hostTable := HostManager.Query().SubQuery()
sq := hostStorageTable.Query(hostStorageTable.Field("storage_id")).Join(hostTable,
sqlchemy.Equals(hostTable.Field("id"), hostStorageTable.Field("host_id"))).
Filter(sqlchemy.Equals(hostTable.Field("host_status"), api.HOST_ONLINE))
sq1 := hostStorageTable.Query(hostStorageTable.Field("storage_id")).
Join(hostTable, sqlchemy.Equals(hostTable.Field("id"), hostStorageTable.Field("host_id"))).
Filter(sqlchemy.Equals(hostTable.Field("host_status"), api.HOST_ONLINE)).
Filter(sqlchemy.IsTrue(hostTable.Field("enabled"))).
Filter(sqlchemy.IsNullOrEmpty(hostTable.Field("manager_id")))
q = q.Filter(sqlchemy.In(q.Field("id"), sq)).
providerTable := CloudproviderManager.Query().SubQuery()
sq2 := hostStorageTable.Query(hostStorageTable.Field("storage_id")).
Join(hostTable, sqlchemy.Equals(hostTable.Field("id"), hostStorageTable.Field("host_id"))).
Join(providerTable, sqlchemy.Equals(hostTable.Field("manager_id"), providerTable.Field("id"))).
Filter(sqlchemy.IsTrue(providerTable.Field("enabled"))).
Filter(sqlchemy.In(providerTable.Field("status"), api.CLOUD_PROVIDER_VALID_STATUS)).
Filter(sqlchemy.In(providerTable.Field("health_status"), api.CLOUD_PROVIDER_VALID_HEALTH_STATUS))
q = q.Filter(
sqlchemy.OR(
sqlchemy.In(q.Field("id"), sq1),
sqlchemy.In(q.Field("id"), sq2),
)).
Filter(sqlchemy.In(q.Field("status"), []string{api.STORAGE_ENABLED, api.STORAGE_ONLINE})).
Filter(sqlchemy.IsTrue(q.Field("enabled")))
}