Skip to content

Commit

Permalink
Merge pull request #405 from adamwg/awg/docr-subscriptions
Browse files Browse the repository at this point in the history
registry: Support subscription options
  • Loading branch information
bentranter authored Nov 2, 2020
2 parents a3919b4 + fc093bc commit a8dc84a
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 4 deletions.
75 changes: 73 additions & 2 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type RegistryService interface {
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)
}

var _ RegistryService = &RegistryServiceOp{}
Expand All @@ -43,7 +45,8 @@ type RegistryServiceOp struct {

// RegistryCreateRequest represents a request to create a registry.
type RegistryCreateRequest struct {
Name string `json:"name,omitempty"`
Name string `json:"name,omitempty"`
SubscriptionTierSlug string `json:"subscription_tier_slug,omitempty"`
}

// RegistryDockerCredentialsRequest represents a request to retrieve docker
Expand Down Expand Up @@ -121,6 +124,41 @@ 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"`
}

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"`
// EligibilityReaons is included when Eligible is false, and indicates the
// reasons why this tier is not availble 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"`
}

// 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)
Expand Down Expand Up @@ -321,7 +359,7 @@ func (svc *RegistryServiceOp) GetGarbageCollection(ctx context.Context, registry
return root.GarbageCollection, resp, nil
}

// ListGarbageCollection retrieves all garbage collections (active and
// 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)
Expand Down Expand Up @@ -371,3 +409,36 @@ func (svc *RegistryServiceOp) UpdateGarbageCollection(ctx context.Context, regis

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
}
163 changes: 161 additions & 2 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,29 @@ func TestRegistry_Create(t *testing.T) {
}

createRequest := &RegistryCreateRequest{
Name: want.Name,
Name: want.Name,
SubscriptionTierSlug: "basic",
}

createResponseJSON := `
{
"registry": {
"name": "` + testRegistry + `",
"created_at": "` + testTimeString + `"
}
},
"subscription": {
"tier": {
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}`

mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -494,3 +508,148 @@ func TestGarbageCollection_Update(t *testing.T) {
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestRegistry_GetOptions(t *testing.T) {
responseJSON := `
{
"options": {
"subscription_tiers": [
{
"name": "Starter",
"slug": "starter",
"included_repositories": 1,
"included_storage_bytes": 524288000,
"allow_storage_overage": false,
"included_bandwidth_bytes": 524288000,
"monthly_price_in_cents": 0,
"eligible": false,
"eligibility_reasons": [
"OverStorageLimit",
"OverRepositoryLimit"
]
},
{
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500,
"eligible": false,
"eligibility_reasons": [
"OverRepositoryLimit"
]
},
{
"name": "Professional",
"slug": "professional",
"included_repositories": 0,
"included_storage_bytes": 107374182400,
"allow_storage_overage": true,
"included_bandwidth_bytes": 107374182400,
"monthly_price_in_cents": 2000,
"eligible": true
}
]
}
}`
want := &RegistryOptions{
SubscriptionTiers: []*RegistrySubscriptionTier{
{
Name: "Starter",
Slug: "starter",
IncludedRepositories: 1,
IncludedStorageBytes: 524288000,
AllowStorageOverage: false,
IncludedBandwidthBytes: 524288000,
MonthlyPriceInCents: 0,
Eligible: false,
EligibilityReasons: []string{
"OverStorageLimit",
"OverRepositoryLimit",
},
},
{
Name: "Basic",
Slug: "basic",
IncludedRepositories: 5,
IncludedStorageBytes: 5368709120,
AllowStorageOverage: true,
IncludedBandwidthBytes: 5368709120,
MonthlyPriceInCents: 500,
Eligible: false,
EligibilityReasons: []string{
"OverRepositoryLimit",
},
},
{
Name: "Professional",
Slug: "professional",
IncludedRepositories: 0,
IncludedStorageBytes: 107374182400,
AllowStorageOverage: true,
IncludedBandwidthBytes: 107374182400,
MonthlyPriceInCents: 2000,
Eligible: true,
},
},
}

setup()
defer teardown()

mux.HandleFunc("/v2/registry/options", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, responseJSON)
})

got, _, err := client.Registry.GetOptions(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}

func TestRegistry_GetSubscription(t *testing.T) {
setup()
defer teardown()

want := &RegistrySubscription{
Tier: &RegistrySubscriptionTier{
Name: "Basic",
Slug: "basic",
IncludedRepositories: 5,
IncludedStorageBytes: 5368709120,
AllowStorageOverage: true,
IncludedBandwidthBytes: 5368709120,
MonthlyPriceInCents: 500,
},
CreatedAt: testTime,
UpdatedAt: testTime,
}

getResponseJSON := `
{
"subscription": {
"tier": {
"name": "Basic",
"slug": "basic",
"included_repositories": 5,
"included_storage_bytes": 5368709120,
"allow_storage_overage": true,
"included_bandwidth_bytes": 5368709120,
"monthly_price_in_cents": 500
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}
`

mux.HandleFunc("/v2/registry/subscription", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, getResponseJSON)
})
got, _, err := client.Registry.GetSubscription(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}

0 comments on commit a8dc84a

Please sign in to comment.