Skip to content

Commit 2610f7b

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

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

docs/resources/cloud_plugin_installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resource "grafana_cloud_plugin_installation" "test" {
4141

4242
### Optional
4343

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.
44+
- `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`.
4545

4646
### Read-Only
4747

internal/resources/cloud/resource_cloud_plugin_installation.go

Lines changed: 12 additions & 2 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. Defaults to 'latest' and installs the most recent version. Terraform will detect new version as drift for plan/apply.",
4749
Type: schema.TypeString,
4850
Optional: 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)