github.com/fastly/go-fastly/v6@v6.8.0/fastly/custom_tls_activation.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/google/jsonapi"
    10  )
    11  
    12  // TLSActivation represents a /tls/activations response.
    13  type TLSActivation struct {
    14  	ID            string                `jsonapi:"primary,tls_activation"`
    15  	Configuration *TLSConfiguration     `jsonapi:"relation,tls_configuration"` // TLSConfiguration type shared with BulkCertificate
    16  	Domain        *TLSDomain            `jsonapi:"relation,tls_domain"`        // TLSDomain type shared with BulkCertificate
    17  	Certificate   *CustomTLSCertificate `jsonapi:"relation,tls_certificate"`
    18  	CreatedAt     *time.Time            `jsonapi:"attr,created_at,iso8601"`
    19  }
    20  
    21  // ListTLSActivationsInput is used as input to the ListTLSActivations function.
    22  type ListTLSActivationsInput struct {
    23  	FilterTLSCertificateID   string // Limit the returned activations to a specific certificate.
    24  	FilterTLSConfigurationID string // Limit the returned activations to a specific TLS configuration.
    25  	FilterTLSDomainID        string // Limit the returned rules to a specific domain name.
    26  	Include                  string // Include related objects. Optional, comma-separated values. Permitted values: tls_certificate, tls_configuration, and tls_domain.
    27  	PageNumber               int    // The page index for pagination.
    28  	PageSize                 int    // The number of activations per page.
    29  }
    30  
    31  // formatFilters converts user input into query parameters for filtering.
    32  func (i *ListTLSActivationsInput) formatFilters() map[string]string {
    33  	result := map[string]string{}
    34  	pairings := map[string]interface{}{
    35  		"filter[tls_certificate.id]":   i.FilterTLSCertificateID,
    36  		"filter[tls_configuration.id]": i.FilterTLSConfigurationID,
    37  		"filter[tls_domain.id]":        i.FilterTLSDomainID,
    38  		"include":                      i.Include,
    39  		"page[number]":                 i.PageNumber,
    40  		"page[size]":                   i.PageSize,
    41  	}
    42  
    43  	for key, value := range pairings {
    44  		switch t := reflect.TypeOf(value).String(); t {
    45  		case "string":
    46  			if value != "" {
    47  				result[key] = value.(string)
    48  			}
    49  		case "int":
    50  			if value != 0 {
    51  				result[key] = strconv.Itoa(value.(int))
    52  			}
    53  		}
    54  	}
    55  
    56  	return result
    57  }
    58  
    59  // ListTLSActivations list all activations.
    60  func (c *Client) ListTLSActivations(i *ListTLSActivationsInput) ([]*TLSActivation, error) {
    61  	p := "/tls/activations"
    62  	filters := &RequestOptions{
    63  		Params: i.formatFilters(),
    64  		Headers: map[string]string{
    65  			"Accept": "application/vnd.api+json", // this is required otherwise the filters don't work
    66  		},
    67  	}
    68  
    69  	r, err := c.Get(p, filters)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	data, err := jsonapi.UnmarshalManyPayload(r.Body, reflect.TypeOf(new(TLSActivation)))
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	a := make([]*TLSActivation, len(data))
    80  	for i := range data {
    81  		typed, ok := data[i].(*TLSActivation)
    82  		if !ok {
    83  			return nil, fmt.Errorf("unexpected response type: %T", data[i])
    84  		}
    85  		a[i] = typed
    86  	}
    87  
    88  	return a, nil
    89  }
    90  
    91  // GetTLSActivationInput is used as input to the GetTLSActivation function.
    92  type GetTLSActivationInput struct {
    93  	ID      string
    94  	Include *string // Include related objects. Optional, comma-separated values. Permitted values: tls_certificate, tls_configuration, and tls_domain.
    95  }
    96  
    97  // GetTLSActivation retrieve a single activation.
    98  func (c *Client) GetTLSActivation(i *GetTLSActivationInput) (*TLSActivation, error) {
    99  	if i.ID == "" {
   100  		return nil, ErrMissingID
   101  	}
   102  
   103  	p := fmt.Sprintf("/tls/activations/%s", i.ID)
   104  
   105  	ro := &RequestOptions{
   106  		Headers: map[string]string{
   107  			"Accept": "application/vnd.api+json", // this is required otherwise the params don't work
   108  		},
   109  	}
   110  
   111  	if i.Include != nil {
   112  		ro.Params = map[string]string{"include": *i.Include}
   113  	}
   114  
   115  	r, err := c.Get(p, ro)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	var a TLSActivation
   121  	if err := jsonapi.UnmarshalPayload(r.Body, &a); err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	return &a, nil
   126  }
   127  
   128  // CreateTLSActivationInput is used as input to the CreateTLSActivation function.
   129  type CreateTLSActivationInput struct {
   130  	ID            string                `jsonapi:"primary,tls_activation"`   // ID value does not need to be set.
   131  	Certificate   *CustomTLSCertificate `jsonapi:"relation,tls_certificate"` // Only ID of CustomTLSCertificate needs to be set.
   132  	Configuration *TLSConfiguration     `jsonapi:"relation,tls_configuration,omitempty"`
   133  	Domain        *TLSDomain            `jsonapi:"relation,tls_domain"`
   134  }
   135  
   136  // CreateTLSActivation enable TLS for a domain using a custom certificate.
   137  func (c *Client) CreateTLSActivation(i *CreateTLSActivationInput) (*TLSActivation, error) {
   138  	if i.Certificate == nil {
   139  		return nil, ErrMissingTLSCertificate
   140  	}
   141  	if i.Domain == nil {
   142  		return nil, ErrMissingTLSDomain
   143  	}
   144  
   145  	p := "/tls/activations"
   146  
   147  	r, err := c.PostJSONAPI(p, i, nil)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	var a TLSActivation
   153  	if err := jsonapi.UnmarshalPayload(r.Body, &a); err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	return &a, nil
   158  }
   159  
   160  // UpdateTLSActivationInput is used as input to the UpdateTLSActivation function.
   161  type UpdateTLSActivationInput struct {
   162  	ID          string                `jsonapi:"primary,tls_activation"`
   163  	Certificate *CustomTLSCertificate `jsonapi:"relation,tls_certificate"` // Only ID of CustomTLSCertificate needs to be set.
   164  }
   165  
   166  // UpdateTLSActivation updates the certificate used to terminate TLS traffic for the domain associated with this TLS activation.
   167  func (c *Client) UpdateTLSActivation(i *UpdateTLSActivationInput) (*TLSActivation, error) {
   168  	if i.ID == "" {
   169  		return nil, ErrMissingID
   170  	}
   171  	if i.Certificate == nil {
   172  		return nil, ErrMissingTLSCertificate
   173  	}
   174  
   175  	path := fmt.Sprintf("/tls/activations/%s", i.ID)
   176  	resp, err := c.PatchJSONAPI(path, i, nil)
   177  	if err != nil {
   178  		return nil, err
   179  	}
   180  	defer resp.Body.Close()
   181  
   182  	var ta TLSActivation
   183  	if err := jsonapi.UnmarshalPayload(resp.Body, &ta); err != nil {
   184  		return nil, err
   185  	}
   186  	return &ta, nil
   187  }
   188  
   189  // DeleteTLSActivationInput used for deleting a certificate.
   190  type DeleteTLSActivationInput struct {
   191  	ID string
   192  }
   193  
   194  // DeleteTLSActivation destroy a certificate.
   195  func (c *Client) DeleteTLSActivation(i *DeleteTLSActivationInput) error {
   196  	if i.ID == "" {
   197  		return ErrMissingID
   198  	}
   199  
   200  	path := fmt.Sprintf("/tls/activations/%s", i.ID)
   201  	_, err := c.Delete(path, nil)
   202  	return err
   203  }