| 1 | package lb |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/CenturyLinkCloud/clc-sdk/api" |
| 7 | ) |
| 8 | |
| 9 | func New(client api.HTTP) *Service { |
| 10 | return &Service{ |
| 11 | client: client, |
| 12 | config: client.Config(), |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | type Service struct { |
| 17 | client api.HTTP |
| 18 | config *api.Config |
| 19 | } |
| 20 | |
| 21 | func (s *Service) Get(dc, id string) (*LoadBalancer, error) { |
| 22 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s", s.config.BaseURL, s.config.Alias, dc, id) |
| 23 | resp := &LoadBalancer{} |
| 24 | err := s.client.Get(url, resp) |
| 25 | return resp, err |
| 26 | } |
| 27 | |
| 28 | func (s *Service) GetAll(dc string) ([]*LoadBalancer, error) { |
| 29 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s", s.config.BaseURL, s.config.Alias, dc) |
| 30 | resp := make([]*LoadBalancer, 0) |
| 31 | err := s.client.Get(url, &resp) |
| 32 | return resp, err |
| 33 | } |
| 34 | |
| 35 | func (s *Service) Create(dc string, lb LoadBalancer) (*LoadBalancer, error) { |
| 36 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s", s.config.BaseURL, s.config.Alias, dc) |
| 37 | resp := &LoadBalancer{} |
| 38 | err := s.client.Post(url, lb, resp) |
| 39 | return resp, err |
| 40 | } |
| 41 | |
| 42 | func (s *Service) Update(dc, id string, lb LoadBalancer) error { |
| 43 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s", s.config.BaseURL, s.config.Alias, dc, id) |
| 44 | err := s.client.Put(url, lb, nil) |
| 45 | return err |
| 46 | } |
| 47 | |
| 48 | func (s *Service) Delete(dc, id string) error { |
| 49 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s", s.config.BaseURL, s.config.Alias, dc, id) |
| 50 | return s.client.Delete(url, nil) |
| 51 | } |
| 52 | |
| 53 | func (s *Service) GetPool(dc, lb, pool string) (*Pool, error) { |
| 54 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools/%s", s.config.BaseURL, s.config.Alias, dc, lb, pool) |
| 55 | resp := &Pool{} |
| 56 | err := s.client.Get(url, resp) |
| 57 | return resp, err |
| 58 | } |
| 59 | |
| 60 | func (s *Service) GetAllPools(dc, lb string) ([]*Pool, error) { |
| 61 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools", s.config.BaseURL, s.config.Alias, dc, lb) |
| 62 | resp := make([]*Pool, 0) |
| 63 | err := s.client.Get(url, &resp) |
| 64 | return resp, err |
| 65 | } |
| 66 | |
| 67 | func (s *Service) CreatePool(dc, lb string, pool Pool) (*Pool, error) { |
| 68 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools", s.config.BaseURL, s.config.Alias, dc, lb) |
| 69 | resp := &Pool{} |
| 70 | err := s.client.Post(url, pool, resp) |
| 71 | return resp, err |
| 72 | } |
| 73 | |
| 74 | func (s *Service) UpdatePool(dc, lb, id string, pool Pool) error { |
| 75 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools/%s", s.config.BaseURL, s.config.Alias, dc, lb, id) |
| 76 | err := s.client.Put(url, pool, nil) |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | func (s *Service) DeletePool(dc, lb, pool string) error { |
| 81 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools/%s", s.config.BaseURL, s.config.Alias, dc, lb, pool) |
| 82 | return s.client.Delete(url, nil) |
| 83 | } |
| 84 | |
| 85 | func (s *Service) GetAllNodes(dc, lb, pool string) ([]*Node, error) { |
| 86 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools/%s/nodes", s.config.BaseURL, s.config.Alias, dc, lb, pool) |
| 87 | resp := make([]*Node, 0) |
| 88 | err := s.client.Get(url, &resp) |
| 89 | return resp, err |
| 90 | } |
| 91 | |
| 92 | func (s *Service) UpdateNodes(dc, lb, pool string, nodes ...Node) error { |
| 93 | url := fmt.Sprintf("%s/sharedLoadBalancers/%s/%s/%s/pools/%s/nodes", s.config.BaseURL, s.config.Alias, dc, lb, pool) |
| 94 | err := s.client.Put(url, nodes, nil) |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | type LoadBalancer struct { |
| 99 | ID string `json:"id,omitempty"` |
| 100 | Name string `json:"name,omitempty"` |
| 101 | Description string `json:"description,omitempty"` |
| 102 | IPaddress string `json:"ipAddress,omitempty"` |
| 103 | Status string `json:"status,omitempty"` |
| 104 | Pools []Pool `json:"pools,omitempty"` |
| 105 | Links api.Links `json:"links,omitempty"` |
| 106 | } |
| 107 | |
| 108 | type Pool struct { |
| 109 | ID string `json:"id,omitempty"` |
| 110 | Port int `json:"port,omitempty"` |
| 111 | Method Method `json:"method"` |
| 112 | Persistence Persistence `json:"persistence"` |
| 113 | Nodes []Node `json:"nodes,omitempty"` |
| 114 | Links api.Links `json:"links,omitempty"` |
| 115 | } |
| 116 | |
| 117 | type Node struct { |
| 118 | Status string `json:"status,omitempty"` |
| 119 | IPaddress string `json:"ipAddress"` |
| 120 | PrivatePort int `json:"privatePort"` |
| 121 | } |
| 122 | |
| 123 | type Persistence string |
| 124 | |
| 125 | const ( |
| 126 | Standard Persistence = "standard" |
| 127 | Sticky Persistence = "sticky" |
| 128 | ) |
| 129 | |
| 130 | type Method string |
| 131 | |
| 132 | const ( |
| 133 | LeastConn Method = "leastConnection" |
| 134 | RoundRobin Method = "roundRobin" |
| 135 | ) |