|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ApiVersion = "v1" |
| 13 | +const Api = "products" |
| 14 | +const ApiEndpoint = "access.redhat.com/product-life-cycles/api/" + ApiVersion + "/" + Api |
| 15 | +const SearchString = "Openshift Container Platform" |
| 16 | + |
| 17 | +type LifeCycleData struct { |
| 18 | + UUID string `json:"uuid"` |
| 19 | + Name string `json:"name"` |
| 20 | + FormerNames []any `json:"former_names"` |
| 21 | + ShowLastMinorRelease bool `json:"show_last_minor_release"` |
| 22 | + ShowFinalMinorRelease bool `json:"show_final_minor_release"` |
| 23 | + IsLayeredProduct bool `json:"is_layered_product"` |
| 24 | + AllPhases []struct { |
| 25 | + Name string `json:"name"` |
| 26 | + Ptype string `json:"ptype"` |
| 27 | + Tooltip any `json:"tooltip"` |
| 28 | + DisplayName string `json:"display_name"` |
| 29 | + AdditionalText string `json:"additional_text"` |
| 30 | + } `json:"all_phases"` |
| 31 | + Versions []struct { |
| 32 | + Name string `json:"name"` |
| 33 | + Type string `json:"type"` |
| 34 | + LastMinorRelease any `json:"last_minor_release"` |
| 35 | + FinalMinorRelease any `json:"final_minor_release"` |
| 36 | + ExtraHeaderValue any `json:"extra_header_value"` |
| 37 | + AdditionalText string `json:"additional_text"` |
| 38 | + Phases []struct { |
| 39 | + Name string `json:"name"` |
| 40 | + Date string `json:"date"` |
| 41 | + DateFormat string `json:"date_format"` |
| 42 | + AdditionalText string `json:"additional_text"` |
| 43 | + } `json:"phases"` |
| 44 | + Tier string `json:"tier"` |
| 45 | + OpenshiftCompatibility string `json:"openshift_compatibility"` |
| 46 | + ExtraDependences []any `json:"extra_dependences"` |
| 47 | + } `json:"versions"` |
| 48 | + Footnote string `json:"footnote"` |
| 49 | + IsOperator bool `json:"is_operator"` |
| 50 | + ShowOpenshiftCompatibility bool `json:"show_openshift_compatibility"` |
| 51 | + ReleaseCadence string `json:"release_cadence"` |
| 52 | + Package string `json:"package"` |
| 53 | + Link string `json:"link"` |
| 54 | + Policies string `json:"policies"` |
| 55 | +} |
| 56 | + |
| 57 | +type Results struct { |
| 58 | + Data []LifeCycleData `json:"data"` |
| 59 | +} |
| 60 | + |
| 61 | +func (*LifeCycleData) GetLifecycleStatus(clusterVersion string) (string, error) { |
| 62 | + |
| 63 | + // Get lifecycle data |
| 64 | + requestURL := fmt.Sprintf("https://%s?name=%s", ApiEndpoint, url.QueryEscape(SearchString)) |
| 65 | + res, err := http.Get(requestURL) |
| 66 | + if err != nil { |
| 67 | + fmt.Printf("Error getting request: %s\n", err) |
| 68 | + return "Unknown", err |
| 69 | + } |
| 70 | + defer res.Body.Close() |
| 71 | + body, err := io.ReadAll(res.Body) |
| 72 | + if err != nil { |
| 73 | + fmt.Printf("Error reading body: %s\n", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Filter to get EOL dates |
| 77 | + var results Results |
| 78 | + err = json.Unmarshal(body, &results) |
| 79 | + if err != nil { |
| 80 | + fmt.Printf("Error unmarshalling json: %s\n", err.Error()) |
| 81 | + } |
| 82 | + for _, product := range results.Data { |
| 83 | + for _, version := range product.Versions { |
| 84 | + if strings.Compare(version.Name, clusterVersion) == 0 { |
| 85 | + return version.Type, nil |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return "Unknown", nil |
| 91 | +} |
0 commit comments