|
| 1 | +package service_clients |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log/slog" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +type MachineConfig struct { |
| 15 | + Name string `json:"name"` |
| 16 | + Config struct { |
| 17 | + Image string `json:"image"` |
| 18 | + Env struct { |
| 19 | + CloneUrl string `json:"DIGGER_GITHUB_REPO_CLONE_URL"` |
| 20 | + Branch string `json:"DIGGER_GITHUB_REPO_CLONE_BRANCH"` |
| 21 | + GithubToken string `json:"DIGGER_GITHUB_TOKEN"` |
| 22 | + RepoFullName string `json:"DIGGER_REPO_FULL_NAME"` |
| 23 | + OrgId string `json:"DIGGER_ORG_ID"` |
| 24 | + } `json:"env"` |
| 25 | + Guest struct { |
| 26 | + CPUs int `json:"cpus"` |
| 27 | + CPUKind string `json:"cpu_kind"` |
| 28 | + MemoryMB int `json:"memory_mb"` |
| 29 | + } `json:"guest"` |
| 30 | + AutoDestroy bool `json:"auto_destroy"` |
| 31 | + } `json:"config"` |
| 32 | +} |
| 33 | + |
| 34 | +type MachineResponse struct { |
| 35 | + ID string `json:"id"` |
| 36 | +} |
| 37 | + |
| 38 | +type QueuedResponse struct { |
| 39 | + Queued string `json:"queued"` |
| 40 | +} |
| 41 | + |
| 42 | +func TriggerProjectsRefreshService(cloneUrl string, branch string, githubToken string, repoFullName string, orgId string) (*MachineResponse, error) { |
| 43 | + |
| 44 | + slog.Debug("awaiting machine fetch") |
| 45 | + |
| 46 | + // Prepare machine configuration |
| 47 | + machineConfig := MachineConfig{ |
| 48 | + Name: fmt.Sprintf("hello-%d", time.Now().UnixMilli()), |
| 49 | + } |
| 50 | + |
| 51 | + machineConfig.Config.Image = "registry.fly.io/projects-refresh-service:latest" |
| 52 | + machineConfig.Config.Env.CloneUrl = cloneUrl |
| 53 | + machineConfig.Config.Env.Branch = branch |
| 54 | + machineConfig.Config.Env.GithubToken = githubToken |
| 55 | + machineConfig.Config.Env.RepoFullName = repoFullName |
| 56 | + machineConfig.Config.Env.OrgId = orgId |
| 57 | + |
| 58 | + machineConfig.Config.Guest.CPUs = 1 |
| 59 | + machineConfig.Config.Guest.CPUKind = "shared" |
| 60 | + machineConfig.Config.Guest.MemoryMB = 256 |
| 61 | + machineConfig.Config.AutoDestroy = true |
| 62 | + |
| 63 | + // Marshal JSON payload |
| 64 | + payload, err := json.Marshal(machineConfig) |
| 65 | + if err != nil { |
| 66 | + slog.Error("Error creating machine config", "error", err) |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + // Create HTTP request |
| 71 | + apiURL := fmt.Sprintf("https://api.machines.dev/v1/apps/%s/machines", os.Getenv("DIGGER_PROJECTS_SVC_APP_NAME")) |
| 72 | + req2, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payload)) |
| 73 | + if err != nil { |
| 74 | + slog.Error("Error creating request", "error", err) |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + // Set headers |
| 79 | + req2.Header.Set("Authorization", "Bearer "+os.Getenv("FLY_PROJECTS_SVC_API_TOKEN")) |
| 80 | + req2.Header.Set("Content-Type", "application/json") |
| 81 | + |
| 82 | + // Make HTTP request |
| 83 | + client := &http.Client{} |
| 84 | + resp, err := client.Do(req2) |
| 85 | + if err != nil { |
| 86 | + slog.Error("Error making request", "error", err) |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + defer resp.Body.Close() |
| 90 | + |
| 91 | + if resp.StatusCode != 200 { |
| 92 | + body, err2 := io.ReadAll(resp.Body) |
| 93 | + slog.Error("Error triggering projects refresh service", "statusCode", resp.StatusCode, "body", body, "readyErr", err2) |
| 94 | + return nil, fmt.Errorf("error triggering projects refresh service") |
| 95 | + } |
| 96 | + |
| 97 | + // Parse response |
| 98 | + var machineResp MachineResponse |
| 99 | + if err := json.NewDecoder(resp.Body).Decode(&machineResp); err != nil { |
| 100 | + slog.Error("Error parsing response", "error", err) |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + slog.Debug("triggered projects refresh service", "machineId", machineResp.ID, "statusCode", resp.StatusCode) |
| 105 | + |
| 106 | + return &MachineResponse{ID: machineResp.ID}, nil |
| 107 | +} |
0 commit comments