Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API calls for egress IPs #104

Merged
merged 5 commits into from
Sep 24, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.14 // indirect
github.com/vektah/gqlparser/v2 v2.5.16 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ github.com/superfly/graphql v0.2.4 h1:Av8hSk4x8WvKJ6MTnEwrLknSVSGPc7DWpgT3z/kt3P
github.com/superfly/graphql v0.2.4/go.mod h1:CVfDl31srm8HnJ9udwLu6hFNUW/P6GUM2dKcG1YQ8jc=
github.com/superfly/macaroon v0.2.13 h1:WEZnifapjW5yuCEsdZxqCq4X8xuzIf+AUVPv6lm7GtI=
github.com/superfly/macaroon v0.2.13/go.mod h1:Kt6/EdSYfFjR4GIe+erMwcJgU8iMu1noYVceQ5dNdKo=
github.com/vektah/gqlparser/v2 v2.5.14 h1:dzLq75BJe03jjQm6n56PdH1oweB8ana42wj7E4jRy70=
github.com/vektah/gqlparser/v2 v2.5.14/go.mod h1:WQQjFc+I1YIzoPvZBhUQX7waZgg3pMLi0r8KymvAE2w=
github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8=
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
Expand Down
68 changes: 68 additions & 0 deletions resource_ip_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,74 @@ func (c *Client) AllocateSharedIPAddress(ctx context.Context, appName string) (n
return net.ParseIP(data.AllocateIPAddress.App.SharedIPAddress), nil
}

func (c *Client) AllocateEgressIPAddress(ctx context.Context, appName string, machineId string) (net.IP, net.IP, error) {
query := `
mutation($input: AllocateEgressIPAddressInput!) {
allocateEgressIpAddress(input: $input) {
v4,
v6
}
}
`

req := c.NewRequest(query)
ctx = ctxWithAction(ctx, "allocate_egress_ip_address")
req.Var("input", AllocateEgressIPAddressInput{AppID: appName, MachineID: machineId})

data, err := c.RunWithContext(ctx, req)
if err != nil {
return nil, nil, err
}

return net.ParseIP(data.AllocateEgressIPAddress.V4), net.ParseIP(data.AllocateEgressIPAddress.V6), nil
}

func (c *Client) GetEgressIPAddresses(ctx context.Context, appName string) (map[string][]EgressIPAddress, error) {
query := `
query ($appName: String!) {
app(name: $appName) {
machines {
nodes {
id
egressIpAddresses {
nodes {
id
ip
version
region
}
}
}
}
}
}
`

req := c.NewRequest(query)
ctx = ctxWithAction(ctx, "get_egress_ip_addresses")
req.Var("appName", appName)

data, err := c.RunWithContext(ctx, req)
if err != nil {
return nil, err
}

ret := make(map[string][]EgressIPAddress)
for _, m := range data.App.Machines.Nodes {
if m.EgressIpAddresses.Nodes == nil || len(m.EgressIpAddresses.Nodes) == 0 {
continue
}

ret[m.ID] = make([]EgressIPAddress, len(m.EgressIpAddresses.Nodes))

for i, ip := range m.EgressIpAddresses.Nodes {
ret[m.ID][i] = *ip
}
}

return ret, nil
}

func (c *Client) ReleaseIPAddress(ctx context.Context, appName string, ip string) error {
query := `
mutation($input: ReleaseIPAddressInput!) {
Expand Down
70 changes: 70 additions & 0 deletions 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 @@ -5006,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 @@ -6321,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
24 changes: 24 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type Query struct {
App App
IPAddress IPAddress
}
AllocateEgressIPAddress struct {
V4 string
V6 string
}
ReleaseIPAddress struct {
App App
}
Expand Down Expand Up @@ -276,6 +280,10 @@ type App struct {
Nodes []LimitedAccessToken
}

Machines struct {
Nodes []GqlMachine
}

CurrentLock *struct {
LockID string
Expiration string
Expand Down Expand Up @@ -509,6 +517,13 @@ type IPAddress struct {
}
}

type EgressIPAddress struct {
ID string
IP string
Version int
Region string
}

type VMSize struct {
Name string
CPUCores float32
Expand Down Expand Up @@ -680,6 +695,11 @@ type AllocateIPAddressInput struct {
Network string `json:"network,omitempty"`
}

type AllocateEgressIPAddressInput struct {
AppID string `json:"appId"`
MachineID string `json:"machineId"`
}

type ReleaseIPAddressInput struct {
AppID *string `json:"appId"`
IPAddressID *string `json:"ipAddressId"`
Expand Down Expand Up @@ -856,6 +876,10 @@ type GqlMachine struct {
IPs struct {
Nodes []*MachineIP
}

EgressIpAddresses struct {
Nodes []*EgressIPAddress
}
}

type Logger interface {
Expand Down
Loading