Skip to content

Commit

Permalink
Merge pull request #36 from vladhss/feature/location-context
Browse files Browse the repository at this point in the history
Add Location Context CRUD
  • Loading branch information
sahaqaa authored Oct 14, 2024
2 parents f36203d + efcd9f4 commit 6ef86a4
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 10 deletions.
22 changes: 12 additions & 10 deletions cloudconnexa/cloudconnexa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ type Client struct {

common service

Connectors *ConnectorsService
DnsRecords *DNSRecordsService
Hosts *HostsService
IPServices *IPServicesService
Applications *ApplicationsService
Networks *NetworksService
Routes *RoutesService
Users *UsersService
UserGroups *UserGroupsService
VPNRegions *VPNRegionsService
Connectors *ConnectorsService
DnsRecords *DNSRecordsService
Hosts *HostsService
IPServices *IPServicesService
Applications *ApplicationsService
Networks *NetworksService
Routes *RoutesService
Users *UsersService
UserGroups *UserGroupsService
VPNRegions *VPNRegionsService
LocationContexts *LocationContextsService
}

type service struct {
Expand Down Expand Up @@ -113,6 +114,7 @@ func NewClient(baseURL, clientId, clientSecret string) (*Client, error) {
c.Users = (*UsersService)(&c.common)
c.UserGroups = (*UserGroupsService)(&c.common)
c.VPNRegions = (*VPNRegionsService)(&c.common)
c.LocationContexts = (*LocationContextsService)(&c.common)
return c, nil
}

Expand Down
173 changes: 173 additions & 0 deletions cloudconnexa/location_contexts.go
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
}

0 comments on commit 6ef86a4

Please sign in to comment.