Skip to content

Commit

Permalink
Implement flyctl m egress-ip list
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterCxy committed Sep 12, 2024
1 parent 4c9ef7f commit 129ac98
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 1 deletion.
87 changes: 86 additions & 1 deletion gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,11 @@ scalar BigInt

enum BillingStatus {
CURRENT
DELINQUENT
PAST_DUE
SOURCE_REQUIRED
TRIAL_ACTIVE
TRIAL_ENDED
}

type Build implements Node {
Expand Down Expand Up @@ -2804,7 +2807,7 @@ input CreateBuildInput {
appName: ID!

"""
Whether builder is remote or local
The kind of builder being used
"""
builderType: String!

Expand Down Expand Up @@ -3452,6 +3455,11 @@ input CreateReleaseInput {
"""
appId: ID!

"""
The build ID linked to the release
"""
buildId: ID

"""
A unique identifier for the client performing the mutation.
"""
Expand Down Expand Up @@ -3626,6 +3634,11 @@ input CreateUserSignupInput {
Timestamp of when the user arrived at the website
"""
startedAt: String!

"""
UTM query string params
"""
utmParams: JSON
}

"""
Expand Down Expand Up @@ -4996,6 +5009,52 @@ type DummyWireGuardPeerPayload {
pubkey: String!
}

type EgressIPAddress implements Node {
"""
ID of the object.
"""
id: ID!
ip: String!
region: String!
version: Int!
}

"""
The connection type for EgressIPAddress.
"""
type EgressIPAddressConnection {
"""
A list of edges.
"""
edges: [EgressIPAddressEdge]

"""
A list of nodes.
"""
nodes: [EgressIPAddress]

"""
Information to aid in pagination.
"""
pageInfo: PageInfo!
totalCount: Int!
}

"""
An edge in a connection.
"""
type EgressIPAddressEdge {
"""
A cursor for use in pagination.
"""
cursor: String!

"""
The item at the end of the edge.
"""
node: EgressIPAddress
}

type EmptyAppRole implements AppRole {
"""
The name of this role
Expand Down Expand Up @@ -5086,6 +5145,11 @@ input EnsureDepotRemoteBuilderInput {
"""
appName: String

"""
The scope of the builder; either "app" or "organization"
"""
builderScope: String

"""
A unique identifier for the client performing the mutation.
"""
Expand Down Expand Up @@ -6306,6 +6370,27 @@ type Machine implements Node {
app: App!
config: JSON!
createdAt: ISO8601DateTime!
egressIpAddresses(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String

"""
Returns the elements in the list that come before the specified cursor.
"""
before: String

"""
Returns the first _n_ elements from the list.
"""
first: Int

"""
Returns the last _n_ elements from the list.
"""
last: Int
): EgressIPAddressConnection!
events(
"""
Returns the elements in the list that come after the specified cursor.
Expand Down
51 changes: 51 additions & 0 deletions internal/command/machine/egress_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package machine
import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)

func newEgressIp() *cobra.Command {
Expand All @@ -25,6 +28,7 @@ func newEgressIp() *cobra.Command {

cmd.AddCommand(
newAllocateEgressIp(),
newListEgressIps(),
)

return cmd
Expand Down Expand Up @@ -52,6 +56,27 @@ func newAllocateEgressIp() *cobra.Command {
return cmd
}

func newListEgressIps() *cobra.Command {
const (
long = `List all allocated static egress IP addresses with their corresponding machine`
short = `List all allocated static egress IPs`
)

cmd := command.New("list", short, long, runListEgressIps,
command.RequireSession,
command.LoadAppNameIfPresent,
)

flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)

cmd.Args = cobra.NoArgs

return cmd
}

func runAllocateEgressIP(ctx context.Context) (err error) {
var (
args = flag.Args(ctx)
Expand Down Expand Up @@ -86,3 +111,29 @@ Are you sure this is what you want?`
fmt.Printf("IPv6: %s\n", ipv6.String())
return nil
}

func runListEgressIps(ctx context.Context) (err error) {
var (
client = flyutil.ClientFromContext(ctx)
appName = appconfig.NameFromContext(ctx)
)

machineIPs, err := client.GetEgressIPAddresses(ctx, appName)
if err != nil {
return err
}

rows := make([][]string, 0, 1)

for machine, ips := range machineIPs {
ipStr := make([]string, len(ips))
for _, ip := range ips {
ipStr = append(ipStr, ip.String())
}
rows = append(rows, []string{machine, strings.Join(ipStr, ",")})
}

out := iostreams.FromContext(ctx).Out
render.Table(out, "", rows, "Machine", "Egress IPs")
return nil
}
1 change: 1 addition & 0 deletions internal/flyutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type Client interface {
GetDomain(ctx context.Context, name string) (*fly.Domain, error)
GetDomains(ctx context.Context, organizationSlug string) ([]*fly.Domain, error)
GetIPAddresses(ctx context.Context, appName string) ([]fly.IPAddress, error)
GetEgressIPAddresses(ctx context.Context, appName string) (map[string][]net.IP, error)
GetLatestImageDetails(ctx context.Context, image string) (*fly.ImageVersion, error)
GetLatestImageTag(ctx context.Context, repository string, snapshotId *string) (string, error)
GetLoggedCertificates(ctx context.Context, slug string) ([]fly.LoggedCertificate, error)
Expand Down
4 changes: 4 additions & 0 deletions internal/inmem/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ func (m *Client) GetIPAddresses(ctx context.Context, appName string) ([]fly.IPAd
return nil, nil // TODO
}

func (c *Client) GetEgressIPAddresses(ctx context.Context, appName string) (map[string][]net.IP, error) {
panic("TODO")
}

func (m *Client) GetLatestImageDetails(ctx context.Context, image string) (*fly.ImageVersion, error) {
panic("TODO")
}
Expand Down
9 changes: 9 additions & 0 deletions internal/mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Client struct {
GetDomainFunc func(ctx context.Context, name string) (*fly.Domain, error)
GetDomainsFunc func(ctx context.Context, organizationSlug string) ([]*fly.Domain, error)
GetIPAddressesFunc func(ctx context.Context, appName string) ([]fly.IPAddress, error)
GetEgressIPAddressesFunc func(ctx context.Context, appName string) (map[string][]net.IP, error)
GetLatestImageDetailsFunc func(ctx context.Context, image string) (*fly.ImageVersion, error)
GetLatestImageTagFunc func(ctx context.Context, repository string, snapshotId *string) (string, error)
GetLoggedCertificatesFunc func(ctx context.Context, slug string) ([]fly.LoggedCertificate, error)
Expand Down Expand Up @@ -117,6 +118,10 @@ func (m *Client) AllocateSharedIPAddress(ctx context.Context, appName string) (n
return m.AllocateSharedIPAddressFunc(ctx, appName)
}

func (m *Client) AllocateEgressIPAddress(ctx context.Context, appName string, machineId string) (net.IP, net.IP, error) {
return m.AllocateEgressIPAddressFunc(ctx, appName, machineId)
}

func (m *Client) AppNameAvailable(ctx context.Context, appName string) (bool, error) {
return m.AppNameAvailableFunc(ctx, appName)
}
Expand Down Expand Up @@ -325,6 +330,10 @@ func (m *Client) GetIPAddresses(ctx context.Context, appName string) ([]fly.IPAd
return m.GetIPAddressesFunc(ctx, appName)
}

func (m *Client) GetEgressIPAddresses(ctx context.Context, appName string) (map[string][]net.IP, error) {
return m.GetEgressIPAddressesFunc(ctx, appName)
}

func (m *Client) GetLatestImageDetails(ctx context.Context, image string) (*fly.ImageVersion, error) {
return m.GetLatestImageDetailsFunc(ctx, image)
}
Expand Down

0 comments on commit 129ac98

Please sign in to comment.