//
// Copyright (c) Microsoft and contributors.  All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//

/**
* Build the create request payload based on the parameters provided from command line
*/
exports.createClusterPayloadWindows = function createClusterPayloadWindows(clusterName, storageAccountName, storageAccountKey, storageContainer, dataNodeCount, headNodeSize, dataNodeSize, location, userName, password) {
	  var clusterPayload = '<Resource xmlns="http://schemas.microsoft.com/windowsazure">';
      clusterPayload += '        <SchemaVersion>2.0</SchemaVersion>';
      clusterPayload += '        <IntrinsicSettings><ClusterCreateParameters z:Id="i1" xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">';
      clusterPayload += '            <DnsName>' + clusterName + '</DnsName>';
      clusterPayload += '            <Location>' + location + '</Location>';
      clusterPayload += '            <Version>3.1</Version>';
      clusterPayload += '            <ClusterRoleCollection>';
      clusterPayload += '              <ClusterRole z:Id="i2">';
      clusterPayload += '                <FriendlyName>ZkRole</FriendlyName>';
      clusterPayload += '                <InstanceCount>1</InstanceCount>';
      clusterPayload += '                <RemoteDesktopSettings z:Id="i3">';
      clusterPayload += '                  <AuthenticationCredential z:Id="i4"/>';
      clusterPayload += '                </RemoteDesktopSettings>';
      clusterPayload += '                <VMSize>Small</VMSize>';
      clusterPayload += '                <VMSizeAsString>Small</VMSizeAsString>';
      clusterPayload += '              </ClusterRole><ClusterRole z:Id="i5">';
      clusterPayload += '              <FriendlyName>HeadNodeRole</FriendlyName>';
      clusterPayload += '              <InstanceCount>2</InstanceCount>';
      clusterPayload += '              <RemoteDesktopSettings z:Id="i6">';
      clusterPayload += '                <AuthenticationCredential z:Id="i7"/>';
      clusterPayload += '              </RemoteDesktopSettings>';
      clusterPayload += '              <VMSize>Large</VMSize>';
      clusterPayload += '              <VMSizeAsString>' + headNodeSize + '</VMSizeAsString>';
      clusterPayload += '            </ClusterRole>';
      clusterPayload += '            <ClusterRole z:Id="i8">';
      clusterPayload += '              <FriendlyName>WorkerNodeRole</FriendlyName>';
      clusterPayload += '              <InstanceCount>' + dataNodeCount + '</InstanceCount>';
      clusterPayload += '              <RemoteDesktopSettings z:Id="i9">';
      clusterPayload += '                <AuthenticationCredential z:Id="i10"/>';
      clusterPayload += '              </RemoteDesktopSettings>';
      clusterPayload += '              <VMSize>Large</VMSize>';
      clusterPayload += '              <VMSizeAsString>' + dataNodeSize + '</VMSizeAsString>';
      clusterPayload += '            </ClusterRole>';
      clusterPayload += '          </ClusterRoleCollection>';
      clusterPayload += '          <Components>';
      clusterPayload += '            <ClusterComponent z:Id="i11" i:type="GatewayComponent">';
      clusterPayload += '              <RestAuthCredential z:Id="i12">';
      clusterPayload += '                <Password>' + password + '</Password>';
      clusterPayload += '                <Username>' + userName + '</Username>';
      clusterPayload += '              </RestAuthCredential>';
      clusterPayload += '            </ClusterComponent><ClusterComponent z:Id="i13" i:type="YarnComponent">';
      clusterPayload += '            <Applications><YarnApplication z:Id="i14" i:type="MapReduceApplication">';
      clusterPayload += '              <AdditionalStorageContainers/>';
      clusterPayload += '              <CapacitySchedulerConfiguration/>';
      clusterPayload += '              <DefaultStorageAccountAndContainer z:Id="i15">';
      clusterPayload += '                <AccountDnsName>' + storageAccountName + '</AccountDnsName>';
      clusterPayload += '                <BlobContainerName>' + storageContainer + '</BlobContainerName>';
      clusterPayload += '                <Key>' + storageAccountKey + '</Key>';
      clusterPayload += '              </DefaultStorageAccountAndContainer>';
      clusterPayload += '              <MapRedSiteXmlProperties/>';
      clusterPayload += '            </YarnApplication>';
      clusterPayload += '          </Applications>';
      clusterPayload += '          <CapacitySchedulerConfiguration/>';
      clusterPayload += '          <Configuration/>';
      clusterPayload += '          <NodeManagerRole z:Ref="i8"/>';
      clusterPayload += '          <ResourceManagerRole z:Ref="i5"/>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '        <ClusterComponent z:Id="i16" i:type="OozieComponent">';
      clusterPayload += '          <Configuration/>';
      clusterPayload += '          <HeadNodeRole z:Ref="i5"/>';
      clusterPayload += '          <Metastore z:Id="i17">';
      clusterPayload += '            <ShouldProvisionNew>true</ShouldProvisionNew>';
      clusterPayload += '          </Metastore>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '        <ClusterComponent z:Id="i18" i:type="HiveComponent">';
      clusterPayload += '          <HeadNodeRole z:Ref="i5"/>';
      clusterPayload += '          <HiveSiteXmlProperties/>';
      clusterPayload += '          <Metastore z:Id="i19">';
      clusterPayload += '            <ShouldProvisionNew>true</ShouldProvisionNew>';
      clusterPayload += '          </Metastore>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '        <ClusterComponent z:Id="i20" i:type="HdfsComponent">';
      clusterPayload += '          <HdfsSiteXmlProperties/>';
      clusterPayload += '          <HeadNodeRole z:Ref="i5"/>';
      clusterPayload += '          <WorkerNodeRole z:Ref="i8"/>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '        <ClusterComponent z:Id="i21" i:type="HadoopCoreComponent">';
      clusterPayload += '          <CoreSiteXmlProperties/>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '        <ClusterComponent z:Id="i22" i:type="ZookeeperComponent">';
      clusterPayload += '          <ZookeeperRole z:Ref="i2"/>';
      clusterPayload += '        </ClusterComponent>';
      clusterPayload += '      </Components>';
      clusterPayload += '      </ClusterCreateParameters></IntrinsicSettings>';
      clusterPayload += '      <ExtensionData>';
      clusterPayload += '      </ExtensionData>';
      clusterPayload += '      </Resource>';
	  
	  return clusterPayload;
};

/**
* Build the create request payload based on the parameters provided from command line
*/
exports.createClusterPayloadLinux = function createClusterPayloadLinux(clusterName, storageAccountName, storageAccountKey, storageContainer, dataNodeCount, headNodeSize, dataNodeSize, location, userName, password, sshUserName, sshPassword, subscriptionId) {
	  var clusterPayload = '<Resource xmlns="http://schemas.microsoft.com/windowsazure">';
      clusterPayload += ' <SchemaVersion>1.0</SchemaVersion><IntrinsicSettings><IaasCluster xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management"><ApiVersion xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">1.0</ApiVersion><DeploymentDocuments xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management"><a:KeyValueOfstringstring><a:Key>AmbariConfiguration</a:Key><a:Value>{';
      clusterPayload += ' "blueprint": "hadoop",';
      clusterPayload += ' "default_password": "' + password + '",';
      clusterPayload += ' "configurations": [';
        clusterPayload += ' {';
          clusterPayload += ' "core-site":{';
            clusterPayload += ' "fs.defaultFS": "wasb://' + storageContainer + '@' + storageAccountName + '",';
            clusterPayload += ' "fs.azure.account.key.' + storageAccountName + '": "' + storageAccountKey + '"';
          clusterPayload += ' }';
        clusterPayload += ' }';
      clusterPayload += ' ],';
      clusterPayload += ' "host_groups": [';
        clusterPayload += ' {';
          clusterPayload += ' "name": "headnode"';
        clusterPayload += ' },';
        clusterPayload += ' {';
          clusterPayload += ' "name": "workernode"';
        clusterPayload += ' }';
      clusterPayload += ' ]';
      clusterPayload += ' }</a:Value></a:KeyValueOfstringstring><a:KeyValueOfstringstring><a:Key>CsmDocument</a:Key><a:Value>{';
        clusterPayload += ' "location": "' + location + '",';
        clusterPayload += ' "resources": [';
            clusterPayload += ' {';
               clusterPayload += ' "name": "gateway",';
                clusterPayload += ' "type": "VirtualMachineGroup",';
                clusterPayload += ' "location": "' + location + '",';
                clusterPayload += ' "properties": {';
                    clusterPayload += ' "instanceCount": 1,';
                    clusterPayload += ' "dnsName": "' + clusterName + '",';
                    clusterPayload += ' "osProfile": {';
                        clusterPayload += ' "computerNamePattern": "gateway\#\#\#\#",';
                        clusterPayload += ' "windowsOperatingSystemProfile": {';
                            clusterPayload += ' "adminUsername": "hdiuser",';
                            clusterPayload += ' "adminPassword": "",';
                            clusterPayload += ' "customData": "",';
                            clusterPayload += ' "storedCertificateSettingsProfile": []';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "hardwareProfile": {';
                        clusterPayload += ' "vmSize": "Medium"';
                    clusterPayload += ' },';
                    clusterPayload += ' "storageProfile": {';
                        clusterPayload += ' "osDisk": {';
                            clusterPayload += ' "name": "Disk1",';
                            clusterPayload += ' "vhdUri": "",';
                            clusterPayload += ' "sourceImageName": ""';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "networkProfile": {';
                        clusterPayload += ' "networkInterfaces": [';
                            clusterPayload += ' {';
                                clusterPayload += ' "name": "NIC1",';
                                clusterPayload += ' "properties": {';
                                    clusterPayload += ' "virtualNetwork": {';
                                        clusterPayload += ' "id": ""';
                                    clusterPayload += ' },';
                                    clusterPayload += ' "ipConfigurations": [';
                                        clusterPayload += ' {';
                                            clusterPayload += ' "name": "IpConfigName1",';
                                            clusterPayload += ' "subnet": {';
                                                clusterPayload += ' "id": ""';
                                            clusterPayload += ' }';
                                        clusterPayload += ' }';
                                    clusterPayload += ' ]';
                                clusterPayload += ' }';
                            clusterPayload += ' }';
                        clusterPayload += ' ],';
                        clusterPayload += ' "inputEndpoints": [';
                            clusterPayload += ' {';
                                clusterPayload += ' "name": "HTTPS",';
                                clusterPayload += ' "protocol": "TCP",';
                                clusterPayload += ' "localPort": 443,';
                                clusterPayload += ' "publicPort": 443';
                            clusterPayload += ' }';
                        clusterPayload += ' ]';
                    clusterPayload += ' },';
                    clusterPayload += ' "certificates": [';
                    clusterPayload += ' ],';
                    clusterPayload += ' "provisionGuestAgent": true';
                clusterPayload += ' }';
            clusterPayload += ' },';
            clusterPayload += ' {';
                clusterPayload += ' "name": "headnode",';
                clusterPayload += ' "type": "VirtualMachineGroup",';
                clusterPayload += ' "location": "' + location + '",';
                clusterPayload += ' "properties": {';
                    clusterPayload += ' "instanceCount": 1,';
                    clusterPayload += ' "dnsName": "' + clusterName + '-ssh",';
                    clusterPayload += ' "osProfile": {';
                        clusterPayload += ' "computerNamePattern": "headnode####",';
                        clusterPayload += ' "linuxOperatingSystemProfile": {';
                            clusterPayload += ' "DisableSshPasswordAuthentication": false,';
                            clusterPayload += ' "username": "' + sshUserName + '",';
                            clusterPayload += ' "userPassword": "' + sshPassword + '",';
                            clusterPayload += ' "sshProfile": {';
                                clusterPayload += ' "publicKeys": [],';
                                clusterPayload += ' "sshKeyPairs": []';
                            clusterPayload += ' },';
                            clusterPayload += ' "customData": ""';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "hardwareProfile": {';
                        clusterPayload += ' "vmSize": "Large"';
                    clusterPayload += ' },';
                    clusterPayload += ' "storageProfile": {';
                        clusterPayload += ' "osDisk": {';
                            clusterPayload += ' "name": "Disk1",';
                            clusterPayload += ' "vhdUri": "",';
                            clusterPayload += ' "sourceImageName": ""';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "networkProfile": {';
                        clusterPayload += ' "networkInterfaces": [';
                            clusterPayload += ' {';
                                clusterPayload += ' "name": "NIC1",';
                                clusterPayload += ' "properties": {';
                                    clusterPayload += ' "virtualNetwork": {';
                                        clusterPayload += ' "id": ""';
                                    clusterPayload += ' },';
                                    clusterPayload += ' "ipConfigurations": [';
                                        clusterPayload += ' {';
                                            clusterPayload += ' "name": "IpConfigName1",';
                                            clusterPayload += ' "subnet": {';
                                                clusterPayload += ' "id": ""';
                                            clusterPayload += ' }';
                                        clusterPayload += ' }';
                                    clusterPayload += ' ]';
                                clusterPayload += ' }';
                            clusterPayload += ' }';
                        clusterPayload += ' ],';
                        clusterPayload += ' "inputEndpoints": [';
                            clusterPayload += ' {';
                                clusterPayload += ' "name": "SSH",';
                                clusterPayload += ' "protocol": "TCP",';
                                clusterPayload += ' "localPort": 22,';
                                clusterPayload += ' "publicPort": 22';
                            clusterPayload += ' }';
                        clusterPayload += ' ]';
                    clusterPayload += ' },';
                    clusterPayload += ' "certificates": [],';
                    clusterPayload += ' "vmExtensions": [';
                        clusterPayload += ' {';
                            clusterPayload += ' "referenceName": "OSPatchingForLinux",';
                            clusterPayload += ' "publisher": "Microsoft.OSTCExtensions",';
                            clusterPayload += ' "name": "OSPatchingForLinux",';
                            clusterPayload += ' "version": "1.0",';
                            clusterPayload += ' "resourceExtensionParameterValues": [';
                                clusterPayload += ' { ';
                                    clusterPayload += ' "Key" : "OSPatchingForLinuxPrivateConfigParameter",';
                                    clusterPayload += ' "Value" : "{\\"startTime\\" : \\"03:00\\", \\"rebootAfterPatch\\" : \\"Required\\", \\"category\\": \\"Important\\"}",';
                                    clusterPayload += ' "Type" : "Private"';
                                clusterPayload += ' }';
                            clusterPayload += ' ]';
                        clusterPayload += ' }';
                    clusterPayload += ' ],';
                    clusterPayload += ' "provisionGuestAgent": true';
                clusterPayload += ' }';
            clusterPayload += ' },';
            clusterPayload += ' {';
                clusterPayload += ' "name": "workernode",';
                clusterPayload += ' "type": "VirtualMachineGroup",';
                clusterPayload += ' "location": "' + location + '",';
                clusterPayload += ' "properties": {';
                    clusterPayload += ' "instanceCount": ' + dataNodeCount + ',';
                    clusterPayload += ' "osProfile": {';
                        clusterPayload += ' "computerNamePattern": "workernode####",';
                        clusterPayload += ' "linuxOperatingSystemProfile": {';
                            clusterPayload += ' "DisableSshPasswordAuthentication": false,';
                            clusterPayload += ' "username": "' + sshUserName + '",';
                            clusterPayload += ' "userPassword": "' + sshPassword + '",';
                            clusterPayload += ' "sshProfile": {';
                                clusterPayload += ' "publicKeys": [],';
                                clusterPayload += ' "sshKeyPairs": []';
                            clusterPayload += ' },';
                            clusterPayload += ' "customData": ""';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "hardwareProfile": {';
                        clusterPayload += ' "vmSize": "Large"';
                    clusterPayload += ' },';
                    clusterPayload += ' "storageProfile": {';
                        clusterPayload += ' "osDisk": {';
                            clusterPayload += ' "name": "Disk1",';
                            clusterPayload += ' "vhdUri": "",';
                            clusterPayload += ' "sourceImageName": ""';
                        clusterPayload += ' }';
                    clusterPayload += ' },';
                    clusterPayload += ' "networkProfile": {';
                        clusterPayload += ' "networkInterfaces": [';
                            clusterPayload += ' {';
                                clusterPayload += ' "name": "NIC1",';
                                clusterPayload += ' "properties": {';
                                    clusterPayload += ' "virtualNetwork": {';
                                        clusterPayload += ' "id": ""';
                                    clusterPayload += ' },';
                                    clusterPayload += ' "ipConfigurations": [';
                                        clusterPayload += ' {';
                                            clusterPayload += ' "name": "IpConfigName1",';
                                            clusterPayload += ' "subnet": {';
                                                clusterPayload += ' "id": ""';
                                            clusterPayload += ' }';
                                        clusterPayload += ' }';
                                    clusterPayload += ' ]';
                                clusterPayload += ' }';
                            clusterPayload += ' }';
                        clusterPayload += ' ],';
                        clusterPayload += ' "inputEndpoints": [';
                        clusterPayload += ' ]';
                    clusterPayload += ' },';
                    clusterPayload += ' "certificates": [],';
                    clusterPayload += ' "vmExtensions": [';
                        clusterPayload += ' {';
                            clusterPayload += ' "referenceName": "OSPatchingForLinux",';
                            clusterPayload += ' "publisher": "Microsoft.OSTCExtensions",';
                            clusterPayload += ' "name": "OSPatchingForLinux",';
                            clusterPayload += ' "version": "1.0",';
                            clusterPayload += ' "resourceExtensionParameterValues": [';
                                clusterPayload += ' { ';
                                    clusterPayload += ' "Key" : "OSPatchingForLinuxPrivateConfigParameter",';
                                    clusterPayload += ' "Value" : "{\\"startTime\\" : \\"03:00\\", \\"rebootAfterPatch\\" : \\"Required\\", \\"category\\": \\"Important\\"}",';
                                    clusterPayload += ' "Type" : "Private"';
                                clusterPayload += ' }';
                            clusterPayload += ' ]';
                        clusterPayload += ' }';
                    clusterPayload += ' ],';
                    clusterPayload += ' "provisionGuestAgent": true';
                clusterPayload += ' }';
            clusterPayload += ' },';
            clusterPayload += ' {';
                clusterPayload += ' "name": "",';
                clusterPayload += ' "type": "VirtualNetwork",';
                clusterPayload += ' "location": "' + location + '",';
                clusterPayload += ' "properties": {';
                    clusterPayload += ' "addressSpace": {';
                        clusterPayload += ' "addressPrefixes": [';
                            clusterPayload += ' "10.0.0.0/20"';
                        clusterPayload += ' ]';
                    clusterPayload += ' },';
                    clusterPayload += ' "subnets": [';
                        clusterPayload += ' {';
                            clusterPayload += ' "name": "",';
                            clusterPayload += ' "properties": {';
                                clusterPayload += ' "addressPrefix": "10.0.0.0/20"';
                            clusterPayload += ' }';
                        clusterPayload += ' }';
                    clusterPayload += ' ]';
                clusterPayload += ' }';
            clusterPayload += ' }';
        clusterPayload += ' ]';
    clusterPayload += ' }';
    clusterPayload += ' </a:Value></a:KeyValueOfstringstring></DeploymentDocuments>' + 
    '<HdiVersion xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">3.2</HdiVersion>' + 
    '<Id xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">' + clusterName + 
    '</Id><Location xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">' + location + 
    '</Location><UserSubscriptionId xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">' + 
    subscriptionId + '</UserSubscriptionId><UserTags xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns="http://schemas.microsoft.com/hdinsight/2014/05/management">' + 
    '<a:KeyValueOfstringstring><a:Key>Client</a:Key><a:Value>HDInsight xplat SDK 1.0.0.0</a:Value></a:KeyValueOfstringstring></UserTags></IaasCluster></IntrinsicSettings></Resource>';
	
	return clusterPayload;
};
