1package main
2
3import (
4	"flag"
5	"net/http"
6	"log"
7	"bytes"
8	"strings"
9	"encoding/json"
10	"github.com/couchbase/indexing/secondary/common"
11)
12
13type IndexRequest struct {
14    Version uint64           `json:"version,omitempty"`
15    Type    string           `json:"type,omitempty"`
16    Index   common.IndexDefn `json:"index,omitempty"`
17}
18
19//
20// main function
21//
22func main() {
23	var curl string
24	var cmd string
25	var meta string
26	var host string
27	var defnId uint64
28
29	flag.StringVar(&curl, "curl", "", "cluster url")
30	flag.StringVar(&cmd, "cmd", "", "command=getIndexStatus/getIndexMetadata/restoreIndexMetadata/dropIndex")
31	flag.StringVar(&meta, "meta", "", "metadata to restore")
32	flag.StringVar(&host, "host", "", "host for index defnition id")
33	flag.Uint64Var(&defnId, "id", 0, "index definition id")
34	flag.Parse()
35
36	cinfo, err := common.NewClusterInfoCache(curl, "default")
37	if err != nil {
38		log.Printf("%v", err)
39		return
40	}
41
42	if err := cinfo.Fetch(); err != nil {
43		log.Printf("%v", err)
44		return
45	}
46
47	nodes := cinfo.GetNodesByServiceType("indexHttp")
48	if len(nodes) == 0 {
49		log.Printf("There is no couchbase server running with indexer service")
50		return
51	}
52
53	indexHttp, err := cinfo.GetServiceAddress(nodes[0], "indexHttp")
54	if err != nil {
55		log.Printf("%v", err)
56		return
57	}
58
59	if cmd == "getIndexStatus" || cmd == "getIndexMetadata" {
60
61		resp, err := http.Get("http://" + indexHttp + "/" + cmd)
62		if err != nil {
63			log.Printf("%v", err)
64			return
65		}
66
67	    buf := new(bytes.Buffer)
68    	if _, err := buf.ReadFrom(resp.Body); err != nil {
69        	log.Printf("%v", err)
70        	return
71    	}
72
73		log.Printf("%v", string(buf.Bytes()))
74
75	} else if cmd == "restoreIndexMetadata" {
76
77		bodybuf := bytes.NewBuffer([]byte(meta))
78		resp, err := http.Post("http://" + indexHttp + "/" + cmd, "application/json", bodybuf)
79		if err != nil {
80			log.Printf("%v", err)
81			return
82		}
83
84	    buf := new(bytes.Buffer)
85    	if _, err := buf.ReadFrom(resp.Body); err != nil {
86        	log.Printf("%v", err)
87        	return
88    	}
89
90		log.Printf("%v", string(buf.Bytes()))
91
92	} else if cmd == "dropIndex" {
93
94		for _, id := range nodes {
95			indexHttp, err := cinfo.GetServiceAddress(id, "indexHttp")
96			if err != nil {
97				log.Printf("%v", err)
98				return
99			}
100
101			if strings.HasPrefix(indexHttp, host) {
102
103				defn := common.IndexDefn{DefnId : common.IndexDefnId(defnId)}
104				request := &IndexRequest{Type : "drop", Index : defn}
105
106	    		body, err := json.Marshal(&request)
107    			if err != nil {
108        			log.Printf("%v", err)
109        			return
110    			}
111
112				log.Printf("dialing http://" + indexHttp + "/" + cmd)
113
114				bodybuf := bytes.NewBuffer(body)
115				resp, err := http.Post("http://" + indexHttp + "/" + cmd, "application/json", bodybuf)
116				if err != nil {
117					log.Printf("%v", err)
118					return
119				}
120
121	    		buf := new(bytes.Buffer)
122    			if _, err := buf.ReadFrom(resp.Body); err != nil {
123        			log.Printf("%v", err)
124        			return
125    			}
126
127				log.Printf("%v", string(buf.Bytes()))
128				return
129			}
130		}
131
132		log.Printf("Cannot find matching host %d", host)
133	}
134}
135