-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update version to add metrics accounts support (#177)
* implement metrics accounts
- Loading branch information
Showing
8 changed files
with
742 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.