Skip to content

Commit 5e985a7

Browse files
committed
ref: check plugin catalog latest version in read
1 parent 14b8761 commit 5e985a7

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

docs/resources/cloud_plugin_installation.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ resource "grafana_cloud_plugin_installation" "test" {
3838

3939
- `slug` (String) Slug of the plugin to be installed.
4040
- `stack_slug` (String) The stack id to which the plugin should be installed.
41-
42-
### Optional
43-
44-
- `version` (String) Version of the plugin to be installed. When omitted, installs the latest available version at the time of creation. Will not auto-update to newer versions. If you already have a plugin installed and want to upgrade, specify the target version explicitly.
41+
- `version` (String) Version of the plugin to be installed. Use 'latest' to install the most recent version. When set to 'latest', Terraform will detect when new version is available and show them as updates. Defaults to `latest`.
4542

4643
### Read-Only
4744

internal/resources/cloud/resource_cloud_plugin_installation.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var (
1616
)
1717
)
1818

19+
const LATEST_VERSION = "latest"
20+
1921
func resourcePluginInstallation() *common.Resource {
2022
schema := &schema.Resource{
2123
Description: `
@@ -43,9 +45,10 @@ Required access policy scopes:
4345
ForceNew: true,
4446
},
4547
"version": {
46-
Description: "Version of the plugin to be installed. When omitted, installs the latest available version at the time of creation. Will not auto-update to newer versions. If you already have a plugin installed and want to upgrade, specify the target version explicitly.",
48+
Description: "Version of the plugin to be installed. Use 'latest' to install the most recent version. When set to 'latest', Terraform will detect when new version is available and show them as updates.",
4749
Type: schema.TypeString,
48-
Optional: true,
50+
Required: true,
51+
Default: LATEST_VERSION,
4952
ForceNew: true,
5053
},
5154
},
@@ -121,10 +124,17 @@ func resourcePluginInstallationRead(ctx context.Context, d *schema.ResourceData,
121124
if err, shouldReturn := common.CheckReadError("plugin", d, err); shouldReturn {
122125
return err
123126
}
127+
catalogPlugin, _, err := client.PluginsAPI.GetPlugin(ctx, pluginSlug.(string)).Execute()
128+
if err, shouldReturn := common.CheckReadError("plugin", d, err); shouldReturn {
129+
return err
130+
}
124131

125132
d.Set("stack_slug", installation.InstanceSlug)
126133
d.Set("slug", installation.PluginSlug)
127-
if _, ok := d.GetOk("version"); ok {
134+
desiredVersion := d.Get("version").(string)
135+
if desiredVersion == LATEST_VERSION && installation.Version == catalogPlugin.Version {
136+
d.Set("version", LATEST_VERSION)
137+
} else {
128138
d.Set("version", installation.Version)
129139
}
130140
d.SetId(resourcePluginInstallationID.Make(stackSlug, pluginSlug))

internal/resources/cloud/resource_cloud_plugin_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ func TestAccResourcePluginInstallation(t *testing.T) {
3737
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation", "version", "1.2.5")),
3838
},
3939
{
40-
Config: testAccGrafanaCloudPluginInstallationNoVersion(stackSlug, pluginSlug),
40+
Config: testAccGrafanaCloudPluginInstallationLatest(stackSlug, "grafana-clock-panel"),
4141
Check: resource.ComposeTestCheckFunc(
4242
testAccStackCheckExists("grafana_cloud_stack.test", &stack),
43-
testAccCloudPluginInstallationCheckExists(stackSlug, pluginSlug),
43+
testAccCloudPluginInstallationCheckExists(stackSlug, "grafana-clock-panel"),
4444
resource.TestCheckResourceAttrSet("grafana_cloud_plugin_installation.test-installation-no-version", "id"),
4545
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "stack_slug", stackSlug),
4646
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "slug", pluginSlug),
47-
// Don't check version attribute since it's not specified in config
47+
resource.TestCheckResourceAttr("grafana_cloud_plugin_installation.test-installation-no-version", "version", "latest"),
4848
),
4949
},
5050
{
@@ -106,6 +106,7 @@ func testAccGrafanaCloudPluginInstallation(stackSlug, name, version string) stri
106106
resource "grafana_cloud_stack" "test" {
107107
name = "%[1]s"
108108
slug = "%[1]s"
109+
delete_protection = false
109110
wait_for_readiness = false
110111
}
111112
@@ -117,17 +118,17 @@ func testAccGrafanaCloudPluginInstallation(stackSlug, name, version string) stri
117118
`, stackSlug, name, version)
118119
}
119120

120-
func testAccGrafanaCloudPluginInstallationNoVersion(stackSlug, name string) string {
121+
func testAccGrafanaCloudPluginInstallationLatest(stackSlug, name string) string {
121122
return fmt.Sprintf(`
122-
resource "grafana_cloud_stack" "test" {
123-
name = "%[1]s"
124-
slug = "%[1]s"
125-
wait_for_readiness = false
126-
}
123+
resource "grafana_cloud_stack" "test" {
124+
name = "%[1]s"
125+
slug = "%[1]s"
126+
delete_protection = false
127+
wait_for_readiness = false
128+
}
127129
resource "grafana_cloud_plugin_installation" "test-installation-no-version" {
128130
stack_slug = grafana_cloud_stack.test.slug
129131
slug = "%[2]s"
130-
# version omitted - should install latest
131132
}
132133
`, stackSlug, name)
133134
}

0 commit comments

Comments
 (0)