const co = require('co');

const OneViewClient = require('..');
const config = require('./config');

// Add storage systems and storage pools into OneView
// Tested only again 3.00 OneView
module.exports = function addStorageSystem(ip) {
  return co(function*() {
    const client = new OneViewClient(ip, config.credential, true);
    yield client.login();

    const storageSystemAddresses = config['storage-systems'];
    let storageSystem;
    let task;
    for(let i = 0; i < storageSystemAddresses.length; i += 1) {
      const storageSystemAddress = storageSystemAddresses[i];
      const postRes = yield client.post({
        uri: '/rest/storage-systems',
        resolveWithFullResponse: true,
        body: {
          'ip_hostname': storageSystemAddress,
          'username': 'dcs',
          'password': 'dcs',
        },
      });
      console.log(`[${ip}] is posted, task: ${postRes.headers.location}`);
      storageSystem = yield client.waitTaskComplete(postRes.headers.location);
      console.log(`[${ip}] ${storageSystemAddress} is ${storageSystem.state}`);

      // Add storage-pools into storage-system
      const storagePools = storageSystem.unmanagedPools
        .filter(storagePool => storagePool.domain === config.storageSystemDomain)
        .map((storagePool) => {
          return {
            deviceType: storagePool.deviceType,
            domain: storagePool.domain,
            name: storagePool.name,
            type: 'StoragePool',
          };
        });
      const unmanagedPorts = storageSystem.unmanagedPorts.map((unmanagedPort) => {
        return {
          actualNetworkSanUri: unmanagedPort.actualNetworkSanUri,
          actualNetworkUri: unmanagedPort.actualNetworkUri,
          expectedNetworkUri: unmanagedPort.expectedNetworkUri,
          groupName: unmanagedPort.groupName,
          label: unmanagedPort.label,
          name: unmanagedPort.name,
          portName: unmanagedPort.portName,
          portWwn: unmanagedPort.portWwn,
          protocolType: unmanagedPort.protocolType,
          type: unmanagedPort.type,
        };
      });
      const putRes = yield client.put({
        uri: storageSystem.uri,
        resolveWithFullResponse: true,
        headers: {
          'If-Match': storageSystem.eTag,
        },
        body: {
          credentials: storageSystem.credentials,
          eTag: storageSystem.eTag,
          managedDomain: config.storageSystemDomain,
          managedPools: storagePools,
          managedPorts: [],
          name: storageSystem.name,
          serialNumber: storageSystem.serialNumber,
          type: storageSystem.type,
          unmanagedPorts,
          uri: storageSystem.uri,
        },
      });
      console.log(`[${ip}] storage-pools are put into storage-system, task: ${putRes.headers.location}`);
      storageSystem = yield client.waitTaskComplete(putRes.headers.location);
      console.log(`[${ip}] ${storageSystem.managedPools.length} storage-pools are in storageSystemUri`);
    }
  });
};

if (require.main === module) {
  module.exports(process.argv[2]).then(() => {
    console.log('Done');
  }).catch((err) => {
    console.error(`[${ip}] ${err.name} ${err.message}, stack:${err.stack}`);
  });
}

@@ -347,24 +347,126 @@
 t.login();%0A%0A
+    const existingStorageSystemList = yield client.get(%7B%0A      uri: '/rest/storage-systems',%0A    %7D);%0A%0A
     const st
@@ -678,16 +678,248 @@
 ses%5Bi%5D;%0A
+      const storageSystemArray = existingStorageSystemList.members.filter((storageSystem) =%3E %7B%0A        return storageSystem.credentials.ip_hostname === storageSystemAddress;%0A      %7D);%0A      if (storageSystemArray.length === 0 ) %7B%0A  
       co
@@ -956,24 +956,26 @@
 t(%7B%0A        
+  
 uri: '/rest/
@@ -988,24 +988,26 @@
 e-systems',%0A
+  
         reso
@@ -1037,32 +1037,34 @@
 ue,%0A        
+ 
+ 
 body: %7B%0A
           'i
@@ -1043,32 +1043,34 @@
         body: %7B%0A
+  
           'ip_ho
@@ -1110,16 +1110,18 @@
         
+ 
+ 
 'usernam
@@ -1123,32 +1123,34 @@
 ername': 'dcs',%0A
+  
           'passw
@@ -1162,35 +1162,39 @@
  'dcs',%0A        
+ 
+ 
 %7D,%0A
+  
       %7D);%0A      
@@ -1185,32 +1185,34 @@
       %7D);%0A      
+ 
+ 
 console.log(%60%5B$%7B
@@ -1256,32 +1256,34 @@
 rs.location%7D%60);%0A
+  
       storageSys
@@ -1343,32 +1343,34 @@
 ocation);%0A      
+ 
+ 
 console.log(%60%5B$%7B
@@ -1426,16 +1426,252 @@
 tate%7D%60);
+%0A      %7D else if (storageSystemArray.length === 1) %7B%0A        storageSystem = storageSystemArray%5B0%5D;%0A      %7D else %7B%0A        console.error(%60%5B$%7Bip%7D%5D two same storage system with same address $%7BJSON.stringify(storageSystemArray)%7D%60);%0A      %7D
 %0A%0A      
@@ -1734,24 +1734,34 @@
 ragePools = 
+%5B%5D.concat(
 storageSyste
@@ -1776,16 +1776,45 @@
 gedPools
+, storageSystem.managedPools)
 %0A       

Add existing StorageSystem check into addStorageSystem