forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription_updater_test.go
117 lines (96 loc) · 2.97 KB
/
subscription_updater_test.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"net/url"
"testing"
"time"
)
type featureSet struct {
ID string `jsonapi:"primary,feature-sets"`
}
type featureSetList struct {
Items []*featureSet
*Pagination
}
type featureSetListOptions struct {
Q string `url:"q,omitempty"`
}
type retryableFn func() (interface{}, error)
type updateFeatureSetOptions struct {
Type string `jsonapi:"primary,subscription"`
RunsCeiling *int `jsonapi:"attr,runs-ceiling,omitempty"`
ContractStartAt *time.Time `jsonapi:"attr,contract-start-at,iso8601,omitempty"`
ContractUserLimit *int `jsonapi:"attr,contract-user-limit,omitempty"`
ContractApplyLimit *int `jsonapi:"attr,contract-apply-limit,omitempty"`
FeatureSet *featureSet `jsonapi:"relation,feature-set"`
}
type organizationSubscriptionUpdater struct {
organization *Organization
planName string
updateOpts updateFeatureSetOptions
}
func newSubscriptionUpdater(organization *Organization) *organizationSubscriptionUpdater {
return &organizationSubscriptionUpdater{
organization: organization,
updateOpts: updateFeatureSetOptions{},
}
}
func (b *organizationSubscriptionUpdater) WithBusinessPlan() *organizationSubscriptionUpdater {
b.planName = "Business"
ceiling := 10
start := time.Now()
userLimit := 1000
applyLimit := 5000
b.updateOpts.RunsCeiling = &ceiling
b.updateOpts.ContractStartAt = &start
b.updateOpts.ContractUserLimit = &userLimit
b.updateOpts.ContractApplyLimit = &applyLimit
return b
}
func (b *organizationSubscriptionUpdater) WithTrialPlan() *organizationSubscriptionUpdater {
b.planName = "Trial"
ceiling := 1
b.updateOpts.RunsCeiling = &ceiling
return b
}
// Attempts to change an organization's subscription to a different plan. Requires a user token with admin access.
func (b *organizationSubscriptionUpdater) Update(t *testing.T) {
if enterpriseEnabled() {
t.Skip("Cannot upgrade an organization's subscription when enterprise is enabled. Set ENABLE_TFE=0 to run.")
}
if b.planName == "" {
t.Fatal("organizationSubscriptionUpdater requires a plan")
return
}
adminClient := testAdminClient(t, provisionLicensesAdmin)
req, err := adminClient.NewRequest("GET", "admin/feature-sets", featureSetListOptions{
Q: b.planName,
})
if err != nil {
t.Fatal(err)
return
}
fsl := &featureSetList{}
err = req.Do(context.Background(), fsl)
if err != nil {
t.Fatalf("failed to enumerate feature sets: %v", err)
return
} else if len(fsl.Items) == 0 {
t.Fatalf("feature set response was empty")
return
}
b.updateOpts.FeatureSet = fsl.Items[0]
u := fmt.Sprintf("admin/organizations/%s/subscription", url.QueryEscape(b.organization.Name))
req, err = adminClient.NewRequest("POST", u, &b.updateOpts)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
return
}
err = req.Do(context.Background(), nil)
if err != nil {
t.Fatalf("Failed to upgrade subscription: %v", err)
}
}