/
total_api_panel.go
114 lines (97 loc) · 3.58 KB
/
total_api_panel.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
package ApiGateway
import (
"encoding/json"
"fmt"
"log"
"github.com/Appkube-awsx/awsx-common/authenticate"
"github.com/Appkube-awsx/awsx-common/awsclient"
"github.com/Appkube-awsx/awsx-common/model"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/spf13/cobra"
)
type TotalApiResult struct {
Value float64 `json:"Value"`
}
var AwsxTotalApiCmd = &cobra.Command{
Use: "total_api_panel",
Short: "get total api metrics data",
Long: `command to get total api metrics data`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("running from child command")
var authFlag, clientAuth, err = authenticate.AuthenticateCommand(cmd)
if err != nil {
log.Printf("Error during authentication: %v\n", err)
err := cmd.Help()
if err != nil {
return
}
return
}
if authFlag {
responseType, _ := cmd.PersistentFlags().GetString("responseType")
jsonResp, cloudwatchMetricResp, err := GetTotalApiData(clientAuth, nil)
if err != nil {
log.Println("Error getting total api data : ", err)
return
}
if responseType == "frame" {
fmt.Println(cloudwatchMetricResp)
} else {
fmt.Println(jsonResp)
}
}
},
}
func GetTotalApiData(clientAuth *model.Auth, apiClient *apigateway.APIGateway) (string, map[string]float64, error) {
cloudwatchMetricData := map[string]float64{}
totalApis, err := GetTotalApi(clientAuth, apiClient)
if err != nil {
log.Println("Error in getting total functions: ", err)
return "", nil, err
}
cloudwatchMetricData["TotalAPIs"] = float64(totalApis)
log.Printf("Total APIs: %d", totalApis)
jsonString, err := json.Marshal(TotalApiResult{Value: float64(totalApis)})
if err != nil {
log.Println("Error in marshalling json in string: ", err)
return "", nil, err
}
return string(jsonString), cloudwatchMetricData, nil
}
func GetTotalApi(clientAuth *model.Auth, apiClient *apigateway.APIGateway) (int, error) {
if apiClient == nil {
apiClient = awsclient.GetClient(*clientAuth, awsclient.APIGATEWAY_CLIENT).(*apigateway.APIGateway)
}
totalApis := 0
// Get total count of REST APIs
restAPIs, err := GetRestAPIs(clientAuth, apiClient)
if err != nil {
return 0, err
}
totalApis += restAPIs
// Get total count of HTTP APIs (API Gateway V2)
httpAPIs, err := GetHttpAPIs(clientAuth, nil)
if err != nil {
return 0, err
}
totalApis += httpAPIs
return totalApis, nil
}
func init() {
AwsxTotalApiCmd.PersistentFlags().String("elementId", "", "element id")
AwsxTotalApiCmd.PersistentFlags().String("elementType", "", "element type")
AwsxTotalApiCmd.PersistentFlags().String("query", "", "query")
AwsxTotalApiCmd.PersistentFlags().String("cmdbApiUrl", "", "cmdb api")
AwsxTotalApiCmd.PersistentFlags().String("vaultUrl", "", "vault end point")
AwsxTotalApiCmd.PersistentFlags().String("vaultToken", "", "vault token")
AwsxTotalApiCmd.PersistentFlags().String("zone", "", "aws region")
AwsxTotalApiCmd.PersistentFlags().String("accessKey", "", "aws access key")
AwsxTotalApiCmd.PersistentFlags().String("secretKey", "", "aws secret key")
AwsxTotalApiCmd.PersistentFlags().String("crossAccountRoleArn", "", "aws cross account role arn")
AwsxTotalApiCmd.PersistentFlags().String("externalId", "", "aws external id")
AwsxTotalApiCmd.PersistentFlags().String("cloudWatchQueries", "", "aws cloudwatch metric queries")
AwsxTotalApiCmd.PersistentFlags().String("instanceId", "", "instance id")
AwsxTotalApiCmd.PersistentFlags().String("startTime", "", "start time")
AwsxTotalApiCmd.PersistentFlags().String("endTime", "", "end time")
AwsxTotalApiCmd.PersistentFlags().String("responseType", "", "response type. json/frame")
}