Skip to content

Commit

Permalink
Add applications crud
Browse files Browse the repository at this point in the history
  • Loading branch information
vladh committed Jul 17, 2024
1 parent 7e9a46d commit 675fef7
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 10 deletions.
178 changes: 178 additions & 0 deletions cloudconnexa/applications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package cloudconnexa

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

type ApplicationRoute struct {
Description string `json:"description"`
Value string `json:"value"`
}

type CustomApplicationType struct {
IcmpType []Range `json:"icmpType"`
Port []Range `json:"port"`
Protocol string `json:"protocol"`
}

type ApplicationConfig struct {
CustomServiceTypes []*CustomApplicationType `json:"customServiceTypes"`
ServiceTypes []string `json:"serviceTypes"`
}

type Application struct {
Name string `json:"name"`
Description string `json:"description"`
NetworkItemType string `json:"networkItemType"`
NetworkItemId string `json:"networkItemId"`
Id string `json:"id"`
Routes []*ApplicationRoute `json:"routes"`
Config *ApplicationConfig `json:"config"`
}

type ApplicationResponse struct {
Application
Routes []*Route `json:"routes"`
}

type ApplicationPageResponse struct {
Content []ApplicationResponse `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 ApplicationsService service

func (c *ApplicationsService) GetApplicationsByPage(page int, pageSize int) (ApplicationPageResponse, error) {
endpoint := fmt.Sprintf("%s/api/beta/applications/page?page=%d&size=%d", c.client.BaseURL, page, pageSize)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return ApplicationPageResponse{}, err
}

body, err := c.client.DoRequest(req)
if err != nil {
return ApplicationPageResponse{}, err
}

var response ApplicationPageResponse
err = json.Unmarshal(body, &response)
if err != nil {
return ApplicationPageResponse{}, err
}
return response, nil
}

func (c *ApplicationsService) List() ([]ApplicationResponse, error) {
var allApplications []ApplicationResponse
page := 0
pageSize := 10

for {
response, err := c.GetApplicationsByPage(page, pageSize)
if err != nil {
return nil, err
}

allApplications = append(allApplications, response.Content...)
if page >= response.TotalPages {
break
}
page++
}
return allApplications, nil
}

func (c *ApplicationsService) Get(id string) (*ApplicationResponse, error) {
endpoint := fmt.Sprintf("%s/api/beta/applications/single?applicationId=%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 application ApplicationResponse
err = json.Unmarshal(body, &application)
if err != nil {
return nil, err
}
return &application, nil
}

func (c *ApplicationsService) Create(application *Application) (*ApplicationResponse, error) {
applicationJson, err := json.Marshal(application)
if err != nil {
return nil, err
}

params := networkUrlParams(application.NetworkItemType, application.NetworkItemId)
endpoint := fmt.Sprintf("%s/api/beta/applications?%s", c.client.BaseURL, params.Encode())

req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(applicationJson))
if err != nil {
return nil, err
}

body, err := c.client.DoRequest(req)
if err != nil {
return nil, err
}

var s ApplicationResponse
err = json.Unmarshal(body, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *ApplicationsService) Update(id string, application *Application) (*ApplicationResponse, error) {
applicationJson, err := json.Marshal(application)
if err != nil {
return nil, err
}

endpoint := fmt.Sprintf("%s/api/beta/applications/%s", c.client.BaseURL, id)

req, err := http.NewRequest(http.MethodPut, endpoint, bytes.NewBuffer(applicationJson))
if err != nil {
return nil, err
}

body, err := c.client.DoRequest(req)
if err != nil {
return nil, err
}

var s ApplicationResponse
err = json.Unmarshal(body, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *ApplicationsService) Delete(id string) error {
endpoint := fmt.Sprintf("%s/api/beta/applications/%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
}
20 changes: 11 additions & 9 deletions cloudconnexa/cloudconnexa.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ type Client struct {

common service

Connectors *ConnectorsService
DnsRecords *DNSRecordsService
Hosts *HostsService
IPServices *IPServicesService
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
}

type service struct {
Expand Down Expand Up @@ -97,6 +98,7 @@ func NewClient(baseURL, clientId, clientSecret string) (*Client, error) {
c.DnsRecords = (*DNSRecordsService)(&c.common)
c.Hosts = (*HostsService)(&c.common)
c.IPServices = (*IPServicesService)(&c.common)
c.Applications = (*ApplicationsService)(&c.common)
c.Networks = (*NetworksService)(&c.common)
c.Routes = (*RoutesService)(&c.common)
c.Users = (*UsersService)(&c.common)
Expand Down
2 changes: 1 addition & 1 deletion cloudconnexa/ip_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Range struct {
LowerValue int `json:"lowerValue"`
UpperValue int `json:"upperValue"`
Value int `json:"value"`
Value int `json:"value,omitempty"`
}

type CustomIPServiceType struct {
Expand Down
1 change: 1 addition & 0 deletions cloudconnexa/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Route struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Subnet string `json:"subnet,omitempty"`
Domain string `json:"domain,omitempty"`
Description string `json:"description,omitempty"`
ParentRouteId string `json:"parentRouteId,omitempty"`
NetworkItemId string `json:"networkItemId,omitempty"`
Expand Down

0 comments on commit 675fef7

Please sign in to comment.