Skip to content

Commit

Permalink
registry: Add support for updating a subscription. (digitalocean#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething authored Nov 5, 2020
1 parent 27a2f41 commit dd16736
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
22 changes: 22 additions & 0 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RegistryService interface {
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)
}

var _ RegistryService = &RegistryServiceOp{}
Expand Down Expand Up @@ -159,6 +160,12 @@ 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"`
}

// 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 @@ -442,3 +449,18 @@ func (svc *RegistryServiceOp) GetSubscription(ctx context.Context) (*RegistrySub
}
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
}
58 changes: 58 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,61 @@ func TestRegistry_GetSubscription(t *testing.T) {
require.NoError(t, err)
require.Equal(t, want, got)
}

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

updateRequest := &RegistrySubscriptionUpdateRequest{
TierSlug: "professional",
}

want := &RegistrySubscription{
Tier: &RegistrySubscriptionTier{
Name: "Professional",
Slug: "professional",
IncludedRepositories: 0,
IncludedStorageBytes: 107374182400,
AllowStorageOverage: true,
IncludedBandwidthBytes: 107374182400,
MonthlyPriceInCents: 2000,
Eligible: true,
},
CreatedAt: testTime,
UpdatedAt: testTime,
}

updateResponseJSON := `{
"subscription": {
"tier": {
"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
},
"created_at": "` + testTimeString + `",
"updated_at": "` + testTimeString + `"
}
}`

mux.HandleFunc("/v2/registry/subscription",
func(w http.ResponseWriter, r *http.Request) {
v := new(RegistrySubscriptionUpdateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}

testMethod(t, r, http.MethodPost)
require.Equal(t, v, updateRequest)
fmt.Fprint(w, updateResponseJSON)
})

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

0 comments on commit dd16736

Please sign in to comment.