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

Stack: Remove Computed attribute on url #1387

Merged
merged 1 commit into from
Feb 28, 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
43 changes: 25 additions & 18 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ available at “https://<stack_slug>.grafana.net".`,
},
"url": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Custom URL for the Grafana instance. Must have a CNAME setup to point to `.grafana.net` before creating the stack",
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
return oldValue == newValue ||
// No diff if we're using the default URL
(oldValue == defaultStackURL(d.Get("slug").(string)) && newValue == "")
},
},
"wait_for_readiness": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -223,7 +227,7 @@ func CreateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
return nil
}
time.Sleep(10 * time.Second) // Do not retry too fast, default is 500ms
return retry.RetryableError(fmt.Errorf("failed to create stack: %v", err))
return retry.RetryableError(fmt.Errorf("failed to create stack: %w", err))
default:
d.SetId(strconv.FormatInt(int64(createdStack.Id), 10))
}
Expand All @@ -243,24 +247,23 @@ func CreateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
func UpdateStack(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaCloudAPI

allowedChanges := []string{"name", "description", "slug", "url", "labels"}
if d.HasChangesExcept(allowedChanges...) {
return diag.Errorf("Error: Only %s and description can be updated.", strings.Join(allowedChanges, ", "))
// Default to the slug if the URL is not set
url := d.Get("url").(string)
if url == "" {
url = defaultStackURL(d.Get("slug").(string))
}

if d.HasChange("name") || d.HasChange("description") || d.HasChanges("slug") || d.HasChanges("url") {
stack := gcom.PostInstanceRequest{
Name: common.Ref(d.Get("name").(string)),
Slug: common.Ref(d.Get("slug").(string)),
Description: common.Ref(d.Get("description").(string)),
Url: common.Ref(d.Get("url").(string)),
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
}
req := client.InstancesAPI.PostInstance(ctx, d.Id()).PostInstanceRequest(stack).XRequestId(ClientRequestID())
_, _, err := req.Execute()
if err != nil {
return apiError(err)
}
stack := gcom.PostInstanceRequest{
Name: common.Ref(d.Get("name").(string)),
Slug: common.Ref(d.Get("slug").(string)),
Description: common.Ref(d.Get("description").(string)),
Url: &url,
Labels: common.Ref(common.UnpackMap[string](d.Get("labels"))),
}
req := client.InstancesAPI.PostInstance(ctx, d.Id()).PostInstanceRequest(stack).XRequestId(ClientRequestID())
_, _, err := req.Execute()
if err != nil {
return apiError(err)
}

if diag := ReadStack(ctx, d, meta); diag != nil {
Expand Down Expand Up @@ -425,3 +428,7 @@ func waitForStackReadiness(ctx context.Context, d *schema.ResourceData) diag.Dia

return nil
}

func defaultStackURL(slug string) string {
return fmt.Sprintf("https://%s.grafana.net", slug)
}
1 change: 1 addition & 0 deletions internal/resources/cloud/resource_cloud_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestResourceStack_Basic(t *testing.T) {
resource.TestMatchResourceAttr("grafana_cloud_stack.test", "id", common.IDRegexp),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "name", resourceName),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "slug", resourceName),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "url", "https://"+resourceName+".grafana.net"),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "description", stackDescription),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "status", "active"),
resource.TestCheckResourceAttr("grafana_cloud_stack.test", "labels.tf", "true"),
Expand Down
Loading