summaryrefslogtreecommitdiffhomepage
path: root/logtail/example/logadopt/logadopt.go
blob: f9104231644b4ba52c6998f2be2e4f557515757f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

// Command logadopt is a CLI tool to adopt a machine into a logtail collection.
package main

import (
	"flag"
	"io"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func main() {
	collection := flag.String("c", "", "logtail collection name")
	publicID := flag.String("m", "", "machine public identifier")
	apiKey := flag.String("p", "", "logtail API key")
	flag.Parse()
	if len(flag.Args()) != 0 {
		flag.Usage()
		os.Exit(1)
	}
	log.SetFlags(0)

	req, err := http.NewRequest("POST", "https://log.tailscale.com/instances", strings.NewReader(url.Values{
		"collection": []string{*collection},
		"instances":  []string{*publicID},
		"adopt":      []string{"true"},
	}.Encode()))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.SetBasicAuth(*apiKey, "")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	b, err := io.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		log.Fatalf("logadopt: response read failed %d: %v", resp.StatusCode, err)
	}
	if resp.StatusCode != 200 {
		log.Fatalf("adoption failed: %d: %s", resp.StatusCode, string(b))
	}
	log.Printf("%s", string(b))
}