Compare commits

...
2 Commits
Author SHA1 Message Date
NightStar 93cea77bba Merge branch 'refactor/extract-model-package' 2026-07-28 19:34:24 +08:00
NightStar bbba314127 refactor: extract domain models from provider into internal/model
- Move Server/Domain/DNSRecord/Bucket/StorageInstance/CDNInstance/Order/Error structs to internal/model/
- Provider interfaces now reference model.* types
- Rainyun provider implementations import model package for data types
- CLI commands updated to use model.DNSRecord directly

Separates pure data structures from service contract interfaces,
following hexagonal architecture: models (entities) live independently
from ports (provider interfaces) and adapters (rainyun/* implementations).
2026-07-28 19:31:22 +08:00
12 changed files with 127 additions and 112 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ package domain
import ( import (
"fmt" "fmt"
"git.yoresee.cc/nightstar/ry/internal/model"
"git.yoresee.cc/nightstar/ry/internal/output" "git.yoresee.cc/nightstar/ry/internal/output"
"git.yoresee.cc/nightstar/ry/internal/provider" "git.yoresee.cc/nightstar/ry/internal/provider"
@@ -66,7 +67,7 @@ func Cmd(prov *provider.Provider, out **output.Printer) *cobra.Command {
value, _ := cmd.Flags().GetString("value") value, _ := cmd.Flags().GetString("value")
ttl, _ := cmd.Flags().GetInt("ttl") ttl, _ := cmd.Flags().GetInt("ttl")
return svc.DNSAdd(cmd.Context(), args[0], return svc.DNSAdd(cmd.Context(), args[0],
provider.DNSRecord{Type: recType, Name: name, Value: value, TTL: ttl}) model.DNSRecord{Type: recType, Name: name, Value: value, TTL: ttl})
}, },
} }
+12
View File
@@ -0,0 +1,12 @@
package model
import "time"
type Order struct {
ID string `json:"id" table:"ID"`
Product string `json:"product" table:"PRODUCT"`
Amount float64 `json:"amount" table:"AMOUNT"`
Status string `json:"status" table:"STATUS"`
Created time.Time `json:"created" table:"CREATED"`
Raw any `json:"-"`
}
+8
View File
@@ -0,0 +1,8 @@
package model
type CDNInstance struct {
ID string `json:"id" table:"ID"`
Domain string `json:"domain" table:"DOMAIN"`
Status string `json:"status" table:"STATUS"`
Raw any `json:"-"`
}
+19
View File
@@ -0,0 +1,19 @@
package model
import "time"
type Domain struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"DOMAIN"`
Status string `json:"status" table:"STATUS"`
ExpireAt time.Time `json:"expires" table:"EXPIRES"`
Raw any `json:"-"`
}
type DNSRecord struct {
ID string `json:"id" table:"ID"`
Type string `json:"type" table:"TYPE"`
Name string `json:"name" table:"NAME"`
Value string `json:"value" table:"VALUE"`
TTL int `json:"ttl" table:"TTL"`
}
+18
View File
@@ -0,0 +1,18 @@
package model
import "fmt"
type Error struct {
Code int
Message string
Detail string
Raw []byte
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) Debug() string {
return fmt.Sprintf("%s\n%s", e.Detail, string(e.Raw))
}
+17
View File
@@ -0,0 +1,17 @@
package model
import "time"
type Server struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Status string `json:"status" table:"STATUS"`
IP string `json:"ip" table:"IP"`
CPU int `json:"cpu" table:"CPU"`
Memory int `json:"memory" table:"MEM"`
Disk int `json:"disk" table:"DISK"`
OS string `json:"os" table:"OS"`
Region string `json:"region" table:"REGION"`
ExpireAt time.Time `json:"expires" table:"EXPIRES"`
Raw any `json:"-"`
}
+14
View File
@@ -0,0 +1,14 @@
package model
type StorageInstance struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Status string `json:"status" table:"STATUS"`
Raw any `json:"-"`
}
type Bucket struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Region string `json:"region" table:"REGION"`
}
+11 -85
View File
@@ -2,8 +2,8 @@ package provider
import ( import (
"context" "context"
"fmt"
"time" "git.yoresee.cc/nightstar/ry/internal/model"
) )
type ProviderConfig struct { type ProviderConfig struct {
@@ -25,8 +25,8 @@ type Provider interface {
} }
type ServerService interface { type ServerService interface {
List(ctx context.Context) ([]Server, error) List(ctx context.Context) ([]model.Server, error)
Get(ctx context.Context, id string) (*Server, error) Get(ctx context.Context, id string) (*model.Server, error)
Start(ctx context.Context, id string) error Start(ctx context.Context, id string) error
Stop(ctx context.Context, id string) error Stop(ctx context.Context, id string) error
Reboot(ctx context.Context, id string) error Reboot(ctx context.Context, id string) error
@@ -37,96 +37,22 @@ type ServerService interface {
} }
type DomainService interface { type DomainService interface {
List(ctx context.Context) ([]Domain, error) List(ctx context.Context) ([]model.Domain, error)
DNSList(ctx context.Context, domain string) ([]DNSRecord, error) DNSList(ctx context.Context, domain string) ([]model.DNSRecord, error)
DNSAdd(ctx context.Context, domain string, record DNSRecord) error DNSAdd(ctx context.Context, domain string, record model.DNSRecord) error
DNSDelete(ctx context.Context, domain string, recordID string) error DNSDelete(ctx context.Context, domain string, recordID string) error
} }
type StorageService interface { type StorageService interface {
List(ctx context.Context) ([]StorageInstance, error) List(ctx context.Context) ([]model.StorageInstance, error)
BucketList(ctx context.Context, instanceID string) ([]Bucket, error) BucketList(ctx context.Context, instanceID string) ([]model.Bucket, error)
BucketCreate(ctx context.Context, instanceID, name string) error BucketCreate(ctx context.Context, instanceID, name string) error
} }
type CDNService interface { type CDNService interface {
List(ctx context.Context) ([]CDNInstance, error) List(ctx context.Context) ([]model.CDNInstance, error)
} }
type BillingService interface { type BillingService interface {
Orders(ctx context.Context) ([]Order, error) Orders(ctx context.Context) ([]model.Order, error)
}
type Server struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Status string `json:"status" table:"STATUS"`
IP string `json:"ip" table:"IP"`
CPU int `json:"cpu" table:"CPU"`
Memory int `json:"memory" table:"MEM"`
Disk int `json:"disk" table:"DISK"`
OS string `json:"os" table:"OS"`
Region string `json:"region" table:"REGION"`
ExpireAt time.Time `json:"expires" table:"EXPIRES"`
Raw any `json:"-"`
}
type Domain struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"DOMAIN"`
Status string `json:"status" table:"STATUS"`
ExpireAt time.Time `json:"expires" table:"EXPIRES"`
Raw any `json:"-"`
}
type DNSRecord struct {
ID string `json:"id" table:"ID"`
Type string `json:"type" table:"TYPE"`
Name string `json:"name" table:"NAME"`
Value string `json:"value" table:"VALUE"`
TTL int `json:"ttl" table:"TTL"`
}
type StorageInstance struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Status string `json:"status" table:"STATUS"`
Raw any `json:"-"`
}
type Bucket struct {
ID string `json:"id" table:"ID"`
Name string `json:"name" table:"NAME"`
Region string `json:"region" table:"REGION"`
}
type CDNInstance struct {
ID string `json:"id" table:"ID"`
Domain string `json:"domain" table:"DOMAIN"`
Status string `json:"status" table:"STATUS"`
Raw any `json:"-"`
}
type Order struct {
ID string `json:"id" table:"ID"`
Product string `json:"product" table:"PRODUCT"`
Amount float64 `json:"amount" table:"AMOUNT"`
Status string `json:"status" table:"STATUS"`
Created time.Time `json:"created" table:"CREATED"`
Raw any `json:"-"`
}
type Error struct {
Code int
Message string
Detail string
Raw []byte
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) Debug() string {
return fmt.Sprintf("%s\n%s", e.Detail, string(e.Raw))
} }
+4 -4
View File
@@ -5,14 +5,14 @@ import (
"fmt" "fmt"
"time" "time"
"git.yoresee.cc/nightstar/ry/internal/provider" "git.yoresee.cc/nightstar/ry/internal/model"
) )
type BillingService struct { type BillingService struct {
client *client client *client
} }
func (b *BillingService) Orders(ctx context.Context) ([]provider.Order, error) { func (b *BillingService) Orders(ctx context.Context) ([]model.Order, error) {
type rainyunOrder struct { type rainyunOrder struct {
ID int `json:"id"` ID int `json:"id"`
ProductName string `json:"product_name"` ProductName string `json:"product_name"`
@@ -24,10 +24,10 @@ func (b *BillingService) Orders(ctx context.Context) ([]provider.Order, error) {
if err := b.client.getList(ctx, "/expense/orders/list", &raw); err != nil { if err := b.client.getList(ctx, "/expense/orders/list", &raw); err != nil {
return nil, err return nil, err
} }
orders := make([]provider.Order, len(raw)) orders := make([]model.Order, len(raw))
for i, r := range raw { for i, r := range raw {
t, _ := time.Parse("2006-01-02 15:04:05", r.CreateDate) t, _ := time.Parse("2006-01-02 15:04:05", r.CreateDate)
orders[i] = provider.Order{ orders[i] = model.Order{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Product: r.ProductName, Product: r.ProductName,
Amount: r.Amount, Amount: r.Amount,
+8 -8
View File
@@ -4,7 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"git.yoresee.cc/nightstar/ry/internal/provider" "git.yoresee.cc/nightstar/ry/internal/model"
) )
type DomainService struct { type DomainService struct {
@@ -18,14 +18,14 @@ type rainyunDomain struct {
ExpireDate string `json:"expire_date"` ExpireDate string `json:"expire_date"`
} }
func (d *DomainService) List(ctx context.Context) ([]provider.Domain, error) { func (d *DomainService) List(ctx context.Context) ([]model.Domain, error) {
var raw []rainyunDomain var raw []rainyunDomain
if err := d.client.getList(ctx, "/product/domain/", &raw); err != nil { if err := d.client.getList(ctx, "/product/domain/", &raw); err != nil {
return nil, err return nil, err
} }
domains := make([]provider.Domain, len(raw)) domains := make([]model.Domain, len(raw))
for i, r := range raw { for i, r := range raw {
domains[i] = provider.Domain{ domains[i] = model.Domain{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Name: r.DomainName, Name: r.DomainName,
} }
@@ -33,7 +33,7 @@ func (d *DomainService) List(ctx context.Context) ([]provider.Domain, error) {
return domains, nil return domains, nil
} }
func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider.DNSRecord, error) { func (d *DomainService) DNSList(ctx context.Context, domain string) ([]model.DNSRecord, error) {
type rainyunDNSRecord struct { type rainyunDNSRecord struct {
ID int `json:"id"` ID int `json:"id"`
RecordType string `json:"record_type"` RecordType string `json:"record_type"`
@@ -46,9 +46,9 @@ func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider.
if err := d.client.get(ctx, path, &raw); err != nil { if err := d.client.get(ctx, path, &raw); err != nil {
return nil, err return nil, err
} }
records := make([]provider.DNSRecord, len(raw)) records := make([]model.DNSRecord, len(raw))
for i, r := range raw { for i, r := range raw {
records[i] = provider.DNSRecord{ records[i] = model.DNSRecord{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Type: r.RecordType, Type: r.RecordType,
Name: r.Host, Name: r.Host,
@@ -59,7 +59,7 @@ func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider.
return records, nil return records, nil
} }
func (d *DomainService) DNSAdd(ctx context.Context, domain string, record provider.DNSRecord) error { func (d *DomainService) DNSAdd(ctx context.Context, domain string, record model.DNSRecord) error {
body := map[string]any{ body := map[string]any{
"record_type": record.Type, "record_type": record.Type,
"host": record.Name, "host": record.Name,
+7 -7
View File
@@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"git.yoresee.cc/nightstar/ry/internal/provider" "git.yoresee.cc/nightstar/ry/internal/model"
) )
// ServerService implements provider.ServerService for 雨云. // ServerService implements provider.ServerService for 雨云.
@@ -27,19 +27,19 @@ type rainyunServer struct {
ExpireDate int64 `json:"ExpDate"` // Unix timestamp ExpireDate int64 `json:"ExpDate"` // Unix timestamp
} }
func (s *ServerService) List(ctx context.Context) ([]provider.Server, error) { func (s *ServerService) List(ctx context.Context) ([]model.Server, error) {
var raw []rainyunServer var raw []rainyunServer
if err := s.client.getList(ctx, "/product/rcs/", &raw); err != nil { if err := s.client.getList(ctx, "/product/rcs/", &raw); err != nil {
return nil, err return nil, err
} }
servers := make([]provider.Server, len(raw)) servers := make([]model.Server, len(raw))
for i, r := range raw { for i, r := range raw {
servers[i] = toServer(r) servers[i] = toServer(r)
} }
return servers, nil return servers, nil
} }
func (s *ServerService) Get(ctx context.Context, id string) (*provider.Server, error) { func (s *ServerService) Get(ctx context.Context, id string) (*model.Server, error) {
var raw rainyunServer var raw rainyunServer
path := fmt.Sprintf("/product/rcs/%s/", id) path := fmt.Sprintf("/product/rcs/%s/", id)
if err := s.client.get(ctx, path, &raw); err != nil { if err := s.client.get(ctx, path, &raw); err != nil {
@@ -85,10 +85,10 @@ func (s *ServerService) Upgrade(ctx context.Context, id, plan string) error {
return s.client.post(ctx, fmt.Sprintf("/product/rcs/%s/upgrade", id), body, nil) return s.client.post(ctx, fmt.Sprintf("/product/rcs/%s/upgrade", id), body, nil)
} }
// toServer maps a rainyunServer to the provider.Server model. // toServer maps a rainyunServer to the model.Server model.
func toServer(r rainyunServer) provider.Server { func toServer(r rainyunServer) model.Server {
expire := time.Unix(r.ExpireDate, 0) expire := time.Unix(r.ExpireDate, 0)
return provider.Server{ return model.Server{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Name: r.Name, Name: r.Name,
Status: r.Status, Status: r.Status,
+7 -7
View File
@@ -4,14 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"git.yoresee.cc/nightstar/ry/internal/provider" "git.yoresee.cc/nightstar/ry/internal/model"
) )
type StorageService struct { type StorageService struct {
client *client client *client
} }
func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance, error) { func (s *StorageService) List(ctx context.Context) ([]model.StorageInstance, error) {
var raw []struct { var raw []struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@@ -20,9 +20,9 @@ func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance,
if err := s.client.getList(ctx, "/product/ros/instance", &raw); err != nil { if err := s.client.getList(ctx, "/product/ros/instance", &raw); err != nil {
return nil, err return nil, err
} }
instances := make([]provider.StorageInstance, len(raw)) instances := make([]model.StorageInstance, len(raw))
for i, r := range raw { for i, r := range raw {
instances[i] = provider.StorageInstance{ instances[i] = model.StorageInstance{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Name: r.Name, Name: r.Name,
Raw: r, Raw: r,
@@ -31,7 +31,7 @@ func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance,
return instances, nil return instances, nil
} }
func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]provider.Bucket, error) { func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]model.Bucket, error) {
type rainyunBucket struct { type rainyunBucket struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@@ -42,9 +42,9 @@ func (s *StorageService) BucketList(ctx context.Context, instanceID string) ([]p
if err := s.client.get(ctx, path, &raw); err != nil { if err := s.client.get(ctx, path, &raw); err != nil {
return nil, err return nil, err
} }
buckets := make([]provider.Bucket, len(raw)) buckets := make([]model.Bucket, len(raw))
for i, r := range raw { for i, r := range raw {
buckets[i] = provider.Bucket{ buckets[i] = model.Bucket{
ID: fmt.Sprintf("%d", r.ID), ID: fmt.Sprintf("%d", r.ID),
Name: r.Name, Name: r.Name,
} }