|
8 | 8 | "fmt"
|
9 | 9 | "io"
|
10 | 10 | "net/http"
|
| 11 | + "time" |
11 | 12 |
|
12 | 13 | "github.com/content-services/content-sources-backend/pkg/api"
|
13 | 14 | "github.com/content-services/content-sources-backend/pkg/cache"
|
@@ -35,6 +36,18 @@ type AppstreamEntity struct {
|
35 | 36 | Impl string `json:"impl"`
|
36 | 37 | }
|
37 | 38 |
|
| 39 | +type LifecycleResponse struct { |
| 40 | + Data []LifecycleEntity `json:"data"` |
| 41 | +} |
| 42 | + |
| 43 | +type LifecycleEntity struct { |
| 44 | + Name string `json:"name"` |
| 45 | + StartDate string `json:"start_date"` |
| 46 | + EndDate string `json:"end_date"` |
| 47 | + Major int `json:"major"` |
| 48 | + Minor int `json:"minor"` |
| 49 | +} |
| 50 | + |
38 | 51 | func encodedIdentity(ctx context.Context) (string, error) {
|
39 | 52 | id := identity.GetIdentity(ctx)
|
40 | 53 | jsonIdentity, err := json.Marshal(id)
|
@@ -107,3 +120,89 @@ func (rc roadmapClient) GetAppstreams(ctx context.Context) (AppstreamsResponse,
|
107 | 120 |
|
108 | 121 | return appStreamResp, statusCode, nil
|
109 | 122 | }
|
| 123 | + |
| 124 | +func (rc roadmapClient) GetRhelLifecycle(ctx context.Context) (LifecycleResponse, int, error) { |
| 125 | + statusCode := http.StatusInternalServerError |
| 126 | + server := config.Get().Clients.Roadmap.Server |
| 127 | + var err error |
| 128 | + var lifecycleResponse LifecycleResponse |
| 129 | + var body []byte |
| 130 | + |
| 131 | + appstreams, err := rc.cache.GetRoadmapRhelLifecycle(ctx) |
| 132 | + if err != nil && !errors.Is(err, cache.NotFound) { |
| 133 | + log.Error().Err(err).Msg("GetAppstreams - error reading from cache") |
| 134 | + } |
| 135 | + if appstreams != nil { |
| 136 | + err = json.Unmarshal(appstreams, &lifecycleResponse) |
| 137 | + if err != nil { |
| 138 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error during unmarshal response body: %w", err) |
| 139 | + } |
| 140 | + return lifecycleResponse, http.StatusOK, nil |
| 141 | + } |
| 142 | + |
| 143 | + fullPath := server + "/lifecycle/rhel" |
| 144 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, fullPath, nil) |
| 145 | + if err != nil { |
| 146 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error building request: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + if config.Get().Clients.Roadmap.Username != "" && config.Get().Clients.Roadmap.Password != "" { |
| 150 | + req.SetBasicAuth(config.Get().Clients.Roadmap.Username, config.Get().Clients.Roadmap.Password) |
| 151 | + } |
| 152 | + |
| 153 | + encodedXRHID, err := encodedIdentity(ctx) |
| 154 | + if err != nil { |
| 155 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error getting encoded XRHID: %w", err) |
| 156 | + } |
| 157 | + req.Header.Set(api.IdentityHeader, encodedXRHID) |
| 158 | + |
| 159 | + resp, err := rc.client.Do(req) |
| 160 | + if resp != nil { |
| 161 | + defer resp.Body.Close() |
| 162 | + |
| 163 | + body, err = io.ReadAll(resp.Body) |
| 164 | + if err != nil { |
| 165 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error reading response body: %w", err) |
| 166 | + } |
| 167 | + if resp.StatusCode != 0 { |
| 168 | + statusCode = resp.StatusCode |
| 169 | + } |
| 170 | + } |
| 171 | + if err != nil { |
| 172 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error sending request: %w", err) |
| 173 | + } |
| 174 | + if statusCode < 200 || statusCode >= 300 { |
| 175 | + return LifecycleResponse{}, statusCode, fmt.Errorf("unexpected status code with body: %s", string(body)) |
| 176 | + } |
| 177 | + |
| 178 | + rc.cache.SetRoadmapRhelLifecycle(ctx, body) |
| 179 | + |
| 180 | + err = json.Unmarshal(body, &lifecycleResponse) |
| 181 | + if err != nil { |
| 182 | + return LifecycleResponse{}, statusCode, fmt.Errorf("error during unmarshal response body: %w", err) |
| 183 | + } |
| 184 | + |
| 185 | + return lifecycleResponse, statusCode, nil |
| 186 | +} |
| 187 | + |
| 188 | +func (rc roadmapClient) GetRhelLifecycleForLatestMajorVersions(ctx context.Context) (map[int]LifecycleEntity, error) { |
| 189 | + lifecycleResp, _, err := rc.GetRhelLifecycle(ctx) |
| 190 | + if err != nil { |
| 191 | + return nil, err |
| 192 | + } |
| 193 | + |
| 194 | + currentDate := time.Now().UTC().Truncate(24 * time.Hour) |
| 195 | + rhelEolMap := make(map[int]LifecycleEntity) |
| 196 | + for _, item := range lifecycleResp.Data { |
| 197 | + startDate, err := time.Parse("2006-01-02", item.StartDate) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + if startDate.Before(currentDate) || startDate.Equal(currentDate) { |
| 202 | + if existing, found := rhelEolMap[item.Major]; !found || (item.Minor > existing.Minor) { |
| 203 | + rhelEolMap[item.Major] = item |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + return rhelEolMap, nil |
| 208 | +} |
0 commit comments