Skip to content

Commit 9b4876d

Browse files
[APIGW] api version switch (#649)
[APIGW] api version switch Added missing endpoints What this PR does / why we need it Which issue this PR fixes Special notes for your reviewer Reviewed-by: Artem Lifshits Reviewed-by: Aloento
1 parent 2cb9c8f commit 9b4876d

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

acceptance/openstack/apigw/v2/api_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ func TestApiLifecycle(t *testing.T) {
7878
th.AssertEquals(t, manageResp.EnvID, envResp.ID)
7979
th.AssertEquals(t, manageResp.Description, manageOpts.Description)
8080

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+
8187
getResp, err := api.Get(client, gatewayID, createResp.ID)
8288
th.AssertNoErr(t, err)
8389

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package api
2+
3+
import (
4+
"bytes"
5+
6+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
7+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
8+
"github.com/opentelekomcloud/gophertelekomcloud/pagination"
9+
)
10+
11+
type ListHistoryOpts struct {
12+
Offset int64 `q:"offset"`
13+
Limit int `q:"limit"`
14+
EnvID string `q:"env_id"`
15+
EnvName string `q:"env_name"`
16+
}
17+
18+
func GetHistory(client *golangsdk.ServiceClient, gatewayID, apiID string, opts ListHistoryOpts) ([]VersionResp, error) {
19+
url, err := golangsdk.NewURLBuilder().
20+
WithEndpoints("apigw", "instances", gatewayID, "apis", "publish", apiID).
21+
WithQueryParams(&opts).Build()
22+
if err != nil {
23+
return nil, err
24+
}
25+
pages, err := pagination.Pager{
26+
Client: client,
27+
InitialURL: client.ServiceURL(url.String()),
28+
CreatePage: func(r pagination.NewPageResult) pagination.NewPage {
29+
return VersionPage{NewSinglePageBase: pagination.NewSinglePageBase{NewPageResult: r}}
30+
},
31+
}.NewAllPages()
32+
33+
if err != nil {
34+
return nil, err
35+
}
36+
return ExtractVersions(pages)
37+
}
38+
39+
type VersionPage struct {
40+
pagination.NewSinglePageBase
41+
}
42+
43+
func ExtractVersions(r pagination.NewPage) ([]VersionResp, error) {
44+
var s struct {
45+
Versions []VersionResp `json:"api_versions"`
46+
}
47+
err := extract.Into(bytes.NewReader((r.(VersionPage)).Body), &s)
48+
return s.Versions, err
49+
}
50+
51+
type VersionResp struct {
52+
VersionID string `json:"version_id"`
53+
Version string `json:"version_no"`
54+
ApiID string `json:"api_id"`
55+
EnvID string `json:"env_id"`
56+
EnvName string `json:"env_name"`
57+
Description string `json:"remark"`
58+
PublishTime string `json:"publish_time"`
59+
Status int `json:"status"`
60+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package api
2+
3+
import (
4+
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
5+
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
6+
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
7+
)
8+
9+
type VersionApiOpts struct {
10+
GatewayID string `json:"-"`
11+
ApiID string `json:"-"`
12+
VersionID string `json:"version_id" required:"true"`
13+
}
14+
15+
func SwitchVersion(client *golangsdk.ServiceClient, opts VersionApiOpts) (*ManageApiResp, error) {
16+
b, err := build.RequestBody(opts, "")
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
raw, err := client.Put(client.ServiceURL("apigw", "instances", opts.GatewayID, "apis", "publish", opts.ApiID), b, nil, &golangsdk.RequestOpts{
22+
OkCodes: []int{200},
23+
})
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
var res ManageApiResp
29+
30+
err = extract.Into(raw.Body, &res)
31+
return &res, err
32+
}

0 commit comments

Comments
 (0)