Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
feat: SNIs API
Browse files Browse the repository at this point in the history
Create service for creating, getting, deleting, updating
and listing SNIs.
  • Loading branch information
faabiosr committed Jun 9, 2018
1 parent a662438 commit bb2c2f6
Show file tree
Hide file tree
Showing 6 changed files with 435 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fixtures/snis_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": [
{
"name": "example.com",
"created_at": 1523289670,
"ssl_certificate_id": "ce9832aa-5d26-41c4-af86-197c7732df1c"
}
]
}
5 changes: 5 additions & 0 deletions fixtures/snis_payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "example.com",
"created_at": 1523289670,
"ssl_certificate_id": "ce9832aa-5d26-41c4-af86-197c7732df1c"
}
4 changes: 4 additions & 0 deletions kongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type (

//Certificates api service
Certificates Certificates

//SNIs api service
SNIs SNIs
}

// An ErrorResponse report the error caused by and API request
Expand Down Expand Up @@ -80,6 +83,7 @@ func NewClient(client *http.Client, baseURL *url.URL) (*Kongo, error) {
k.Routes = &RoutesService{k}
k.Customers = &CustomersService{k}
k.Certificates = &CertificatesService{k}
k.SNIs = &SNIsService{k}

return k, nil
}
Expand Down
1 change: 1 addition & 0 deletions kongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (s *KongoTestSuite) TestInstance() {
s.assert.Implements(new(Routes), s.client.Routes)
s.assert.Implements(new(Customers), s.client.Customers)
s.assert.Implements(new(Certificates), s.client.Certificates)
s.assert.Implements(new(SNIs), s.client.SNIs)
}

func (s *KongoTestSuite) TestCreateRequestWithInvalidMethod() {
Expand Down
196 changes: 196 additions & 0 deletions sni.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package kongo

import (
"context"
"net/http"
"net/url"
"path"
)

const (
snisResourcePath = "/snis"
)

type (
// SNIs manages the Kong SNIs.
SNIs interface {
// Create creates a new sni.
Create(sni *SNI) (*SNI, *http.Response, error)

// CreateWithContext creates a new sni.
CreateWithContext(ctx context.Context, sni *SNI) (*SNI, *http.Response, error)

// Delete deletes registered sni by name.
Delete(name string) (*http.Response, error)

// DeleteWithContext deletes registered sni by name.
DeleteWithContext(ctx context.Context, name string) (*http.Response, error)

// Get retrieves registered sni by name.
Get(name string) (*SNI, *http.Response, error)

// GetWithContext retrieves registered sni by name.
GetWithContext(ctx context.Context, name string) (*SNI, *http.Response, error)

// List retrieves a list of registered snis.
List() ([]*SNI, *http.Response, error)

// ListWithContext retrieves a list of registered snis.
ListWithContext(ctx context.Context) ([]*SNI, *http.Response, error)

// Update updates a sni registered by name.
Update(name string, sni *SNI) (*SNI, *http.Response, error)

// UpdateWithContext updates a sni registered by name.
UpdateWithContext(ctx context.Context, name string, sni *SNI) (*SNI, *http.Response, error)
}

// SNIsService it's a concrete instance of SNIs.
SNIsService struct {
// Kongo client manages communication by API.
client *Kongo
}

// SNI it's a structure of API result.
SNI struct {
// The date when the sni was registered.
CreatedAt Time `json:"created_at"`

// The SNI name to associate with the given certificate.
Name string `json:"name"`

// The "id" (a UUID) of the certificate with which to associate the SNI hostname.
SSLCertificateId string `json:"ssl_certificate_id"`
}

// SNIsRoot it's a structure of API result list.
SNIsRoot struct {
// List of snis.
SNIs []*SNI `json:"data"`
}
)

// CreateWithContext creates a new sni.
func (s *SNIsService) CreateWithContext(ctx context.Context, sni *SNI) (*SNI, *http.Response, error) {
resource, _ := url.Parse(snisResourcePath)

req, err := s.client.NewRequest(ctx, http.MethodPost, resource, sni)

if err != nil {
return nil, nil, err
}

root := new(SNI)

res, err := s.client.Do(req, root)

if err != nil {
return nil, res, err
}

return root, res, nil
}

// Create creates a new sni.
func (s *SNIsService) Create(sni *SNI) (*SNI, *http.Response, error) {
return s.CreateWithContext(context.TODO(), sni)
}

// DeleteWithContext retrieves registered sni by ID or SNI.
func (s *SNIsService) DeleteWithContext(ctx context.Context, idOrSNI string) (*http.Response, error) {
resource, _ := url.Parse(snisResourcePath)
resource.Path = path.Join(resource.Path, idOrSNI)

req, err := s.client.NewRequest(ctx, http.MethodDelete, resource, nil)

if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}

// Delete retrieves registered sni by ID or SNI.
func (s *SNIsService) Delete(idOrSNI string) (*http.Response, error) {
return s.DeleteWithContext(context.TODO(), idOrSNI)
}

// GetWithContext retrieves registered sni by ID or SNI.
func (s *SNIsService) GetWithContext(ctx context.Context, idOrSNI string) (*SNI, *http.Response, error) {
resource, _ := url.Parse(snisResourcePath)
resource.Path = path.Join(resource.Path, idOrSNI)

req, err := s.client.NewRequest(ctx, http.MethodGet, resource, nil)

if err != nil {
return nil, nil, err
}

sni := new(SNI)

res, err := s.client.Do(req, sni)

if err != nil {
return nil, res, err
}

return sni, res, nil
}

// Get retrieves registered sni by ID.
func (s *SNIsService) Get(idOrSNI string) (*SNI, *http.Response, error) {
return s.GetWithContext(context.TODO(), idOrSNI)
}

// ListWithContext retrieves a list of registered snis.
func (s *SNIsService) ListWithContext(ctx context.Context) ([]*SNI, *http.Response, error) {
resource, _ := url.Parse(snisResourcePath)

req, err := s.client.NewRequest(ctx, http.MethodGet, resource, nil)

if err != nil {
return nil, nil, err
}

root := new(SNIsRoot)

res, err := s.client.Do(req, root)

if err != nil {
return nil, res, err
}

return root.SNIs, res, nil
}

// List retrieves a list of registered snis.
func (s *SNIsService) List() ([]*SNI, *http.Response, error) {
return s.ListWithContext(context.TODO())
}

// UpdateWithContext updates a sni registered by ID or SNI.
func (s *SNIsService) UpdateWithContext(ctx context.Context, idOrSNI string, sni *SNI) (*SNI, *http.Response, error) {
resource, _ := url.Parse(snisResourcePath)
resource.Path = path.Join(resource.Path, idOrSNI)

req, err := s.client.NewRequest(ctx, http.MethodPatch, resource, sni)

if err != nil {
return nil, nil, err
}

root := new(SNI)

res, err := s.client.Do(req, root)

if err != nil {
return nil, res, err
}

return root, res, nil
}

// Update updates a sni registered by ID or SNI.
func (s *SNIsService) Update(idOrSNI string, sni *SNI) (*SNI, *http.Response, error) {
return s.UpdateWithContext(context.TODO(), idOrSNI, sni)
}

0 comments on commit bb2c2f6

Please sign in to comment.