Skip to content

Commit

Permalink
fix(cockpit): add default project id in cockpit resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed Sep 18, 2024
1 parent f6912dd commit 4e17cfa
Show file tree
Hide file tree
Showing 3 changed files with 525 additions and 3 deletions.
8 changes: 5 additions & 3 deletions internal/services/cockpit/cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func ResourceCockpit() *schema.Resource {
"plan": {
Type: schema.TypeString,
Optional: true,
Default: "free",
Description: "Name or ID of the plan",
},
"plan_id": {
Expand Down Expand Up @@ -98,6 +99,7 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf
}

projectID := d.Get("project_id").(string)

if targetPlanI, ok := d.GetOk("plan"); ok {
targetPlan := targetPlanI.(string)

Expand Down Expand Up @@ -127,7 +129,6 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf
}
}

d.SetId(projectID)
return ResourceCockpitRead(ctx, d, m)
}

Expand All @@ -148,7 +149,6 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
if err != nil {
return diag.FromErr(err)
}
_ = d.Set("project_id", d.Get("project_id").(string))
_ = d.Set("plan", res.Name.String())
_ = d.Set("plan_id", res.Name.String())

Expand All @@ -160,6 +160,8 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
if err != nil {
return diag.FromErr(err)
}
_ = d.Set("project_id", dataSourcesRes.DataSources[0].ProjectID)
d.SetId(dataSourcesRes.DataSources[0].ProjectID)

grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
ProjectID: d.Get("project_id").(string),
Expand All @@ -168,7 +170,7 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
return diag.FromErr(err)
}
if grafana.GrafanaURL == "" {
grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region)
grafana.GrafanaURL = createGrafanaURL(dataSourcesRes.DataSources[0].ProjectID, region)
}

alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
Expand Down
25 changes: 25 additions & 0 deletions internal/services/cockpit/cockpit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
)

func TestAccCockpit_Simple(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isCockpitDestroyed(tt),
Steps: []resource.TestStep{
{
Config: `
resource scaleway_cockpit main {
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
),
},
},
})
}

func TestAccCockpit_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
Expand Down
Loading

0 comments on commit 4e17cfa

Please sign in to comment.