-
Notifications
You must be signed in to change notification settings - Fork 1
/
plans.go
83 lines (73 loc) · 1.82 KB
/
plans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package goss
import (
"context"
"fmt"
"github.com/google/go-querystring/query"
"net/http"
"net/url"
)
type Plans struct {
client *Client
}
type PlansServiceOp interface {
Find(ctx context.Context, planFindRequest *PlanFindRequest) (*Plan, error)
List(ctx context.Context) ([]*Plan, error)
Get(ctx context.Context, id string) (*Plan, error)
}
type Plan struct {
ID string `json:"id"`
Kind string `json:"kind"`
Name string `json:"name"`
Price float64 `json:"price"`
Cloud string `json:"cloud"`
Region string `json:"region"`
}
type PlanFindRequest struct {
Kind string `url:"kind"`
Name string `url:"name"`
Cloud string `url:"cloud"`
Region string `url:"region"`
}
func (s *Plans) Find(ctx context.Context, planFindRequest *PlanFindRequest) (*Plan, error) {
values, err := query.Values(planFindRequest)
if err != nil {
return nil, err
}
u := url.URL{
Path: fmt.Sprintf("%s/new", plansUrl),
RawQuery: values.Encode(),
}
request, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
plan := new(Plan)
if err := s.client.Do(ctx, request, plan); err != nil {
return nil, err
}
return plan, nil
}
const plansUrl = "/v1/plans"
func (s *Plans) Get(ctx context.Context, id string) (*Plan, error) {
path := fmt.Sprintf("%s/%s", plansUrl, id)
request, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
plan := new(Plan)
if err := s.client.Do(ctx, request, plan); err != nil {
return nil, err
}
return plan, nil
}
func (s *Plans) List(ctx context.Context) ([]*Plan, error) {
request, err := s.client.NewRequest(http.MethodGet, plansUrl, nil)
if err != nil {
return nil, err
}
plans := make([]*Plan, 0)
if err := s.client.Do(ctx, request, plans); err != nil {
return nil, err
}
return plans, nil
}