11/*
22Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3-
43*/
54package cmd
65
@@ -16,81 +15,160 @@ import (
1615// orchsCmd represents the orchs command
1716var orchsCmd = & cobra.Command {
1817 Use : "orchs" ,
19- Short : "Keyfactor agents APIs and utilities." ,
18+ Short : "Keyfactor agents/orchestrators APIs and utilities." ,
2019 Long : `A collections of APIs and utilities for interacting with Keyfactor orchestrators.` ,
2120}
2221
22+ // getOrchestratorCmd represents the get orchestrator command
2323var getOrchestratorCmd = & cobra.Command {
2424 Use : "get" ,
25- Short : "Get orchestrator by ID or machine/host name." ,
26- Long : `Get orchestrator by ID or machine/host name.` ,
25+ Short : "Get orchestrator by machine/client name." ,
26+ Long : `Get orchestrator by machine/client name.` ,
2727 Run : func (cmd * cobra.Command , args []string ) {
28- fmt .Println ("orchestrator get called" )
28+ log .SetOutput (ioutil .Discard )
29+ client := cmd .Flag ("client" ).Value .String ()
30+ kfClient , _ := initClient ()
31+ agents , aErr := kfClient .GetAgent (client )
32+ if aErr != nil {
33+ fmt .Printf ("Error, unable to get orchestrator %s. %s\n " , client , aErr )
34+ log .Fatalf ("Error: %s" , aErr )
35+ }
36+ output , jErr := json .Marshal (agents )
37+ if jErr != nil {
38+ fmt .Println ("Error invalid API response from Keyfactor." )
39+ log .Fatalf ("Error: %s" , jErr )
40+ }
41+ fmt .Printf ("%s" , output )
2942 },
3043}
3144
45+ // listOrchestratorsCmd represents the list orchestrators command
3246var approveOrchestratorCmd = & cobra.Command {
3347 Use : "approve" ,
34- Short : "Approve orchestrator by ID or machine/host name." ,
35- Long : `Approve orchestrator by ID or machine/host name.` ,
48+ Short : "Approve orchestrator by ID or machine/client name." ,
49+ Long : `Approve orchestrator by ID or machine/client name.` ,
3650 Run : func (cmd * cobra.Command , args []string ) {
37- fmt .Println ("orchestrator approve called" )
51+ log .SetOutput (ioutil .Discard )
52+ client := cmd .Flag ("client" ).Value .String ()
53+ kfClient , cErr := initClient ()
54+ if cErr != nil {
55+ fmt .Println ("Error, unable to connect to Keyfactor." )
56+ log .Fatalf ("Error: %s" , cErr )
57+ }
58+ agents , aErr := kfClient .GetAgent (client )
59+ if aErr != nil {
60+ fmt .Printf ("Error, unable to get orchestrator %s. %s\n " , client , aErr )
61+ log .Fatalf ("[ERROR]: %s" , aErr )
62+ }
63+ agent := agents [0 ]
64+ _ , aErr = kfClient .ApproveAgent (agent .AgentId )
65+ if aErr != nil {
66+ fmt .Printf ("Error, unable to approve orchestrator %s. %s\n " , client , aErr )
67+ log .Fatalf ("[ERROR]: %s" , aErr )
68+ }
69+ fmt .Printf ("Orchestrator %s approved.\n " , client )
3870 },
3971}
4072
73+ // disapproveOrchestratorCmd represents the disapprove orchestrator command
4174var disapproveOrchestratorCmd = & cobra.Command {
4275 Use : "disapprove" ,
43- Short : "Disapprove orchestrator by ID or machine/host name." ,
44- Long : `Disapprove orchestrator by ID or machine/host name.` ,
76+ Short : "Disapprove orchestrator by ID or machine/client name." ,
77+ Long : `Disapprove orchestrator by ID or machine/client name.` ,
4578 Run : func (cmd * cobra.Command , args []string ) {
46- fmt .Println ("orchestrator disapprove called" )
79+ log .SetOutput (ioutil .Discard )
80+ client := cmd .Flag ("client" ).Value .String ()
81+ kfClient , cErr := initClient ()
82+ if cErr != nil {
83+ fmt .Println ("Error, unable to connect to Keyfactor." )
84+ log .Fatalf ("Error: %s" , cErr )
85+ }
86+ agents , aErr := kfClient .GetAgent (client )
87+ if aErr != nil {
88+ fmt .Printf ("Error, unable to get orchestrator %s. %s\n " , client , aErr )
89+ log .Fatalf ("[ERROR]: %s" , aErr )
90+ }
91+ agent := agents [0 ]
92+ _ , aErr = kfClient .DisApproveAgent (agent .AgentId )
93+ if aErr != nil {
94+ fmt .Printf ("Error, unable to disapprove orchestrator %s. %s\n " , client , aErr )
95+ log .Fatalf ("[ERROR]: %s" , aErr )
96+ }
97+ fmt .Printf ("Orchestrator %s disapproved.\n " , client )
4798 },
4899}
49100
101+ // resetOrchestratorCmd represents the reset orchestrator command
50102var resetOrchestratorCmd = & cobra.Command {
51103 Use : "reset" ,
52- Short : "Reset orchestrator by ID or machine/host name." ,
53- Long : `Reset orchestrator by ID or machine/host name.` ,
104+ Short : "Reset orchestrator by ID or machine/client name." ,
105+ Long : `Reset orchestrator by ID or machine/client name.` ,
54106 Run : func (cmd * cobra.Command , args []string ) {
55107 fmt .Println ("orchestrator reset called" )
56108 },
57109}
58110
111+ // getLogsOrchestratorCmd represents the get logs orchestrator command
59112var getLogsOrchestratorCmd = & cobra.Command {
60113 Use : "logs" ,
61- Short : "Get orchestrator logs by ID or machine/host name." ,
62- Long : `Get orchestrator logs by ID or machine/host name.` ,
114+ Short : "Get orchestrator logs by ID or machine/client name." ,
115+ Long : `Get orchestrator logs by ID or machine/client name.` ,
63116 Run : func (cmd * cobra.Command , args []string ) {
64- fmt .Println ("orchestrator logs called" )
117+ log .SetOutput (ioutil .Discard )
118+ client := cmd .Flag ("client" ).Value .String ()
119+ kfClient , cErr := initClient ()
120+ if cErr != nil {
121+ fmt .Println ("Error, unable to connect to Keyfactor." )
122+ log .Fatalf ("Error: %s" , cErr )
123+ }
124+ agents , aErr := kfClient .GetAgent (client )
125+ if aErr != nil {
126+ fmt .Printf ("Error, unable to get logs for orchestrator %s. %s\n " , client , aErr )
127+ log .Fatalf ("[ERROR]: %s" , aErr )
128+ }
129+ agent := agents [0 ]
130+ _ , aErr = kfClient .FetchAgentLogs (agent .AgentId )
131+ if aErr != nil {
132+ fmt .Printf ("Error, unable to get logs for orchestrator %s. %s\n " , client , aErr )
133+ log .Fatalf ("[ERROR]: %s" , aErr )
134+ }
135+ fmt .Printf ("Fetching logs from %s successful.\n " , client )
65136 },
66137}
67138
139+ // listOrchestratorsCmd represents the list orchestrators command
68140var listOrchestratorsCmd = & cobra.Command {
69141 Use : "list" ,
70142 Short : "List orchestrators." ,
71143 Long : `Returns a JSON list of Keyfactor orchestrators.` ,
72144 Run : func (cmd * cobra.Command , args []string ) {
73145 log .SetOutput (ioutil .Discard )
74146 kfClient , _ := initClient ()
75- agents , err := kfClient .GetAgentList ()
76- if err != nil {
77- log .Printf ("Error: %s" , err )
147+ agents , aErr := kfClient .GetAgentList ()
148+ if aErr != nil {
149+ fmt .Printf ("Error, unable to get orchestrators list. %s\n " , aErr )
150+ log .Fatalf ("Error: %s" , aErr )
78151 }
79152 output , jErr := json .Marshal (agents )
80153 if jErr != nil {
81- log .Printf ("Error: %s" , jErr )
154+ fmt .Println ("Error, unable to get orchestrators list." )
155+ log .Fatalf ("Error: %s" , jErr )
82156 }
83157 fmt .Printf ("%s" , output )
84158 },
85159}
86160
87161func init () {
162+ var client string
163+
88164 rootCmd .AddCommand (orchsCmd )
89165
90166 // LIST orchestrators command
91167 orchsCmd .AddCommand (listOrchestratorsCmd )
92168 // GET orchestrator command
93169 orchsCmd .AddCommand (getOrchestratorCmd )
170+ getOrchestratorCmd .Flags ().StringVarP (& client , "client" , "c" , "" , "Get a specific orchestrator by machine or client name." )
171+ getOrchestratorCmd .MarkFlagRequired ("client" )
94172 // CREATE orchestrator command TODO: API NOT SUPPORTED
95173 //orchsCmd.AddCommand(createOrchestratorCmd)
96174 // UPDATE orchestrator command TODO: API NOT SUPPORTED
@@ -99,14 +177,23 @@ func init() {
99177 //orchsCmd.AddCommand(deleteOrchestratorCmd)
100178 // APPROVE orchestrator command
101179 orchsCmd .AddCommand (approveOrchestratorCmd )
180+ approveOrchestratorCmd .Flags ().StringVarP (& client , "client" , "c" , "" , "Approve a specific orchestrator by machine or client name." )
181+ approveOrchestratorCmd .MarkFlagRequired ("client" )
102182 // DISAPPROVE orchestrator command
103183 orchsCmd .AddCommand (disapproveOrchestratorCmd )
184+ disapproveOrchestratorCmd .Flags ().StringVarP (& client , "client" , "c" , "" , "Disapprove a specific orchestrator by machine or client name." )
185+ disapproveOrchestratorCmd .MarkFlagRequired ("client" )
104186 // RESET orchestrator command
105187 orchsCmd .AddCommand (resetOrchestratorCmd )
188+ resetOrchestratorCmd .Flags ().StringVarP (& client , "client" , "c" , "" , "Reset a specific orchestrator by machine or client name." )
189+ resetOrchestratorCmd .MarkFlagRequired ("client" )
106190 // GET orchestrator logs command
107191 orchsCmd .AddCommand (getLogsOrchestratorCmd )
192+ getLogsOrchestratorCmd .Flags ().StringVarP (& client , "client" , "c" , "" , "Get logs for a specific orchestrator by machine or client name." )
193+ getLogsOrchestratorCmd .MarkFlagRequired ("client" )
108194 // SET orchestrator auth certificate reenrollment command TODO: Not implemented
109195 //orchsCmd.AddCommand(setOrchestratorAuthCertReenrollCmd)
110196 // Utility commands
111197 //orchsCmd.AddCommand(downloadOrchestrator) TODO: Not implemented
198+ //orchsCmd.AddCommand(installOrchestrator) TODO: Not implemented
112199}
0 commit comments