Skip to content

Commit a58fa25

Browse files
committed
Add certificates resource CRUD operations
1 parent 539b5ae commit a58fa25

11 files changed

Lines changed: 579 additions & 8 deletions

syntheticsclientv2/common_models.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ type DowntimeConfigurationV2Input struct {
270270
}
271271

272272
type DowntimeConfigurationsV2Response struct {
273-
Page int `json:"nextPageLink"`
273+
Page string `json:"nextPageLink"`
274274
Pagelimt int `json:"perPage"`
275275
Totalcount int `json:"totalCount"`
276276
Downtimeconfigurations []DowntimeConfiguration `json:"downtimeConfigurations"`
@@ -303,8 +303,8 @@ type LocationV2Input struct {
303303
}
304304

305305
type ChecksV2Response struct {
306-
Nextpagelink int `json:"nextPageLink"`
307-
Perpage int `json:"perPage"`
306+
Nextpagelink string `json:"nextPageLink"`
307+
Perpage int `json:"perPage"`
308308
Tests `json:"tests"`
309309
Totalcount int `json:"totalCount"`
310310
}
@@ -498,3 +498,44 @@ type End struct {
498498
Type string `json:"type"`
499499
Value string `json:"value"`
500500
}
501+
502+
// MFA Certificate models
503+
type MfaCertificate struct {
504+
ID int `json:"id,omitempty"`
505+
Name string `json:"name"`
506+
Description string `json:"description,omitempty"`
507+
Domain string `json:"domain"`
508+
CreatedAt time.Time `json:"createdAt,omitempty"`
509+
UpdatedAt time.Time `json:"updatedAt,omitempty"`
510+
ExpiresAt time.Time `json:"expiresAt,omitempty"`
511+
OrganizationID int `json:"organizationId,omitempty"`
512+
PrivateKey CertificateKey `json:"privateKey,omitempty"`
513+
PublicKey CertificateKey `json:"publicKey,omitempty"`
514+
}
515+
516+
type CertificateKey struct {
517+
Content string `json:"content"`
518+
Filename string `json:"filename,omitempty"`
519+
FileExtension string `json:"fileExtension,omitempty"`
520+
Password string `json:"password,omitempty"`
521+
}
522+
523+
type MfaCertificateV2Response struct {
524+
Certificate MfaCertificate `json:"certificate"`
525+
}
526+
527+
type MfaCertificateV2Input struct {
528+
Certificate MfaCertificateInput `json:"certificate"`
529+
}
530+
531+
type MfaCertificateInput struct {
532+
Name string `json:"name"`
533+
Description string `json:"description,omitempty"`
534+
Domain string `json:"domain"`
535+
PrivateKey *CertificateKey `json:"privateKey,omitempty"`
536+
PublicKey *CertificateKey `json:"publicKey,omitempty"`
537+
}
538+
539+
type MfaCertificatesV2Response struct {
540+
Certificates []MfaCertificate `json:"certificates"`
541+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 Splunk, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package syntheticsclientv2
16+
17+
import (
18+
"bytes"
19+
"encoding/json"
20+
)
21+
22+
func (c Client) CreateMfaCertificateV2(certificateDetails *MfaCertificateV2Input) (*MfaCertificateV2Response, *RequestDetails, error) {
23+
body, err := json.Marshal(certificateDetails)
24+
if err != nil {
25+
return nil, nil, err
26+
}
27+
28+
details, err := c.makePublicAPICall("POST", "/certificates", bytes.NewBuffer(body), nil)
29+
if err != nil {
30+
return nil, details, err
31+
}
32+
33+
newCertificate, err := parseMfaCertificateV2Response(details.ResponseBody)
34+
if err != nil {
35+
return newCertificate, details, err
36+
}
37+
38+
return newCertificate, details, nil
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build unit_tests
2+
// +build unit_tests
3+
4+
package syntheticsclientv2
5+
6+
import (
7+
"encoding/json"
8+
"net/http"
9+
"reflect"
10+
"testing"
11+
)
12+
13+
func TestCreateMfaCertificateV2(t *testing.T) {
14+
setup()
15+
defer teardown()
16+
17+
mockBody := `{"certificate": {"id": 789, "name": "Created MFA Certificate", "createdAt": "2025-06-09T12:00:00Z", "updatedAt": "2025-06-09T12:30:00Z", "domain": "created.com"}}`
18+
19+
testMux.HandleFunc("/certificates", func(w http.ResponseWriter, r *http.Request) {
20+
testMethod(t, r, "POST")
21+
_, err := w.Write([]byte(mockBody))
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
})
26+
27+
input := parseCreateMfaCertificateV2Input(mockBody)
28+
createInput := &MfaCertificateV2Input{
29+
Certificate: MfaCertificateInput{
30+
Name: "Created MFA Certificate",
31+
Domain: "created.com",
32+
},
33+
}
34+
35+
resp, _, err := testClient.CreateMfaCertificateV2(createInput)
36+
if err != nil {
37+
t.Fatalf("unexpected error: %v", err)
38+
}
39+
40+
if !reflect.DeepEqual(*resp, *input) {
41+
t.Errorf("returned\n\n%#v want\n\n%#v", *resp, *input)
42+
}
43+
}
44+
45+
func parseCreateMfaCertificateV2Input(input string) *MfaCertificateV2Response {
46+
cert := &MfaCertificateV2Response{}
47+
err := json.Unmarshal([]byte(input), cert)
48+
if err != nil {
49+
panic(err)
50+
}
51+
return cert
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021 Splunk, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package syntheticsclientv2
16+
17+
import (
18+
"bytes"
19+
"errors"
20+
"fmt"
21+
"strconv"
22+
)
23+
24+
func (c Client) DeleteMfaCertificateV2(id int) (int, error) {
25+
requestDetails, err := c.makePublicAPICall("DELETE", fmt.Sprintf("/certificates/%d", id), bytes.NewBufferString("{}"), nil)
26+
if err != nil {
27+
return 1, err
28+
}
29+
var status = requestDetails.StatusCode
30+
31+
if status >= 300 || status < 200 {
32+
errorMsg := fmt.Sprintf("error: Response code %v. Expecting 2XX.", strconv.Itoa(status))
33+
return status, errors.New(errorMsg)
34+
}
35+
36+
return status, err
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build unit_tests
2+
// +build unit_tests
3+
4+
package syntheticsclientv2
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
)
10+
11+
func TestDeleteMfaCertificateV2(t *testing.T) {
12+
setup()
13+
defer teardown()
14+
15+
mockBody := `{"result": "success", "message": "Certificate deleted"}`
16+
17+
testMux.HandleFunc("/certificates/123", func(w http.ResponseWriter, r *http.Request) {
18+
testMethod(t, r, "DELETE")
19+
_, err := w.Write([]byte(mockBody))
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
})
24+
25+
status, err := testClient.DeleteMfaCertificateV2(123)
26+
if err != nil {
27+
t.Fatalf("unexpected error: %v", err)
28+
}
29+
30+
if status != http.StatusOK {
31+
t.Errorf("returned\n\n%#v want\n\n%#v", status, http.StatusOK)
32+
}
33+
}

syntheticsclientv2/get_checksv2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func integersQueryParam(params []int, queryParamName string) string {
108108
if len(params) == 0 {
109109
return ""
110110
}
111-
return queryParamName + strings.Trim(strings.Replace(fmt.Sprint(params), " ", queryParamName, -1), "[]")
111+
return queryParamName + strings.Trim(strings.ReplaceAll(fmt.Sprint(params), " ", queryParamName), "[]")
112112
}
113113

114114
func stringsQueryParam(params []string, queryParamName string) string {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2021 Splunk, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package syntheticsclientv2
16+
17+
import (
18+
"bytes"
19+
"encoding/json"
20+
"fmt"
21+
)
22+
23+
func parseMfaCertificateV2Response(response string) (*MfaCertificateV2Response, error) {
24+
// Parse the response and return the certificate object
25+
var certificate MfaCertificateV2Response
26+
err := json.Unmarshal([]byte(response), &certificate)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &certificate, err
32+
}
33+
34+
func (c Client) GetMfaCertificateV2(id int) (*MfaCertificateV2Response, *RequestDetails, error) {
35+
details, err := c.makePublicAPICall("GET",
36+
fmt.Sprintf("/certificates/%d", id),
37+
bytes.NewBufferString("{}"),
38+
nil)
39+
40+
if err != nil {
41+
return nil, details, err
42+
}
43+
44+
certificate, err := parseMfaCertificateV2Response(details.ResponseBody)
45+
if err != nil {
46+
return certificate, details, err
47+
}
48+
49+
return certificate, details, nil
50+
}
51+
52+
func parseMfaCertificatesV2Response(response string) (*MfaCertificatesV2Response, error) {
53+
// Parse the response and return the certificates object
54+
var certificates MfaCertificatesV2Response
55+
err := json.Unmarshal([]byte(response), &certificates)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
return &certificates, err
61+
}
62+
63+
func (c Client) GetMfaCertificatesV2() (*MfaCertificatesV2Response, *RequestDetails, error) {
64+
details, err := c.makePublicAPICall("GET",
65+
"/certificates",
66+
bytes.NewBufferString("{}"),
67+
nil)
68+
69+
if err != nil {
70+
return nil, details, err
71+
}
72+
73+
certificates, err := parseMfaCertificatesV2Response(details.ResponseBody)
74+
if err != nil {
75+
return certificates, details, err
76+
}
77+
78+
return certificates, details, nil
79+
}

0 commit comments

Comments
 (0)