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).
This commit is contained in:
@@ -3,6 +3,7 @@ package domain
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
"git.yoresee.cc/nightstar/ry/internal/output"
|
||||
"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")
|
||||
ttl, _ := cmd.Flags().GetInt("ttl")
|
||||
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})
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -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:"-"`
|
||||
}
|
||||
@@ -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:"-"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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:"-"`
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -2,8 +2,8 @@ package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
)
|
||||
|
||||
type ProviderConfig struct {
|
||||
@@ -25,8 +25,8 @@ type Provider interface {
|
||||
}
|
||||
|
||||
type ServerService interface {
|
||||
List(ctx context.Context) ([]Server, error)
|
||||
Get(ctx context.Context, id string) (*Server, error)
|
||||
List(ctx context.Context) ([]model.Server, error)
|
||||
Get(ctx context.Context, id string) (*model.Server, error)
|
||||
Start(ctx context.Context, id string) error
|
||||
Stop(ctx context.Context, id string) error
|
||||
Reboot(ctx context.Context, id string) error
|
||||
@@ -37,96 +37,22 @@ type ServerService interface {
|
||||
}
|
||||
|
||||
type DomainService interface {
|
||||
List(ctx context.Context) ([]Domain, error)
|
||||
DNSList(ctx context.Context, domain string) ([]DNSRecord, error)
|
||||
DNSAdd(ctx context.Context, domain string, record DNSRecord) error
|
||||
List(ctx context.Context) ([]model.Domain, error)
|
||||
DNSList(ctx context.Context, domain string) ([]model.DNSRecord, error)
|
||||
DNSAdd(ctx context.Context, domain string, record model.DNSRecord) error
|
||||
DNSDelete(ctx context.Context, domain string, recordID string) error
|
||||
}
|
||||
|
||||
type StorageService interface {
|
||||
List(ctx context.Context) ([]StorageInstance, error)
|
||||
BucketList(ctx context.Context, instanceID string) ([]Bucket, error)
|
||||
List(ctx context.Context) ([]model.StorageInstance, error)
|
||||
BucketList(ctx context.Context, instanceID string) ([]model.Bucket, error)
|
||||
BucketCreate(ctx context.Context, instanceID, name string) error
|
||||
}
|
||||
|
||||
type CDNService interface {
|
||||
List(ctx context.Context) ([]CDNInstance, error)
|
||||
List(ctx context.Context) ([]model.CDNInstance, error)
|
||||
}
|
||||
|
||||
type BillingService interface {
|
||||
Orders(ctx context.Context) ([]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))
|
||||
Orders(ctx context.Context) ([]model.Order, error)
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
)
|
||||
|
||||
type BillingService struct {
|
||||
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 {
|
||||
ID int `json:"id"`
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
orders := make([]provider.Order, len(raw))
|
||||
orders := make([]model.Order, len(raw))
|
||||
for i, r := range raw {
|
||||
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),
|
||||
Product: r.ProductName,
|
||||
Amount: r.Amount,
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
)
|
||||
|
||||
type DomainService struct {
|
||||
@@ -18,14 +18,14 @@ type rainyunDomain struct {
|
||||
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
|
||||
if err := d.client.getList(ctx, "/product/domain/", &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
domains := make([]provider.Domain, len(raw))
|
||||
domains := make([]model.Domain, len(raw))
|
||||
for i, r := range raw {
|
||||
domains[i] = provider.Domain{
|
||||
domains[i] = model.Domain{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Name: r.DomainName,
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (d *DomainService) List(ctx context.Context) ([]provider.Domain, error) {
|
||||
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 {
|
||||
ID int `json:"id"`
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
records := make([]provider.DNSRecord, len(raw))
|
||||
records := make([]model.DNSRecord, len(raw))
|
||||
for i, r := range raw {
|
||||
records[i] = provider.DNSRecord{
|
||||
records[i] = model.DNSRecord{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Type: r.RecordType,
|
||||
Name: r.Host,
|
||||
@@ -59,7 +59,7 @@ func (d *DomainService) DNSList(ctx context.Context, domain string) ([]provider.
|
||||
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{
|
||||
"record_type": record.Type,
|
||||
"host": record.Name,
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
)
|
||||
|
||||
// ServerService implements provider.ServerService for 雨云.
|
||||
@@ -27,19 +27,19 @@ type rainyunServer struct {
|
||||
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
|
||||
if err := s.client.getList(ctx, "/product/rcs/", &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
servers := make([]provider.Server, len(raw))
|
||||
servers := make([]model.Server, len(raw))
|
||||
for i, r := range raw {
|
||||
servers[i] = toServer(r)
|
||||
}
|
||||
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
|
||||
path := fmt.Sprintf("/product/rcs/%s/", id)
|
||||
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)
|
||||
}
|
||||
|
||||
// toServer maps a rainyunServer to the provider.Server model.
|
||||
func toServer(r rainyunServer) provider.Server {
|
||||
// toServer maps a rainyunServer to the model.Server model.
|
||||
func toServer(r rainyunServer) model.Server {
|
||||
expire := time.Unix(r.ExpireDate, 0)
|
||||
return provider.Server{
|
||||
return model.Server{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Name: r.Name,
|
||||
Status: r.Status,
|
||||
|
||||
@@ -4,14 +4,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
"git.yoresee.cc/nightstar/ry/internal/model"
|
||||
)
|
||||
|
||||
type StorageService struct {
|
||||
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 {
|
||||
ID int `json:"id"`
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
instances := make([]provider.StorageInstance, len(raw))
|
||||
instances := make([]model.StorageInstance, len(raw))
|
||||
for i, r := range raw {
|
||||
instances[i] = provider.StorageInstance{
|
||||
instances[i] = model.StorageInstance{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Name: r.Name,
|
||||
Raw: r,
|
||||
@@ -31,7 +31,7 @@ func (s *StorageService) List(ctx context.Context) ([]provider.StorageInstance,
|
||||
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 {
|
||||
ID int `json:"id"`
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
buckets := make([]provider.Bucket, len(raw))
|
||||
buckets := make([]model.Bucket, len(raw))
|
||||
for i, r := range raw {
|
||||
buckets[i] = provider.Bucket{
|
||||
buckets[i] = model.Bucket{
|
||||
ID: fmt.Sprintf("%d", r.ID),
|
||||
Name: r.Name,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user