Skip to content

Commit

Permalink
Merge pull request #23 from dstdfx/feature/services-supports
Browse files Browse the repository at this point in the history
Services support
  • Loading branch information
dstdfx authored Jun 20, 2019
2 parents e951317 + 25a8c70 commit 712e4b8
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 123 deletions.
127 changes: 4 additions & 123 deletions gcore/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (
)

const (
resellUsersURL = "/users"
resellClientsURL = "/clients"
resellClientURL = "/clients/%d"
resellUserTokenURL = "/users/%d/token"
resellClientServicesURL = "/clients/%d/services"
resellClientServiceURL = "/clients/%d/services/%d"
resellUsersURL = "/users"
resellClientsURL = "/clients"
resellClientURL = "/clients/%d"
resellUserTokenURL = "/users/%d/token"
)

// ClientsService handles communication with the client related methods
Expand Down Expand Up @@ -174,120 +172,3 @@ func (s *ClientsService) GetCommonClient(ctx context.Context, userID int) (*Comm

return commonClient, resp, nil
}

// PaidService represents G-Core paid service.
type PaidService struct {
ID int `json:"id"`
Name string `json:"name"`
}

// SuspendCDN method suspends CDN-service for given clientID.
// This feature has been taken from the admin web-panel, is not documented at all.
// It allows to pause CDN service for specific client.
func (s *ClientsService) SuspendCDN(ctx context.Context, clientID int) (*http.Response, error) {

url := fmt.Sprintf(resellClientServicesURL, clientID)
opts := struct {
Name string `param:"name"`
}{"CDN"}

// Add service name param as "CDN" to get CDN-service details
queryParams, err := BuildQueryParameters(opts)
if err != nil {
return nil, err
}

if queryParams != "" {
url = strings.Join([]string{url, queryParams}, "?")
}

// Get CDN-service details
req, err := s.client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

paidServices := make([]PaidService, 0)

resp, err := s.client.Do(req, &paidServices)
if err != nil {
return resp, err
}

body := struct {
Enabled bool `json:"enabled"`
Status string `json:"status"`
}{false, "paused"}

// TODO: fixme
// The only one CDN service is supposed to be
req, err = s.client.NewRequest(ctx,
http.MethodPut,
fmt.Sprintf(resellClientServiceURL, clientID, paidServices[0].ID),
body)
if err != nil {
return nil, err
}

resp, err = s.client.Do(req, nil)
if err != nil {
return resp, err
}

return resp, nil
}

// ResumeCDN method resumes CDN-service for given clientID.
// This feature has been taken from the admin web-panel, is not documented at all.
// It allows to resume CDN service for specific client.
func (s *ClientsService) ResumeCDN(ctx context.Context, clientID int) (*http.Response, error) {

url := fmt.Sprintf(resellClientServicesURL, clientID)
opts := struct {
Name string `param:"name"`
}{"CDN"}

// Add service name param as "CDN" to get CDN-service details
queryParams, err := BuildQueryParameters(opts)
if err != nil {
return nil, err
}

if queryParams != "" {
url = strings.Join([]string{url, queryParams}, "?")
}

req, err := s.client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

paidServices := make([]PaidService, 0)

resp, err := s.client.Do(req, &paidServices)
if err != nil {
return resp, err
}

body := struct {
Enabled bool `json:"enabled"`
Status string `json:"status"`
}{true, "active"}

// TODO: fixme
// The only one CDN service is supposed to be
req, err = s.client.NewRequest(ctx,
http.MethodPut,
fmt.Sprintf(resellClientServiceURL, clientID, paidServices[0].ID),
body)
if err != nil {
return nil, err
}

resp, err = s.client.Do(req, nil)
if err != nil {
return resp, err
}

return resp, nil
}
3 changes: 3 additions & 0 deletions gcore/gcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type CommonServices struct {
type ResellerServices struct {
Clients *ClientsService
GeoRestrictions *GeoRestrictionsService
Services *ServicesService
}

// AuthOptions is G-Core account credentials.
Expand Down Expand Up @@ -219,6 +220,7 @@ func NewResellerClient(logger ...log.GenericLogger) *ResellerClient {
resellerServices := ResellerServices{}
resellerServices.Clients = (*ClientsService)(&c.common)
resellerServices.GeoRestrictions = (*GeoRestrictionsService)(&c.common)
resellerServices.Services = (*ServicesService)(&c.common)
resellClient := &ResellerClient{
Client: c,
ResellerServices: resellerServices,
Expand Down Expand Up @@ -246,6 +248,7 @@ func NewResellerClientWithCustomHTTP(customClient *http.Client, logger ...log.Ge
resellerServices := ResellerServices{}
resellerServices.Clients = (*ClientsService)(&c.common)
resellerServices.GeoRestrictions = (*GeoRestrictionsService)(&c.common)
resellerServices.Services = (*ServicesService)(&c.common)
resellClient := &ResellerClient{
Client: c,
ResellerServices: resellerServices,
Expand Down
71 changes: 71 additions & 0 deletions gcore/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gcore

import (
"context"
"fmt"
"net/http"
)

const (
serviceListURL = "/clients/%d/services"
serviceUpdateURL = serviceListURL + "/%d"
)

// ServicesService handles communication with service related methods
// of the G-Core CDN API.
type ServicesService service

// Service represents G-Core client's service.
type Service struct {
ID int `json:"id"`
Name string `json:"name"`
Client int `json:"client"`
Status string `json:"status"`
Enabled bool `json:"enabled"`
Start *Time `json:"start"`
// TODO: add trial options, options
}

// UpdateServiceBody represents request body for service updating.
type UpdateServiceBody struct {
Enabled bool `json:"enabled"`
Status string `json:"status"`
}

// List method returns list of the client's services.
func (s *ServicesService) List(ctx context.Context, clientID int) ([]*Service, *http.Response, error) {
url := fmt.Sprintf(serviceListURL, clientID)

req, err := s.client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}

services := make([]*Service, 0)

resp, err := s.client.Do(req, &services)
if err != nil {
return nil, resp, err
}

return services, resp, nil
}

// Update method updates service status.
func (s *ServicesService) Update(ctx context.Context, clientID, serviceID int, body *UpdateServiceBody) (*Service, *http.Response, error) {
url := fmt.Sprintf(serviceUpdateURL, clientID, serviceID)

req, err := s.client.NewRequest(ctx, http.MethodPatch, url, body)
if err != nil {
return nil, nil, err
}

service := &Service{}

resp, err := s.client.Do(req, service)
if err != nil {
return nil, resp, err
}

return service, resp, nil
}
Loading

0 comments on commit 712e4b8

Please sign in to comment.