repo init

This commit is contained in:
2024-12-13 16:37:20 -06:00
commit 4d98678275
12 changed files with 305 additions and 0 deletions

42
api/client.go Normal file
View File

@@ -0,0 +1,42 @@
package api
import "net/http"
// Client handles API communication with the Vast.ai API
type Client struct {
apiKey string
baseURL string
httpClient *http.Client
}
// Error represents an API error response
type APIError struct {
Success bool `json:"success"`
Error string `json:"error"`
Msg string `json:"msg"`
}
// NewClient creates a new Vast.ai API client
func NewClient(apiKey string) *Client {
return &Client{
apiKey: apiKey,
baseURL: "https://console.vast.ai/api/v0",
httpClient: &http.Client{},
}
}
// Apply applies the Bearer token to the request
func (c *Client) Apply(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+c.apiKey)
}
// makeRequest creates and sends an HTTP request with the provided method, path and body
func (c *Client) makeRequest(method, path string, body interface{}) (*http.Response, error) {
req, err := http.NewRequest(method, c.baseURL+path, nil)
if err != nil {
return nil, err
}
c.Apply(req)
return c.httpClient.Do(req)
}

3
api/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module xevion.dev/quickvast/api
go 1.23.3

52
api/instances.go Normal file
View File

@@ -0,0 +1,52 @@
package api
import (
"encoding/json"
"fmt"
"net/http"
)
// InstanceGetResponse represents the response from GET /instances
type InstanceGetResponse struct {
Instances []Instance `json:"instances"`
}
// GetInstances retrieves all instances for the authenticated user
func (c *Client) GetInstances() (*InstanceGetResponse, error) {
resp, err := c.makeRequest(http.MethodGet, "/instances", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result InstanceGetResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
// DeleteInstance removes an instance by ID
func (c *Client) DeleteInstance(id int) error {
path := fmt.Sprintf("/instances/%d", id)
resp, err := c.makeRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
// PutInstance updates an instance's status
func (c *Client) PutInstance(id int, status string) error {
path := fmt.Sprintf("/instances/%d", id)
data := map[string]string{"status": status}
resp, err := c.makeRequest(http.MethodPut, path, data)
if err != nil {
return err
}
resp.Body.Close()
return nil
}

99
api/types.go Normal file
View File

@@ -0,0 +1,99 @@
package api
type PortMapping struct {
HostIp string `json:"HostIp"`
HostPort string `json:"HostPort"`
}
type Ports struct {
TCP22 []PortMapping `json:"22/tcp"`
TCP8080 []PortMapping `json:"8080/tcp"`
UDP8080 []PortMapping `json:"8080/udp"`
}
type Instance struct {
IsBid bool `json:"is_bid"`
InetUpBilled float64 `json:"inet_up_billed"`
InetDownBilled float64 `json:"inet_down_billed"`
External bool `json:"external"`
Webpage *string `json:"webpage"`
Logo string `json:"logo"`
Rentable bool `json:"rentable"`
ComputeCap int `json:"compute_cap"`
DriverVersion string `json:"driver_version"`
CudaMaxGood int `json:"cuda_max_good"`
MachineID int `json:"machine_id"`
HostingType *string `json:"hosting_type"`
PublicIPAddr string `json:"public_ipaddr"`
Geolocation string `json:"geolocation"`
FlopsPerDPHTotal float64 `json:"flops_per_dphtotal"`
DLPerfPerDPHTotal float64 `json:"dlperf_per_dphtotal"`
Reliability2 float64 `json:"reliability2"`
HostRunTime int64 `json:"host_run_time"`
HostID int `json:"host_id"`
ID int `json:"id"`
BundleID int `json:"bundle_id"`
NumGPUs int `json:"num_gpus"`
TotalFlops float64 `json:"total_flops"`
MinBid float64 `json:"min_bid"`
DPHBase float64 `json:"dph_base"`
DPHTotal float64 `json:"dph_total"`
GPUName string `json:"gpu_name"`
GPURam int `json:"gpu_ram"`
GPUDisplayActive bool `json:"gpu_display_active"`
GPUMemBW float64 `json:"gpu_mem_bw"`
BWNVLink int `json:"bw_nvlink"`
DirectPortCount int `json:"direct_port_count"`
GPULanes int `json:"gpu_lanes"`
PCIeBW float64 `json:"pcie_bw"`
PCIGen int `json:"pci_gen"`
DLPerf float64 `json:"dlperf"`
CPUName string `json:"cpu_name"`
MoboName string `json:"mobo_name"`
CPURam int `json:"cpu_ram"`
CPUCores int `json:"cpu_cores"`
CPUCoresEffective float64 `json:"cpu_cores_effective"`
GPUFrac float64 `json:"gpu_frac"`
HasAVX int `json:"has_avx"`
DiskSpace float64 `json:"disk_space"`
DiskName string `json:"disk_name"`
DiskBW float64 `json:"disk_bw"`
InetUp float64 `json:"inet_up"`
InetDown float64 `json:"inet_down"`
StartDate float64 `json:"start_date"`
EndDate int64 `json:"end_date"`
Duration float64 `json:"duration"`
StorageCost float64 `json:"storage_cost"`
InetUpCost float64 `json:"inet_up_cost"`
InetDownCost float64 `json:"inet_down_cost"`
StorageTotalCost float64 `json:"storage_total_cost"`
Verification string `json:"verification"`
Score float64 `json:"score"`
SSHIdx string `json:"ssh_idx"`
SSHHost string `json:"ssh_host"`
SSHPort int `json:"ssh_port"`
ActualStatus string `json:"actual_status"`
IntendedStatus string `json:"intended_status"`
CurState string `json:"cur_state"`
NextState string `json:"next_state"`
ImageUUID string `json:"image_uuid"`
ImageArgs []string `json:"image_args"`
ImageRuntype string `json:"image_runtype"`
ExtraEnv string `json:"extra_env"`
OnStart string `json:"onstart"`
Label *string `json:"label"`
JupyterToken string `json:"jupyter_token"`
StatusMsg string `json:"status_msg"`
GPUUtil float64 `json:"gpu_util"`
DiskUtil float64 `json:"disk_util"`
GPUTemp float64 `json:"gpu_temp"`
LocalIPAddrs string `json:"local_ipaddrs"`
DirectPortEnd int `json:"direct_port_end"`
DirectPortStart int `json:"direct_port_start"`
CPUUtil float64 `json:"cpu_util"`
MemUsage float64 `json:"mem_usage"`
MemLimit float64 `json:"mem_limit"`
VMemUsage float64 `json:"vmem_usage"`
MachineDirSSHPort int `json:"machine_dir_ssh_port"`
Ports Ports `json:"ports"`
}