diff --git a/resource_volumes.go b/resource_volumes.go index ece0f86..10b562f 100644 --- a/resource_volumes.go +++ b/resource_volumes.go @@ -1,6 +1,8 @@ package fly -import "context" +import ( + "context" +) func (c *Client) GetAppNameFromVolume(ctx context.Context, volID string) (*string, error) { query := ` @@ -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 +} diff --git a/types.go b/types.go index 69357f5..381ed39 100644 --- a/types.go +++ b/types.go @@ -32,6 +32,10 @@ type Query struct { App struct { Name string } + State string + Snapshots struct { + Nodes []VolumeSnapshotGql + } } Domain *Domain diff --git a/volume_types.go b/volume_types.go index aa7fa74..9fe8476 100644 --- a/volume_types.go +++ b/volume_types.go @@ -1,6 +1,9 @@ package fly -import "time" +import ( + "strconv" + "time" +) type Volume struct { ID string `json:"id"` @@ -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, + } +}