Skip to content

Commit

Permalink
basic wg api
Browse files Browse the repository at this point in the history
  • Loading branch information
tqbf committed Dec 1, 2020
1 parent 10b37af commit 71a28ec
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
72 changes: 72 additions & 0 deletions api/resource_wireguard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package api

func (c *Client) GetWireGuardPeers(slug string) ([]*WireGuardPeer, error) {
req := c.NewRequest(`
query($slug: String!) {
organization(slug: $slug) {
wireGuardPeers {
nodes {
id
name
pubkey
region
peerip
}
}
}
}
`)
req.Var("slug", slug)

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

return *data.Organization.WireGuardPeers.Nodes, nil
}

func (c *Client) CreateWireGuardPeer(org *Organization, region, name string) (*CreatedWireGuardPeer, error) {
req := c.NewRequest(`
mutation($input: AddWireGuardPeerInput!) {
addWireGuardPeer(input: $input) {
pubkey
privkey
peerip
endpointip
}
}
`)
req.Var("input", map[string]interface{}{
"organizationId": org.ID,
"region": region,
"name": name,
})

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

return &data.AddWireGuardPeer, nil
}

func (c *Client) RemoveWireGuardPeer(org *Organization, name string) error {
req := c.NewRequest(`
mutation($input: RemoveWireGuardPeerInput!) {
removeWireGuardPeer(input: $input) {
organization {
id
}
}
}
`)
req.Var("input", map[string]interface{}{
"organizationId": org.ID,
"name": name,
})

_, err := c.Run(req)

return err
}
30 changes: 30 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ type Query struct {

CreateVolume CreateVolumePayload
DeleteVolume DeleteVolumePayload

AddWireGuardPeer CreatedWireGuardPeer

RemoveWireGuardPeer struct {
Organization Organization
}
}

// carries the privkey; this is the only time it can be retrieved
type CreatedWireGuardPeer struct {
Pubkey string
Privkey string
Peerip string
Endpointip string
}

type Definition map[string]interface{}
Expand Down Expand Up @@ -278,6 +292,14 @@ type Organization struct {
Node *Domain
}
}

WireGuardPeers struct {
Nodes *[]*WireGuardPeer
Edges *[]*struct {
Cursor *string
Node *WireGuardPeer
}
}
}

type OrganizationDetails struct {
Expand Down Expand Up @@ -736,3 +758,11 @@ type ImportDnsWarning struct {
}
Message string
}

type WireGuardPeer struct {
ID string
Pubkey string
Region string
Name string
Peerip string
}

0 comments on commit 71a28ec

Please sign in to comment.