github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/apigw/v2/api_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 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 14 ) 15 16 func TestApiLifecycle(t *testing.T) { 17 gatewayID := os.Getenv("GATEWAY_ID") 18 19 if gatewayID == "" { 20 t.Skip("`GATEWAY_ID`needs to be defined") 21 } 22 23 client, err := clients.NewAPIGWClient() 24 th.AssertNoErr(t, err) 25 26 groupResp := CreateGroup(client, t, gatewayID) 27 t.Cleanup(func() { 28 th.AssertNoErr(t, group.Delete(client, gatewayID, groupResp.ID)) 29 }) 30 31 groupID := groupResp.ID 32 33 createOpts, createResp := CreateAPI(client, t, gatewayID, groupID) 34 35 t.Cleanup(func() { 36 th.AssertNoErr(t, api.Delete(client, gatewayID, createResp.ID)) 37 }) 38 39 createOpts.Name += "_updated" 40 createOpts.Type = 1 41 createOpts.ReqMethod = "POST" 42 createOpts.ReqProtocol = "HTTPS" 43 44 updateResp, err := api.Update(client, createResp.ID, createOpts) 45 th.AssertNoErr(t, err) 46 47 th.AssertEquals(t, createOpts.Name, updateResp.Name) 48 th.AssertEquals(t, createOpts.Type, updateResp.Type) 49 th.AssertEquals(t, createOpts.ReqMethod, updateResp.ReqMethod) 50 th.AssertEquals(t, createOpts.ReqProtocol, updateResp.ReqProtocol) 51 52 envResp := CreateEnv(client, t, gatewayID) 53 54 t.Cleanup(func() { 55 manageOpts := api.ManageOpts{ 56 GatewayID: gatewayID, 57 Action: "offline", 58 EnvID: envResp.ID, 59 ApiID: createResp.ID, 60 Description: "test-api-publish", 61 } 62 63 _, err = api.ManageApi(client, manageOpts) 64 th.AssertNoErr(t, err) 65 th.AssertNoErr(t, env.Delete(client, gatewayID, envResp.ID)) 66 }) 67 68 manageOpts := api.ManageOpts{ 69 GatewayID: gatewayID, 70 Action: "online", 71 EnvID: envResp.ID, 72 ApiID: createResp.ID, 73 Description: "test-api-publish", 74 } 75 76 manageResp, err := api.ManageApi(client, manageOpts) 77 th.AssertNoErr(t, err) 78 th.AssertEquals(t, manageResp.EnvID, envResp.ID) 79 th.AssertEquals(t, manageResp.Description, manageOpts.Description) 80 81 history, err := api.GetHistory(client, gatewayID, createResp.ID, api.ListHistoryOpts{ 82 EnvID: envResp.ID, 83 }) 84 th.AssertNoErr(t, err) 85 th.AssertEquals(t, len(history), 1) 86 87 getResp, err := api.Get(client, gatewayID, createResp.ID) 88 th.AssertNoErr(t, err) 89 90 th.AssertEquals(t, getResp.Name, createOpts.Name) 91 th.AssertEquals(t, getResp.Type, createOpts.Type) 92 th.AssertEquals(t, getResp.ReqMethod, createOpts.ReqMethod) 93 th.AssertEquals(t, getResp.ReqProtocol, createOpts.ReqProtocol) 94 } 95 96 func TestApiList(t *testing.T) { 97 gatewayID := os.Getenv("GATEWAY_ID") 98 99 if gatewayID == "" { 100 t.Skip("`GATEWAY_ID` needs to be defined") 101 } 102 103 client, err := clients.NewAPIGWClient() 104 th.AssertNoErr(t, err) 105 106 listResp, err := api.List(client, api.ListOpts{ 107 GatewayID: gatewayID, 108 }) 109 th.AssertNoErr(t, err) 110 111 tools.PrintResource(t, listResp) 112 } 113 114 func CreateAPI(client *golangsdk.ServiceClient, t *testing.T, gatewayID, groupID string) (api.CreateOpts, *api.ApiResp) { 115 name := tools.RandomString("test_api_", 5) 116 117 createOpts := api.CreateOpts{ 118 GatewayID: gatewayID, 119 Description: "test env", 120 Name: name, 121 GroupID: groupID, 122 Type: 2, 123 ReqProtocol: "HTTP", 124 ReqMethod: "GET", 125 ReqUri: "/test/http", 126 AuthType: "IAM", 127 BackendType: "HTTP", 128 BackendApi: &api.BackendApi{ 129 UrlDomain: "192.168.189.156:12346", 130 ReqProtocol: "HTTP", 131 ReqMethod: "GET", 132 ReqUri: "/test/benchmark", 133 Timeout: 5000, 134 RetryCount: "-1", 135 }, 136 } 137 138 createResp, err := api.Create(client, createOpts) 139 th.AssertNoErr(t, err) 140 141 return createOpts, createResp 142 }