File: ca_certificate_service.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (144 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package kong

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
)

// CACertificateService handles Certificates in Kong.
type CACertificateService service

// Create creates a CACertificate in Kong.
// If an ID is specified, it will be used to
// create a certificate in Kong, otherwise an ID
// is auto-generated.
func (s *CACertificateService) Create(ctx context.Context,
	certificate *CACertificate) (*CACertificate, error) {

	queryPath := "/ca_certificates"
	method := "POST"
	if certificate.ID != nil {
		queryPath = queryPath + "/" + *certificate.ID
		method = "PUT"
	}
	req, err := s.client.NewRequest(method, queryPath, nil, certificate)

	if err != nil {
		return nil, err
	}

	var createdCACertificate CACertificate
	_, err = s.client.Do(ctx, req, &createdCACertificate)
	if err != nil {
		return nil, err
	}
	return &createdCACertificate, nil
}

// Get fetches a CACertificate in Kong.
func (s *CACertificateService) Get(ctx context.Context,
	ID *string) (*CACertificate, error) {

	if isEmptyString(ID) {
		return nil, errors.New("ID cannot be nil for Get operation")
	}

	endpoint := fmt.Sprintf("/ca_certificates/%v", *ID)
	req, err := s.client.NewRequest("GET", endpoint, nil, nil)
	if err != nil {
		return nil, err
	}

	var certificate CACertificate
	_, err = s.client.Do(ctx, req, &certificate)
	if err != nil {
		return nil, err
	}
	return &certificate, nil
}

// Update updates a CACertificate in Kong
func (s *CACertificateService) Update(ctx context.Context,
	certificate *CACertificate) (*CACertificate, error) {

	if isEmptyString(certificate.ID) {
		return nil, errors.New("ID cannot be nil for Update op           eration")
	}

	endpoint := fmt.Sprintf("/ca_certificates/%v", *certificate.ID)
	req, err := s.client.NewRequest("PATCH", endpoint, nil, certificate)
	if err != nil {
		return nil, err
	}

	var updatedAPI CACertificate
	_, err = s.client.Do(ctx, req, &updatedAPI)
	if err != nil {
		return nil, err
	}
	return &updatedAPI, nil
}

// Delete deletes a CACertificate in Kong
func (s *CACertificateService) Delete(ctx context.Context,
	ID *string) error {

	if isEmptyString(ID) {
		return errors.New("ID cannot be nil for Delete operation")
	}

	endpoint := fmt.Sprintf("/ca_certificates/%v", *ID)
	req, err := s.client.NewRequest("DELETE", endpoint, nil, nil)
	if err != nil {
		return err
	}

	_, err = s.client.Do(ctx, req, nil)
	return err
}

// List fetches a list of certificate in Kong.
// opt can be used to control pagination.
func (s *CACertificateService) List(ctx context.Context,
	opt *ListOpt) ([]*CACertificate, *ListOpt, error) {
	data, next, err := s.client.list(ctx, "/ca_certificates", opt)
	if err != nil {
		return nil, nil, err
	}
	var certificates []*CACertificate
	for _, object := range data {
		b, err := object.MarshalJSON()
		if err != nil {
			return nil, nil, err
		}
		var certificate CACertificate
		err = json.Unmarshal(b, &certificate)
		if err != nil {
			return nil, nil, err
		}
		certificates = append(certificates, &certificate)
	}

	return certificates, next, nil
}

// ListAll fetches all Certificates in Kong.
// This method can take a while if there
// a lot of Certificates present.
func (s *CACertificateService) ListAll(ctx context.Context) ([]*CACertificate,
	error) {
	var certificates, data []*CACertificate
	var err error
	opt := &ListOpt{Size: pageSize}

	for opt != nil {
		data, opt, err = s.List(ctx, opt)
		if err != nil {
			return nil, err
		}
		certificates = append(certificates, data...)
	}
	return certificates, nil
}