Skip to content
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
5 changes: 4 additions & 1 deletion docs/resources/cloud_plugin_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ resource "grafana_cloud_plugin_installation" "test" {

- `slug` (String) Slug of the plugin to be installed.
- `stack_slug` (String) The stack id to which the plugin should be installed.
- `version` (String) Version of the plugin to be installed.

### Optional

- `version` (String) Version of the plugin to be installed. Defaults to 'latest' and installs the most recent version. Terraform will detect new version as drift for plan/apply. Defaults to `latest`.

### Read-Only

Expand Down
26 changes: 22 additions & 4 deletions internal/resources/cloud/resource_cloud_plugin_installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var (
)
)

const LatestVersion = "latest"

func resourcePluginInstallation() *common.Resource {
schema := &schema.Resource{
Description: `
Expand Down Expand Up @@ -46,9 +48,10 @@ Required access policy scopes:
ForceNew: true,
},
"version": {
Description: "Version of the plugin to be installed.",
Description: "Version of the plugin to be installed. Defaults to 'latest' and installs the most recent version. Terraform will detect new version as drift for plan/apply.",
Type: schema.TypeString,
Required: true,
Optional: true,
Default: LatestVersion,
ForceNew: true,
},
},
Expand Down Expand Up @@ -92,10 +95,11 @@ func listStackPlugins(ctx context.Context, client *gcom.APIClient, data *ListerD
func resourcePluginInstallationCreate(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
stackSlug := d.Get("stack_slug").(string)
pluginSlug := d.Get("slug").(string)
version := d.Get("version").(string)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaults to latest


req := gcom.PostInstancePluginsRequest{
Plugin: pluginSlug,
Version: common.Ref(d.Get("version").(string)),
Version: common.Ref(version),
}

err := retry.RetryContext(ctx, 2*time.Minute, func() *retry.RetryError {
Expand Down Expand Up @@ -133,10 +137,24 @@ func resourcePluginInstallationRead(ctx context.Context, d *schema.ResourceData,
if err, shouldReturn := common.CheckReadError("plugin", d, err); shouldReturn {
return err
}
desiredVersion := d.Get("version").(string)
catalogVersion := ""
if desiredVersion == LatestVersion {
catalogPlugin, _, err := client.PluginsAPI.GetPlugin(ctx, pluginSlug.(string)).Execute()
if err, shouldReturn := common.CheckReadError("plugin", d, err); shouldReturn {
return err
}
catalogVersion = catalogPlugin.Version
}

d.Set("stack_slug", installation.InstanceSlug)
d.Set("slug", installation.PluginSlug)
d.Set("version", installation.Version)

if desiredVersion == LatestVersion && installation.Version == catalogVersion {
d.Set("version", LatestVersion)
} else {
d.Set("version", installation.Version)
}
d.SetId(resourcePluginInstallationID.Make(stackSlug, pluginSlug))

return nil
Expand Down
27 changes: 27 additions & 0 deletions internal/resources/cloud/resource_cloud_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ func TestAccResourcePluginInstallation(t *testing.T) {
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation", "slug", "grafana-googlesheets-datasource"),
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation", "version", "1.2.5")),
},
{
Config: testAccGrafanaCloudPluginInstallationLatest(stackSlug, "grafana-clock-panel"),
Check: resource.ComposeTestCheckFunc(
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
testAccCloudPluginInstallationCheckExists(stackSlug, "grafana-clock-panel"),
resource.TestCheckResourceAttrSet("grafana_cloud_plugin_installation.test-installation-no-version", "id"),
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "stack_slug", stackSlug),
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "slug", pluginSlug),
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "version", "latest"),
),
},
{
ResourceName: "grafana_cloud_plugin_installation.test-installation",
ImportState: true,
Expand Down Expand Up @@ -95,6 +106,7 @@ func testAccGrafanaCloudPluginInstallation(stackSlug, name, version string) stri
resource "grafana_cloud_stack" "test" {
name = "%[1]s"
slug = "%[1]s"
delete_protection = false
wait_for_readiness = false
}

Expand All @@ -105,3 +117,18 @@ func testAccGrafanaCloudPluginInstallation(stackSlug, name, version string) stri
}
`, stackSlug, name, version)
}

func testAccGrafanaCloudPluginInstallationLatest(stackSlug, name string) string {
return fmt.Sprintf(`
resource "grafana_cloud_stack" "test" {
name = "%[1]s"
slug = "%[1]s"
delete_protection = false
wait_for_readiness = false
}
resource "grafana_cloud_plugin_installation" "test-installation-no-version" {
stack_slug = grafana_cloud_stack.test.slug
slug = "%[2]s"
}
`, stackSlug, name)
}
Loading