-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (93 loc) · 2.87 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/go-resty/resty/v2"
)
var snowInstance = os.Getenv("SN_INSTANCE")
var snowUser = os.Getenv("SN_USER")
var snowPassword = os.Getenv("SN_PASS")
var ciTable = "cmdb_ci_linux_server"
type Result struct {
Result []Server `json:"result"`
}
type Server struct {
SysID string `json:"sys_id"`
Name string `json:"name"`
ShortDescription string `json:"short_description"`
}
type UpdatePayload struct {
Name string `json:"name,omitempty"`
ShortDescription string `json:"short_description,omitempty"`
}
func main() {
listAllFlag := flag.Bool("list-all", false, "List all CMDB CI servers")
getServerFlag := flag.String("get-server", "", "Get details of a server by sys_id")
helpFlag := flag.Bool("help", false, "Show help message")
flag.Parse()
if *helpFlag {
flag.Usage()
os.Exit(0)
}
client := resty.New()
if *listAllFlag {
servers, err := getCMDBServers(client)
if err != nil {
log.Fatalf("Error retrieving servers: %v", err)
}
fmt.Println("List of CMDB CI Servers:")
for _, server := range servers {
fmt.Printf("SysID: %s, Name: %s, Description: %s\n", server.SysID, server.Name, server.ShortDescription)
}
} else if *getServerFlag != "" {
server, err := getCMDBServer(client, *getServerFlag)
if err != nil {
log.Fatalf("Error fetching server: %v", err)
}
fmt.Printf("Fetched Server - SysID: %s, Name: %s, Description: %s\n", server.SysID, server.Name, server.ShortDescription)
} else {
flag.Usage()
}
}
func getCMDBServers(client *resty.Client) ([]Server, error) {
resp, err := client.R().
SetBasicAuth(snowUser, snowPassword).
SetHeader("Accept", "application/json").
Get(fmt.Sprintf("https://%s.service-now.com/api/now/table/%s", snowInstance, ciTable))
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("received non-200 response: %s", resp.Status())
}
var result Result
err = json.Unmarshal(resp.Body(), &result)
if err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
return result.Result, nil
}
func getCMDBServer(client *resty.Client, sysID string) (*Server, error) {
resp, err := client.R().
SetBasicAuth(snowUser, snowPassword).
SetHeader("Accept", "application/json").
Get(fmt.Sprintf("https://%s.service-now.com/api/now/table/%s/%s", snowInstance, ciTable, sysID))
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp.StatusCode() != http.StatusOK {
return nil, fmt.Errorf("received non-200 response: %s", resp.Status())
}
// Debug
fmt.Printf("Response Body: %s\n", resp.Body())
var server Server
err = json.Unmarshal(resp.Body(), &server)
if err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
return &server, nil
}