From 675fef7d570e5a74c719c445002ce8a7c03884c7 Mon Sep 17 00:00:00 2001 From: vladh Date: Wed, 17 Jul 2024 14:25:05 +0300 Subject: [PATCH] Add applications crud --- cloudconnexa/applications.go | 178 +++++++++++++++++++++++++++++++++++ cloudconnexa/cloudconnexa.go | 20 ++-- cloudconnexa/ip_services.go | 2 +- cloudconnexa/routes.go | 1 + 4 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 cloudconnexa/applications.go diff --git a/cloudconnexa/applications.go b/cloudconnexa/applications.go new file mode 100644 index 0000000..bbf66f4 --- /dev/null +++ b/cloudconnexa/applications.go @@ -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 +} diff --git a/cloudconnexa/cloudconnexa.go b/cloudconnexa/cloudconnexa.go index 4f43763..fa08b53 100644 --- a/cloudconnexa/cloudconnexa.go +++ b/cloudconnexa/cloudconnexa.go @@ -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 { @@ -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) diff --git a/cloudconnexa/ip_services.go b/cloudconnexa/ip_services.go index bcb6767..cbfdd7d 100644 --- a/cloudconnexa/ip_services.go +++ b/cloudconnexa/ip_services.go @@ -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 { diff --git a/cloudconnexa/routes.go b/cloudconnexa/routes.go index 3de5df4..3bec578 100644 --- a/cloudconnexa/routes.go +++ b/cloudconnexa/routes.go @@ -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"`