Skip to content

Commit

Permalink
View & View Folders Clients (#210)
Browse files Browse the repository at this point in the history
* feat: views/views-folders clients

* feat: views/views-folders clients

* Write missing examples, and fix clients

---------

Co-authored-by: Claus Matzinger <claus.matzinger+kb@gmail.com>
  • Loading branch information
lootag and celaus authored Mar 11, 2025
1 parent b6aae86 commit 3914f65
Show file tree
Hide file tree
Showing 10 changed files with 1,064 additions and 0 deletions.
117 changes: 117 additions & 0 deletions go/examples/data-exploration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2025 Coralogix Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package examples

import (
"context"
"testing"

cxsdk "github.com/coralogix/coralogix-management-sdk/go"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/wrapperspb"
)

func TestViews(t *testing.T) {
region, err := cxsdk.CoralogixRegionFromEnv()
assertNilAndPrintError(t, err)
authContext, err := cxsdk.AuthContextFromEnv()
assertNilAndPrintError(t, err)
creator := cxsdk.NewCallPropertiesCreator(region, authContext)
c := cxsdk.NewViewsClient(creator)

view, e := c.Create(context.Background(), &cxsdk.CreateViewRequest{
Name: &wrapperspb.StringValue{Value: "GoTestView"},
SearchQuery: &cxsdk.SearchQuery{
Query: &wrapperspb.StringValue{Value: "source logs | filter $l.applicationname == 'default'"},
},
TimeSelection: &cxsdk.TimeSelection{
SelectionType: &cxsdk.ViewTimeSelectionQuick{
QuickSelection: &cxsdk.QuickTimeSelection{
Seconds: uint32(86400), // 24h
},
},
},
Filters: &cxsdk.SelectedFilters{},
})
assertNilAndPrintError(t, e)

view.View.Name = &wrapperspb.StringValue{Value: "GoTestViewUpdated"}

_, e = c.Replace(context.Background(), &cxsdk.ReplaceViewRequest{
View: view.View,
})

assertNilAndPrintError(t, e)

_, e = c.Get(context.Background(), &cxsdk.GetViewRequest{
Id: view.View.Id,
})

assertNilAndPrintError(t, e)

c.Delete(context.Background(), &cxsdk.DeleteViewRequest{
Id: view.View.Id,
})
}

func TestViewFolders(t *testing.T) {
region, err := cxsdk.CoralogixRegionFromEnv()
assertNilAndPrintError(t, err)
authContext, err := cxsdk.AuthContextFromEnv()
assertNilAndPrintError(t, err)
creator := cxsdk.NewCallPropertiesCreator(region, authContext)
c := cxsdk.NewViewFoldersClient(creator)

allFolders, e := c.List(context.Background(), &cxsdk.ListViewFoldersRequest{})
assertNilAndPrintError(t, e)

numOfFolders := len(allFolders.Folders)

createResponse, e := c.Create(context.Background(), &cxsdk.CreateViewFolderRequest{
Name: &wrapperspb.StringValue{Value: "GoTestViewFolder"},
})
assertNilAndPrintError(t, e)

createResponse.Folder.Name = &wrapperspb.StringValue{Value: "GoTestViewFolderUpdated"}

_, e = c.Replace(context.Background(), &cxsdk.ReplaceViewFolderRequest{
Folder: createResponse.Folder,
})
assertNilAndPrintError(t, e)

updatedFolder, e := c.Get(context.Background(), &cxsdk.GetViewFolderRequest{
Id: createResponse.Folder.Id,
})

assertNilAndPrintError(t, e)
assert.Equal(t, updatedFolder.Folder.Name.Value, "GoTestViewFolderUpdated")

allFoldersWithNewFolder, e := c.List(context.Background(), &cxsdk.ListViewFoldersRequest{})

assertNilAndPrintError(t, e)
assert.Equal(t, numOfFolders+1, len(allFoldersWithNewFolder.Folders))

c.Delete(context.Background(), &cxsdk.DeleteViewFolderRequest{
Id: createResponse.Folder.Id,
})

assertNilAndPrintError(t, e)

allFolders, e = c.List(context.Background(), &cxsdk.ListViewFoldersRequest{})

assertNilAndPrintError(t, e)
assert.Equal(t, numOfFolders, len(allFolders.Folders))

}
167 changes: 167 additions & 0 deletions go/view-folders-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright 2025 Coralogix Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cxsdk

import (
"context"

views "github.com/coralogix/coralogix-management-sdk/go/internal/coralogixapis/views/v1"

vservices "github.com/coralogix/coralogix-management-sdk/go/internal/coralogixapis/views/v1/services"
)

// CreateViewFolderRequest is a request to create a new view
type CreateViewFolderRequest = vservices.CreateViewFolderRequest

// CreateViewFolderResponse is a response to create a new view
type CreateViewFolderResponse = vservices.CreateViewFolderResponse

// GetViewFolderRequest is a request to get a view folder
type GetViewFolderRequest = vservices.GetViewFolderRequest

// GetViewFolderResponse is a response when getting a view folder
type GetViewFolderResponse = vservices.GetViewFolderResponse

// ListViewFoldersRequest is a request to list all views
type ListViewFoldersRequest = vservices.ListViewFoldersRequest

// ListViewFoldersResponse is a response when listing views
type ListViewFoldersResponse = vservices.ListViewFoldersResponse

// DeleteViewFolderRequest is a request to delete a view folder
type DeleteViewFolderRequest = vservices.DeleteViewFolderRequest

// DeleteViewFolderResponse is a response when deleting a view folder
type DeleteViewFolderResponse = vservices.DeleteViewFolderResponse

// ReplaceViewFolderRequest is a request to replacing a view folder
type ReplaceViewFolderRequest = vservices.ReplaceViewFolderRequest

// ReplaceViewFolderResponse is a response to replacing a view folder
type ReplaceViewFolderResponse = vservices.ReplaceViewFolderResponse

// ViewFolder is a view folder
type ViewFolder = views.ViewFolder

const viewFolderFeatureGroupID = "data-exploration"

// RPC Name values
const (
CreateViewFolderRPC = vservices.ViewsFoldersService_CreateViewFolder_FullMethodName
DeleteViewFolderRPC = vservices.ViewsFoldersService_DeleteViewFolder_FullMethodName
GetViewFolderRPC = vservices.ViewsFoldersService_GetViewFolder_FullMethodName
ListViewFoldersRPC = vservices.ViewsFoldersService_ListViewFolders_FullMethodName
ReplaceViewFolderRPC = vservices.ViewsFoldersService_ReplaceViewFolder_FullMethodName
)

// ViewFoldersClient is a client for the view folders service
type ViewFoldersClient struct {
callPropertiesCreator *CallPropertiesCreator
}

// Create creates a new view folder
func (c ViewFoldersClient) Create(ctx context.Context, req *CreateViewFolderRequest) (*CreateViewFolderResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}

conn := callProperties.Connection
defer conn.Close()
client := vservices.NewViewsFoldersServiceClient(conn)

response, err := client.CreateViewFolder(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, CreateViewFolderRPC, viewFolderFeatureGroupID)
}
return response, nil
}

// Get gets a view folder by its ID
func (c ViewFoldersClient) Get(ctx context.Context, req *GetViewFolderRequest) (*GetViewFolderResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}

conn := callProperties.Connection
defer conn.Close()
client := vservices.NewViewsFoldersServiceClient(conn)

response, err := client.GetViewFolder(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, GetViewFolderRPC, viewFolderFeatureGroupID)
}
return response, nil
}

// List lists all view folders
func (c ViewFoldersClient) List(ctx context.Context, req *ListViewFoldersRequest) (*ListViewFoldersResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}

conn := callProperties.Connection
defer conn.Close()
client := vservices.NewViewsFoldersServiceClient(conn)

response, err := client.ListViewFolders(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, ListViewFoldersRPC, viewFolderFeatureGroupID)
}
return response, nil
}

// Replace updates a view folder
func (c ViewFoldersClient) Replace(ctx context.Context, req *ReplaceViewFolderRequest) (*ReplaceViewFolderResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}

conn := callProperties.Connection
defer conn.Close()
client := vservices.NewViewsFoldersServiceClient(conn)

response, err := client.ReplaceViewFolder(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, ReplaceViewFolderRPC, viewFolderFeatureGroupID)
}
return response, nil
}

// Delete deletes a view folder
func (c ViewFoldersClient) Delete(ctx context.Context, req *DeleteViewFolderRequest) (*DeleteViewFolderResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
if err != nil {
return nil, err
}

conn := callProperties.Connection
defer conn.Close()
client := vservices.NewViewsFoldersServiceClient(conn)

response, err := client.DeleteViewFolder(callProperties.Ctx, req, callProperties.CallOptions...)
if err != nil {
return nil, NewSdkAPIError(err, DeleteViewFolderRPC, viewFolderFeatureGroupID)
}
return response, nil
}

// NewViewFoldersClient creates a new ViewFoldersClient
func NewViewFoldersClient(c *CallPropertiesCreator) *ViewFoldersClient {
return &ViewFoldersClient{callPropertiesCreator: c}
}
Loading

0 comments on commit 3914f65

Please sign in to comment.