@@ -13,6 +13,25 @@ const createConnection = async (connection) => {
1313 )
1414}
1515
16+ const createAndAttachConnection = async ( connection , domainId ) => {
17+ const allConnections = await engineGet ( 'api/storageconnections' )
18+ const connectionsInList = allConnections . storage_connection . filter ( ( con ) => con . address === connection . address )
19+ if ( connectionsInList . length === 1 ) {
20+ await attachConnection ( connectionsInList [ 0 ] . id , domainId )
21+ } else {
22+ const createdConnection = await createConnection ( connection )
23+ await attachConnection ( createdConnection . id , domainId )
24+ }
25+ }
26+
27+ const findConnections = async ( ip , hostId , port ) => {
28+ const connectionsFoundJson = await enginePost (
29+ `api/hosts/${ hostId } /discoveriscsi` ,
30+ JSON . stringify ( { iscsi : { address : ip , port : port } } )
31+ )
32+ return connectionsFoundJson . discovered_targets . iscsi_details
33+ }
34+
1635const editConnection = async ( connection , connectionId ) => {
1736 return await enginePut (
1837 `api/storageconnections/${ connectionId } ` ,
@@ -36,13 +55,17 @@ const detachConnection = async (connectionId, domainId) => {
3655}
3756
3857const fetchData = async ( stgDomain ) => {
58+ const headers = {
59+ 'Cache-Control' : 'no-cache' ,
60+ }
3961 if ( ! stgDomain ?. id || ! stgDomain ?. dataCenterId ) {
4062 throw new Error ( 'StorageConnectionsDataProvider: invalid Storage Domain' )
4163 }
42- const [ allConnectionsJson , storageDomain , domainConnectionsJson ] = await Promise . all ( [
64+ const [ allConnectionsJson , storageDomain , domainConnectionsJson , hostsJson ] = await Promise . all ( [
4365 engineGet ( 'api/storageconnections' ) ,
4466 engineGet ( `api/datacenters/${ stgDomain . dataCenterId } /storagedomains/${ stgDomain . id } ` ) ,
4567 engineGet ( `api/storagedomains/${ stgDomain . id } /storageconnections` ) ,
68+ engineGet ( 'api/hosts' , headers ) ,
4669 ] )
4770
4871 if ( ! storageDomain ) {
@@ -51,6 +74,7 @@ const fetchData = async (stgDomain) => {
5174
5275 const allConnections = allConnectionsJson ?. storage_connection
5376 const domainConnections = domainConnectionsJson ?. storage_connection
77+ const hosts = hostsJson ?. host . filter ( host => host . status === 'up' )
5478
5579 if ( ! allConnections || allConnections . error ) {
5680 throw new Error ( 'StorageConnectionsDataProvider: failed to fetch storage connections' )
@@ -64,16 +88,17 @@ const fetchData = async (stgDomain) => {
6488 const domainConnectionsIds = new Set ( domainConnections . map ( connection => connection . id ) )
6589 const allConnectionsByTypeSorted = allConnections
6690 . filter ( connection => connection . type === storageDomain . storage ?. type )
67- . map ( ( conn ) => {
68- return { ...conn , isAttachedToDomain : domainConnectionsIds . has ( conn . id ) }
91+ . map ( ( connection ) => {
92+ return { ...connection , isAttachedToDomain : domainConnectionsIds . has ( connection . id ) }
6993 } )
70- . sort ( ( conn1 , conn2 ) => {
71- return ( conn1 . isAttachedToDomain === conn2 . isAttachedToDomain ) ? 0 : conn1 . isAttachedToDomain ? - 1 : 1
94+ . sort ( ( firstConnection , secondConnection ) => {
95+ return ( firstConnection . isAttachedToDomain === secondConnection . isAttachedToDomain ) ? 0 : firstConnection . isAttachedToDomain ? - 1 : 1
7296 } )
7397
7498 return {
7599 storageDomain : storageDomain ,
76100 connections : allConnectionsByTypeSorted ,
101+ hosts : hosts ,
77102 }
78103}
79104
@@ -93,17 +118,20 @@ const StorageConnectionsDataProvider = ({ children, storageDomain }) => (
93118 return React . cloneElement ( child , { isLoading : true } )
94119 }
95120
96- const { storageDomain, connections } = data
121+ const { storageDomain, connections, hosts } = data
97122
98123 return React . cloneElement ( child , {
99124 storageDomain,
100125 connections,
126+ hosts,
101127 lastUpdated,
102128 doConnectionCreate : createConnection ,
129+ doConnectionCreateAndAttach : createAndAttachConnection ,
103130 doConnectionEdit : editConnection ,
104131 doConnectionDelete : deleteConnection ,
105132 doConnectionAttach : attachConnection ,
106133 doConnectionDetach : detachConnection ,
134+ doConnectionSearch : findConnections ,
107135 doRefreshConnections : ( ) => { fetchAndUpdateData ( ) } ,
108136 } )
109137 } }
0 commit comments