-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from vladhss/feature/location-context
Add Location Context CRUD
- Loading branch information
Showing
2 changed files
with
185 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package cloudconnexa | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type LocationContext struct { | ||
Id string `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
UserGroupsIds []string `json:"userGroupsIds"` | ||
IpPolicy *IpPolicy `json:"ipPolicy,omitempty"` | ||
CountryPolicy *CountryPolicy `json:"countryPolicy,omitempty"` | ||
DefaultPolicy *DefaultPolicy `json:"defaultPolicy"` | ||
} | ||
|
||
type IpPolicy struct { | ||
Allowed bool `json:"allowed"` | ||
Ips []Ip `json:"ips"` | ||
} | ||
|
||
type CountryPolicy struct { | ||
Allowed bool `json:"allowed"` | ||
Countries []string `json:"countries"` | ||
} | ||
|
||
type DefaultPolicy struct { | ||
Allowed bool `json:"allowed"` | ||
} | ||
|
||
type Ip struct { | ||
Ip string `json:"ip"` | ||
Description string `json:"description"` | ||
} | ||
|
||
type LocationContextPageResponse struct { | ||
Content []LocationContext `json:"content"` | ||
NumberOfElements int `json:"numberOfElements"` | ||
Page int `json:"page"` | ||
Size int `json:"size"` | ||
Success bool `json:"success"` | ||
TotalElements int `json:"totalElements"` | ||
TotalPages int `json:"totalPages"` | ||
} | ||
|
||
type LocationContextsService service | ||
|
||
func (c *LocationContextsService) GetLocationContextByPage(page int, pageSize int) (LocationContextPageResponse, error) { | ||
endpoint := fmt.Sprintf("%s/api/beta/location-contexts/page?page=%d&size=%d", c.client.BaseURL, page, pageSize) | ||
req, err := http.NewRequest(http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return LocationContextPageResponse{}, err | ||
} | ||
|
||
body, err := c.client.DoRequest(req) | ||
if err != nil { | ||
return LocationContextPageResponse{}, err | ||
} | ||
|
||
var response LocationContextPageResponse | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return LocationContextPageResponse{}, err | ||
} | ||
return response, nil | ||
} | ||
|
||
func (c *LocationContextsService) List() ([]LocationContext, error) { | ||
var allLocationContexts []LocationContext | ||
page := 0 | ||
pageSize := 10 | ||
|
||
for { | ||
response, err := c.GetLocationContextByPage(page, pageSize) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allLocationContexts = append(allLocationContexts, response.Content...) | ||
if page >= response.TotalPages { | ||
break | ||
} | ||
page++ | ||
} | ||
return allLocationContexts, nil | ||
} | ||
|
||
func (c *LocationContextsService) Get(id string) (*LocationContext, error) { | ||
endpoint := fmt.Sprintf("%s/api/beta/location-contexts/%s", c.client.BaseURL, id) | ||
req, err := http.NewRequest(http.MethodGet, endpoint, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.client.DoRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var locationContext LocationContext | ||
err = json.Unmarshal(body, &locationContext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &locationContext, nil | ||
} | ||
|
||
func (c *LocationContextsService) Create(locationContext *LocationContext) (*LocationContext, error) { | ||
locationContextJson, err := json.Marshal(locationContext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
endpoint := fmt.Sprintf("%s/api/beta/location-contexts/", c.client.BaseURL) | ||
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(locationContextJson)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.client.DoRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var s LocationContext | ||
err = json.Unmarshal(body, &s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &s, nil | ||
} | ||
|
||
func (c *LocationContextsService) Update(id string, locationContext *LocationContext) (*LocationContext, error) { | ||
locationContextJson, err := json.Marshal(locationContext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
endpoint := fmt.Sprintf("%s/api/beta/location-contexts/%s", c.client.BaseURL, id) | ||
req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(locationContextJson)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.client.DoRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var s LocationContext | ||
err = json.Unmarshal(body, &s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &s, nil | ||
} | ||
|
||
func (c *LocationContextsService) Delete(id string) error { | ||
endpoint := fmt.Sprintf("%s/api/beta/location-contexts/%s", c.client.BaseURL, id) | ||
req, err := http.NewRequest(http.MethodDelete, endpoint, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.client.DoRequest(req) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |