Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cli/cmd/enterprise_portal_invite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"fmt"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/kotsclient"
"github.com/replicatedhq/replicated/pkg/types"
"github.com/spf13/cobra"
)

func (r *runners) InitEnterprisePortalInviteCmd(parent *cobra.Command) *cobra.Command {
var customer string
var outputFormat string

cmd := &cobra.Command{
Use: "invite",

RunE: func(cmd *cobra.Command, args []string) error {
return r.enterprisePortalInvite(cmd, r.appID, customer, args, outputFormat)
},
SilenceUsage: true,
}
parent.AddCommand(cmd)

cmd.Flags().StringVar(&customer, "customer", "", "The customer name or ID to invite")
cmd.Flags().StringVar(&outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")

return cmd
}

func (r *runners) enterprisePortalInvite(cmd *cobra.Command, appID string, customer string, emailAddresses []string, outputFormat string) error {
var c *types.Customer

// try to get the customer as if we have an id first
cc, err := r.api.GetCustomerByID(customer)
if err != nil && err != kotsclient.ErrNotFound {
return errors.Wrapf(err, "find customer %q", customer)
}
if cc != nil {
c = cc
}

if appID == "" {
return errors.Errorf("app required")
}

if c == nil {
// try to get the customer as if we have a name
cc, err := r.api.GetCustomerByName(r.appID, customer)
if err != nil {
return errors.Wrapf(err, "find customer %q", customer)
}
if cc != nil {
c = cc
}
}

if c == nil {
return errors.Errorf("customer %q not found", customer)
}

for _, emailAddress := range emailAddresses {
url, err := r.kotsAPI.SendEnterprisePortalInvite(appID, c.ID, emailAddress)
if err != nil {
return err
}

fmt.Printf("%s: %s\n", emailAddress, url)
}
return nil
}
14 changes: 14 additions & 0 deletions cli/cmd/enterprise_portal_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/spf13/cobra"
)

func (r *runners) InitEnterprisePortalUserCmd(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "user",
}
parent.AddCommand(cmd)

return cmd
}
49 changes: 49 additions & 0 deletions cli/cmd/enterprise_portal_user_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

type listEnterprisePortalUsersOpts struct {
includeInvites bool
}

func (r *runners) InitEnterprisePortalUserLsCmd(parent *cobra.Command) *cobra.Command {
opts := listEnterprisePortalUsersOpts{}
var outputFormat string

cmd := &cobra.Command{
Use: "ls",
Short: "List enterprise portal users",
Long: `List all users associated with the enterprise portal for an application.`,
Example: "",
RunE: func(cmd *cobra.Command, args []string) error {
return r.enterprisePortalUserLs(cmd, r.appID, opts, outputFormat)
},
SilenceUsage: true,
}
parent.AddCommand(cmd)

cmd.Flags().BoolVar(&opts.includeInvites, "include-invites", false, "Include invites")
cmd.Flags().StringVar(&outputFormat, "output", "table", "The output format to use. One of: json|table (default: table)")

return cmd
}

func (r *runners) enterprisePortalUserLs(cmd *cobra.Command, appID string, opts listEnterprisePortalUsersOpts, outputFormat string) error {
users, err := r.kotsAPI.ListEnterprisePortalUsers(appID, opts.includeInvites)
if err != nil {
return err
}

if len(users) == 0 {
return nil
}

for _, user := range users {
fmt.Printf("%s\n", user.Email)
}
return nil
}
3 changes: 3 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func Execute(rootCmd *cobra.Command, stdin io.Reader, stdout io.Writer, stderr i
enterprisePortalStatusCmd := runCmds.InitEnterprisePortalStatusCmd(enterprisePortalCmd)
runCmds.InitEnterprisePortalStatusGetCmd(enterprisePortalStatusCmd)
runCmds.InitEnterprisePortalStatusUpdateCmd(enterprisePortalStatusCmd)
runCmds.InitEnterprisePortalInviteCmd(enterprisePortalCmd)
enterprisePortalUserCmd := runCmds.InitEnterprisePortalUserCmd(enterprisePortalCmd)
runCmds.InitEnterprisePortalUserLsCmd(enterprisePortalUserCmd)

registryCmd := runCmds.InitRegistryCommand(runCmds.rootCmd)
runCmds.InitRegistryList(registryCmd)
Expand Down
47 changes: 47 additions & 0 deletions pkg/kotsclient/enterprise-portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,50 @@ func (c *VendorV3Client) UpdateEnterprisePortalStatus(appID string, status strin

return response.Status, nil
}

type InviteEnterprisePortalResponse struct {
URL string `json:"url"`
}

type InviteEnterprisePortalRequest struct {
CustomerID string `json:"customer_id"`
EmailAddress string `json:"email_address"`
}

func (c *VendorV3Client) SendEnterprisePortalInvite(appID string, customerID string, emailAddress string) (string, error) {
var response = InviteEnterprisePortalResponse{}

var request = InviteEnterprisePortalRequest{
CustomerID: customerID,
EmailAddress: emailAddress,
}

err := c.DoJSON("POST", fmt.Sprintf("/v3/app/%s/enterprise-portal/customer-user", appID), http.StatusCreated, request, &response)
if err != nil {
return "", errors.Wrap(err, "send enterprise portal invite")
}

return response.URL, nil
}

type EnterprisePortalUser struct {
Email string `json:"email"`
}

func (c *VendorV3Client) ListEnterprisePortalUsers(appID string, includeInvites bool) ([]EnterprisePortalUser, error) {
var response = struct {
Users []EnterprisePortalUser `json:"users"`
}{}

endpoint := fmt.Sprintf("/v3/app/%s/enterprise-portal/customer-users", appID)
if includeInvites {
endpoint = fmt.Sprintf("%s?includeInvites=true", endpoint)
}

err := c.DoJSON("GET", endpoint, http.StatusOK, nil, &response)
if err != nil {
return nil, errors.Wrap(err, "list enterprise portal users")
}

return response.Users, nil
}
Loading