-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
111 lines (93 loc) · 2.75 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
// Copyright (c) 2023 Cisco and/or its affiliates.
// This software is licensed to you under the terms of the Cisco Sample
// Code License, Version 1.1 (the "License"). You may obtain a copy of the
// License at
// https://developer.cisco.com/docs/licenses
// All use of the material herein must be in accordance with the terms of
// the License. All rights not expressly granted by the License are
// reserved. Unless required by applicable law or agreed to separately in
// writing, software distributed under the License is distributed on an "AS
// IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"rna/pkg/archive"
"rna/pkg/cli"
"rna/pkg/logger"
"rna/pkg/req"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
)
// Version comes from CL
var (
log zerolog.Logger
args Args
)
func main() {
log = logger.New()
args = newArgs()
cfg := cli.Config{
Host: args.Dnac,
Username: args.Username,
Password: args.Password,
RetryDelay: args.RetryDelay,
RequestRetryCount: args.RequestRetryCount,
BatchSize: args.BatchSize,
}
// Initialize DNA HTTP client
client, err := cli.GetClient(cfg)
if err != nil {
log.Fatal().Err(err).Msg("Error initializing Catalyst Center client.")
}
// Create results archive
arc, err := archive.NewWriter(args.Output)
if err != nil {
log.Fatal().Err(err).Msgf("Error creating archive file: %s.", args.Output)
}
defer arc.Close()
// Initiate requests
reqs, err := req.GetRequests()
if err != nil {
log.Fatal().Err(err).Msgf("Error reading requests.")
}
// Batch and fetch queries in parallel
batch := 1
for i := 0; i < len(reqs); i += args.BatchSize {
var g errgroup.Group
fmt.Println(strings.Repeat("=", 30))
fmt.Println("Fetching request batch", batch)
fmt.Println(strings.Repeat("=", 30))
for j := i; j < i+args.BatchSize && j < len(reqs); j++ {
req := reqs[j]
g.Go(func() error {
return cli.FetchResource(client, req, arc, cfg)
})
}
err = g.Wait()
if err != nil {
log.Error().Err(err).Msg("Error fetching data.")
}
batch++
}
cli.CreateDummyFiles(arc)
fmt.Println(strings.Repeat("=", 30))
fmt.Println("Complete")
fmt.Println(strings.Repeat("=", 30))
path, err := os.Getwd()
if err != nil {
log.Fatal().Err(err).Msg("cannot read current working directory")
}
outPath := filepath.Join(path, args.Output)
if err != nil {
log.Warn().Err(err).Msg("some data could not be fetched")
log.Info().Err(err).Msgf("Available data written to %s.", outPath)
} else {
log.Info().Msg("Collection complete.")
log.Info().Msgf("Please provide %s to Cisco Services for further analysis.", outPath)
}
cli.AddLogFile(arc)
}