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

Additional volume queries & types #51

Merged
merged 1 commit into from
Apr 27, 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
65 changes: 64 additions & 1 deletion resource_volumes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fly

import "context"
import (
"context"
)

func (c *Client) GetAppNameFromVolume(ctx context.Context, volID string) (*string, error) {
query := `
Expand All @@ -27,3 +29,64 @@ query($id: ID!) {

return &data.Volume.App.Name, nil
}

func (c *Client) GetAppNameStateFromVolume(ctx context.Context, volID string) (*string, *string, error) {
query := `
query GetAppNameStateFromVolume($id: ID!) {
volume: node(id: $id) {
... on Volume {
app {
name
}
state
}
}
}
`

req := c.NewRequest(query)

req.Var("id", volID)
ctx = ctxWithAction(ctx, "get_app_name_state_from_volume")

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

return &data.Volume.App.Name, &data.Volume.State, nil
}

func (c *Client) GetSnapshotsFromVolume(ctx context.Context, volID string) ([]VolumeSnapshot, error) {
query := `
query GetSnapshotsFromVolume($id: ID!) {
volume: node(id: $id) {
... on Volume {
snapshots {
nodes {
id
size
digest
createdAt
}
}
}
}
}
`

req := c.NewRequest(query)

req.Var("id", volID)
ctx = ctxWithAction(ctx, "get_snapshots_from_volume")

data, err := c.RunWithContext(ctx, req)
if err != nil {
return nil, err
}
var snapshots []VolumeSnapshot
for _, snapshot := range data.Volume.Snapshots.Nodes {
snapshots = append(snapshots, NewVolumeSnapshotFrom(snapshot))
}
return snapshots, nil
}
4 changes: 4 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type Query struct {
App struct {
Name string
}
State string
Snapshots struct {
Nodes []VolumeSnapshotGql
}
}
Domain *Domain

Expand Down
24 changes: 23 additions & 1 deletion volume_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fly

import "time"
import (
"strconv"
"time"
)

type Volume struct {
ID string `json:"id"`
Expand Down Expand Up @@ -59,3 +62,22 @@ type VolumeSnapshot struct {
CreatedAt time.Time `json:"created_at"`
Status string `json:"status"`
}

type VolumeSnapshotGql struct {
ID string `json:"id"`
Size string `json:"size"`
Digest string `json:"digest"`
CreatedAt time.Time `json:"createdAt"`
Status string `json:"status"`
}

func NewVolumeSnapshotFrom(v VolumeSnapshotGql) VolumeSnapshot {
size, _ := strconv.Atoi(v.Size)
return VolumeSnapshot{
ID: v.ID,
Size: size,
Digest: v.Digest,
CreatedAt: v.CreatedAt,
Status: v.Status,
}
}