-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsdk.go
2307 lines (2073 loc) · 101 KB
/
sdk.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sdk
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"time"
)
// NewClient initialised the Client to communicate to the Neon Platform.
func NewClient(cfg Config) (*Client, error) {
if _, ok := (cfg.HTTPClient).(mockHTTPClient); !ok && cfg.Key == "" {
return nil, errors.New(
"authorization key must be provided: https://neon.tech/docs/reference/api-reference/#authentication",
)
}
c := &Client{
baseURL: baseURL,
cfg: cfg,
}
if c.cfg.HTTPClient == nil {
c.cfg.HTTPClient = &http.Client{Timeout: defaultTimeout}
}
return c, nil
}
// Config defines the client's configuration.
type Config struct {
// Key defines the access API key.
Key string
// HTTPClient HTTP client to communicate with the API.
HTTPClient HTTPClient
}
const (
baseURL = "https://console.neon.tech/api/v2"
defaultTimeout = 2 * time.Minute
)
// Client defines the Neon SDK client.
type Client struct {
cfg Config
baseURL string
}
// HTTPClient client to handle http requests.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func setHeaders(req *http.Request, token string) {
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
if token != "" {
req.Header.Add("Authorization", "Bearer "+token)
}
}
func (c Client) requestHandler(url string, t string, reqPayload interface{}, responsePayload interface{}) error {
var body io.Reader
var err error
if reqPayload != nil {
if v := reflect.ValueOf(reqPayload); v.Kind() == reflect.Struct || !v.IsNil() {
b, err := json.Marshal(reqPayload)
if err != nil {
return err
}
body = bytes.NewReader(b)
}
}
req, _ := http.NewRequest(t, url, body)
setHeaders(req, c.cfg.Key)
res, err := c.cfg.HTTPClient.Do(req)
if err != nil {
return err
}
if res.StatusCode > 299 {
return convertErrorResponse(res)
}
if responsePayload != nil {
buf, err := io.ReadAll(res.Body)
defer func() { _ = res.Body.Close() }()
if err != nil {
return err
}
return json.Unmarshal(buf, responsePayload)
}
return nil
}
// AddProjectJWKS Add a new JWKS URL to a project, such that it can be used for verifying JWTs used as the authentication mechanism for the specified project.
// The URL must be a valid HTTPS URL that returns a JSON Web Key Set.
// The `provider_name` field allows you to specify which authentication provider you're using (e.g., Clerk, Auth0, AWS Cognito, etc.).
// The `branch_id` can be used to specify on which branches the JWKS URL will be accepted. If not specified, then it will work on any branch.
// The `role_names` can be used to specify for which roles the JWKS URL will be accepted.
// The `jwt_audience` can be used to specify which "aud" values should be accepted by Neon in the JWTs that are used for authentication.
func (c Client) AddProjectJWKS(projectID string, cfg AddProjectJWKSRequest) (JWKSCreationOperation, error) {
var v JWKSCreationOperation
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/jwks", "POST", cfg, &v); err != nil {
return JWKSCreationOperation{}, err
}
return v, nil
}
// CreateApiKey Creates an API key.
// The `key_name` is a user-specified name for the key.
// This method returns an `id` and `key`. The `key` is a randomly generated, 64-bit token required to access the Neon API.
// API keys can also be managed in the Neon Console.
// See [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) CreateApiKey(cfg ApiKeyCreateRequest) (ApiKeyCreateResponse, error) {
var v ApiKeyCreateResponse
if err := c.requestHandler(c.baseURL+"/api_keys", "POST", cfg, &v); err != nil {
return ApiKeyCreateResponse{}, err
}
return v, nil
}
// CreateOrgApiKey Creates an API key for the specified organization.
// The `key_name` is a user-specified name for the key.
// This method returns an `id` and `key`. The `key` is a randomly generated, 64-bit token required to access the Neon API.
// API keys can also be managed in the Neon Console.
// See [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) CreateOrgApiKey(orgID string, cfg OrgApiKeyCreateRequest) (OrgApiKeyCreateResponse, error) {
var v OrgApiKeyCreateResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/api_keys", "POST", cfg, &v); err != nil {
return OrgApiKeyCreateResponse{}, err
}
return v, nil
}
// CreateOrganizationInvitations Creates invitations for a specific organization.
// If the invited user has an existing account, they automatically join as a member.
// If they don't yet have an account, they are invited to create one, after which they become a member.
// Each invited user receives an email notification.
func (c Client) CreateOrganizationInvitations(orgID string, cfg OrganizationInvitesCreateRequest) (OrganizationInvitationsResponse, error) {
var v OrganizationInvitationsResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/invitations", "POST", cfg, &v); err != nil {
return OrganizationInvitationsResponse{}, err
}
return v, nil
}
// CreateProject Creates a Neon project.
// A project is the top-level object in the Neon object hierarchy.
// Plan limits define how many projects you can create.
// For more information, see [Manage projects](https://neon.tech/docs/manage/projects/).
// You can specify a region and Postgres version in the request body.
// Neon currently supports PostgreSQL 14, 15, 16, and 17.
// For supported regions and `region_id` values, see [Regions](https://neon.tech/docs/introduction/regions/).
func (c Client) CreateProject(cfg ProjectCreateRequest) (CreatedProject, error) {
var v CreatedProject
if err := c.requestHandler(c.baseURL+"/projects", "POST", cfg, &v); err != nil {
return CreatedProject{}, err
}
return v, nil
}
// CreateProjectBranch Creates a branch in the specified project.
// You can obtain a `project_id` by listing the projects for your Neon account.
// This method does not require a request body, but you can specify one to create a compute endpoint for the branch or to select a non-default parent branch.
// The default behavior is to create a branch from the project's default branch with no compute endpoint, and the branch name is auto-generated.
// There is a maximum of one read-write endpoint per branch.
// A branch can have multiple read-only endpoints.
// For related information, see [Manage branches](https://neon.tech/docs/manage/branches/).
func (c Client) CreateProjectBranch(projectID string, cfg *CreateProjectBranchReqObj) (CreatedBranch, error) {
var v CreatedBranch
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches", "POST", cfg, &v); err != nil {
return CreatedBranch{}, err
}
return v, nil
}
// CreateProjectBranchDatabase Creates a database in the specified branch.
// A branch can have multiple databases.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
func (c Client) CreateProjectBranchDatabase(projectID string, branchID string, cfg DatabaseCreateRequest) (DatabaseOperations, error) {
var v DatabaseOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/databases", "POST", cfg, &v); err != nil {
return DatabaseOperations{}, err
}
return v, nil
}
// CreateProjectBranchRole Creates a Postgres role in the specified branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
// Connections established to the active compute endpoint will be dropped.
// If the compute endpoint is idle, the endpoint becomes active for a short period of time and is suspended afterward.
func (c Client) CreateProjectBranchRole(projectID string, branchID string, cfg RoleCreateRequest) (RoleOperations, error) {
var v RoleOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles", "POST", cfg, &v); err != nil {
return RoleOperations{}, err
}
return v, nil
}
// CreateProjectEndpoint Creates a compute endpoint for the specified branch.
// An endpoint is a Neon compute instance.
// There is a maximum of one read-write compute endpoint per branch.
// If the specified branch already has a read-write compute endpoint, the operation fails.
// A branch can have multiple read-only compute endpoints.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain `branch_id` by listing the project's branches.
// A `branch_id` has a `br-` prefix.
// For supported regions and `region_id` values, see [Regions](https://neon.tech/docs/introduction/regions/).
// For more information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) CreateProjectEndpoint(projectID string, cfg EndpointCreateRequest) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints", "POST", cfg, &v); err != nil {
return EndpointOperations{}, err
}
return v, nil
}
// DeleteProject Deletes the specified project.
// You can obtain a `project_id` by listing the projects for your Neon account.
// Deleting a project is a permanent action.
// Deleting a project also deletes endpoints, branches, databases, and users that belong to the project.
func (c Client) DeleteProject(projectID string) (ProjectResponse, error) {
var v ProjectResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID, "DELETE", nil, &v); err != nil {
return ProjectResponse{}, err
}
return v, nil
}
// DeleteProjectBranch Deletes the specified branch from a project, and places
// all compute endpoints into an idle state, breaking existing client connections.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain a `branch_id` by listing the project's branches.
// For related information, see [Manage branches](https://neon.tech/docs/manage/branches/).
// When a successful response status is received, the compute endpoints are still active,
// and the branch is not yet deleted from storage.
// The deletion occurs after all operations finish.
// You cannot delete a project's root or default branch, and you cannot delete a branch that has a child branch.
// A project must have at least one branch.
func (c Client) DeleteProjectBranch(projectID string, branchID string) (BranchOperations, error) {
var v BranchOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID, "DELETE", nil, &v); err != nil {
return BranchOperations{}, err
}
return v, nil
}
// DeleteProjectBranchDatabase Deletes the specified database from the branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` and `database_name` by listing the branch's databases.
// For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
func (c Client) DeleteProjectBranchDatabase(projectID string, branchID string, databaseName string) (DatabaseOperations, error) {
var v DatabaseOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/databases/"+databaseName, "DELETE", nil, &v); err != nil {
return DatabaseOperations{}, err
}
return v, nil
}
// DeleteProjectBranchRole Deletes the specified Postgres role from the branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// You can obtain the `role_name` by listing the roles for a branch.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
func (c Client) DeleteProjectBranchRole(projectID string, branchID string, roleName string) (RoleOperations, error) {
var v RoleOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles/"+roleName, "DELETE", nil, &v); err != nil {
return RoleOperations{}, err
}
return v, nil
}
// DeleteProjectEndpoint Delete the specified compute endpoint.
// A compute endpoint is a Neon compute instance.
// Deleting a compute endpoint drops existing network connections to the compute endpoint.
// The deletion is completed when last operation in the chain finishes successfully.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) DeleteProjectEndpoint(projectID string, endpointID string) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID, "DELETE", nil, &v); err != nil {
return EndpointOperations{}, err
}
return v, nil
}
// DeleteProjectJWKS Deletes a JWKS URL from the specified project
func (c Client) DeleteProjectJWKS(projectID string, jwksID string) (JWKS, error) {
var v JWKS
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/jwks/"+jwksID, "DELETE", nil, &v); err != nil {
return JWKS{}, err
}
return v, nil
}
// GetActiveRegions Retrieves the list of supported Neon regions
func (c Client) GetActiveRegions() (ActiveRegionsResponse, error) {
var v ActiveRegionsResponse
if err := c.requestHandler(c.baseURL+"/regions", "GET", nil, &v); err != nil {
return ActiveRegionsResponse{}, err
}
return v, nil
}
// GetConnectionURI Retrieves a connection URI for the specified database.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `database_name` by listing the databases for a branch.
// You can obtain a `role_name` by listing the roles for a branch.
func (c Client) GetConnectionURI(projectID string, branchID *string, endpointID *string, databaseName string, roleName string, pooled *bool) (ConnectionURIResponse, error) {
var (
queryElements []string
query string
)
queryElements = append(queryElements, "database_name="+databaseName)
queryElements = append(queryElements, "role_name="+roleName)
if branchID != nil {
queryElements = append(queryElements, "branch_id="+*branchID)
}
if endpointID != nil {
queryElements = append(queryElements, "endpoint_id="+*endpointID)
}
if pooled != nil {
queryElements = append(queryElements, "pooled="+func(pooled bool) string {
if pooled {
return "true"
}
return "false"
}(*pooled))
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ConnectionURIResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/connection_uri"+query, "GET", nil, &v); err != nil {
return ConnectionURIResponse{}, err
}
return v, nil
}
// GetConsumptionHistoryPerAccount Retrieves consumption metrics for Scale and Business plan accounts. History begins at the time of upgrade.
// Available for Scale and Business plan users only.
func (c Client) GetConsumptionHistoryPerAccount(from time.Time, to time.Time, granularity ConsumptionHistoryGranularity, orgID *string, includeV1Metrics *bool) (ConsumptionHistoryPerAccountResponse, error) {
var (
queryElements []string
query string
)
queryElements = append(queryElements, "from="+from.Format(time.RFC3339))
queryElements = append(queryElements, "to="+to.Format(time.RFC3339))
queryElements = append(queryElements, "granularity="+string(granularity))
if orgID != nil {
queryElements = append(queryElements, "org_id="+*orgID)
}
if includeV1Metrics != nil {
queryElements = append(queryElements, "include_v1_metrics="+func(includeV1Metrics bool) string {
if includeV1Metrics {
return "true"
}
return "false"
}(*includeV1Metrics))
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ConsumptionHistoryPerAccountResponse
if err := c.requestHandler(c.baseURL+"/consumption_history/account"+query, "GET", nil, &v); err != nil {
return ConsumptionHistoryPerAccountResponse{}, err
}
return v, nil
}
// GetConsumptionHistoryPerProject Retrieves consumption metrics for Scale and Business plan projects. History begins at the time of upgrade.
// Available for Scale and Business plan users only.
// Issuing a call to this API does not wake a project's compute endpoint.
func (c Client) GetConsumptionHistoryPerProject(cursor *string, limit *int, projectIDs []string, from time.Time, to time.Time, granularity ConsumptionHistoryGranularity, orgID *string, includeV1Metrics *bool) (GetConsumptionHistoryPerProjectRespObj, error) {
var (
queryElements []string
query string
)
queryElements = append(queryElements, "from="+from.Format(time.RFC3339))
queryElements = append(queryElements, "to="+to.Format(time.RFC3339))
queryElements = append(queryElements, "granularity="+string(granularity))
if cursor != nil {
queryElements = append(queryElements, "cursor="+*cursor)
}
if limit != nil {
queryElements = append(queryElements, "limit="+strconv.FormatInt(int64(*limit), 10))
}
if len(projectIDs) > 0 {
queryElements = append(queryElements, "project_ids="+strings.Join(projectIDs, ","))
}
if orgID != nil {
queryElements = append(queryElements, "org_id="+*orgID)
}
if includeV1Metrics != nil {
queryElements = append(queryElements, "include_v1_metrics="+func(includeV1Metrics bool) string {
if includeV1Metrics {
return "true"
}
return "false"
}(*includeV1Metrics))
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v GetConsumptionHistoryPerProjectRespObj
if err := c.requestHandler(c.baseURL+"/consumption_history/projects"+query, "GET", nil, &v); err != nil {
return GetConsumptionHistoryPerProjectRespObj{}, err
}
return v, nil
}
// GetCurrentUserInfo Retrieves information about the current Neon user account.
func (c Client) GetCurrentUserInfo() (CurrentUserInfoResponse, error) {
var v CurrentUserInfoResponse
if err := c.requestHandler(c.baseURL+"/users/me", "GET", nil, &v); err != nil {
return CurrentUserInfoResponse{}, err
}
return v, nil
}
// GetCurrentUserOrganizations Retrieves information about the current Neon user's organizations
func (c Client) GetCurrentUserOrganizations() (OrganizationsResponse, error) {
var v OrganizationsResponse
if err := c.requestHandler(c.baseURL+"/users/me/organizations", "GET", nil, &v); err != nil {
return OrganizationsResponse{}, err
}
return v, nil
}
// GetOrganization Retrieves information about the specified organization.
func (c Client) GetOrganization(orgID string) (Organization, error) {
var v Organization
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID, "GET", nil, &v); err != nil {
return Organization{}, err
}
return v, nil
}
// GetOrganizationInvitations Retrieves information about extended invitations for the specified organization
func (c Client) GetOrganizationInvitations(orgID string) (OrganizationInvitationsResponse, error) {
var v OrganizationInvitationsResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/invitations", "GET", nil, &v); err != nil {
return OrganizationInvitationsResponse{}, err
}
return v, nil
}
// GetOrganizationMember Retrieves information about the specified organization member.
func (c Client) GetOrganizationMember(orgID string, memberID string) (Member, error) {
var v Member
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/members/"+memberID, "GET", nil, &v); err != nil {
return Member{}, err
}
return v, nil
}
// GetOrganizationMembers Retrieves information about the specified organization members.
func (c Client) GetOrganizationMembers(orgID string) (OrganizationMembersResponse, error) {
var v OrganizationMembersResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/members", "GET", nil, &v); err != nil {
return OrganizationMembersResponse{}, err
}
return v, nil
}
// GetProject Retrieves information about the specified project.
// A project is the top-level object in the Neon object hierarchy.
// You can obtain a `project_id` by listing the projects for your Neon account.
func (c Client) GetProject(projectID string) (ProjectResponse, error) {
var v ProjectResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID, "GET", nil, &v); err != nil {
return ProjectResponse{}, err
}
return v, nil
}
// GetProjectBranch Retrieves information about the specified branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain a `branch_id` by listing the project's branches.
// A `branch_id` value has a `br-` prefix.
// Each Neon project is initially created with a root and default branch named `main`.
// A project can contain one or more branches.
// A parent branch is identified by a `parent_id` value, which is the `id` of the parent branch.
// For related information, see [Manage branches](https://neon.tech/docs/manage/branches/).
func (c Client) GetProjectBranch(projectID string, branchID string) (GetProjectBranchRespObj, error) {
var v GetProjectBranchRespObj
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID, "GET", nil, &v); err != nil {
return GetProjectBranchRespObj{}, err
}
return v, nil
}
// GetProjectBranchDatabase Retrieves information about the specified database.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` and `database_name` by listing the branch's databases.
// For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
func (c Client) GetProjectBranchDatabase(projectID string, branchID string, databaseName string) (DatabaseResponse, error) {
var v DatabaseResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/databases/"+databaseName, "GET", nil, &v); err != nil {
return DatabaseResponse{}, err
}
return v, nil
}
// GetProjectBranchRole Retrieves details about the specified role.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// You can obtain the `role_name` by listing the roles for a branch.
// In Neon, the terms "role" and "user" are synonymous.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
func (c Client) GetProjectBranchRole(projectID string, branchID string, roleName string) (RoleResponse, error) {
var v RoleResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles/"+roleName, "GET", nil, &v); err != nil {
return RoleResponse{}, err
}
return v, nil
}
// GetProjectBranchRolePassword Retrieves the password for the specified Postgres role, if possible.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// You can obtain the `role_name` by listing the roles for a branch.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
func (c Client) GetProjectBranchRolePassword(projectID string, branchID string, roleName string) (RolePasswordResponse, error) {
var v RolePasswordResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles/"+roleName+"/reveal_password", "GET", nil, &v); err != nil {
return RolePasswordResponse{}, err
}
return v, nil
}
// GetProjectBranchSchema Retrieves the schema from the specified database. The `lsn` and `timestamp` values cannot be specified at the same time. If both are omitted, the database schema is retrieved from database's head.
func (c Client) GetProjectBranchSchema(projectID string, branchID string, dbName string, lsn *string, timestamp *time.Time) (BranchSchemaResponse, error) {
var (
queryElements []string
query string
)
queryElements = append(queryElements, "db_name="+dbName)
if lsn != nil {
queryElements = append(queryElements, "lsn="+*lsn)
}
if timestamp != nil {
queryElements = append(queryElements, "timestamp="+timestamp.Format(time.RFC3339))
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v BranchSchemaResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/schema"+query, "GET", nil, &v); err != nil {
return BranchSchemaResponse{}, err
}
return v, nil
}
// GetProjectEndpoint Retrieves information about the specified compute endpoint.
// A compute endpoint is a Neon compute instance.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) GetProjectEndpoint(projectID string, endpointID string) (EndpointResponse, error) {
var v EndpointResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID, "GET", nil, &v); err != nil {
return EndpointResponse{}, err
}
return v, nil
}
// GetProjectJWKS Returns all the available JWKS URLs that can be used for verifying JWTs used as the authentication mechanism for the specified project.
func (c Client) GetProjectJWKS(projectID string) (ProjectJWKSResponse, error) {
var v ProjectJWKSResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/jwks", "GET", nil, &v); err != nil {
return ProjectJWKSResponse{}, err
}
return v, nil
}
// GetProjectOperation Retrieves details for the specified operation.
// An operation is an action performed on a Neon project resource.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain a `operation_id` by listing operations for the project.
func (c Client) GetProjectOperation(projectID string, operationID string) (OperationResponse, error) {
var v OperationResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/operations/"+operationID, "GET", nil, &v); err != nil {
return OperationResponse{}, err
}
return v, nil
}
// GrantPermissionToProject Grants project access to the account associated with the specified email address
func (c Client) GrantPermissionToProject(projectID string, cfg GrantPermissionToProjectRequest) (ProjectPermission, error) {
var v ProjectPermission
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/permissions", "POST", cfg, &v); err != nil {
return ProjectPermission{}, err
}
return v, nil
}
// ListApiKeys Retrieves the API keys for your Neon account.
// The response does not include API key tokens. A token is only provided when creating an API key.
// API keys can also be managed in the Neon Console.
// For more information, see [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) ListApiKeys() ([]ApiKeysListResponseItem, error) {
var v []ApiKeysListResponseItem
if err := c.requestHandler(c.baseURL+"/api_keys", "GET", nil, &v); err != nil {
return nil, err
}
return v, nil
}
// ListOrgApiKeys Retrieves the API keys for the specified organization.
// The response does not include API key tokens. A token is only provided when creating an API key.
// API keys can also be managed in the Neon Console.
// For more information, see [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) ListOrgApiKeys(orgID string) ([]OrgApiKeysListResponseItem, error) {
var v []OrgApiKeysListResponseItem
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/api_keys", "GET", nil, &v); err != nil {
return nil, err
}
return v, nil
}
// ListProjectBranchDatabases Retrieves a list of databases for the specified branch.
// A branch can have multiple databases.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
func (c Client) ListProjectBranchDatabases(projectID string, branchID string) (DatabasesResponse, error) {
var v DatabasesResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/databases", "GET", nil, &v); err != nil {
return DatabasesResponse{}, err
}
return v, nil
}
// ListProjectBranchEndpoints Retrieves a list of compute endpoints for the specified branch.
// Neon permits only one read-write compute endpoint per branch.
// A branch can have multiple read-only compute endpoints.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
func (c Client) ListProjectBranchEndpoints(projectID string, branchID string) (EndpointsResponse, error) {
var v EndpointsResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/endpoints", "GET", nil, &v); err != nil {
return EndpointsResponse{}, err
}
return v, nil
}
// ListProjectBranchRoles Retrieves a list of Postgres roles from the specified branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
func (c Client) ListProjectBranchRoles(projectID string, branchID string) (RolesResponse, error) {
var v RolesResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles", "GET", nil, &v); err != nil {
return RolesResponse{}, err
}
return v, nil
}
// ListProjectBranches Retrieves a list of branches for the specified project.
// You can obtain a `project_id` by listing the projects for your Neon account.
// Each Neon project has a root branch named `main`.
// A `branch_id` value has a `br-` prefix.
// A project may contain child branches that were branched from `main` or from another branch.
// A parent branch is identified by the `parent_id` value, which is the `id` of the parent branch.
// For related information, see [Manage branches](https://neon.tech/docs/manage/branches/).
func (c Client) ListProjectBranches(projectID string, search *string) (ListProjectBranchesRespObj, error) {
var (
queryElements []string
query string
)
if search != nil {
queryElements = append(queryElements, "search="+*search)
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ListProjectBranchesRespObj
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches"+query, "GET", nil, &v); err != nil {
return ListProjectBranchesRespObj{}, err
}
return v, nil
}
// ListProjectEndpoints Retrieves a list of compute endpoints for the specified project.
// A compute endpoint is a Neon compute instance.
// You can obtain a `project_id` by listing the projects for your Neon account.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) ListProjectEndpoints(projectID string) (EndpointsResponse, error) {
var v EndpointsResponse
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints", "GET", nil, &v); err != nil {
return EndpointsResponse{}, err
}
return v, nil
}
// ListProjectOperations Retrieves a list of operations for the specified Neon project.
// You can obtain a `project_id` by listing the projects for your Neon account.
// The number of operations returned can be large.
// To paginate the response, issue an initial request with a `limit` value.
// Then, add the `cursor` value that was returned in the response to the next request.
func (c Client) ListProjectOperations(projectID string, cursor *string, limit *int) (ListOperations, error) {
var (
queryElements []string
query string
)
if cursor != nil {
queryElements = append(queryElements, "cursor="+*cursor)
}
if limit != nil {
queryElements = append(queryElements, "limit="+strconv.FormatInt(int64(*limit), 10))
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ListOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/operations"+query, "GET", nil, &v); err != nil {
return ListOperations{}, err
}
return v, nil
}
// ListProjectPermissions Retrieves details about users who have access to the project, including the permission `id`, the granted-to email address, and the date project access was granted.
func (c Client) ListProjectPermissions(projectID string) (ProjectPermissions, error) {
var v ProjectPermissions
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/permissions", "GET", nil, &v); err != nil {
return ProjectPermissions{}, err
}
return v, nil
}
// ListProjects Retrieves a list of projects for the Neon account.
// A project is the top-level object in the Neon object hierarchy.
// For more information, see [Manage projects](https://neon.tech/docs/manage/projects/).
func (c Client) ListProjects(cursor *string, limit *int, search *string, orgID *string) (ListProjectsRespObj, error) {
var (
queryElements []string
query string
)
if cursor != nil {
queryElements = append(queryElements, "cursor="+*cursor)
}
if limit != nil {
queryElements = append(queryElements, "limit="+strconv.FormatInt(int64(*limit), 10))
}
if search != nil {
queryElements = append(queryElements, "search="+*search)
}
if orgID != nil {
queryElements = append(queryElements, "org_id="+*orgID)
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ListProjectsRespObj
if err := c.requestHandler(c.baseURL+"/projects"+query, "GET", nil, &v); err != nil {
return ListProjectsRespObj{}, err
}
return v, nil
}
// ListSharedProjects Retrieves a list of shared projects for the Neon account.
// A project is the top-level object in the Neon object hierarchy.
// For more information, see [Manage projects](https://neon.tech/docs/manage/projects/).
func (c Client) ListSharedProjects(cursor *string, limit *int, search *string) (ListSharedProjectsRespObj, error) {
var (
queryElements []string
query string
)
if cursor != nil {
queryElements = append(queryElements, "cursor="+*cursor)
}
if limit != nil {
queryElements = append(queryElements, "limit="+strconv.FormatInt(int64(*limit), 10))
}
if search != nil {
queryElements = append(queryElements, "search="+*search)
}
if len(queryElements) > 0 {
query = "?" + strings.Join(queryElements, "&")
}
var v ListSharedProjectsRespObj
if err := c.requestHandler(c.baseURL+"/projects/shared"+query, "GET", nil, &v); err != nil {
return ListSharedProjectsRespObj{}, err
}
return v, nil
}
// RemoveOrganizationMember Remove member from the organization.
// Only an admin of the organization can perform this action.
// If another admin is being removed, it will not be allows in case it is the only admin left in the organization.
func (c Client) RemoveOrganizationMember(orgID string, memberID string) (EmptyResponse, error) {
var v EmptyResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/members/"+memberID, "DELETE", nil, &v); err != nil {
return EmptyResponse{}, err
}
return v, nil
}
// ResetProjectBranchRolePassword Resets the password for the specified Postgres role.
// Returns a new password and operations. The new password is ready to use when the last operation finishes.
// The old password remains valid until last operation finishes.
// Connections to the compute endpoint are dropped. If idle,
// the compute endpoint becomes active for a short period of time.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// You can obtain the `role_name` by listing the roles for a branch.
// For related information, see [Manage roles](https://neon.tech/docs/manage/roles/).
func (c Client) ResetProjectBranchRolePassword(projectID string, branchID string, roleName string) (RoleOperations, error) {
var v RoleOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/roles/"+roleName+"/reset_password", "POST", nil, &v); err != nil {
return RoleOperations{}, err
}
return v, nil
}
// RestartProjectEndpoint Restart the specified compute endpoint: suspend immediately followed by start operations.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) RestartProjectEndpoint(projectID string, endpointID string) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID+"/restart", "POST", nil, &v); err != nil {
return EndpointOperations{}, err
}
return v, nil
}
// RestoreProjectBranch Restores a branch to an earlier state in its own or another branch's history
func (c Client) RestoreProjectBranch(projectID string, branchID string, cfg BranchRestoreRequest) (BranchOperations, error) {
var v BranchOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/restore", "POST", cfg, &v); err != nil {
return BranchOperations{}, err
}
return v, nil
}
// RevokeApiKey Revokes the specified API key.
// An API key that is no longer needed can be revoked.
// This action cannot be reversed.
// You can obtain `key_id` values by listing the API keys for your Neon account.
// API keys can also be managed in the Neon Console.
// See [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) RevokeApiKey(keyID int64) (ApiKeyRevokeResponse, error) {
var v ApiKeyRevokeResponse
if err := c.requestHandler(c.baseURL+"/api_keys/"+strconv.FormatInt(keyID, 10), "DELETE", nil, &v); err != nil {
return ApiKeyRevokeResponse{}, err
}
return v, nil
}
// RevokeOrgApiKey Revokes the specified organization API key.
// An API key that is no longer needed can be revoked.
// This action cannot be reversed.
// You can obtain `key_id` values by listing the API keys for an organization.
// API keys can also be managed in the Neon Console.
// See [Manage API keys](https://neon.tech/docs/manage/api-keys/).
func (c Client) RevokeOrgApiKey(orgID string, keyID int64) (OrgApiKeyRevokeResponse, error) {
var v OrgApiKeyRevokeResponse
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/api_keys/"+strconv.FormatInt(keyID, 10), "DELETE", nil, &v); err != nil {
return OrgApiKeyRevokeResponse{}, err
}
return v, nil
}
// RevokePermissionFromProject Revokes project access from the user associted with the specified permisison `id`. You can retrieve a user's permission `id` by listing project access.
func (c Client) RevokePermissionFromProject(projectID string, permissionID string) (ProjectPermission, error) {
var v ProjectPermission
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/permissions/"+permissionID, "DELETE", nil, &v); err != nil {
return ProjectPermission{}, err
}
return v, nil
}
// SetDefaultProjectBranch Sets the specified branch as the project's default branch.
// The default designation is automatically removed from the previous default branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For more information, see [Manage branches](https://neon.tech/docs/manage/branches/).
func (c Client) SetDefaultProjectBranch(projectID string, branchID string) (BranchOperations, error) {
var v BranchOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/set_as_default", "POST", nil, &v); err != nil {
return BranchOperations{}, err
}
return v, nil
}
// StartProjectEndpoint Starts a compute endpoint. The compute endpoint is ready to use
// after the last operation in chain finishes successfully.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) StartProjectEndpoint(projectID string, endpointID string) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID+"/start", "POST", nil, &v); err != nil {
return EndpointOperations{}, err
}
return v, nil
}
// SuspendProjectEndpoint Suspend the specified compute endpoint
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix.
// For information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
func (c Client) SuspendProjectEndpoint(projectID string, endpointID string) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID+"/suspend", "POST", nil, &v); err != nil {
return EndpointOperations{}, err
}
return v, nil
}
// TransferProjectsFromUserToOrg Transfers selected projects, identified by their IDs, from your personal account to a specified organization.
func (c Client) TransferProjectsFromUserToOrg(cfg TransferProjectsToOrganizationRequest) (EmptyResponse, error) {
var v EmptyResponse
if err := c.requestHandler(c.baseURL+"/users/me/projects/transfer", "POST", cfg, &v); err != nil {
return EmptyResponse{}, err
}
return v, nil
}
// UpdateOrganizationMember Only an admin can perform this action.
func (c Client) UpdateOrganizationMember(orgID string, memberID string, cfg OrganizationMemberUpdateRequest) (Member, error) {
var v Member
if err := c.requestHandler(c.baseURL+"/organizations/"+orgID+"/members/"+memberID, "PATCH", cfg, &v); err != nil {
return Member{}, err
}
return v, nil
}
// UpdateProject Updates the specified project.
// You can obtain a `project_id` by listing the projects for your Neon account.
// Neon permits updating the project name only.
func (c Client) UpdateProject(projectID string, cfg ProjectUpdateRequest) (UpdateProjectRespObj, error) {
var v UpdateProjectRespObj
if err := c.requestHandler(c.baseURL+"/projects/"+projectID, "PATCH", cfg, &v); err != nil {
return UpdateProjectRespObj{}, err
}
return v, nil
}
// UpdateProjectBranch Updates the specified branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` by listing the project's branches.
// For more information, see [Manage branches](https://neon.tech/docs/manage/branches/).
func (c Client) UpdateProjectBranch(projectID string, branchID string, cfg BranchUpdateRequest) (BranchOperations, error) {
var v BranchOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID, "PATCH", cfg, &v); err != nil {
return BranchOperations{}, err
}
return v, nil
}
// UpdateProjectBranchDatabase Updates the specified database in the branch.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain the `branch_id` and `database_name` by listing the branch's databases.
// For related information, see [Manage databases](https://neon.tech/docs/manage/databases/).
func (c Client) UpdateProjectBranchDatabase(projectID string, branchID string, databaseName string, cfg DatabaseUpdateRequest) (DatabaseOperations, error) {
var v DatabaseOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/branches/"+branchID+"/databases/"+databaseName, "PATCH", cfg, &v); err != nil {
return DatabaseOperations{}, err
}
return v, nil
}
// UpdateProjectEndpoint Updates the specified compute endpoint.
// You can obtain a `project_id` by listing the projects for your Neon account.
// You can obtain an `endpoint_id` and `branch_id` by listing your project's compute endpoints.
// An `endpoint_id` has an `ep-` prefix. A `branch_id` has a `br-` prefix.
// For more information about compute endpoints, see [Manage computes](https://neon.tech/docs/manage/endpoints/).
// If the returned list of operations is not empty, the compute endpoint is not ready to use.
// The client must wait for the last operation to finish before using the compute endpoint.
// If the compute endpoint was idle before the update, it becomes active for a short period of time,
// and the control plane suspends it again after the update.
func (c Client) UpdateProjectEndpoint(projectID string, endpointID string, cfg EndpointUpdateRequest) (EndpointOperations, error) {
var v EndpointOperations
if err := c.requestHandler(c.baseURL+"/projects/"+projectID+"/endpoints/"+endpointID, "PATCH", cfg, &v); err != nil {
return EndpointOperations{}, err