Skip to content

Commit

Permalink
Improvement: Updated AddNotifications to utilize machinery (#307)
Browse files Browse the repository at this point in the history
* Updated AddNotifications to utilize machinery

* Updated org output

* Fixing up case for flag

* Updated to latest Machinery
  • Loading branch information
CGoodwin90 committed Dec 27, 2023
1 parent 3887317 commit 2291443
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 69 deletions.
44 changes: 32 additions & 12 deletions cmd/notificationsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"
)

var addNotificationEmailCmd = &cobra.Command{
Expand All @@ -37,37 +39,54 @@ It does not configure a project to send notifications to email though, you need
if err != nil {
return err
}
organizationID, err := cmd.Flags().GetUint("organization-id")
if err != nil {
return err
}
if name == "" || email == "" {
return fmt.Errorf("Missing arguments: name or email is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to create an email notification '%s' with email address '%s', are you sure?", name, email)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
&token,
debug)
notification := &schema.AddNotificationEmailInput{

notification := s.AddNotificationEmailInput{
Name: name,
EmailAddress: email,
Organization: &organizationID,
}
result, err := lagoon.AddNotificationEmail(context.TODO(), notification, lc)

result, err := l.AddNotificationEmail(context.TODO(), &notification, lc)
if err != nil {
return err
}
data := []output.Data{
[]string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.EmailAddress)),
},
var data []output.Data
notificationData := []string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.EmailAddress)),
}
if result.Organization != nil {
organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc)
if err != nil {
return err
}
notificationData = append(notificationData, fmt.Sprintf("%s", organization.Name))
} else {
notificationData = append(notificationData, "-")
}
data = append(data, notificationData)
output.RenderOutput(output.Table{
Header: []string{
"ID",
"Name",
"EmailAddress",
"Organization",
},
Data: data,
}, outputOptions)
Expand Down Expand Up @@ -376,6 +395,7 @@ var updateEmailNotificationCmd = &cobra.Command{
func init() {
addNotificationEmailCmd.Flags().StringP("name", "n", "", "The name of the notification")
addNotificationEmailCmd.Flags().StringP("email", "E", "", "The email address of the notification")
addNotificationEmailCmd.Flags().Uint("organization-id", 0, "ID of the Organization")
addProjectNotificationEmailCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteProjectEmailNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteEmailNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
Expand Down
51 changes: 36 additions & 15 deletions cmd/notificationsrocketchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"context"
"encoding/json"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
Expand Down Expand Up @@ -41,40 +44,57 @@ It does not configure a project to send notifications to RocketChat though, you
if err != nil {
return err
}
organizationID, err := cmd.Flags().GetUint("organization-id")
if err != nil {
return err
}
if name == "" || channel == "" || webhook == "" {
return fmt.Errorf("Missing arguments: name, webhook, or email is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to create an RocketChat notification '%s' with webhook '%s' channel '%s', are you sure?", name, webhook, channel)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
&token,
debug)
notification := &schema.AddNotificationRocketChatInput{
Name: name,
Webhook: webhook,
Channel: channel,

notification := s.AddNotificationRocketChatInput{
Name: name,
Webhook: webhook,
Channel: channel,
Organization: &organizationID,
}
result, err := lagoon.AddNotificationRocketChat(context.TODO(), notification, lc)

result, err := l.AddNotificationRocketChat(context.TODO(), &notification, lc)
if err != nil {
return err
}
data := []output.Data{
[]string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
returnNonEmptyString(fmt.Sprintf("%v", result.Channel)),
},
var data []output.Data
notificationData := []string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
returnNonEmptyString(fmt.Sprintf("%v", result.Channel)),
}
if result.Organization != nil {
organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc)
if err != nil {
return err
}
notificationData = append(notificationData, fmt.Sprintf("%s", organization.Name))
} else {
notificationData = append(notificationData, "-")
}
data = append(data, notificationData)
output.RenderOutput(output.Table{
Header: []string{
"ID",
"Name",
"Webhook",
"Channel",
"Organization",
},
Data: data,
}, outputOptions)
Expand Down Expand Up @@ -395,6 +415,7 @@ func init() {
addNotificationRocketchatCmd.Flags().StringP("name", "n", "", "The name of the notification")
addNotificationRocketchatCmd.Flags().StringP("webhook", "w", "", "The webhook URL of the notification")
addNotificationRocketchatCmd.Flags().StringP("channel", "c", "", "The channel for the notification")
addNotificationRocketchatCmd.Flags().Uint("organization-id", 0, "ID of the Organization")
addProjectNotificationRocketChatCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteProjectRocketChatNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteRocketChatNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
Expand Down
51 changes: 36 additions & 15 deletions cmd/notificationsslack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"context"
"encoding/json"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
Expand Down Expand Up @@ -41,40 +44,57 @@ It does not configure a project to send notifications to Slack though, you need
if err != nil {
return err
}
organizationID, err := cmd.Flags().GetUint("organization-id")
if err != nil {
return err
}
if name == "" || channel == "" || webhook == "" {
return fmt.Errorf("Missing arguments: name, webhook, or email is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to create an Slack notification '%s' with webhook '%s' channel '%s', are you sure?", name, webhook, channel)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
&token,
debug)
notification := &schema.AddNotificationSlackInput{
Name: name,
Webhook: webhook,
Channel: channel,

notification := s.AddNotificationSlackInput{
Name: name,
Webhook: webhook,
Channel: channel,
Organization: &organizationID,
}
result, err := lagoon.AddNotificationSlack(context.TODO(), notification, lc)

result, err := l.AddNotificationSlack(context.TODO(), &notification, lc)
if err != nil {
return err
}
data := []output.Data{
[]string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
returnNonEmptyString(fmt.Sprintf("%v", result.Channel)),
},
var data []output.Data
notificationData := []string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
returnNonEmptyString(fmt.Sprintf("%v", result.Channel)),
}
if result.Organization != nil {
organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc)
if err != nil {
return err
}
notificationData = append(notificationData, fmt.Sprintf("%s", organization.Name))
} else {
notificationData = append(notificationData, "-")
}
data = append(data, notificationData)
output.RenderOutput(output.Table{
Header: []string{
"ID",
"Name",
"Webhook",
"Channel",
"Organization",
},
Data: data,
}, outputOptions)
Expand Down Expand Up @@ -395,6 +415,7 @@ func init() {
addNotificationSlackCmd.Flags().StringP("name", "n", "", "The name of the notification")
addNotificationSlackCmd.Flags().StringP("webhook", "w", "", "The webhook URL of the notification")
addNotificationSlackCmd.Flags().StringP("channel", "c", "", "The channel for the notification")
addNotificationSlackCmd.Flags().Uint("organization-id", 0, "ID of the Organization")
addProjectNotificationSlackCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteProjectSlackNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteSlackNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
Expand Down
47 changes: 34 additions & 13 deletions cmd/notificationsteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"context"
"encoding/json"
"fmt"
l "github.com/uselagoon/machinery/api/lagoon"
lclient "github.com/uselagoon/machinery/api/lagoon/client"
s "github.com/uselagoon/machinery/api/schema"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
Expand Down Expand Up @@ -37,37 +40,54 @@ It does not configure a project to send notifications to Microsoft Teams though,
if err != nil {
return err
}
organizationID, err := cmd.Flags().GetUint("organization-id")
if err != nil {
return err
}
if name == "" || webhook == "" {
return fmt.Errorf("Missing arguments: name or webhook is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to create a Microsoft Teams notification '%s' with webhook url '%s', are you sure?", name, webhook)) {
current := lagoonCLIConfig.Current
lc := client.New(
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
&token,
debug)
notification := &schema.AddNotificationMicrosoftTeamsInput{
Name: name,
Webhook: webhook,

notification := s.AddNotificationMicrosoftTeamsInput{
Name: name,
Webhook: webhook,
Organization: &organizationID,
}
result, err := lagoon.AddNotificationMicrosoftTeams(context.TODO(), notification, lc)

result, err := l.AddNotificationMicrosoftTeams(context.TODO(), &notification, lc)
if err != nil {
return err
}
data := []output.Data{
[]string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
},
var data []output.Data
notificationData := []string{
returnNonEmptyString(fmt.Sprintf("%v", result.ID)),
returnNonEmptyString(fmt.Sprintf("%v", result.Name)),
returnNonEmptyString(fmt.Sprintf("%v", result.Webhook)),
}
if result.Organization != nil {
organization, err := l.GetOrganizationByID(context.TODO(), organizationID, lc)
if err != nil {
return err
}
notificationData = append(notificationData, fmt.Sprintf("%s", organization.Name))
} else {
notificationData = append(notificationData, "-")
}
data = append(data, notificationData)
output.RenderOutput(output.Table{
Header: []string{
"ID",
"Name",
"Webhook",
"Organization",
},
Data: data,
}, outputOptions)
Expand Down Expand Up @@ -376,6 +396,7 @@ var updateMicrosoftTeamsNotificationCmd = &cobra.Command{
func init() {
addNotificationMicrosoftTeamsCmd.Flags().StringP("name", "n", "", "The name of the notification")
addNotificationMicrosoftTeamsCmd.Flags().StringP("webhook", "w", "", "The webhook URL of the notification")
addNotificationMicrosoftTeamsCmd.Flags().Uint("organization-id", 0, "ID of the Organization")
addProjectNotificationMicrosoftTeamsCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteProjectMicrosoftTeamsNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
deleteMicrosoftTeamsNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification")
Expand Down

0 comments on commit 2291443

Please sign in to comment.