Skip to content

Commit

Permalink
Develop to master (#62)
Browse files Browse the repository at this point in the history
* consistent use of int64 for ids (was int32)

* Release 1 2 mocks (#41)

* #39 - added mocking for "users" library tests, split out integration tests from unit tests - integration tests now tagged and won't run as part of the normal go test (hopefully!)

* phase 1 of adding proper unit tests and moving the integration tests into their own runnable unit
- added httptest library
- reorganised the integration tests into their own files
- wrote unit tests for users

* unforced error with the naming of the function that gets the base url for logz.io mid-refactor - bad programmer

* encapsulated all of the local endpoints functions into the endpoints client

* removed unused imports

* integration tests only run on tag "integration"

* started refactor of alerts tests, added coveralls back in for travis

* added delete alert unit test

* fixed bug in update alert not detecting error state from API, added unit tests for update alert

* started refactor of endpoints, broke out test setup into integration and unit tests

* try out circle ci

* added correct details for circle ci build

* circleci:changed build instructions to be the same as travis...

* removed API_TOKEN as mandatory for unit tests

* removed create alert integration tests from the main build

* circleci:added new workflow for integration testing

* added correct (?) workflow syntax

* circleci:added coveralls

* circleci:added correct coveralls service name

* Renamed all the endpoints tests to have the word Integration in them
Added >80% unit test coverage for endpoints

* - refactored endpoint tests to include proper unit tests (rather than integration tests)
- fixed a couple of issues with non-detection of error states in the endpoints client

* alerts: Allow CreatedAt and CreatedBy to be nullable (#47)

* missing = from travis coveralls command...

* go fmt (#48)

* go fmt

* added scheduled workflow for integration tests

* Switch from dep to Go modules for dependency management (#49)

* Make clients respect baseUrl environment variable (#50)

* Update README.md with instructions on how to contribute (#51)

* CI: Add race detection to test step and use GO111MODULE=on (#52)

* Make Endpoint description nullable (#54)

* added unit test for "user-get"

* Fix endoint types for consistency with the Logz API (#56)

Previously, the endpoint type values were not matching what was returned
from the Logz.io API. This made the consumers of the package to manage
their own constants when using the endpoint types. This commit fixes
that by using the same values returned from the Logz API as the constant
for endpoint type. It also adds a helper function to get the URL for
each endoint type.

* Pass baseUrl when creating clients (#55)

* First pass sub_accounts support  (#57)

* added framework for sub-accounts

* sub_account create

* added
- create sub account
- delete sub account
- list sub accounts
- get sub account
and unit tests (and some API oddities)

* added
- update
and unit test

* changes to deal with new client New func

* alerts: Add missing severity constants "INFO" and "SEVERE"

* Update go.yml

* Add subaccounts support (#61)

* Get sub accounts detailed info

Signed-off-by: yyyogev <[email protected]>

* Structs

Signed-off-by: yyyogev <[email protected]>

* Sub account detailed info type

Signed-off-by: yyyogev <[email protected]>

* parse json

Signed-off-by: yyyogev <[email protected]>

* get detailed info by id

Signed-off-by: yyyogev <[email protected]>

* get detailed subaccounts tests

Signed-off-by: yyyogev <[email protected]>

* dates tests

Signed-off-by: yyyogev <[email protected]>

* Update README.md

Co-authored-by: Jon Boydell <[email protected]>
Co-authored-by: Peter Nguyen <[email protected]>
Co-authored-by: Peter Nguyen <[email protected]>
Co-authored-by: Isidro López <[email protected]>
Co-authored-by: Isidro López <[email protected]>
  • Loading branch information
6 people authored Jun 18, 2020
1 parent f4fbb85 commit 51ae1e4
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Go
on: [push]
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build
run: go build -v .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Logz.io client library
# Logz.io Terraform client library

DEVELOP - [![Build Status](https://travis-ci.org/jonboydell/logzio_client.svg?branch=develop)](https://travis-ci.org/jonboydell/logzio_client) [![Coverage Status](https://coveralls.io/repos/github/jonboydell/logzio_client/badge.svg?branch=develop)](https://coveralls.io/github/jonboydell/logzio_client?branch=develop)

Expand Down
74 changes: 74 additions & 0 deletions sub_accounts/client_sub_account_get_detailed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package sub_accounts

import (
"encoding/json"
"fmt"
"github.com/jonboydell/logzio_client"
"github.com/jonboydell/logzio_client/client"
"io/ioutil"
"net/http"
)

const (
getDetailedServiceUrl string = subAccountServiceEndpoint + "/detailed/%d"
getDetailedServiceMethod string = http.MethodGet
getDetailedServiceSuccess int = http.StatusOK
)

func (c *SubAccountClient) getDetailedValidateRequest(id int64) (error, bool) {
return nil, true
}

func (c *SubAccountClient) getDetailedApiRequest(apiToken string, id int64) (*http.Request, error) {

url := fmt.Sprintf(getDetailedServiceUrl, c.BaseUrl, id)
req, err := http.NewRequest(getDetailedServiceMethod, url, nil)
logzio_client.AddHttpHeaders(apiToken, req)
return req, err
}

func (c *SubAccountClient) getDetailedHttpRequest(req *http.Request) (map[string]interface{}, error) {
httpClient := client.GetHttpClient(req)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jsonBytes, err := ioutil.ReadAll(resp.Body)
if !logzio_client.CheckValidStatus(resp, []int{getDetailedServiceSuccess}) {
return nil, fmt.Errorf("%d %s", resp.StatusCode, jsonBytes)
}
var target map[string]interface{}
err = json.Unmarshal(jsonBytes, &target)
if err != nil {
return nil, err
}
return target, nil
}

func (c *SubAccountClient) getDetailedCheckResponse(response map[string]interface{}) error {
return nil
}

func (c *SubAccountClient) GetDetailedSubAccount(id int64) (*SubAccountDetailed, error) {
if err, ok := c.getDetailedValidateRequest(id); !ok {
return nil, err
}
req, _ := c.getDetailedApiRequest(c.ApiToken, id)

target, err := c.getDetailedHttpRequest(req)
if err != nil {
return nil, err
}

err = c.getDetailedCheckResponse(target)
if err != nil {
return nil, err
}

subAccount, err := jsonToDetailedSubAccount(target)
if err != nil {
return nil, err
}
return subAccount, nil
}
76 changes: 76 additions & 0 deletions sub_accounts/client_sub_account_list_detailed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package sub_accounts

import (
"encoding/json"
"fmt"
"github.com/jonboydell/logzio_client"
"github.com/jonboydell/logzio_client/client"
"io/ioutil"
"net/http"
)

const (
detailedServiceUrl string = subAccountServiceEndpoint + "/detailed"
detailedServiceMethod string = http.MethodGet
detailedServiceSuccess int = http.StatusOK
)

func (c *SubAccountClient) detailedValidateRequest(id int64) (error, bool) {
return nil, true
}

func (c *SubAccountClient) detailedApiRequest(apiToken string) (*http.Request, error) {
url := fmt.Sprintf(detailedServiceUrl, c.BaseUrl)
req, err := http.NewRequest(detailedServiceMethod, url, nil)
logzio_client.AddHttpHeaders(apiToken, req)
return req, err
}

func (c *SubAccountClient) detailedHttpRequest(req *http.Request) ([]map[string]interface{}, error) {
httpClient := client.GetHttpClient(req)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jsonBytes, err := ioutil.ReadAll(resp.Body)
if !logzio_client.CheckValidStatus(resp, []int{detailedServiceSuccess}) {
return nil, fmt.Errorf("%d %s", resp.StatusCode, jsonBytes)
}
var target []map[string]interface{}
err = json.Unmarshal(jsonBytes, &target)
if err != nil {
return nil, err
}
return target, nil
}

func (c *SubAccountClient) detailedCheckResponse(response []map[string]interface{}) error {
return nil
}

func (c *SubAccountClient) DetailedSubAccounts() ([]SubAccountDetailed, error) {

req, _ := c.detailedApiRequest(c.ApiToken)

target, err := c.detailedHttpRequest(req)
if err != nil {
return nil, err
}

err = c.detailedCheckResponse(target)
if err != nil {
return nil, err
}

var subAccountsDetailed []SubAccountDetailed
for _, jsonObject := range target {
subAccount, err := jsonToDetailedSubAccount(jsonObject)
if err != nil {
return nil, err
}
subAccountsDetailed = append(subAccountsDetailed, *subAccount)
}

return subAccountsDetailed, nil
}
44 changes: 44 additions & 0 deletions sub_accounts/sub_account_get_detailed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sub_accounts_test

import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"strconv"
"testing"
)

func TestSubAccount_GetDetailedValidSubAccount(t *testing.T) {
underTest, err, teardown := setupSubAccountsTest()
assert.NoError(t, err)
defer teardown()

subAccountId := int64(1234567)

mux.HandleFunc("/v1/account-management/time-based-accounts/", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Contains(t, r.URL.String(), strconv.FormatInt(int64(subAccountId), 10))
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, fixture("get_detailed_subaccount.json"))
})

subAccount, err := underTest.GetDetailedSubAccount(subAccountId)
assert.NoError(t, err)
assert.NotNil(t, subAccount)

assert.Equal(t, "testAccount", subAccount.Account.AccountName)
assert.Equal(t, int64(12345), subAccount.Account.AccountId)
assert.Equal(t, "testToken", subAccount.Account.AccountToken)
assert.Equal(t, "testIndex", subAccount.Account.EsIndexPrefix)
assert.Equal(t, int64(12), subAccount.Account.MaxDailyGB)
assert.Equal(t, int64(123), subAccount.Account.RetentionDays)
assert.Equal(t, float64(5), subAccount.UtilizationSettings["frequencyMinutes"])
assert.True(t, subAccount.SubAccountRelation.Searchable)
assert.Equal(t, int64(1584532201), subAccount.SubAccountRelation.CreatedDate)
assert.Equal(t, int64(1584532202), subAccount.SubAccountRelation.LastUpdatedDate)
assert.True(t, subAccount.DocSizeSetting)
assert.False(t, subAccount.SubAccountRelation.Accessible)
sharingAccountObjects := subAccount.SharingObjectAccounts[0].(map[string]interface{})
assert.Equal(t, 7, len(sharingAccountObjects))
}
25 changes: 25 additions & 0 deletions sub_accounts/sub_account_list_detailed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sub_accounts_test

import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestSubAccount_ListDetailedSubAccounts(t *testing.T) {
underTest, err, teardown := setupSubAccountsTest()
assert.NoError(t, err)
defer teardown()

mux.HandleFunc("/v1/account-management/time-based-accounts/detailed", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, fixture("list_subaccounts_detailed.json"))
})

subAccounts, err := underTest.DetailedSubAccounts()
assert.NoError(t, err)
assert.Equal(t, 1, len(subAccounts))
}
47 changes: 47 additions & 0 deletions sub_accounts/testdata/fixtures/list_subaccounts_detailed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[
{
"subAccountRelation": {
"ownerAccountId": 88765,
"subAccountId": 1234567,
"searchable": true,
"accessible": false,
"createdDate": 1584532201,
"lastUpdatedDate": 1584532201,
"lastUpdaterUserId": 33342,
"type": "SUB_ACCOUNT"
},
"account": {
"accountId": 12345,
"accountName": "string",
"accountToken": "string",
"active": true,
"esIndexPrefix": "string",
"maxDailyGB": 12,
"retentionDays": 123
},
"sharingObjectsAccounts": [
{
"accountId": 12345,
"accountName": "string",
"accountToken": "string",
"active": true,
"esIndexPrefix": "string",
"maxDailyGB": 12,
"retentionDays": 123
}
],
"utilizationSettings": {
"frequencyMinutes": 5,
"utilizationEnabled": true
},
"dailyUsagesList": {
"usage": [
{
"date": 0,
"bytes": 0
}
]
},
"docSizeSetting": true
}
]

0 comments on commit 51ae1e4

Please sign in to comment.