temp=$(kubectl get svc -n imxc -o wide | grep elasticsearch | grep NodePort | awk {'print $5'})
export ES_NODEPORT=${temp:5:(-4)}
echo $ES_NODEPORT

export MASTER_IP=$(kubectl get nodes -o wide | grep master | awk {'print $6'})
echo $MASTER_IP

export NUM_SHARDS=2
export NUM_REPLICAS=1

SOURCE_INDEX='license_history'
DEST_INDEX='license_history_backup'

# 기존 index 재매핑
curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/'"${SOURCE_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/'"${SOURCE_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "'${SOURCE_INDEX}'-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "2",
      "number_of_replicas": "1",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "'${SOURCE_INDEX}'"
      },
      "sort.field": "checkTime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "licenseType": {
        "type": "integer"
      },
      "expireDate": {
        "type": "text"
      },
      "targetNodesCount": {
        "type": "integer"
      },
      "realNodesCount": {
        "type": "integer"
      },
      "targetPodsCount": {
        "type": "integer"
      },
      "realPodsCount": {
        "type": "integer"
      },
      "targetSvcsCount": {
        "type": "integer"
      },
      "realSvcsCount": {
        "type": "integer"
      },
      "targetCoreCount": {
        "type": "integer"
      },
      "realCoreCount": {
        "type": "integer"
      },
      "allowableRange": {
        "type": "integer"
      },
      "licenseClusterId": {
        "type": "keyword"
      },
      "tenantId": {
        "type": "keyword"
      },
      "checkTime": {
        "type": "date",
        "format": "epoch_millis"
      },
      "checkResult": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "'${SOURCE_INDEX}'": {}
  }
}'

# 기존 index 데이터 백업용 index 매핑
curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_ilm/policy/'"${DEST_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "policy": {
    "phases": {
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}'

curl -X PUT 'http://'"${MASTER_IP}"':'"${ES_NODEPORT}"'/_template/'"${DEST_INDEX}"'' -H 'Content-Type: application/json' -d '{
  "order": 0,
  "index_patterns": [
    "'${DEST_INDEX}'-*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "2",
      "number_of_replicas": "1",
      "refresh_interval": "1s",
      "lifecycle": {
        "name": "'${DEST_INDEX}'"
      },
      "sort.field": "checkTime",
      "sort.order": "desc"
    }
  },
  "mappings": {
    "properties": {
      "licenseType": {
        "type": "integer"
      },
      "expireDate": {
        "type": "text"
      },
      "targetNodesCount": {
        "type": "integer"
      },
      "realNodesCount": {
        "type": "integer"
      },
      "targetPodsCount": {
        "type": "integer"
      },
      "realPodsCount": {
        "type": "integer"
      },
      "targetSvcsCount": {
        "type": "integer"
      },
      "realSvcsCount": {
        "type": "integer"
      },
      "targetCoreCount": {
        "type": "integer"
      },
      "realCoreCount": {
        "type": "integer"
      },
      "allowableRange": {
        "type": "integer"
      },
      "licenseClusterId": {
        "type": "keyword"
      },
      "tenantId": {
        "type": "keyword"
      },
      "checkTime": {
        "type": "date",
        "format": "epoch_millis"
      },
      "checkResult": {
        "type": "integer"
      }
    }
  },
  "aliases": {
    "'${DEST_INDEX}'": {}
  }
}'