Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Resources: Use OpenAPI client for tests #1322

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-openapi/strfmt v0.22.0
github.com/grafana/amixr-api-go-client v0.0.11
github.com/grafana/grafana-api-golang-client v0.27.0
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240126175209-247049a95c63
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697
github.com/grafana/machine-learning-go-client v0.5.0
github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ github.com/grafana/amixr-api-go-client v0.0.11 h1:jlE+5t0tRuCtjbpM81j70Dr2J4eCyS
github.com/grafana/amixr-api-go-client v0.0.11/go.mod h1:N6x26XUrM5zGtK5zL5vNJnAn2JFMxLFPPLTw/6pDkFE=
github.com/grafana/grafana-api-golang-client v0.27.0 h1:zIwMXcbCB4n588i3O2N6HfNcQogCNTd/vPkEXTr7zX8=
github.com/grafana/grafana-api-golang-client v0.27.0/go.mod h1:uNLZEmgKtTjHBtCQMwNn3qsx2mpMb8zU+7T4Xv3NR9Y=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240126175209-247049a95c63 h1:Hvxkgh0MDYUlnyYOMai4kVbjRH6EuzctE1I5AFJRtOI=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240126175209-247049a95c63/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa h1:mip69FpVOw1HM3GMA5DlwjSFmV22n1qrksgG/tt02BU=
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240131153028-3f1ec24f35fa/go.mod h1:6sYY1qgwYfSDNQhKQA0tar8Oc38cIGfyqwejhxoOsPs=
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697 h1:8io6OuKJrhH8SEfwXV1ZpAwgaGhJ25/pjaA3vYmMxxE=
github.com/grafana/grafana-openapi-client-go v0.0.0-20240126032018-bd23c00af697/go.mod h1:af7rlJw/VtbvAfI5VWzYO4p/pT58FXrN6XqZBnkwBxo=
github.com/grafana/machine-learning-go-client v0.5.0 h1:Q1K+MPSy8vfMm2jsk3WQ7O77cGr2fM5hxwtPSoPc5NU=
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/cloud/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)

func clientRequestID() string {
func ClientRequestID() string {
uuid, err := uuid.GenerateUUID()
if err != nil {
return ""
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/cloud/data_source_cloud_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
Expand All @@ -15,7 +15,7 @@ func TestAccDataSourceStack_Basic(t *testing.T) {
prefix := "tfdatatest"

resourceName := GetRandomStackName(prefix)
var stack gapi.Stack
var stack gcom.FormattedApiInstance
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccDeleteExistingStacks(t, prefix)
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/cloud/resource_cloud_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ResourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte

resp, _, err := c.OrgsAPI.PostApiKeys(ctx, org).
PostApiKeysRequest(req).
XRequestId(clientRequestID()).
XRequestId(ClientRequestID()).
Execute()
if err != nil {
return apiError(err)
Expand Down Expand Up @@ -103,7 +103,7 @@ func ResourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interf
func ResourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).GrafanaCloudAPIOpenAPI

_, err := c.OrgsAPI.DelApiKey(ctx, d.Get("name").(string), d.Get("cloud_org_slug").(string)).XRequestId(clientRequestID()).Execute()
_, err := c.OrgsAPI.DelApiKey(ctx, d.Get("name").(string), d.Get("cloud_org_slug").(string)).XRequestId(ClientRequestID()).Execute()
d.SetId("")
return apiError(err)
}
56 changes: 18 additions & 38 deletions internal/resources/cloud/resource_cloud_api_key_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cloud_test

import (
"context"
"fmt"
"os"
"strings"
"testing"

"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/grafana/terraform-provider-grafana/internal/resources/cloud"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -35,12 +37,12 @@ func TestAccCloudApiKey_Basic(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccCheckCloudAPIKeyDestroy,
CheckDestroy: testAccCheckCloudAPIKeyDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccCloudAPIKeyConfig(resourceName, tt.role),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudAPIKeyExists("grafana_cloud_api_key.test"),
testAccCheckCloudAPIKeyExists(resourceName),
resource.TestCheckResourceAttrSet("grafana_cloud_api_key.test", "id"),
resource.TestCheckResourceAttrSet("grafana_cloud_api_key.test", "key"),
resource.TestCheckResourceAttr("grafana_cloud_api_key.test", "name", resourceName),
Expand All @@ -59,67 +61,45 @@ func TestAccCloudApiKey_Basic(t *testing.T) {
}
}

func testAccCheckCloudAPIKeyExists(resourceName string) resource.TestCheckFunc {
func testAccCheckCloudAPIKeyExists(apiKeyName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource `%s` not found", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource `%s` has no ID set", resourceName)
}

client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPI
res, err := client.ListCloudAPIKeys(rs.Primary.Attributes["cloud_org_slug"])
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPIOpenAPI
res, _, err := client.OrgsAPI.GetApiKeys(context.Background(), os.Getenv("GRAFANA_CLOUD_ORG")).Execute()
if err != nil {
return err
}

for _, apiKey := range res.Items {
if apiKey.Name == rs.Primary.Attributes["name"] {
if apiKey.Name == apiKeyName {
return nil
}
}

return fmt.Errorf("resource `%s` not found via API", resourceName)
return fmt.Errorf("API Key `%s` not found via API", apiKeyName)
}
}

func testAccCheckCloudAPIKeyDestroy(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPI

for name, rs := range s.RootModule().Resources {
if rs.Type != "grafana_cloud_api_key" {
continue
}

res, err := client.ListCloudAPIKeys(rs.Primary.Attributes["cloud_org_slug"])
if err != nil {
return err
}

for _, apiKey := range res.Items {
if apiKey.Name == rs.Primary.Attributes["name"] {
return fmt.Errorf("resource `%s` still exists via API", name)
}
func testAccCheckCloudAPIKeyDestroy(apiKeyName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
getErr := testAccCheckCloudAPIKeyExists(apiKeyName)(s)
if getErr == nil {
return fmt.Errorf("API Key `%s` still exists via API", apiKeyName)
}
return nil
}

return nil
}

func testAccDeleteExistingCloudAPIKeys(t *testing.T, prefix string) {
org := os.Getenv("GRAFANA_CLOUD_ORG")
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPI
resp, err := client.ListCloudAPIKeys(org)
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPIOpenAPI
resp, _, err := client.OrgsAPI.GetApiKeys(context.Background(), org).Execute()
if err != nil {
t.Error(err)
}

for _, key := range resp.Items {
if strings.HasPrefix(key.Name, prefix) {
err := client.DeleteCloudAPIKey(org, key.Name)
_, err := client.OrgsAPI.DelApiKey(context.Background(), org, key.Name).XRequestId(cloud.ClientRequestID()).Execute()
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/cloud/resource_cloud_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func ResourcePluginInstallationCreate(ctx context.Context, d *schema.ResourceDat
}
_, _, err := client.InstancesAPI.PostInstancePlugins(ctx, stackSlug).
PostInstancePluginsRequest(req).
XRequestId(clientRequestID()).Execute()
XRequestId(ClientRequestID()).Execute()
if err != nil {
return apiError(err)
}
Expand Down Expand Up @@ -93,6 +93,6 @@ func ResourcePluginInstallationDelete(ctx context.Context, d *schema.ResourceDat
splitID := strings.SplitN(d.Id(), "_", 2)
stackSlug, pluginSlug := splitID[0], splitID[1]

_, _, err := client.InstancesAPI.DeleteInstancePlugin(ctx, stackSlug, pluginSlug).XRequestId(clientRequestID()).Execute()
_, _, err := client.InstancesAPI.DeleteInstancePlugin(ctx, stackSlug, pluginSlug).XRequestId(ClientRequestID()).Execute()
return apiError(err)
}
9 changes: 5 additions & 4 deletions internal/resources/cloud/resource_cloud_plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cloud_test

import (
"context"
"fmt"
"testing"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-com-public-clients/go/gcom"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -14,7 +15,7 @@ import (
func TestAccResourcePluginInstallation(t *testing.T) {
testutils.CheckCloudAPITestsEnabled(t)

var stack gapi.Stack
var stack gcom.FormattedApiInstance
stackPrefix := "tfplugin"
stackSlug := GetRandomStackName(stackPrefix)
pluginSlug := "aws-datasource-provisioner-app"
Expand Down Expand Up @@ -54,8 +55,8 @@ func TestAccResourcePluginInstallation(t *testing.T) {

func testAccCloudPluginInstallationCheckExists(stackSlug string, pluginSlug string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPI
_, err := client.GetCloudPluginInstallation(stackSlug, pluginSlug)
client := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPIOpenAPI
_, _, err := client.InstancesAPI.GetInstancePlugin(context.Background(), stackSlug, pluginSlug).Execute()
if err != nil {
return fmt.Errorf("error getting installation: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func CreateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
}

err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
req := client.InstancesAPI.PostInstances(ctx).PostInstancesRequest(stack).XRequestId(clientRequestID())
req := client.InstancesAPI.PostInstances(ctx).PostInstancesRequest(stack).XRequestId(ClientRequestID())
createdStack, _, err := req.Execute()
switch {
case err != nil && strings.Contains(strings.ToLower(err.Error()), "conflict"):
Expand Down Expand Up @@ -315,7 +315,7 @@ func UpdateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
Description: common.Ref(d.Get("description").(string)),
Url: common.Ref(d.Get("url").(string)),
}
req := client.InstancesAPI.PostInstance(ctx, d.Id()).PostInstanceRequest(stack).XRequestId(clientRequestID())
req := client.InstancesAPI.PostInstance(ctx, d.Id()).PostInstanceRequest(stack).XRequestId(ClientRequestID())
_, _, err := req.Execute()
if err != nil {
return apiError(err)
Expand All @@ -331,7 +331,7 @@ func UpdateStack(ctx context.Context, d *schema.ResourceData, meta interface{})

func DeleteStack(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaCloudAPIOpenAPI
req := client.InstancesAPI.DeleteInstance(ctx, d.Id()).XRequestId(clientRequestID())
req := client.InstancesAPI.DeleteInstance(ctx, d.Id()).XRequestId(ClientRequestID())
_, _, err := req.Execute()
return apiError(err)
}
Expand Down
Loading
Loading