This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
77 lines (57 loc) · 1.45 KB
/
main.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/BurntSushi/toml"
"github.com/gorilla/mux"
)
type tomlConfig struct {
Port int
Metadata metadata
}
type metadata struct {
Port int
AmiID string
LocalHostname string
ProductCodes string
ReservationID string
PublicHostname string
PublicIPV4 string
}
var m map[string]string
func main() {
configFile := "metadata.toml"
if len(os.Args) == 2 {
configFile = os.Args[1]
}
fmt.Printf("Starting EC2 metadata simluator from config %s\n", configFile)
var config tomlConfig
if _, err := toml.DecodeFile(configFile, &config); err != nil {
fmt.Println(err)
return
}
m = make(map[string]string)
m["ami-id"] = config.Metadata.AmiID
m["local-hostname"] = config.Metadata.LocalHostname
m["reservation-id"] = config.Metadata.ReservationID
m["product-codes"] = config.Metadata.ProductCodes
m["public-hostname"] = config.Metadata.PublicHostname
m["public-ipv4"] = config.Metadata.PublicIPV4
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/latest/meta-data/{category}", handle).Methods("GET")
host := fmt.Sprintf(":%d", config.Port)
fmt.Printf("Listening on: %s\n", host)
log.Fatal(http.ListenAndServe(host, router))
}
func handle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
i, ok := m[vars["category"]]
if ok {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, i)
} else {
w.WriteHeader(http.StatusNotFound)
}
}