plans.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package packngo
  2. const planBasePath = "/plans"
  3. // PlanService interface defines available plan methods
  4. type PlanService interface {
  5. List() ([]Plan, *Response, error)
  6. }
  7. type planRoot struct {
  8. Plans []Plan `json:"plans"`
  9. }
  10. // Plan represents a Packet service plan
  11. type Plan struct {
  12. ID string `json:"id"`
  13. Slug string `json:"slug,omitempty"`
  14. Name string `json:"name,omitempty"`
  15. Description string `json:"description,omitempty"`
  16. Line string `json:"line,omitempty"`
  17. Specs *Specs `json:"specs,omitempty"`
  18. Pricing *Pricing `json:"pricing,omitempty"`
  19. }
  20. func (p Plan) String() string {
  21. return Stringify(p)
  22. }
  23. // Specs - the server specs for a plan
  24. type Specs struct {
  25. Cpus []*Cpus `json:"cpus,omitempty"`
  26. Memory *Memory `json:"memory,omitempty"`
  27. Drives []*Drives `json:"drives,omitempty"`
  28. Nics []*Nics `json:"nics,omitempty"`
  29. Features *Features `json:"features,omitempty"`
  30. }
  31. func (s Specs) String() string {
  32. return Stringify(s)
  33. }
  34. // Cpus - the CPU config details for specs on a plan
  35. type Cpus struct {
  36. Count int `json:"count,omitempty"`
  37. Type string `json:"type,omitempty"`
  38. }
  39. func (c Cpus) String() string {
  40. return Stringify(c)
  41. }
  42. // Memory - the RAM config details for specs on a plan
  43. type Memory struct {
  44. Total string `json:"total,omitempty"`
  45. }
  46. func (m Memory) String() string {
  47. return Stringify(m)
  48. }
  49. // Drives - the storage config details for specs on a plan
  50. type Drives struct {
  51. Count int `json:"count,omitempty"`
  52. Size string `json:"size,omitempty"`
  53. Type string `json:"type,omitempty"`
  54. }
  55. func (d Drives) String() string {
  56. return Stringify(d)
  57. }
  58. // Nics - the network hardware details for specs on a plan
  59. type Nics struct {
  60. Count int `json:"count,omitempty"`
  61. Type string `json:"type,omitempty"`
  62. }
  63. func (n Nics) String() string {
  64. return Stringify(n)
  65. }
  66. // Features - other features in the specs for a plan
  67. type Features struct {
  68. Raid bool `json:"raid,omitempty"`
  69. Txt bool `json:"txt,omitempty"`
  70. }
  71. func (f Features) String() string {
  72. return Stringify(f)
  73. }
  74. // Pricing - the pricing options on a plan
  75. type Pricing struct {
  76. Hourly float32 `json:"hourly,omitempty"`
  77. Monthly float32 `json:"monthly,omitempty"`
  78. }
  79. func (p Pricing) String() string {
  80. return Stringify(p)
  81. }
  82. // PlanServiceOp implements PlanService
  83. type PlanServiceOp struct {
  84. client *Client
  85. }
  86. // List method returns all available plans
  87. func (s *PlanServiceOp) List() ([]Plan, *Response, error) {
  88. req, err := s.client.NewRequest("GET", planBasePath, nil)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. root := new(planRoot)
  93. resp, err := s.client.Do(req, root)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return root.Plans, resp, err
  98. }