From efcd9f47b3df038e24a16178eac39ee757b7decc Mon Sep 17 00:00:00 2001 From: vladhanzha Date: Mon, 14 Oct 2024 11:14:55 +0300 Subject: [PATCH] Add Location Context CRUD --- cloudconnexa/cloudconnexa.go | 22 ++-- cloudconnexa/location_contexts.go | 173 ++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 10 deletions(-) create mode 100644 cloudconnexa/location_contexts.go diff --git a/cloudconnexa/cloudconnexa.go b/cloudconnexa/cloudconnexa.go index 8873ffb..8a83ac4 100644 --- a/cloudconnexa/cloudconnexa.go +++ b/cloudconnexa/cloudconnexa.go @@ -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 { @@ -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 } diff --git a/cloudconnexa/location_contexts.go b/cloudconnexa/location_contexts.go new file mode 100644 index 0000000..9262d4a --- /dev/null +++ b/cloudconnexa/location_contexts.go @@ -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 +}