Traefik_Control/backend/internal/traefik/adapter.go

157 lines
4.8 KiB
Go

package traefik
import (
"context"
"github.com/traefik/traefik-gui/backend/internal/models"
)
// LegacyAdapter adapts TraefikAPI (v3.7 typed) to the legacy TraefikClient interface (models)
type LegacyAdapter struct {
API TraefikAPI
}
func NewLegacyAdapter(api TraefikAPI) *LegacyAdapter {
return &LegacyAdapter{API: api}
}
func (a *LegacyAdapter) GetRouters(ctx context.Context) ([]models.Router, error) {
routers, err := a.API.GetRouters(ctx)
if err != nil {
return nil, err
}
out := make([]models.Router, len(routers))
for i, r := range routers {
out[i] = models.Router{
Name: r.Name,
Provider: r.Provider,
Rule: r.Rule,
EntryPoints: r.EntryPoints,
Service: r.Service,
Middlewares: r.Middlewares,
Priority: r.Priority,
Status: r.Status,
Using: r.Using,
}
if r.TLS != nil {
out[i].TLS = &models.RouterTLSConfig{
Options: r.TLS.Options,
CertResolver: r.TLS.CertResolver,
}
for _, d := range r.TLS.Domains {
out[i].TLS.Domains = append(out[i].TLS.Domains, models.Domain{Main: d.Main, SANs: d.SANs})
}
}
}
return out, nil
}
func (a *LegacyAdapter) GetServices(ctx context.Context) ([]models.Service, error) {
services, err := a.API.GetServices(ctx)
if err != nil {
return nil, err
}
out := make([]models.Service, len(services))
for i, s := range services {
out[i] = models.Service{
Name: s.Name,
Provider: s.Provider,
Type: s.Type,
Status: s.Status,
Middlewares: s.Middlewares,
ServerStatus: s.ServerStatus,
}
if s.LoadBalancer != nil {
out[i].LoadBalancer = &models.LoadBalancer{
Strategy: s.LoadBalancer.Strategy,
PassHostHeader: s.LoadBalancer.PassHostHeader,
}
for _, srv := range s.LoadBalancer.Servers {
out[i].LoadBalancer.Servers = append(out[i].LoadBalancer.Servers, models.Server{URL: srv.URL, Weight: srv.Weight})
}
}
}
return out, nil
}
func (a *LegacyAdapter) GetMiddlewares(ctx context.Context) ([]models.Middleware, error) {
mws, err := a.API.GetMiddlewares(ctx)
if err != nil {
return nil, err
}
out := make([]models.Middleware, len(mws))
for i, m := range mws {
out[i] = models.Middleware{
Name: m.Name,
Provider: m.Provider,
Type: m.Type,
Status: m.Status,
}
}
return out, nil
}
func (a *LegacyAdapter) GetCertificates(ctx context.Context) ([]models.Certificate, error) {
certs, err := a.API.GetCertificates(ctx)
if err != nil {
return nil, err
}
out := make([]models.Certificate, len(certs))
for i, c := range certs {
out[i] = models.Certificate{
Name: c.Store,
CommonName: "",
SANs: c.SANs,
NotAfter: c.NotAfter,
NotBefore: c.NotBefore,
}
if len(c.Names) > 0 {
out[i].Name = c.Names[0]
out[i].CommonName = c.Names[0]
}
if c.Issuer != "" {
out[i].IssuerOrg = c.Issuer
}
}
return out, nil
}
func (a *LegacyAdapter) GetEntryPoints(ctx context.Context) ([]models.EntryPoint, error) {
eps, err := a.API.GetEntrypoints(ctx)
if err != nil {
return nil, err
}
out := make([]models.EntryPoint, len(eps))
for i, ep := range eps {
out[i] = models.EntryPoint{Name: ep.Name, Address: ep.Address}
if ep.HTTP != nil {
out[i].HTTP = &models.EntryPointHTTP{Middlewares: ep.HTTP.Middlewares}
if ep.HTTP.TLS != nil {
out[i].HTTP.TLS = &models.EntryPointTLS{CertResolver: ep.HTTP.TLS.CertResolver, Options: ep.HTTP.TLS.Options}
}
}
}
return out, nil
}
func (a *LegacyAdapter) GetOverview(ctx context.Context) (*models.Overview, error) {
ov, err := a.API.GetOverview(ctx)
if err != nil {
return nil, err
}
// Convert to models.Overview
out := &models.Overview{
Providers: ov.Providers,
}
out.HTTP.Routers = models.Section{Total: ov.HTTP.Routers.Total, Warnings: ov.HTTP.Routers.Warnings, Errors: ov.HTTP.Routers.Errors}
out.HTTP.Services = models.Section{Total: ov.HTTP.Services.Total, Warnings: ov.HTTP.Services.Warnings, Errors: ov.HTTP.Services.Errors}
out.HTTP.Middlewares = models.Section{Total: ov.HTTP.Middlewares.Total, Warnings: ov.HTTP.Middlewares.Warnings, Errors: ov.HTTP.Middlewares.Errors}
out.TCP.Routers = models.Section{Total: ov.TCP.Routers.Total, Warnings: ov.TCP.Routers.Warnings, Errors: ov.TCP.Routers.Errors}
out.TCP.Services = models.Section{Total: ov.TCP.Services.Total, Warnings: ov.TCP.Services.Warnings, Errors: ov.TCP.Services.Errors}
out.TCP.Middlewares = models.Section{Total: ov.TCP.Middlewares.Total, Warnings: ov.TCP.Middlewares.Warnings, Errors: ov.TCP.Middlewares.Errors}
out.UDP.Routers = models.Section{Total: ov.UDP.Routers.Total, Warnings: ov.UDP.Routers.Warnings, Errors: ov.UDP.Routers.Errors}
out.UDP.Services = models.Section{Total: ov.UDP.Services.Total, Warnings: ov.UDP.Services.Warnings, Errors: ov.UDP.Services.Errors}
return out, nil
}
var _ TraefikClient = (*LegacyAdapter)(nil)