provider.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package cloud
  2. import (
  3. "context"
  4. "time"
  5. )
  6. // InstanceSize represents the size configuration for a VM instance
  7. type InstanceSize struct {
  8. ID string `json:"id"`
  9. Name string `json:"name"`
  10. CPUCores int `json:"cpu_cores"`
  11. MemoryGB int `json:"memory_gb"`
  12. DiskGB int `json:"disk_gb"`
  13. Price float64 `json:"price"` // Hourly price
  14. }
  15. // Region represents a geographic region
  16. type Region struct {
  17. ID string `json:"id"`
  18. Name string `json:"name"`
  19. Zone string `json:"zone"`
  20. }
  21. // Instance represents a virtual machine instance
  22. type Instance struct {
  23. ID string `json:"id"`
  24. Name string `json:"name"`
  25. Region string `json:"region"`
  26. Size string `json:"size"`
  27. ImageID string `json:"image_id"`
  28. IPAddress string `json:"ip_address"`
  29. PrivateIP string `json:"private_ip,omitempty"`
  30. Status string `json:"status"` // Creating, Running, Stopping, Stopped, Restarting, Terminated
  31. CreatedAt time.Time `json:"created_at"`
  32. Tags map[string]string `json:"tags,omitempty"`
  33. SecurityGroups []string `json:"security_groups,omitempty"`
  34. }
  35. // InstanceCreateOpts are options to configure a new instance
  36. type InstanceCreateOpts struct {
  37. Name string `json:"name"`
  38. Region string `json:"region"`
  39. Size string `json:"size"`
  40. ImageID string `json:"image_id"`
  41. SSHKeyIDs []string `json:"ssh_key_ids,omitempty"`
  42. UserData string `json:"user_data,omitempty"`
  43. Tags map[string]string `json:"tags,omitempty"`
  44. SecurityGroups []string `json:"security_groups,omitempty"`
  45. }
  46. // SSHKey represents an SSH key
  47. type SSHKey struct {
  48. ID string `json:"id"`
  49. Name string `json:"name"`
  50. Fingerprint string `json:"fingerprint"`
  51. PublicKey string `json:"public_key"`
  52. CreatedAt time.Time `json:"created_at"`
  53. }
  54. // Image represents an operating system image
  55. type Image struct {
  56. ID string `json:"id"`
  57. Name string `json:"name"`
  58. Description string `json:"description"`
  59. Type string `json:"type"` // base, snapshot, backup
  60. Status string `json:"status"`
  61. CreatedAt time.Time `json:"created_at"`
  62. MinDiskGB int `json:"min_disk_gb,omitempty"`
  63. SizeGB int `json:"size_gb,omitempty"`
  64. }
  65. // Provider defines the interface that all cloud providers must implement
  66. type Provider interface {
  67. // Initialize sets up the provider with credentials and configuration
  68. Initialize(config map[string]string) error
  69. // Validate checks if the provider credentials are valid
  70. Validate(ctx context.Context) (bool, error)
  71. // ListRegions lists all available regions
  72. ListRegions(ctx context.Context) ([]Region, error)
  73. // ListInstanceSizes lists available VM sizes
  74. ListInstanceSizes(ctx context.Context, region string) ([]InstanceSize, error)
  75. // ListInstances lists all instances
  76. ListInstances(ctx context.Context) ([]Instance, error)
  77. // GetInstance gets a specific instance by ID
  78. GetInstance(ctx context.Context, id string) (*Instance, error)
  79. // CreateInstance creates a new instance
  80. CreateInstance(ctx context.Context, opts InstanceCreateOpts) (*Instance, error)
  81. // DeleteInstance deletes an instance
  82. DeleteInstance(ctx context.Context, id string) error
  83. // StartInstance starts an instance
  84. StartInstance(ctx context.Context, id string) error
  85. // StopInstance stops an instance
  86. StopInstance(ctx context.Context, id string) error
  87. // RestartInstance restarts an instance
  88. RestartInstance(ctx context.Context, id string) error
  89. // ListImages lists available OS images
  90. ListImages(ctx context.Context) ([]Image, error)
  91. // ListSSHKeys lists SSH keys
  92. ListSSHKeys(ctx context.Context) ([]SSHKey, error)
  93. // CreateSSHKey creates a new SSH key
  94. CreateSSHKey(ctx context.Context, name, publicKey string) (*SSHKey, error)
  95. // DeleteSSHKey deletes an SSH key
  96. DeleteSSHKey(ctx context.Context, id string) error
  97. // GetInstanceStatus gets the current status of an instance
  98. GetInstanceStatus(ctx context.Context, id string) (string, error)
  99. // WaitForInstanceStatus waits for an instance to reach a specific status
  100. WaitForInstanceStatus(ctx context.Context, id, status string, timeout time.Duration) error
  101. }
  102. // ProviderFactory is a function that creates a new provider instance
  103. type ProviderFactory func() Provider
  104. // providers holds a map of provider factories
  105. var providers = make(map[string]ProviderFactory)
  106. // RegisterProvider registers a new provider factory
  107. func RegisterProvider(name string, factory ProviderFactory) {
  108. providers[name] = factory
  109. }
  110. // GetProvider returns a provider by name
  111. func GetProvider(name string) (Provider, bool) {
  112. factory, ok := providers[name]
  113. if !ok {
  114. return nil, false
  115. }
  116. return factory(), true
  117. }
  118. // GetSupportedProviders returns a list of supported provider names
  119. func GetSupportedProviders() []string {
  120. var names []string
  121. for name := range providers {
  122. names = append(names, name)
  123. }
  124. return names
  125. }