Skip to content

Commit

Permalink
update version to add metrics accounts support (#177)
Browse files Browse the repository at this point in the history
* implement metrics accounts
  • Loading branch information
8naama authored Apr 21, 2024
1 parent d17c92f commit 7e08328
Show file tree
Hide file tree
Showing 8 changed files with 742 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
github.com/logzio/logzio_terraform_client v1.20.1
github.com/logzio/logzio_terraform_client v1.21.0
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.8.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/logzio/logzio_terraform_client v1.20.1 h1:ZSrYwKqYavvPNUfjV4R1Ld+Aa13dmCaxhL6bsiUN7TY=
github.com/logzio/logzio_terraform_client v1.20.1/go.mod h1:hEQixCq9RPpvyzWerxIWKf0SYgangyWpPeogN7nytC0=
github.com/logzio/logzio_terraform_client v1.21.0 h1:LVGyek1qsZmo7nL/7V0VOOZ6udiFqqlgUjzaASNmb3c=
github.com/logzio/logzio_terraform_client v1.21.0/go.mod h1:hEQixCq9RPpvyzWerxIWKf0SYgangyWpPeogN7nytC0=
github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down
120 changes: 120 additions & 0 deletions logzio/datasource_metrics_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package logzio

import (
"context"
"fmt"
"github.com/avast/retry-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/logzio/logzio_terraform_client/metrics_accounts"
"strconv"
"strings"
"time"
)

func dataSourceMetricsAccount() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMetricsAccountReadWrapper,
Schema: map[string]*schema.Schema{
metricsAccountId: {
Type: schema.TypeInt,
Optional: true,
},
metricsAccountName: {
Type: schema.TypeString,
Optional: true,
},
metricsAccountEmail: {
Type: schema.TypeString,
Optional: true,
},
metricsAccountToken: {
Type: schema.TypeString,
Optional: true,
},
metricsAccountPlanUts: {
Type: schema.TypeInt,
Optional: true,
},
metricsAccountAuthorizedAccounts: {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Optional: true,
},
},
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Second),
},
}
}

func dataSourceMetricsAccountReadWrapper(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var err error
readErr := retry.Do(
func() error {
if err = dataSourceMetricsAccountRead(d, m); err != nil {
return err
}

return nil
},
retry.RetryIf(
func(err error) bool {
if err != nil {
if strings.Contains(err.Error(), "failed with missing metrics account") ||
strings.Contains(err.Error(), "failed with status code 500") {
return true
}
}
return false
}),
retry.DelayType(retry.BackOffDelay),
retry.Attempts(15),
)

if readErr != nil {
return diag.FromErr(readErr)
}

return nil
}

func dataSourceMetricsAccountRead(d *schema.ResourceData, m interface{}) error {
var client *metrics_accounts.MetricsAccountClient
var clientErr error
client, clientErr = metrics_accounts.New(m.(Config).apiToken, m.(Config).baseUrl)

if clientErr != nil {
return clientErr
}

accountId, ok := d.GetOk(metricsAccountId)
if ok {
metricsAccount, err := client.GetMetricsAccount(int64(accountId.(int)))
if err != nil {
return err
}
d.SetId(strconv.FormatInt(int64(accountId.(int)), 10))
setMetricsAccount(d, metricsAccount)
return nil
}

accountName, ok := d.GetOk(metricsAccountName)
if ok {
metricsAccounts, err := client.ListMetricsAccounts()
if err != nil {
return err
}

for _, account := range metricsAccounts {
if account.AccountName == accountName.(string) {
d.SetId(strconv.FormatInt(int64(account.Id), 10))
setMetricsAccount(d, &account)
return nil
}
}
}
return fmt.Errorf("couldn't find metrics account with specified attributes")
}
128 changes: 128 additions & 0 deletions logzio/datasource_metrics_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package logzio

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/logzio/logzio_terraform_provider/logzio/utils"
"os"
"regexp"
"strconv"
"testing"
)

func TestAccDataSourceMetricsAccount(t *testing.T) {
resourceName := "logzio_metrics_account.metrics_account_datasource"
dataSourceName := "data.logzio_metrics_account.metrics_account_datasource_by_id"
accountId, _ := strconv.ParseInt(os.Getenv(envLogzioAccountId), utils.BASE_10, utils.BITSIZE_64)
email := os.Getenv(envLogzioEmail)
accountName := "test_datasource_create"
defer utils.SleepAfterTest()

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckApiToken(t)
testAccPreCheckEmail(t)
testAccPreCheckAccountId(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccMetricsAccountDataSourceResource(email, accountId, accountName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_name", accountName),
resource.TestCheckResourceAttr(resourceName, "plan_uts", "100"),
),
},
{
Config: testAccMetricsAccountDataSourceResource(email, accountId, accountName) +
testAccCheckLogzioMetricsAccountDatasourceConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "account_name", accountName),
resource.TestCheckResourceAttr(dataSourceName, "plan_uts", "100"),
),
},
},
})
}

func TestAccDataSourceMetricsAccountByAccountName(t *testing.T) {
resourceName := "logzio_metrics_account.metrics_account_datasource"
dataSourceName := "data.logzio_metrics_account.metrics_account_datasource_by_id"
accountId, _ := strconv.ParseInt(os.Getenv(envLogzioAccountId), utils.BASE_10, utils.BITSIZE_64)
email := os.Getenv(envLogzioEmail)
accountName := "test_datasource_account_name"
defer utils.SleepAfterTest()

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckApiToken(t)
testAccPreCheckEmail(t)
testAccPreCheckAccountId(t)
},
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccMetricsAccountDataSourceResource(email, accountId, accountName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "account_name", accountName),
resource.TestCheckResourceAttr(resourceName, "plan_uts", "100"),
),
},
{
Config: testAccMetricsAccountDataSourceResource(email, accountId, accountName) +
testAccCheckLogzioMetricsAccountDatasourceConfigAccountName(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "account_name", accountName),
resource.TestCheckResourceAttr(dataSourceName, "plan_uts", "100"),
),
},
},
})
}

func TestAccDataSourceMetricsAccountNotExists(t *testing.T) {
defer utils.SleepAfterTest()

resource.Test(t, resource.TestCase{
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckLogzioMetricsAccountDatasourceConfigNotExist(),
ExpectError: regexp.MustCompile("couldn't find metrics account with specified attributes"),
},
},
})
}

func testAccMetricsAccountDataSourceResource(email string, accountId int64, accountName string) string {
return fmt.Sprintf(`resource "logzio_metrics_account" "metrics_account_datasource" {
email = "%s"
account_name = "%s"
plan_uts = 100
authorized_accounts = [
%d
]
}
`, email, accountName, accountId)
}

func testAccCheckLogzioMetricsAccountDatasourceConfig() string {
return fmt.Sprint(`data "logzio_metrics_account" "metrics_account_datasource_by_id" {
account_id = "${logzio_metrics_account.metrics_account_datasource.Id}"
}
`)
}

func testAccCheckLogzioMetricsAccountDatasourceConfigAccountName() string {
return fmt.Sprint(`data "logzio_metrics_account" "metrics_account_datasource_by_id" {
account_name = "${logzio_metrics_account.metrics_account_datasource.account_name}"
}
`)
}

func testAccCheckLogzioMetricsAccountDatasourceConfigNotExist() string {
return fmt.Sprint(`data "logzio_metrics_account" "metrics_account_datasource_by_id" {
account_name = "name_not_exist"
}
`)
}
3 changes: 3 additions & 0 deletions logzio/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
resourceEndpointType = "logzio_endpoint"
resourceUserType = "logzio_user"
resourceSubAccountType = "logzio_subaccount"
resourceMetricsAccountType = "logzio_metrics_account"
resourceLogShippingTokenType = "logzio_log_shipping_token"
resourceDropFilterType = "logzio_drop_filter"
resourceArchiveLogsType = "logzio_archive_logs"
Expand Down Expand Up @@ -58,6 +59,7 @@ func Provider() *schema.Provider {
resourceEndpointType: dataSourceEndpoint(),
resourceUserType: dataSourceUser(),
resourceSubAccountType: dataSourceSubAccount(),
resourceMetricsAccountType: dataSourceMetricsAccount(),
resourceAlertV2Type: dataSourceAlertV2(),
resourceLogShippingTokenType: dataSourceLogShippingToken(),
resourceDropFilterType: dataSourceDropFilter(),
Expand All @@ -73,6 +75,7 @@ func Provider() *schema.Provider {
resourceEndpointType: resourceEndpoint(),
resourceUserType: resourceUser(),
resourceSubAccountType: resourceSubAccount(),
resourceMetricsAccountType: resourceMetricsAccount(),
resourceAlertV2Type: resourceAlertV2(),
resourceLogShippingTokenType: resourceLogShippingToken(),
resourceDropFilterType: resourceDropFilter(),
Expand Down
Loading

0 comments on commit 7e08328

Please sign in to comment.