diff --git a/cmd/notificationsemail.go b/cmd/notificationsemail.go index 79dd4647..ac3f96f2 100644 --- a/cmd/notificationsemail.go +++ b/cmd/notificationsemail.go @@ -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{ @@ -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(), ¬ification, 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) @@ -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") diff --git a/cmd/notificationsrocketchat.go b/cmd/notificationsrocketchat.go index ed31fae6..c74b76fd 100644 --- a/cmd/notificationsrocketchat.go +++ b/cmd/notificationsrocketchat.go @@ -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" @@ -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(), ¬ification, 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) @@ -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") diff --git a/cmd/notificationsslack.go b/cmd/notificationsslack.go index 2220e876..4d4074e0 100644 --- a/cmd/notificationsslack.go +++ b/cmd/notificationsslack.go @@ -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" @@ -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(), ¬ification, 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) @@ -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") diff --git a/cmd/notificationsteams.go b/cmd/notificationsteams.go index 3f7281ad..bac5212c 100644 --- a/cmd/notificationsteams.go +++ b/cmd/notificationsteams.go @@ -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" @@ -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(), ¬ification, 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) @@ -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") diff --git a/cmd/notificationswebhook.go b/cmd/notificationswebhook.go index f6703417..35021065 100644 --- a/cmd/notificationswebhook.go +++ b/cmd/notificationswebhook.go @@ -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" @@ -37,37 +40,54 @@ It does not configure a project to send notifications to webhook though, you nee 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 webhook 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.AddNotificationWebhookInput{ - Name: name, - Webhook: webhook, + + notification := s.AddNotificationWebhookInput{ + Name: name, + Webhook: webhook, + Organization: &organizationID, } - result, err := lagoon.AddNotificationWebhook(context.TODO(), notification, lc) + + result, err := l.AddNotificationWebhook(context.TODO(), ¬ification, 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) @@ -376,6 +396,7 @@ var updateWebhookNotificationCmd = &cobra.Command{ func init() { addNotificationWebhookCmd.Flags().StringP("name", "n", "", "The name of the notification") addNotificationWebhookCmd.Flags().StringP("webhook", "w", "", "The webhook URL of the notification") + addNotificationWebhookCmd.Flags().Uint("organization-id", 0, "ID of the Organization") addProjectNotificationWebhookCmd.Flags().StringP("name", "n", "", "The name of the notification") deleteProjectWebhookNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification") deleteWebhookNotificationCmd.Flags().StringP("name", "n", "", "The name of the notification") diff --git a/go.mod b/go.mod index e3ab3077..8053cdde 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/spf13/cobra v0.0.5 github.com/spf13/pflag v1.0.3 github.com/stretchr/testify v1.8.2 - github.com/uselagoon/machinery v0.0.14 + github.com/uselagoon/machinery v0.0.15 golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b gopkg.in/yaml.v2 v2.2.8 sigs.k8s.io/yaml v1.2.0 diff --git a/go.sum b/go.sum index b43ef6e6..500408d3 100644 --- a/go.sum +++ b/go.sum @@ -113,6 +113,10 @@ github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYv github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/uselagoon/machinery v0.0.14 h1:2w0JAgxasGXyh61yLEmvGLqVpwCm5ip02v17Wicuuio= github.com/uselagoon/machinery v0.0.14/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.15-0.20231214035056-b3e9d4c4b4dd h1:8cWhSDjFg96h95V190E4315dzJQk/G/o3/A8kG/eKgU= +github.com/uselagoon/machinery v0.0.15-0.20231214035056-b3e9d4c4b4dd/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= +github.com/uselagoon/machinery v0.0.15 h1:mkHloqCNwqAqFJyuCGsAtnYCbsQB3Wez1o7DY8ypTBo= +github.com/uselagoon/machinery v0.0.15/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=