Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Location Context CRUD #36

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading