github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/apigw/v2/throttling_policy_test.go (about) 1 package v2 2 3 import ( 4 "os" 5 "testing" 6 7 golangsdk "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients" 9 "github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/apigw/v2/api" 11 "github.com/opentelekomcloud/gophertelekomcloud/openstack/apigw/v2/env" 12 "github.com/opentelekomcloud/gophertelekomcloud/openstack/apigw/v2/group" 13 policy "github.com/opentelekomcloud/gophertelekomcloud/openstack/apigw/v2/tr_policy" 14 "github.com/opentelekomcloud/gophertelekomcloud/openstack/common/pointerto" 15 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 16 ) 17 18 func TestThrottlingPolicyLifecycle(t *testing.T) { 19 gatewayID := os.Getenv("GATEWAY_ID") 20 21 if gatewayID == "" { 22 t.Skip("`GATEWAY_ID` needs to be defined") 23 } 24 25 client, err := clients.NewAPIGWClient() 26 th.AssertNoErr(t, err) 27 28 createResp := CreatePolicy(client, t, gatewayID) 29 30 t.Cleanup(func() { 31 th.AssertNoErr(t, policy.Delete(client, gatewayID, createResp.ID)) 32 }) 33 34 updateOpts := policy.UpdateOpts{ 35 GatewayID: gatewayID, 36 ThrottleID: createResp.ID, 37 Name: createResp.Name + "_updated", 38 ApiCallLimits: pointerto.Int(199), 39 TimeInterval: pointerto.Int(999), 40 TimeUnit: "MINUTE", 41 Description: "test throttling policy updated", 42 AppCallLimits: pointerto.Int(50), 43 UserCallLimits: pointerto.Int(50), 44 } 45 updateResp, err := policy.Update(client, updateOpts) 46 th.AssertNoErr(t, err) 47 48 getResp, err := policy.Get(client, gatewayID, updateResp.ID) 49 th.AssertNoErr(t, err) 50 51 tools.PrintResource(t, getResp) 52 } 53 54 func TestThrottlingPolicyList(t *testing.T) { 55 gatewayID := os.Getenv("GATEWAY_ID") 56 57 if gatewayID == "" { 58 t.Skip("`GATEWAY_ID` needs to be defined") 59 } 60 61 client, err := clients.NewAPIGWClient() 62 th.AssertNoErr(t, err) 63 64 listResp, err := policy.List(client, policy.ListOpts{ 65 GatewayID: gatewayID, 66 }) 67 th.AssertNoErr(t, err) 68 69 tools.PrintResource(t, listResp) 70 } 71 72 func TestThrottlingPolicyBinding(t *testing.T) { 73 gatewayID := os.Getenv("GATEWAY_ID") 74 75 if gatewayID == "" { 76 t.Skip("`GATEWAY_ID` needs to be defined") 77 } 78 79 client, err := clients.NewAPIGWClient() 80 th.AssertNoErr(t, err) 81 82 createResp := CreatePolicy(client, t, gatewayID) 83 84 t.Cleanup(func() { 85 th.AssertNoErr(t, policy.Delete(client, gatewayID, createResp.ID)) 86 }) 87 88 groupResp := CreateGroup(client, t, gatewayID) 89 t.Cleanup(func() { 90 th.AssertNoErr(t, group.Delete(client, gatewayID, groupResp.ID)) 91 }) 92 93 groupID := groupResp.ID 94 95 _, createAPIResp := CreateAPI(client, t, gatewayID, groupID) 96 97 t.Cleanup(func() { 98 th.AssertNoErr(t, api.Delete(client, gatewayID, createAPIResp.ID)) 99 }) 100 101 envResp := CreateEnv(client, t, gatewayID) 102 103 t.Cleanup(func() { 104 manageOpts := api.ManageOpts{ 105 GatewayID: gatewayID, 106 Action: "offline", 107 EnvID: envResp.ID, 108 ApiID: createAPIResp.ID, 109 Description: "test-api-publish", 110 } 111 112 _, err = api.ManageApi(client, manageOpts) 113 th.AssertNoErr(t, err) 114 th.AssertNoErr(t, env.Delete(client, gatewayID, envResp.ID)) 115 }) 116 117 manageOpts := api.ManageOpts{ 118 GatewayID: gatewayID, 119 Action: "online", 120 EnvID: envResp.ID, 121 ApiID: createAPIResp.ID, 122 Description: "test-api-publish", 123 } 124 125 publishAPI, err := api.ManageApi(client, manageOpts) 126 th.AssertNoErr(t, err) 127 128 listUnbound, err := policy.ListAPIUnoundPolicy(client, policy.ListBoundOpts{ 129 GatewayID: gatewayID, 130 ThrottleID: createResp.ID, 131 }) 132 th.AssertNoErr(t, err) 133 134 tools.PrintResource(t, listUnbound) 135 136 bindOpts := policy.BindOpts{ 137 GatewayID: gatewayID, 138 PolicyID: createResp.ID, 139 PublishIds: []string{ 140 publishAPI.PublishID, 141 }, 142 } 143 144 bindResp, err := policy.BindPolicy(client, bindOpts) 145 t.Cleanup(func() { 146 th.AssertNoErr(t, policy.UnbindPolicy(client, gatewayID, bindResp[0].ID)) 147 }) 148 th.AssertNoErr(t, err) 149 150 tools.PrintResource(t, bindResp) 151 152 listBound, err := policy.ListAPIBoundPolicy(client, policy.ListBoundOpts{ 153 GatewayID: gatewayID, 154 ThrottleID: createResp.ID, 155 }) 156 th.AssertNoErr(t, err) 157 tools.PrintResource(t, listBound) 158 159 listBoundPolicies, err := policy.ListBoundPolicies(client, policy.ListBindingOpts{ 160 GatewayID: gatewayID, 161 ThrottleID: createResp.ID, 162 ApiID: createAPIResp.ID, 163 }) 164 th.AssertNoErr(t, err) 165 166 tools.PrintResource(t, listBoundPolicies) 167 } 168 169 func CreatePolicy(client *golangsdk.ServiceClient, t *testing.T, gatewayID string) *policy.ThrottlingResp { 170 name := tools.RandomString("test_policy_", 5) 171 172 createOpts := policy.CreateOpts{ 173 GatewayID: gatewayID, 174 Name: name, 175 ApiCallLimits: pointerto.Int(200), 176 TimeInterval: pointerto.Int(10000), 177 TimeUnit: "SECOND", 178 Description: "test throttling policy", 179 AppCallLimits: pointerto.Int(100), 180 UserCallLimits: pointerto.Int(100), 181 } 182 183 createResp, err := policy.Create(client, createOpts) 184 th.AssertNoErr(t, err) 185 return createResp 186 }