Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom TLS API #225

Merged
merged 62 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
07ca039
adds custom cert types and can GET list certs
joel-g Oct 19, 2020
d1cb5ce
can get a single custom certificate
joel-g Oct 19, 2020
da7e6e6
adds delete, updated, create custom TLS
joel-g Oct 19, 2020
04d9b79
cleaned up for feedback
joel-g Oct 20, 2020
51d3428
get, list and patch custom tls configurations
joel-g Oct 20, 2020
c4e5eb8
adds test fixtures
joel-g Oct 21, 2020
10182e0
adds TLSActivation API operations
joel-g Oct 21, 2020
b42b8fd
go fmt
joel-g Oct 21, 2020
6f60919
adds tls configuration tests and fixtures
joel-g Oct 22, 2020
72b317d
adds tests for activation, fixes relationships
joel-g Nov 3, 2020
31793e2
Merge branch 'master' of https://github.com/fastly/go-fastly
joel-g Nov 3, 2020
2def394
fixes server error due to API not accepting "type" to be blank
joel-g Nov 4, 2020
edb7a22
adds "Type" tag to update activation
joel-g Nov 4, 2020
7356a3f
updated comments, fmt
joel-g Nov 4, 2020
0e060aa
update errors
joel-g Nov 4, 2020
bb51a55
adds Type field to input structs to get around API bug
joel-g Nov 4, 2020
fe1449c
Merge branch 'master' into custom-tls
joel-g Nov 4, 2020
55f05d0
updates Create and Update input struct tags per feedback
joel-g Nov 4, 2020
69b9704
adds comments to exported functions
joel-g Nov 5, 2020
445ef45
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
91c63c2
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
5e0ae8e
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
8c4490d
Update fastly/custom_tls_configuration.go
joel-g Nov 6, 2020
75085e5
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
f93ba45
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
caa5a30
Update fastly/custom_tls_activation.go
joel-g Nov 6, 2020
2278fb1
Update fastly/custom_tls_certificate.go
joel-g Nov 6, 2020
0368ea3
adds include param to GetTLSActivation()
joel-g Nov 6, 2020
c4db157
adds tls protocols to CustomTLSConfiguration
joel-g Nov 6, 2020
4fc5651
adds include to ListCustomTLSConfigurationsInput
joel-g Nov 6, 2020
8a2e97a
removes white space and compares i.Include to nil rather than an empt…
joel-g Nov 6, 2020
a3e7505
updates tls activation tests to match changes to tls activation child…
joel-g Nov 6, 2020
532dc0b
Refactors CustomCertificate to CustomTLSCertificate and updates all r…
joel-g Nov 6, 2020
cfd22dc
removes redudant struct tag, adds tests to confirm child struct still…
joel-g Nov 6, 2020
3548d48
removes redudant "TLS" in Activation input types, updates tests to ch…
joel-g Nov 9, 2020
87b2450
adds robust input unit testing for TLS activation operations
joel-g Nov 9, 2020
d772e8a
adds input validation tests for custom TLS certificate operations
joel-g Nov 9, 2020
d53357e
adds input validation unit tests to TLS configuration operations
joel-g Nov 9, 2020
ca2eb8e
adds missing params to ListCustomCertificates
joel-g Nov 9, 2020
1b191e7
Update fastly/custom_tls_activation.go
joel-g Nov 10, 2020
d9cfa49
Update fastly/custom_tls_certificate.go
joel-g Nov 10, 2020
9582d70
Update fastly/custom_tls_activation.go
joel-g Nov 10, 2020
28f284c
Update fastly/custom_tls_activation.go
joel-g Nov 10, 2020
99777a2
remove pesky type
joel-g Nov 10, 2020
f5af37e
Update fastly/custom_tls_configuration.go
joel-g Nov 12, 2020
5f937e4
Update fastly/custom_tls_configuration.go
joel-g Nov 12, 2020
520f5ed
Update fastly/errors.go
joel-g Nov 12, 2020
ce2d715
Update fastly/errors.go
joel-g Nov 12, 2020
79f7c5d
small changes to match convention, per feedback
joel-g Nov 12, 2020
c7c4e71
Merge branch 'master' of https://github.com/fastly/go-fastly into cus…
joel-g Nov 18, 2020
436cc6c
converts list operations to not use pointers in their input structs
joel-g Nov 18, 2020
9d5ec1e
Update fastly/errors.go
joel-g Dec 8, 2020
3372671
Update fastly/errors.go
joel-g Dec 8, 2020
4a70090
Update fastly/errors.go
joel-g Dec 8, 2020
010b024
fixes ListCustomTLSCertificates filter
joel-g Dec 8, 2020
103981d
Merge branch 'custom-tls' of https://github.com/joel-g/go-fastly into…
joel-g Dec 8, 2020
f135dfb
go fmt
joel-g Dec 8, 2020
f741b0d
Update fastly/custom_tls_certificate.go
joel-g Dec 8, 2020
272b805
Update fastly/errors.go
joel-g Dec 8, 2020
4e84200
Update fastly/errors.go
joel-g Dec 8, 2020
91c7600
Update fastly/errors.go
joel-g Dec 8, 2020
ba046de
pushing filter fix where GH "commit suggestion" didnt work
joel-g Dec 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions fastly/custom_tls_activation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package fastly

import (
"fmt"
"reflect"
"time"

"github.com/google/jsonapi"
)

// TLSActivation represents a /tls/activations response.
type TLSActivation struct {
ID string `jsonapi:"primary,tls_activation"`
TLSConfiguration *TLSConfiguration `jsonapi:"relation,tls_configuration"` // TLSConfiguration type shared with BulkCertificate
TLSDomain *TLSDomain `jsonapi:"relation,tls_domain"` // TLSDomain type shared with BulkCertificate
TLSCertificate *TLSCertificate `jsonapi:"relation,tls_certificate"`
joel-g marked this conversation as resolved.
Show resolved Hide resolved
joel-g marked this conversation as resolved.
Show resolved Hide resolved
CreatedAt *time.Time `jsonapi:"attr,created_at,iso8601"`
}

// TLSCertificate represents a certificate relationship. See CustomTLSCertificate for the /tlsrtificates API/ce
joel-g marked this conversation as resolved.
Show resolved Hide resolved
type TLSCertificate struct {
ID string `jsonapi:"primary,tls_certificate"`
Type string `jsonapi:"attr,type"`
}
joel-g marked this conversation as resolved.
Show resolved Hide resolved

// ListTLSActivationsInput is used as input to the ListTLSActivations function.
type ListTLSActivationsInput struct {
FilterTLSCertificateID *string // Limit the returned activations to a specific certificate.
Integralist marked this conversation as resolved.
Show resolved Hide resolved
FilterTLSConfigurationID *string // Limit the returned activations to a specific TLS configuration.
FilterTLSDomainID *string // Limit the returned rules to a specific domain name.
Include *string // Include related objects. Optional, comma-separated values. Permitted values: tls_certificate, tls_configuration, and tls_domain.
PageNumber *uint // The page index for pagination.
PageSize *uint // The number of activations per page.
}

// formatFilters converts user input into query parameters for filtering.
func (i *ListTLSActivationsInput) formatFilters() map[string]string {
result := map[string]string{}
pairings := map[string]interface{}{
"filter[tls_certificate.id]": i.FilterTLSCertificateID,
"filter[tls_configuration.id]": i.FilterTLSConfigurationID,
"filter[tls_domain.id]": i.FilterTLSDomainID,
"include": i.Include,
"page[number]": i.PageNumber,
"page[size]": i.PageSize,
}
for key, value := range pairings {
if !reflect.ValueOf(value).IsNil() {
result[key] = fmt.Sprintf("%v", reflect.ValueOf(value).Elem())
}
}

return result
}

// ListTLSActivations list all activations.
func (c *Client) ListTLSActivations(i *ListTLSActivationsInput) ([]*TLSActivation, error) {

joel-g marked this conversation as resolved.
Show resolved Hide resolved
p := "/tls/activations"
filters := &RequestOptions{
Params: i.formatFilters(),
Headers: map[string]string{
"Accept": "application/vnd.api+json", // this is required otherwise the filters don't work
},
}

r, err := c.Get(p, filters)
if err != nil {
return nil, err
}

data, err := jsonapi.UnmarshalManyPayload(r.Body, reflect.TypeOf(new(TLSActivation)))
if err != nil {
return nil, err
}

a := make([]*TLSActivation, len(data))
for i := range data {
typed, ok := data[i].(*TLSActivation)
if !ok {
return nil, fmt.Errorf("got back a non-TLSActivation response")
joel-g marked this conversation as resolved.
Show resolved Hide resolved
}
a[i] = typed
}

return a, nil
}

// GetTLSActivationInput is used as input to the GetTLSActivation function.
type GetTLSActivationInput struct {
ID string
}
joel-g marked this conversation as resolved.
Show resolved Hide resolved

// GetTLSActivation retrieve a single activation.
func (c *Client) GetTLSActivation(i *GetTLSActivationInput) (*TLSActivation, error) {

if i.ID == "" {
return nil, ErrMissingID
}

p := fmt.Sprintf("/tls/activations/%s", i.ID)
joel-g marked this conversation as resolved.
Show resolved Hide resolved

r, err := c.Get(p, nil)
joel-g marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

var a TLSActivation
if err := jsonapi.UnmarshalPayload(r.Body, &a); err != nil {
return nil, err
}

return &a, nil
}

// CreateTLSActivationInput is used as input to the CreateTLSActivation function.
type CreateTLSActivationInput struct {
TLSCertificate *TLSCertificate `jsonapi:"relation,tls_certificate,tls_certificate"`
TLSConfiguration *TLSConfiguration `jsonapi:"relation,tls_configuration,tls_configuration"`
joel-g marked this conversation as resolved.
Show resolved Hide resolved
TLSDomain *TLSDomain `jsonapi:"relation,tls_domain,tls_domain"`
joel-g marked this conversation as resolved.
Show resolved Hide resolved
Type string `jsonapi:"primary,tls_activation"` // Type value does not need to be set but existence of this key prevents server error due to API bug that requires "type" to be present.
phamann marked this conversation as resolved.
Show resolved Hide resolved
}

// CreateTLSActivation enable TLS for a domain using a custom certificate.
func (c *Client) CreateTLSActivation(i *CreateTLSActivationInput) (*TLSActivation, error) {

if i.TLSCertificate == nil {
return nil, ErrMissingTLSCertificate
}
if i.TLSConfiguration == nil {
return nil, ErrMissingTLSConfiguration
}
if i.TLSDomain == nil {
return nil, ErrMissingTLSDomain
}

p := "/tls/activations"

r, err := c.PostJSONAPI(p, i, nil)
if err != nil {
return nil, err
}

var a TLSActivation
if err := jsonapi.UnmarshalPayload(r.Body, &a); err != nil {
return nil, err
}

return &a, nil
}

// UpdateTLSActivationInput is used as input to the UpdateTLSActivation function.
type UpdateTLSActivationInput struct {
ID string `jsonapi:"attr,id"`
TLSCertificate *TLSCertificate `jsonapi:"relation,tls_certificate,tls_certificate"`
phamann marked this conversation as resolved.
Show resolved Hide resolved
Type string `jsonapi:"primary,tls_activation"` // Type value does not need to be set but existence of this key prevents server error due to API bug that requires "type" to be present.
}

// UpdateTLSActivation
joel-g marked this conversation as resolved.
Show resolved Hide resolved
func (c *Client) UpdateTLSActivation(i *UpdateTLSActivationInput) (*TLSActivation, error) {
if i.ID == "" {
return nil, ErrMissingID
}
if i.TLSCertificate == nil {
return nil, ErrMissingTLSCertificate
}
joel-g marked this conversation as resolved.
Show resolved Hide resolved

path := fmt.Sprintf("/tls/activations/%s", i.ID)
resp, err := c.PatchJSONAPI(path, i, nil)
if err != nil {
return nil, err
}

var ta TLSActivation
if err := jsonapi.UnmarshalPayload(resp.Body, &ta); err != nil {
return nil, err
}
return &ta, nil
}

// DeleteTLSActivationInput used for deleting a certificate.
type DeleteTLSActivationInput struct {
ID string
}

// DeleteTLSActivation destroy a certificate.
func (c *Client) DeleteTLSActivation(i *DeleteTLSActivationInput) error {
if i.ID == "" {
return ErrMissingID
}

path := fmt.Sprintf("/tls/activations/%s", i.ID)
_, err := c.Delete(path, nil)
return err
}
165 changes: 165 additions & 0 deletions fastly/custom_tls_activation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package fastly

import (
"testing"
)

func TestClient_TLSActivation(t *testing.T) {
t.Parallel()

fixtureBase := "custom_tls_activation/"

// Create
var err error
var ta *TLSActivation
record(t, fixtureBase+"create", func(c *Client) {
ta, err = c.CreateTLSActivation(&CreateTLSActivationInput{
TLSCertificate: &TLSCertificate{ID: "CERTIFICATE_ID"},
TLSConfiguration: &TLSConfiguration{ID: "CONFIGURATION_ID"},
TLSDomain: &TLSDomain{ID: "DOMAIN_NAME"},
})
})
if err != nil {
t.Fatal(err)
}

// Ensure deleted
defer func() {
record(t, fixtureBase+"cleanup", func(c *Client) {
c.DeleteTLSActivation(&DeleteTLSActivationInput{
ID: ta.ID,
})
})
}()

// List
var lta []*TLSActivation
record(t, fixtureBase+"list", func(c *Client) {
lta, err = c.ListTLSActivations(&ListTLSActivationsInput{})
})
if err != nil {
t.Fatal(err)
}
if len(lta) < 1 {
t.Errorf("bad TLS activations: %v", lta)
}
if lta[0].TLSCertificate == nil {
t.Errorf("TLS certificate relation should not be nil: %v", lta)
}
if lta[0].TLSConfiguration == nil {
t.Errorf("TLS configuration relation should not be nil: %v", lta)
}
if lta[0].TLSDomain == nil {
t.Errorf("TLS domain relation should not be nil: %v", lta)
}

// Get
var gta *TLSActivation
record(t, fixtureBase+"get", func(c *Client) {
gta, err = c.GetTLSActivation(&GetTLSActivationInput{
ID: ta.ID,
})
})
if err != nil {
t.Fatal(err)
}
if ta.ID != gta.ID {
t.Errorf("bad ID: %q (%q)", ta.ID, gta.ID)
}
joel-g marked this conversation as resolved.
Show resolved Hide resolved

// Update
var uta *TLSActivation
record(t, fixtureBase+"update", func(c *Client) {
uta, err = c.UpdateTLSActivation(&UpdateTLSActivationInput{
ID: "ACTIVATION_ID",
TLSCertificate: &TLSCertificate{},
})
})
if err != nil {
t.Fatal(err)
}
if ta.ID != uta.ID {
t.Errorf("bad ID: %q (%q)", ta.ID, uta.ID)
}

// Delete
record(t, fixtureBase+"delete", func(c *Client) {
err = c.DeleteTLSActivation(&DeleteTLSActivationInput{
ID: ta.ID,
})
})
if err != nil {
t.Fatal(err)
}
}

func TestClient_CreateTLSActivation_validation(t *testing.T) {
joel-g marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

var err error
record(t, "custom_tls_activation/create", func(c *Client) {
_, err = c.CreateTLSActivation(&CreateTLSActivationInput{
TLSCertificate: &TLSCertificate{},
TLSConfiguration: &TLSConfiguration{},
TLSDomain: &TLSDomain{},
})
})
if err != nil {
t.Fatal(err)
}
}

func TestClient_DeleteTLSActivation_validation(t *testing.T) {
t.Parallel()

var err error
record(t, "custom_tls_activation/delete", func(c *Client) {
err = c.DeleteTLSActivation(&DeleteTLSActivationInput{
ID: "ACTIVATION_ID",
})
})
if err != nil {
t.Fatal(err)
}
}

func TestClient_ListTLSActivations_validation(t *testing.T) {
t.Parallel()

var err error
record(t, "custom_tls_activation/list", func(c *Client) {
_, err = c.ListTLSActivations(&ListTLSActivationsInput{})
})
if err != nil {
t.Fatal(err)
}
}

func TestClient_GetTLSActivation_validation(t *testing.T) {
t.Parallel()

var err error
record(t, "custom_tls_activation/get", func(c *Client) {
_, err = c.GetTLSActivation(&GetTLSActivationInput{
ID: "ACTIVATION_ID",
})
})
if err != nil {
t.Fatal(err)
}
}

func TestClient_UpdateTLSActivation_validation(t *testing.T) {
t.Parallel()

var err error
record(t, "custom_tls_activation/update", func(c *Client) {
_, err = c.UpdateTLSActivation(&UpdateTLSActivationInput{
ID: "ACTIVATION_ID",
TLSCertificate: &TLSCertificate{ID: "CERTIFICATE_ID"},
})
})
if err != nil {
t.Fatal(err)
}
}
Loading