Skip to content

Commit 7a76482

Browse files
sahaqaavladhanzha
andauthored
Migrate to API V1 (#46)
Co-authored-by: vladhanzha <vladyslav.hanzha@openvpn.com>
1 parent a60e763 commit 7a76482

15 files changed

+191
-123
lines changed

cloudconnexa/access_groups.go

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,39 @@ import (
77
"net/http"
88
)
99

10-
type AccessGroupRequest struct {
11-
Name string `json:"name"`
12-
Description string `json:"description,omitempty"`
13-
Source []AccessItemRequest `json:"source"`
14-
Destination []AccessItemRequest `json:"destination"`
10+
type AccessGroup struct {
11+
Id string `json:"id"`
12+
Name string `json:"name"`
13+
Description string `json:"description,omitempty"`
14+
Source []AccessItem `json:"source"`
15+
Destination []AccessItem `json:"destination"`
1516
}
1617

17-
type AccessGroupResponse struct {
18-
Id string `json:"id"`
19-
Name string `json:"name"`
20-
Description string `json:"description"`
21-
Source []AccessItemResponse `json:"source"`
22-
Destination []AccessItemResponse `json:"destination"`
23-
}
24-
25-
type AccessItemRequest struct {
18+
type AccessItem struct {
2619
Type string `json:"type"`
2720
AllCovered bool `json:"allCovered"`
2821
Parent string `json:"parent,omitempty"`
2922
Children []string `json:"children,omitempty"`
3023
}
3124

32-
type AccessItemResponse struct {
33-
Type string `json:"type"`
34-
AllCovered bool `json:"allCovered"`
35-
Parent *Item `json:"parent"`
36-
Children []Item `json:"children"`
37-
}
38-
3925
type Item struct {
4026
Id string `json:"id"`
4127
}
4228

4329
type AccessGroupPageResponse struct {
44-
Content []AccessGroupResponse `json:"content"`
45-
NumberOfElements int `json:"numberOfElements"`
46-
Page int `json:"page"`
47-
Size int `json:"size"`
48-
Success bool `json:"success"`
49-
TotalElements int `json:"totalElements"`
50-
TotalPages int `json:"totalPages"`
30+
Content []AccessGroup `json:"content"`
31+
NumberOfElements int `json:"numberOfElements"`
32+
Page int `json:"page"`
33+
Size int `json:"size"`
34+
Success bool `json:"success"`
35+
TotalElements int `json:"totalElements"`
36+
TotalPages int `json:"totalPages"`
5137
}
5238

5339
type AccessGroupsService service
5440

5541
func (c *AccessGroupsService) GetAccessGroupsByPage(page int, size int) (AccessGroupPageResponse, error) {
56-
endpoint := fmt.Sprintf("%s/api/beta/access-groups/page?page=%d&size=%d", c.client.BaseURL, page, size)
42+
endpoint := fmt.Sprintf("%s/access-groups?page=%d&size=%d", c.client.GetV1Url(), page, size)
5743
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
5844
if err != nil {
5945
return AccessGroupPageResponse{}, err
@@ -72,8 +58,8 @@ func (c *AccessGroupsService) GetAccessGroupsByPage(page int, size int) (AccessG
7258
return response, nil
7359
}
7460

75-
func (c *AccessGroupsService) List() ([]AccessGroupResponse, error) {
76-
var allGroups []AccessGroupResponse
61+
func (c *AccessGroupsService) List() ([]AccessGroup, error) {
62+
var allGroups []AccessGroup
7763
page := 0
7864
pageSize := 10
7965

@@ -92,7 +78,7 @@ func (c *AccessGroupsService) List() ([]AccessGroupResponse, error) {
9278
return allGroups, nil
9379
}
9480

95-
func (c *AccessGroupsService) Get(id string) (*AccessGroupResponse, error) {
81+
func (c *AccessGroupsService) Get(id string) (*AccessGroup, error) {
9682
groups, err := c.List()
9783
if err != nil {
9884
return nil, err
@@ -106,13 +92,13 @@ func (c *AccessGroupsService) Get(id string) (*AccessGroupResponse, error) {
10692
return nil, nil
10793
}
10894

109-
func (c *AccessGroupsService) Create(accessGroup *AccessGroupRequest) (*AccessGroupResponse, error) {
95+
func (c *AccessGroupsService) Create(accessGroup *AccessGroup) (*AccessGroup, error) {
11096
accessGroupJson, err := json.Marshal(accessGroup)
11197
if err != nil {
11298
return nil, err
11399
}
114100

115-
endpoint := fmt.Sprintf("%s/api/beta/access-groups", c.client.BaseURL)
101+
endpoint := fmt.Sprintf("%s/access-groups", c.client.GetV1Url())
116102

117103
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(accessGroupJson))
118104
if err != nil {
@@ -124,21 +110,21 @@ func (c *AccessGroupsService) Create(accessGroup *AccessGroupRequest) (*AccessGr
124110
return nil, err
125111
}
126112

127-
var s AccessGroupResponse
113+
var s AccessGroup
128114
err = json.Unmarshal(body, &s)
129115
if err != nil {
130116
return nil, err
131117
}
132118
return &s, nil
133119
}
134120

135-
func (c *AccessGroupsService) Update(id string, accessGroup *AccessGroupRequest) (*AccessGroupResponse, error) {
121+
func (c *AccessGroupsService) Update(id string, accessGroup *AccessGroup) (*AccessGroup, error) {
136122
accessGroupJson, err := json.Marshal(accessGroup)
137123
if err != nil {
138124
return nil, err
139125
}
140126

141-
endpoint := fmt.Sprintf("%s/api/beta/access-groups/%s", c.client.BaseURL, id)
127+
endpoint := fmt.Sprintf("%s/access-groups/%s", c.client.GetV1Url(), id)
142128

143129
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(accessGroupJson))
144130
if err != nil {
@@ -150,7 +136,7 @@ func (c *AccessGroupsService) Update(id string, accessGroup *AccessGroupRequest)
150136
return nil, err
151137
}
152138

153-
var s AccessGroupResponse
139+
var s AccessGroup
154140
err = json.Unmarshal(body, &s)
155141
if err != nil {
156142
return nil, err
@@ -159,7 +145,7 @@ func (c *AccessGroupsService) Update(id string, accessGroup *AccessGroupRequest)
159145
}
160146

161147
func (c *AccessGroupsService) Delete(id string) error {
162-
endpoint := fmt.Sprintf("%s/api/beta/access-groups/%s", c.client.BaseURL, id)
148+
endpoint := fmt.Sprintf("%s/access-groups/%s", c.client.GetV1Url(), id)
163149
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
164150
if err != nil {
165151
return err

cloudconnexa/applications.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,12 @@ type ApplicationPageResponse struct {
5050

5151
type ApplicationsService service
5252

53-
func (c *ApplicationsService) GetApplicationsByPage(page int, pageSize int) (ApplicationPageResponse, error) {
54-
endpoint := fmt.Sprintf("%s/api/beta/applications/page?page=%d&size=%d", c.client.BaseURL, page, pageSize)
53+
func (c *ApplicationsService) GetApplicationsByPage(page int, pageSize int, networkItemType string) (ApplicationPageResponse, error) {
54+
path, err := GetPath(networkItemType)
55+
if err != nil {
56+
return ApplicationPageResponse{}, err
57+
}
58+
endpoint := fmt.Sprintf("%s/%s/applications?page=%d&size=%d", c.client.GetV1Url(), path, page, pageSize)
5559
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
5660
if err != nil {
5761
return ApplicationPageResponse{}, err
@@ -70,13 +74,13 @@ func (c *ApplicationsService) GetApplicationsByPage(page int, pageSize int) (App
7074
return response, nil
7175
}
7276

73-
func (c *ApplicationsService) List() ([]ApplicationResponse, error) {
77+
func (c *ApplicationsService) List(networkItemType string) ([]ApplicationResponse, error) {
7478
var allApplications []ApplicationResponse
7579
page := 0
7680
pageSize := 10
7781

7882
for {
79-
response, err := c.GetApplicationsByPage(page, pageSize)
83+
response, err := c.GetApplicationsByPage(page, pageSize, networkItemType)
8084
if err != nil {
8185
return nil, err
8286
}
@@ -90,8 +94,12 @@ func (c *ApplicationsService) List() ([]ApplicationResponse, error) {
9094
return allApplications, nil
9195
}
9296

93-
func (c *ApplicationsService) Get(id string) (*ApplicationResponse, error) {
94-
endpoint := fmt.Sprintf("%s/api/beta/applications/single?applicationId=%s", c.client.BaseURL, id)
97+
func (c *ApplicationsService) Get(id string, networkItemType string) (*ApplicationResponse, error) {
98+
path, err := GetPath(networkItemType)
99+
if err != nil {
100+
return nil, err
101+
}
102+
endpoint := fmt.Sprintf("%s/%s/applications/%s", c.client.GetV1Url(), path, id)
95103
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
96104
if err != nil {
97105
return nil, err
@@ -110,8 +118,8 @@ func (c *ApplicationsService) Get(id string) (*ApplicationResponse, error) {
110118
return &application, nil
111119
}
112120

113-
func (c *ApplicationsService) GetByName(name string) (*ApplicationResponse, error) {
114-
applications, err := c.List()
121+
func (c *ApplicationsService) GetByName(name string, networkItemType string) (*ApplicationResponse, error) {
122+
applications, err := c.List(networkItemType)
115123
if err != nil {
116124
return nil, err
117125
}
@@ -125,13 +133,17 @@ func (c *ApplicationsService) GetByName(name string) (*ApplicationResponse, erro
125133
}
126134

127135
func (c *ApplicationsService) Create(application *Application) (*ApplicationResponse, error) {
136+
path, err := GetPath(application.NetworkItemType)
137+
if err != nil {
138+
return nil, err
139+
}
128140
applicationJson, err := json.Marshal(application)
129141
if err != nil {
130142
return nil, err
131143
}
132144

133145
params := networkUrlParams(application.NetworkItemType, application.NetworkItemId)
134-
endpoint := fmt.Sprintf("%s/api/beta/applications?%s", c.client.BaseURL, params.Encode())
146+
endpoint := fmt.Sprintf("%s/%s/applications?%s", c.client.GetV1Url(), path, params.Encode())
135147

136148
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(applicationJson))
137149
if err != nil {
@@ -152,12 +164,17 @@ func (c *ApplicationsService) Create(application *Application) (*ApplicationResp
152164
}
153165

154166
func (c *ApplicationsService) Update(id string, application *Application) (*ApplicationResponse, error) {
167+
path, err := GetPath(application.NetworkItemType)
168+
if err != nil {
169+
return nil, err
170+
}
171+
155172
applicationJson, err := json.Marshal(application)
156173
if err != nil {
157174
return nil, err
158175
}
159176

160-
endpoint := fmt.Sprintf("%s/api/beta/applications/%s", c.client.BaseURL, id)
177+
endpoint := fmt.Sprintf("%s/%s/applications/%s", c.client.GetV1Url(), path, id)
161178

162179
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(applicationJson))
163180
if err != nil {
@@ -177,8 +194,12 @@ func (c *ApplicationsService) Update(id string, application *Application) (*Appl
177194
return &s, nil
178195
}
179196

180-
func (c *ApplicationsService) Delete(id string) error {
181-
endpoint := fmt.Sprintf("%s/api/beta/applications/%s", c.client.BaseURL, id)
197+
func (c *ApplicationsService) Delete(id string, networkItemType string) error {
198+
path, err := GetPath(networkItemType)
199+
if err != nil {
200+
return err
201+
}
202+
endpoint := fmt.Sprintf("%s/%s/applications/%s", c.client.GetV1Url(), path, id)
182203
req, err := http.NewRequest(http.MethodDelete, endpoint, nil)
183204
if err != nil {
184205
return err

cloudconnexa/cloudconnexa.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewClient(baseURL, clientId, clientSecret string) (*Client, error) {
7070
return nil, err
7171
}
7272

73-
tokenURL := fmt.Sprintf("%s/api/beta/oauth/token", strings.TrimRight(baseURL, "/"))
73+
tokenURL := fmt.Sprintf("%s/api/v1/oauth/token", strings.TrimRight(baseURL, "/"))
7474
req, err := http.NewRequest(http.MethodPost, tokenURL, bytes.NewBuffer(jsonData))
7575

7676
if err != nil {
@@ -146,3 +146,7 @@ func (c *Client) DoRequest(req *http.Request) ([]byte, error) {
146146

147147
return body, nil
148148
}
149+
150+
func (c *Client) GetV1Url() string {
151+
return c.BaseURL + "/api/v1"
152+
}

cloudconnexa/cloudconnexa_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func setupMockServer() *httptest.Server {
1414
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1515
switch r.URL.Path {
16-
case "/api/beta/oauth/token":
16+
case "/api/v1/oauth/token":
1717
if r.Method == "POST" {
1818
w.Header().Set("Content-Type", "application/json")
1919
response := Credentials{AccessToken: "mocked-token"}

0 commit comments

Comments
 (0)