-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(COR-1082): add admin command to notify admins of failed clusters
- Loading branch information
1 parent
afa9e63
commit c4adda8
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/qovery/qovery-cli/pkg" | ||
"github.com/qovery/qovery-cli/utils" | ||
) | ||
|
||
var ( | ||
adminNotifyUsersClusterFailureCmd = &cobra.Command{ | ||
Use: "notify-users-cluster-failure", | ||
Short: "Notify users of a cluster failure", | ||
Long: `Notify users by email of a cluster having FAILED status. | ||
- (Default) With --cluster-id, only admins of the cluster with the given id will be notified. | ||
- Without --cluster-id, admins of all clusters with FAILED status will be notified. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
notifyUsersClusterFailure() | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
adminNotifyUsersClusterFailureCmd.Flags().StringVarP(&clusterId, "cluster-id", "c", "", "Cluster ID") | ||
adminCmd.AddCommand(adminNotifyUsersClusterFailureCmd) | ||
} | ||
|
||
func notifyUsersClusterFailure() { | ||
utils.CheckAdminUrl() | ||
|
||
err := pkg.NotifyUsersClusterFailure(&clusterId) | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(1) | ||
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/qovery/qovery-cli/utils" | ||
) | ||
|
||
func NotifyUsersClusterFailure(clusterId *string) error { | ||
var body string | ||
if clusterId != nil { | ||
body = fmt.Sprintf(`{"cluster_ids": ["%s"]}`, *clusterId) | ||
} else { | ||
body = `{"all_failing_clusters": true}` | ||
} | ||
|
||
notifiedClustersResponse, err := postWithBody(utils.AdminUrl+"/cluster/notifyFailedClustersAdmins", body) | ||
if err != nil { | ||
return err | ||
} | ||
result, _ := io.ReadAll(notifiedClustersResponse.Body) | ||
if !strings.Contains(notifiedClustersResponse.Status, "200") { | ||
return fmt.Errorf("could not notify (error %s: %s)", notifiedClustersResponse.Status, string(result)) | ||
} | ||
|
||
utils.Println(fmt.Sprintf("Notification sent for admins of these clusters %s", string(result))) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func postWithBody(url string, bodyAsString string) (*http.Response, error) { | ||
tokenType, token, err := utils.GetAccessToken() | ||
if err != nil { | ||
utils.PrintlnError(err) | ||
os.Exit(0) | ||
} | ||
|
||
body := bytes.NewBuffer([]byte(bodyAsString)) | ||
|
||
req, err := http.NewRequest(http.MethodPost, url, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
return http.DefaultClient.Do(req) | ||
} |