-
Notifications
You must be signed in to change notification settings - Fork 301
/
registry.go
732 lines (638 loc) · 25.1 KB
/
registry.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
package godo
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
const (
registryPath = "/v2/registry"
// RegistryServer is the hostname of the DigitalOcean registry service
RegistryServer = "registry.digitalocean.com"
// Multi-registry Open Beta API constants
registriesPath = "/v2/registries"
)
// RegistryService is an interface for interfacing with the Registry endpoints
// of the DigitalOcean API.
// See: https://docs.digitalocean.com/reference/api/api-reference/#tag/Container-Registry
type RegistryService interface {
Create(context.Context, *RegistryCreateRequest) (*Registry, *Response, error)
Get(context.Context) (*Registry, *Response, error)
Delete(context.Context) (*Response, error)
DockerCredentials(context.Context, *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error)
ListRepositories(context.Context, string, *ListOptions) ([]*Repository, *Response, error)
ListRepositoriesV2(context.Context, string, *TokenListOptions) ([]*RepositoryV2, *Response, error)
ListRepositoryTags(context.Context, string, string, *ListOptions) ([]*RepositoryTag, *Response, error)
DeleteTag(context.Context, string, string, string) (*Response, error)
ListRepositoryManifests(context.Context, string, string, *ListOptions) ([]*RepositoryManifest, *Response, error)
DeleteManifest(context.Context, string, string, string) (*Response, error)
StartGarbageCollection(context.Context, string, ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetGarbageCollection(context.Context, string) (*GarbageCollection, *Response, error)
ListGarbageCollections(context.Context, string, *ListOptions) ([]*GarbageCollection, *Response, error)
UpdateGarbageCollection(context.Context, string, string, *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetOptions(context.Context) (*RegistryOptions, *Response, error)
GetSubscription(context.Context) (*RegistrySubscription, *Response, error)
UpdateSubscription(context.Context, *RegistrySubscriptionUpdateRequest) (*RegistrySubscription, *Response, error)
ValidateName(context.Context, *RegistryValidateNameRequest) (*Response, error)
}
var _ RegistryService = &RegistryServiceOp{}
// RegistryServiceOp handles communication with Registry methods of the DigitalOcean API.
type RegistryServiceOp struct {
client *Client
}
// RegistryCreateRequest represents a request to create a registry.
type RegistryCreateRequest struct {
Name string `json:"name,omitempty"`
SubscriptionTierSlug string `json:"subscription_tier_slug,omitempty"`
Region string `json:"region,omitempty"`
}
// RegistryDockerCredentialsRequest represents a request to retrieve docker
// credentials for a registry.
type RegistryDockerCredentialsRequest struct {
ReadWrite bool `json:"read_write"`
ExpirySeconds *int `json:"expiry_seconds,omitempty"`
}
// Registry represents a registry.
type Registry struct {
Name string `json:"name,omitempty"`
StorageUsageBytes uint64 `json:"storage_usage_bytes,omitempty"`
StorageUsageBytesUpdatedAt time.Time `json:"storage_usage_bytes_updated_at,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Region string `json:"region,omitempty"`
}
// Repository represents a repository
type Repository struct {
RegistryName string `json:"registry_name,omitempty"`
Name string `json:"name,omitempty"`
LatestTag *RepositoryTag `json:"latest_tag,omitempty"`
TagCount uint64 `json:"tag_count,omitempty"`
}
// RepositoryV2 represents a repository in the V2 format
type RepositoryV2 struct {
RegistryName string `json:"registry_name,omitempty"`
Name string `json:"name,omitempty"`
TagCount uint64 `json:"tag_count,omitempty"`
ManifestCount uint64 `json:"manifest_count,omitempty"`
LatestManifest *RepositoryManifest `json:"latest_manifest,omitempty"`
}
// RepositoryTag represents a repository tag
type RepositoryTag struct {
RegistryName string `json:"registry_name,omitempty"`
Repository string `json:"repository,omitempty"`
Tag string `json:"tag,omitempty"`
ManifestDigest string `json:"manifest_digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
SizeBytes uint64 `json:"size_bytes,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
// RepositoryManifest represents a repository manifest
type RepositoryManifest struct {
RegistryName string `json:"registry_name,omitempty"`
Repository string `json:"repository,omitempty"`
Digest string `json:"digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
SizeBytes uint64 `json:"size_bytes,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Tags []string `json:"tags,omitempty"`
Blobs []*Blob `json:"blobs,omitempty"`
}
// Blob represents a registry blob
type Blob struct {
Digest string `json:"digest,omitempty"`
CompressedSizeBytes uint64 `json:"compressed_size_bytes,omitempty"`
}
type registryRoot struct {
Registry *Registry `json:"registry,omitempty"`
}
type repositoriesRoot struct {
Repositories []*Repository `json:"repositories,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoriesV2Root struct {
Repositories []*RepositoryV2 `json:"repositories,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoryTagsRoot struct {
Tags []*RepositoryTag `json:"tags,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type repositoryManifestsRoot struct {
Manifests []*RepositoryManifest `json:"manifests,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
// GarbageCollection represents a garbage collection.
type GarbageCollection struct {
UUID string `json:"uuid"`
RegistryName string `json:"registry_name"`
Status string `json:"status"`
Type GarbageCollectionType `json:"type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BlobsDeleted uint64 `json:"blobs_deleted"`
FreedBytes uint64 `json:"freed_bytes"`
}
type garbageCollectionRoot struct {
GarbageCollection *GarbageCollection `json:"garbage_collection,omitempty"`
}
type garbageCollectionsRoot struct {
GarbageCollections []*GarbageCollection `json:"garbage_collections,omitempty"`
Links *Links `json:"links,omitempty"`
Meta *Meta `json:"meta"`
}
type GarbageCollectionType string
const (
// GCTypeUntaggedManifestsOnly indicates that a garbage collection should
// only delete untagged manifests.
GCTypeUntaggedManifestsOnly = GarbageCollectionType("untagged manifests only")
// GCTypeUnreferencedBlobsOnly indicates that a garbage collection should
// only delete unreferenced blobs.
GCTypeUnreferencedBlobsOnly = GarbageCollectionType("unreferenced blobs only")
// GCTypeUntaggedManifestsAndUnreferencedBlobs indicates that a garbage
// collection should delete both untagged manifests and unreferenced blobs.
GCTypeUntaggedManifestsAndUnreferencedBlobs = GarbageCollectionType("untagged manifests and unreferenced blobs")
)
// StartGarbageCollectionRequest represents options to a garbage collection
// start request.
type StartGarbageCollectionRequest struct {
Type GarbageCollectionType `json:"type"`
}
// UpdateGarbageCollectionRequest represents a request to update a garbage
// collection.
type UpdateGarbageCollectionRequest struct {
Cancel bool `json:"cancel"`
}
// RegistryOptions are options for users when creating or updating a registry.
type RegistryOptions struct {
SubscriptionTiers []*RegistrySubscriptionTier `json:"subscription_tiers,omitempty"`
AvailableRegions []string `json:"available_regions"`
}
type registryOptionsRoot struct {
Options *RegistryOptions `json:"options"`
}
// RegistrySubscriptionTier is a subscription tier for container registry.
type RegistrySubscriptionTier struct {
Name string `json:"name"`
Slug string `json:"slug"`
IncludedRepositories uint64 `json:"included_repositories"`
IncludedStorageBytes uint64 `json:"included_storage_bytes"`
AllowStorageOverage bool `json:"allow_storage_overage"`
IncludedBandwidthBytes uint64 `json:"included_bandwidth_bytes"`
MonthlyPriceInCents uint64 `json:"monthly_price_in_cents"`
Eligible bool `json:"eligible,omitempty"`
// EligibilityReasons is included when Eligible is false, and indicates the
// reasons why this tier is not available to the user.
EligibilityReasons []string `json:"eligibility_reasons,omitempty"`
}
// RegistrySubscription is a user's subscription.
type RegistrySubscription struct {
Tier *RegistrySubscriptionTier `json:"tier"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type registrySubscriptionRoot struct {
Subscription *RegistrySubscription `json:"subscription"`
}
// RegistrySubscriptionUpdateRequest represents a request to update the
// subscription plan for a registry.
type RegistrySubscriptionUpdateRequest struct {
TierSlug string `json:"tier_slug"`
}
// RegistryValidateNameRequest represents a request to validate that a
// container registry name is available for use.
type RegistryValidateNameRequest struct {
Name string `json:"name"`
}
// Multi-registry Open Beta API structs
type registriesRoot struct {
Registries []*Registry `json:"registries,omitempty"`
TotalStorageUsageBytes uint64 `json:"total_storage_usage_bytes,omitempty"`
}
// RegistriesCreateRequest represents a request to create a secondary registry.
type RegistriesCreateRequest struct {
Name string `json:"name,omitempty"`
Region string `json:"region,omitempty"`
}
// Get retrieves the details of a Registry.
func (svc *RegistryServiceOp) Get(ctx context.Context) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodGet, registryPath, nil)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// Create creates a registry.
func (svc *RegistryServiceOp) Create(ctx context.Context, create *RegistryCreateRequest) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodPost, registryPath, create)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// Delete deletes a registry. There is no way to recover a registry once it has
// been destroyed.
func (svc *RegistryServiceOp) Delete(ctx context.Context) (*Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodDelete, registryPath, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DockerCredentials is the content of a Docker config file
// that is used by the docker CLI
// See: https://docs.docker.com/engine/reference/commandline/cli/#configjson-properties
type DockerCredentials struct {
DockerConfigJSON []byte
}
// DockerCredentials retrieves a Docker config file containing the registry's credentials.
func (svc *RegistryServiceOp) DockerCredentials(ctx context.Context, request *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s", registryPath, "docker-credentials")
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("read_write", strconv.FormatBool(request.ReadWrite))
if request.ExpirySeconds != nil {
q.Add("expiry_seconds", strconv.Itoa(*request.ExpirySeconds))
}
req.URL.RawQuery = q.Encode()
var buf bytes.Buffer
resp, err := svc.client.Do(ctx, req, &buf)
if err != nil {
return nil, resp, err
}
dc := &DockerCredentials{
DockerConfigJSON: buf.Bytes(),
}
return dc, resp, nil
}
// ListRepositories returns a list of the Repositories visible with the registry's credentials.
func (svc *RegistryServiceOp) ListRepositories(ctx context.Context, registry string, opts *ListOptions) ([]*Repository, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoriesRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Repositories, resp, nil
}
// ListRepositoriesV2 returns a list of the Repositories in a registry.
func (svc *RegistryServiceOp) ListRepositoriesV2(ctx context.Context, registry string, opts *TokenListOptions) ([]*RepositoryV2, *Response, error) {
path := fmt.Sprintf("%s/%s/repositoriesV2", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoriesV2Root)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.Links = root.Links
resp.Meta = root.Meta
return root.Repositories, resp, nil
}
// ListRepositoryTags returns a list of the RepositoryTags available within the given repository.
func (svc *RegistryServiceOp) ListRepositoryTags(ctx context.Context, registry, repository string, opts *ListOptions) ([]*RepositoryTag, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/tags", registryPath, registry, url.PathEscape(repository))
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoryTagsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}
return root.Tags, resp, nil
}
// DeleteTag deletes a tag within a given repository.
func (svc *RegistryServiceOp) DeleteTag(ctx context.Context, registry, repository, tag string) (*Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/tags/%s", registryPath, registry, url.PathEscape(repository), tag)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// ListRepositoryManifests returns a list of the RepositoryManifests available within the given repository.
func (svc *RegistryServiceOp) ListRepositoryManifests(ctx context.Context, registry, repository string, opts *ListOptions) ([]*RepositoryManifest, *Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/digests", registryPath, registry, url.PathEscape(repository))
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(repositoryManifestsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
resp.Links = root.Links
resp.Meta = root.Meta
return root.Manifests, resp, nil
}
// DeleteManifest deletes a manifest by its digest within a given repository.
func (svc *RegistryServiceOp) DeleteManifest(ctx context.Context, registry, repository, digest string) (*Response, error) {
path := fmt.Sprintf("%s/%s/repositories/%s/digests/%s", registryPath, registry, url.PathEscape(repository), digest)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// StartGarbageCollection requests a garbage collection for the specified
// registry.
func (svc *RegistryServiceOp) StartGarbageCollection(ctx context.Context, registry string, request ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection", registryPath, registry)
var requestParams interface{}
if len(request) < 1 {
// default to only garbage collecting unreferenced blobs for backwards
// compatibility
requestParams = &StartGarbageCollectionRequest{
Type: GCTypeUnreferencedBlobsOnly,
}
} else {
requestParams = request[0]
}
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, requestParams)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, err
}
// GetGarbageCollection retrieves the currently-active garbage collection for
// the specified registry; if there are no active garbage collections, then
// return a 404/NotFound error. There can only be one active garbage
// collection on a registry.
func (svc *RegistryServiceOp) GetGarbageCollection(ctx context.Context, registry string) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection", registryPath, registry)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, nil
}
// ListGarbageCollections retrieves all garbage collections (active and
// inactive) for the specified registry.
func (svc *RegistryServiceOp) ListGarbageCollections(ctx context.Context, registry string, opts *ListOptions) ([]*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collections", registryPath, registry)
path, err := addOptions(path, opts)
if err != nil {
return nil, nil, err
}
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
if root.Links != nil {
resp.Links = root.Links
}
if root.Meta != nil {
resp.Meta = root.Meta
}
return root.GarbageCollections, resp, nil
}
// UpdateGarbageCollection updates the specified garbage collection for the
// specified registry. While only the currently-active garbage collection can
// be updated we still require the exact garbage collection to be specified to
// avoid race conditions that might may arise from issuing an update to the
// implicit "currently-active" garbage collection. Returns the updated garbage
// collection.
func (svc *RegistryServiceOp) UpdateGarbageCollection(ctx context.Context, registry, gcUUID string, request *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection/%s", registryPath, registry, gcUUID)
req, err := svc.client.NewRequest(ctx, http.MethodPut, path, request)
if err != nil {
return nil, nil, err
}
root := new(garbageCollectionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.GarbageCollection, resp, nil
}
// GetOptions returns options the user can use when creating or updating a
// registry.
func (svc *RegistryServiceOp) GetOptions(ctx context.Context) (*RegistryOptions, *Response, error) {
path := fmt.Sprintf("%s/options", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registryOptionsRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Options, resp, nil
}
// GetSubscription retrieves the user's subscription.
func (svc *RegistryServiceOp) GetSubscription(ctx context.Context) (*RegistrySubscription, *Response, error) {
path := fmt.Sprintf("%s/subscription", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registrySubscriptionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subscription, resp, nil
}
// UpdateSubscription updates the user's registry subscription.
func (svc *RegistryServiceOp) UpdateSubscription(ctx context.Context, request *RegistrySubscriptionUpdateRequest) (*RegistrySubscription, *Response, error) {
path := fmt.Sprintf("%s/subscription", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, nil, err
}
root := new(registrySubscriptionRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Subscription, resp, nil
}
// ValidateName validates that a container registry name is available for use.
func (svc *RegistryServiceOp) ValidateName(ctx context.Context, request *RegistryValidateNameRequest) (*Response, error) {
path := fmt.Sprintf("%s/validate-name", registryPath)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, request)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// RegistriesService is an interface for interfacing with the new multiple-registry beta endpoints
// of the DigitalOcean API.
//
// We are creating a separate Service in alignment with the new /v2/registries endpoints.
type RegistriesService interface {
Get(context.Context, string) (*Registry, *Response, error)
List(context.Context) ([]*Registry, *Response, error)
Create(context.Context, *RegistriesCreateRequest) (*Registry, *Response, error)
Delete(context.Context, string) (*Response, error)
DockerCredentials(context.Context, string, *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error)
}
var _ RegistriesService = &RegistriesServiceOp{}
// RegistriesServiceOp handles communication with the multiple-registry beta methods.
type RegistriesServiceOp struct {
client *Client
}
// Get returns the details of a named Registry.
func (svc *RegistriesServiceOp) Get(ctx context.Context, registry string) (*Registry, *Response, error) {
path := fmt.Sprintf("%s/%s", registriesPath, registry)
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// List returns a list of the named Registries.
func (svc *RegistriesServiceOp) List(ctx context.Context) ([]*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodGet, registriesPath, nil)
if err != nil {
return nil, nil, err
}
root := new(registriesRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registries, resp, nil
}
// Create creates a named Registry.
func (svc *RegistriesServiceOp) Create(ctx context.Context, create *RegistriesCreateRequest) (*Registry, *Response, error) {
req, err := svc.client.NewRequest(ctx, http.MethodPost, registriesPath, create)
if err != nil {
return nil, nil, err
}
root := new(registryRoot)
resp, err := svc.client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}
return root.Registry, resp, nil
}
// Delete deletes a named Registry. There is no way to recover a Registry once it has
// been destroyed.
func (svc *RegistriesServiceOp) Delete(ctx context.Context, registry string) (*Response, error) {
path := fmt.Sprintf("%s/%s", registriesPath, registry)
req, err := svc.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
resp, err := svc.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}
return resp, nil
}
// DockerCredentials retrieves a Docker config file containing named Registry's credentials.
func (svc *RegistriesServiceOp) DockerCredentials(ctx context.Context, registry string, request *RegistryDockerCredentialsRequest) (*DockerCredentials, *Response, error) {
path := fmt.Sprintf("%s/%s/%s", registriesPath, registry, "docker-credentials")
req, err := svc.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
q := req.URL.Query()
q.Add("read_write", strconv.FormatBool(request.ReadWrite))
if request.ExpirySeconds != nil {
q.Add("expiry_seconds", strconv.Itoa(*request.ExpirySeconds))
}
req.URL.RawQuery = q.Encode()
var buf bytes.Buffer
resp, err := svc.client.Do(ctx, req, &buf)
if err != nil {
return nil, resp, err
}
dc := &DockerCredentials{
DockerConfigJSON: buf.Bytes(),
}
return dc, resp, nil
}