Skip to content

Commit 10bb47f

Browse files
[Feat.] Cloud WAF product type (#932)
1 parent b87174a commit 10bb47f

4 files changed

Lines changed: 226 additions & 0 deletions

File tree

acceptance/openstack/waf-premium/v1/instance_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
99
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
1010
"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto"
11+
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/cloud"
1112
"github.com/opentelekomcloud/gophertelekomcloud/openstack/waf-premium/v1/instances"
1213
th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
1314
)
@@ -79,3 +80,80 @@ func TestWafPremiumInstanceWorkflow(t *testing.T) {
7980
th.AssertNoErr(t, err)
8081
th.AssertEquals(t, instanceUpdated.Name, updatedName)
8182
}
83+
84+
func TestWafPremiumCloudInstance(t *testing.T) {
85+
client, err := clients.NewWafdV1Client()
86+
th.AssertNoErr(t, err)
87+
88+
queryOpts := cloud.DeleteOpts{
89+
EnterpriseProjectID: os.Getenv("OS_ENTERPRISE_PROJECT_ID"),
90+
}
91+
if queryOpts.EnterpriseProjectID == "" {
92+
queryOpts.EnterpriseProjectID = "0"
93+
}
94+
95+
currentSubscription, err := cloud.Get(client)
96+
th.AssertNoErr(t, err)
97+
if currentSubscription.Type != -1 && currentSubscription.Type != 22 {
98+
t.Skipf("skipping pay-per-use switch test for existing cloud WAF subscription type %d", currentSubscription.Type)
99+
}
100+
101+
enableOpts := cloud.EnableOpts{
102+
ConsoleArea: "dt",
103+
EnterpriseProjectID: queryOpts.EnterpriseProjectID,
104+
}
105+
initialType := currentSubscription.Type
106+
107+
t.Cleanup(func() {
108+
latestSubscription, queryErr := cloud.Get(client)
109+
if queryErr != nil {
110+
t.Logf("failed to query WAF subscription during cleanup: %v", queryErr)
111+
return
112+
}
113+
114+
if initialType == 22 && latestSubscription.Type != 22 {
115+
if _, restoreErr := cloud.Enable(client, enableOpts); restoreErr != nil {
116+
t.Logf("failed to restore pay-per-use subscription during cleanup: %v", restoreErr)
117+
}
118+
}
119+
120+
if initialType == -1 && latestSubscription.Type != -1 {
121+
if restoreErr := cloud.Disable(client, queryOpts); restoreErr != nil {
122+
t.Logf("failed to restore unsubscribed state during cleanup: %v", restoreErr)
123+
}
124+
}
125+
})
126+
127+
if initialType == -1 {
128+
enableResponse, err := cloud.Enable(client, enableOpts)
129+
th.AssertNoErr(t, err)
130+
th.AssertEquals(t, enableResponse.Type, 22)
131+
132+
enabledSubscription, err := cloud.Get(client)
133+
th.AssertNoErr(t, err)
134+
th.AssertEquals(t, enabledSubscription.Type, 22)
135+
136+
err = cloud.Disable(client, queryOpts)
137+
th.AssertNoErr(t, err)
138+
139+
disabledSubscription, err := cloud.Get(client)
140+
th.AssertNoErr(t, err)
141+
th.AssertEquals(t, disabledSubscription.Type, -1)
142+
return
143+
}
144+
145+
err = cloud.Disable(client, queryOpts)
146+
th.AssertNoErr(t, err)
147+
148+
disabledSubscription, err := cloud.Get(client)
149+
th.AssertNoErr(t, err)
150+
th.AssertEquals(t, disabledSubscription.Type, -1)
151+
152+
enableResponse, err := cloud.Enable(client, enableOpts)
153+
th.AssertNoErr(t, err)
154+
th.AssertEquals(t, enableResponse.Type, 22)
155+
156+
enabledSubscription, err := cloud.Get(client)
157+
th.AssertNoErr(t, err)
158+
th.AssertEquals(t, enabledSubscription.Type, 22)
159+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cloud
2+
3+
import (
4+
"github.com/opentelekomcloud/gophertelekomcloud"
5+
)
6+
7+
type DeleteOpts struct {
8+
// Enterprise project ID.
9+
EnterpriseProjectID string `json:"-" q:"enterprise_project_id,omitempty"`
10+
}
11+
12+
func Disable(client *golangsdk.ServiceClient, opts DeleteOpts) (err error) {
13+
url, err := golangsdk.NewURLBuilder().
14+
WithEndpoints("waf", "postpaid").
15+
WithQueryParams(&opts).
16+
Build()
17+
if err != nil {
18+
return err
19+
}
20+
21+
// DELETE /v1/{project_id}/waf/postpaid
22+
_, err = client.Delete(client.ServiceURL(url.String()), &golangsdk.RequestOpts{
23+
OkCodes: []int{200},
24+
MoreHeaders: map[string]string{
25+
"Content-Type": "application/json;charset=utf8",
26+
"region": client.RegionID,
27+
},
28+
})
29+
return
30+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cloud
2+
3+
import (
4+
"github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
6+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
7+
)
8+
9+
type EnableOpts struct {
10+
// Website the account belongs to. The value is dt for Cloud website.
11+
ConsoleArea string `json:"console_area" required:"true"`
12+
// Enterprise project ID.
13+
EnterpriseProjectID string `json:"-" q:"enterprise_project_id,omitempty"`
14+
}
15+
16+
// Enable to enable the pay-per-use billing mode for cloud WAF.
17+
func Enable(client *golangsdk.ServiceClient, opts EnableOpts) (*EnablePostPaidResponse, error) {
18+
b, err := build.RequestBody(opts, "")
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
url, err := golangsdk.NewURLBuilder().
24+
WithEndpoints("waf", "postpaid").
25+
WithQueryParams(&opts).
26+
Build()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
// POST /v1/{project_id}/waf/postpaid
32+
raw, err := client.Post(client.ServiceURL(url.String()), b,
33+
nil, &golangsdk.RequestOpts{
34+
OkCodes: []int{200},
35+
MoreHeaders: map[string]string{
36+
"Content-Type": "application/json;charset=utf8",
37+
"region": client.RegionID,
38+
},
39+
})
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
var res EnablePostPaidResponse
45+
err = extract.Into(raw.Body, &res)
46+
return &res, err
47+
}
48+
49+
type EnablePostPaidResponse struct {
50+
// The edition for the cloud WAF instance.By default,
51+
// 22 is returned when the CreateCloudWafPostPaidResource API is called.
52+
// 22: The pay-per-use edition.
53+
Type int `json:"type"`
54+
// The resource list.
55+
Resources []ResourceResponse `json:"resources"`
56+
// New user or not.
57+
IsNewUser bool `json:"isNewUser"`
58+
}
59+
60+
type ResourceResponse struct {
61+
// Resource ID.
62+
ID string `json:"resourceId"`
63+
// Cloud service type.
64+
CloudServiceType string `json:"cloudServiceType"`
65+
// Cloud service resource type.
66+
// hws.resource.type.waf: yearly/monthly cloud-mode WAF
67+
// hws.resource.type.waf.domain: domain name expansion packages in yearly/monthly cloud-mode WAF
68+
// hws.resource.type.waf.domain: bandwidth expansion packages in yearly/monthly cloud-mode WAF
69+
// hws.resource.type.waf.domain: rule expansion packages in yearly/monthly cloud-mode WAF
70+
// hws.resource.type.waf.instance: dedicated WAF instances
71+
// hws.resource.type.waf.payperuserequest: requests to pay-per-use WAF instances
72+
// hws.resource.type.waf.payperusedomain: domain names protected with pay-per-use WAF instances
73+
// hws.resource.type.waf.payperuserule: rules created in pay-per-use WAF instances
74+
ResourceType string `json:"resourceType"`
75+
// Cloud resource specifications.
76+
ResourceSpecCode string `json:"resourceSpecCode"`
77+
// Resource status. The value can be:
78+
// 0: Unfrozen/Normal.
79+
// 1: Frozen.
80+
// 2: Deleted.
81+
Status int `json:"status"`
82+
// Resource expiration time.
83+
ExpireTime string `json:"expireTime"`
84+
// Resource quantity of your resourceType.
85+
ResourceSize int `json:"resourceSize"`
86+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cloud
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
6+
)
7+
8+
type WafSubscription struct {
9+
EnablePostPaidResponse
10+
Premium WafSubscriptionPremium `json:"premium"`
11+
}
12+
13+
type WafSubscriptionPremium struct {
14+
Purchased bool `json:"purchased"`
15+
Total int `json:"total"`
16+
ELB int `json:"elb"`
17+
Dedicated int `json:"dedicated"`
18+
}
19+
20+
func Get(client *golangsdk.ServiceClient) (*WafSubscription, error) {
21+
raw, err := client.Get(client.ServiceURL("waf", "subscription"), nil, nil)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
var res WafSubscription
27+
if err = extract.Into(raw.Body, &res); err != nil {
28+
return nil, err
29+
}
30+
31+
return &res, nil
32+
}

0 commit comments

Comments
 (0)