Skip to content

Commit

Permalink
Implement ui helpers for OS V1 & V2 (#66)
Browse files Browse the repository at this point in the history
Signed-off-by: raihankhan <[email protected]>
  • Loading branch information
raihankhan authored Jul 10, 2023
1 parent ac6f2b9 commit 31f3da7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 14 deletions.
59 changes: 52 additions & 7 deletions elasticsearch/os_client_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,61 @@ type OSClientV1 struct {
}

func (os *OSClientV1) ClusterHealthInfo() (map[string]interface{}, error) {
return nil, nil
res, err := os.client.Cluster.Health(
os.client.Cluster.Health.WithPretty(),
)
if err != nil {
return nil, err
}
defer res.Body.Close()

response := make(map[string]interface{})
if err2 := json.NewDecoder(res.Body).Decode(&response); err2 != nil {
return nil, errors.Wrap(err2, "failed to parse the response body")
}
return response, nil
}

func (os *OSClientV1) NodesStats() (map[string]interface{}, error) {
return nil, nil
req := opensearchapi.NodesStatsRequest{
Pretty: true,
Human: true,
}

resp, err := req.Do(context.Background(), os.client)
if err != nil {
return nil, err
}
defer resp.Body.Close()

nodesStats := make(map[string]interface{})
if err := json.NewDecoder(resp.Body).Decode(&nodesStats); err != nil {
return nil, fmt.Errorf("failed to deserialize the response: %v", err)
}

return nodesStats, nil
}

// GetIndicesInfo will return the indices' info of an Elasticsearch database
func (os *OSClientV1) GetIndicesInfo() ([]interface{}, error) {
return nil, nil
req := opensearchapi.CatIndicesRequest{
Bytes: "b", // will return resource size field into byte unit
Format: "json",
Pretty: true,
Human: true,
}
resp, err := req.Do(context.Background(), os.client)
if err != nil {
return nil, err
}
defer resp.Body.Close()

indicesInfo := make([]interface{}, 0)
if err := json.NewDecoder(resp.Body).Decode(&indicesInfo); err != nil {
return nil, fmt.Errorf("failed to deserialize the response: %v", err)
}

return indicesInfo, nil
}

func (os *OSClientV1) ClusterStatus() (string, error) {
Expand All @@ -75,10 +120,6 @@ func (os *OSClientV1) ClusterStatus() (string, error) {
return "", errors.New("status is missing")
}

func (os *OSClientV1) SyncCredentialFromSecret(secret *core.Secret) error {
return nil
}

func (os *OSClientV1) GetClusterWriteStatus(ctx context.Context, db *api.Elasticsearch) error {
// Build the request index & request body
// send the db specs as body
Expand Down Expand Up @@ -206,3 +247,7 @@ func (os *OSClientV1) GetTotalDiskUsage(ctx context.Context) (string, error) {

return totalDiskUsage, nil
}

func (os *OSClientV1) SyncCredentialFromSecret(secret *core.Secret) error {
return nil
}
59 changes: 52 additions & 7 deletions elasticsearch/os_client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,61 @@ type OSClientV2 struct {
}

func (os *OSClientV2) ClusterHealthInfo() (map[string]interface{}, error) {
return nil, nil
res, err := os.client.Cluster.Health(
os.client.Cluster.Health.WithPretty(),
)
if err != nil {
return nil, err
}
defer res.Body.Close()

response := make(map[string]interface{})
if err2 := json.NewDecoder(res.Body).Decode(&response); err2 != nil {
return nil, errors.Wrap(err2, "failed to parse the response body")
}
return response, nil
}

func (os *OSClientV2) NodesStats() (map[string]interface{}, error) {
return nil, nil
req := osv2api.NodesStatsRequest{
Pretty: true,
Human: true,
}

resp, err := req.Do(context.Background(), os.client)
if err != nil {
return nil, err
}
defer resp.Body.Close()

nodesStats := make(map[string]interface{})
if err := json.NewDecoder(resp.Body).Decode(&nodesStats); err != nil {
return nil, fmt.Errorf("failed to deserialize the response: %v", err)
}

return nodesStats, nil
}

// GetIndicesInfo will return the indices' info of an Elasticsearch database
func (os *OSClientV2) GetIndicesInfo() ([]interface{}, error) {
return nil, nil
req := osv2api.CatIndicesRequest{
Bytes: "b", // will return resource size field into byte unit
Format: "json",
Pretty: true,
Human: true,
}
resp, err := req.Do(context.Background(), os.client)
if err != nil {
return nil, err
}
defer resp.Body.Close()

indicesInfo := make([]interface{}, 0)
if err := json.NewDecoder(resp.Body).Decode(&indicesInfo); err != nil {
return nil, fmt.Errorf("failed to deserialize the response: %v", err)
}

return indicesInfo, nil
}

func (os *OSClientV2) ClusterStatus() (string, error) {
Expand All @@ -75,10 +120,6 @@ func (os *OSClientV2) ClusterStatus() (string, error) {
return "", errors.New("status is missing")
}

func (os *OSClientV2) SyncCredentialFromSecret(secret *core.Secret) error {
return nil
}

func (os *OSClientV2) GetClusterWriteStatus(ctx context.Context, db *api.Elasticsearch) error {
// Build the request index & request body
// send the db specs as body
Expand Down Expand Up @@ -205,3 +246,7 @@ func (os *OSClientV2) GetTotalDiskUsage(ctx context.Context) (string, error) {

return totalDiskUsage, nil
}

func (os *OSClientV2) SyncCredentialFromSecret(secret *core.Secret) error {
return nil
}

0 comments on commit 31f3da7

Please sign in to comment.