Giter Club home page Giter Club logo

logzio_terraform_client's Issues

Use a mock for tests

Currently, the tests seem to require a valid API token and is actually making requests against the Logz.io API. This makes it non-trivial to contribute to the project as it requires to have an Enterprise account just for running the tests. I suggest to use a mock for the tests and complement with integration tests which can do real requests against the API if desired.

GetSubAccount panics (since nil values are not validated before conversion)

Test code:

func Test(apiToken string, baseUrl string, subAccountNumber int64) error {
    var c *sub_accounts.SubAccountClient
	if len(apiToken) == 0 {
		fmt.Errorf("API token not defined")
	}
	if len(baseUrl) == 0 {
		fmt.Errorf("Base URL not defined")
	}
    c, _ =  sub_accounts.New(apiToken, baseUrl)

    subAccount, err := c.GetSubAccount(subAccountNumber)
	if err != nil {
		return err
	}
    fmt.Println("%s", subAccount.AccountName)
    return nil
}

Result:

panic: interface conversion: interface {} is nil, not float64

Root cause:
Not checking nil value when accessing json['key'] in function jsonToSubAccount (specifically in the field MaxDailyGB)
See the code below:

func jsonToSubAccount(json map[string]interface{}) SubAccount {
 
	subAccount := SubAccount{
		Id:                    int64(json[fldAccountId].(float64)),
		AccountName:           json[fldAccountName].(string),
		MaxDailyGB:            float32(json[fldMaxDailyGB].(float64)),  <---------- BUG ------------
		RetentionDays:         int32(json[fldRetentionDays].(float64)),
		Searchable:            json[fldSearchable].(bool),
		Accessible:            json[fldAccessible].(bool),
		DocSizeSetting:        json[fldDocSizeSetting].(bool),
		SharingObjectAccounts: json[fldSharingAccountObjects].([]interface{}),
		UtilizationSettings:   json[fldUtilizationSettings].(map[string]interface{}),
	}
....
}

Debug log:

{
	"accessible": true,
	"accountId": 12345,
	"accountName": "xxxxx",
	"docSizeSetting": true,
	"email": null,
	"isFlexible": true,
	"maxDailyGB": null,  <------------- The returned value ------------ 
	"reservedDailyGB": 0,
	"retentionDays": 14,
	"searchable": true,
	"sharingObjectsAccounts": [],
	"utilizationSettings": {
		"frequencyMinutes": 60,
		"utilizationEnabled": true
	}
}

The fix:
I was not able to push code even to side branch !
For all field need to validate values are NOT nil (in case of nil set some default):

func jsonToSubAccount(json map[string]interface{}) SubAccount {
	var maxDailyGB float32 = 0
	if json[fldMaxDailyGB] != nil {
		maxDailyGB = float32(json[fldMaxDailyGB].(float64))
	}
	subAccount := SubAccount{
		Id:                    int64(json[fldAccountId].(float64)),
		AccountName:           json[fldAccountName].(string),
		MaxDailyGB:            maxDailyGB,
		RetentionDays:         int32(json[fldRetentionDays].(float64)),
		Searchable:            json[fldSearchable].(bool),
		Accessible:            json[fldAccessible].(bool),
		DocSizeSetting:        json[fldDocSizeSetting].(bool),
		SharingObjectAccounts: json[fldSharingAccountObjects].([]interface{}),
		UtilizationSettings:   json[fldUtilizationSettings].(map[string]interface{}),
	}

		if json[fldUtilizationSettings] != nil {
			subAccount.UtilizationSettings = json[fldUtilizationSettings].(map[string]interface{})
			for key, value := range subAccount.UtilizationSettings {
				if value == nil {
					delete(subAccount.UtilizationSettings, key)
				}
			}
		}
	return subAccount
}

endpoints : expose endpoint types

expose endpoint types consts publicly, so the client (of the client) can provide it's own mapping between type names (if it needs to)

  • slack C
  • slack U
  • custom C
  • custom U
  • pagerduty C
  • pagerduty U
  • bigpanda C
  • bigpanda U
  • datadog C
  • datadog U
  • victorops C
  • victorops U
  • R endpoint by id
  • R endpoint by name
  • list endpoints

create_alerts_list test failing

problem

  • create_alerts_list test failing

infos

github.com/jonboydell/logzio_client.(*Client).ListAlerts(0xc000041740, 0xc000018011, 0x24, 0x105e1e0, 0xc000041720, 0x851aebb9)
	/Users/jon.boydell/go/src/github.com/jonboydell/logzio_client/client_alerts_list.go:61 +0x12bd

done when

  • line 61 GroupByAggregationFields: jsonAlert["groupByAggregationFields"].([]interface{}), can be nil and needs to be dealt with outside the struct initialisation

subaccounts: add subaccounts support

create_alerts_list and create_alerts_get duplicate code

problem:

  • create_alerts_list and create_alerts_get duplicate code for unmarshalling an alert

proposed solution:

  • should either use some custom unmarshaller or be refactored into its own function

complete when:

  • duplicate code is removed

Use go modules

Go modules has become the canonical way to handle dependencies in a Go project. I would be happy to add support for Go modules in this project.

global: refactor packages

each "type" of API to be managed in it's own package, to prevent name space clashes, to compartmentalise the different APIs and to make testing simpler

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.