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
130 changes: 38 additions & 92 deletions internal/services/cockpit/cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,54 @@ func ResourceCockpit() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: "free",
Description: "Name or ID of the plan",
Description: "[DEPRECATED] The plan field is deprecated. Any modification or selection will have no effect.",
Deprecated: "The 'plan' attribute is deprecated and no longer has any effect. Future updates will remove this attribute entirely.",
},
"plan_id": {
Type: schema.TypeString,
Computed: true,
Description: "The plan ID of the cockpit",
Deprecated: "Please use Name only",
Description: "[DEPRECATED] The plan ID of the cockpit. This field is no longer relevant.",
Deprecated: "The 'plan_id' attribute is deprecated and will be removed in a future release.",
},
"endpoints": {
Type: schema.TypeList,
Computed: true,
Description: "Endpoints",
Deprecated: "Please use `scaleway_cockpit_source` instead",
Description: "[DEPRECATED] Endpoints list. Please use 'scaleway_cockpit_source' instead.",
Deprecated: "Use 'scaleway_cockpit_source' instead of 'endpoints'. This field will be removed in future releases.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metrics_url": {
Type: schema.TypeString,
Computed: true,
Description: "The metrics URL",
Description: "The metrics URL.",
},
"logs_url": {
Type: schema.TypeString,
Computed: true,
Description: "The logs URL",
Description: "The logs URL.",
},
"alertmanager_url": {
Type: schema.TypeString,
Computed: true,
Description: "The alertmanager URL",
Description: "The alertmanager URL.",
},
"grafana_url": {
Type: schema.TypeString,
Computed: true,
Description: "The grafana URL",
Description: "The grafana URL.",
},
"traces_url": {
Type: schema.TypeString,
Computed: true,
Description: "The traces URL",
Description: "The traces URL.",
},
},
},
},
"push_url": {
Type: schema.TypeList,
Computed: true,
Description: "Push_url",
Description: "[DEPRECATED] Push_url",
Deprecated: "Please use `scaleway_cockpit_source` instead",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -93,48 +94,22 @@ func ResourceCockpit() *schema.Resource {
}

func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, err := NewGlobalAPI(m)
if err != nil {
return diag.FromErr(err)
}

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

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

plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages()) //nolint:staticcheck
if err != nil {
return diag.FromErr(err)
}

var planName string

for _, plan := range plans.Plans {
if plan.Name.String() == targetPlan {
planName = plan.Name.String()

break
}
}

if planName == "" {
return diag.Errorf("plan %s not found", targetPlan)
}

_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{ //nolint:staticcheck
ProjectID: projectID,
PlanName: cockpit.PlanName(planName),
}, scw.WithContext(ctx))
if projectID == "" {
_, err := getDefaultProjectID(ctx, m)
if err != nil {
return diag.FromErr(err)
}
}

d.SetId(projectID)

return ResourceCockpitRead(ctx, d, m)
}

func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

api, err := NewGlobalAPI(m)
if err != nil {
return diag.FromErr(err)
Expand All @@ -153,15 +128,14 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
}
}

res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{ //nolint:staticcheck
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Deprecated attribute: 'plan'",
Detail: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
})

_ = d.Set("plan", res.Name.String())
_ = d.Set("plan_id", res.Name.String())
_ = d.Set("plan", d.Get("plan"))
_ = d.Set("plan_id", "")

dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
Region: region,
Expand Down Expand Up @@ -203,54 +177,26 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
_ = d.Set("endpoints", endpoints)
_ = d.Set("push_url", createCockpitPushURLList(endpoints))

return nil
return diags
}

func ResourceCockpitUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, err := NewGlobalAPI(m)
if err != nil {
return diag.FromErr(err)
}

projectID := d.Id()

diags := diag.Diagnostics{}
if d.HasChange("plan") {
targetPlan := cockpit.PlanNameFree.String()
if targetPlanI, ok := d.GetOk("plan"); ok {
targetPlan = targetPlanI.(string)
}

plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages()) //nolint:staticcheck
if err != nil {
return diag.FromErr(err)
}

var planName string

for _, plan := range plans.Plans {
if plan.Name.String() == targetPlan {
planName = plan.Name.String()

break
}
}

if planName == "" {
return diag.Errorf("plan %s not found", targetPlan)
}

_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{ //nolint:staticcheck
ProjectID: projectID,
PlanName: cockpit.PlanName(planName),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Deprecated attribute update: 'plan'",
Detail: "Updating 'plan' has no effect as it is deprecated and will be removed in a future version.",
})
}

return ResourceCockpitRead(ctx, d, m)
diags = append(diags, ResourceCockpitRead(ctx, d, m)...)

return diags
}

func ResourceCockpitDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
func ResourceCockpitDelete(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("")

return nil
}
79 changes: 11 additions & 68 deletions internal/services/cockpit/cockpit_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func DataSourceCockpit() *schema.Resource {
// Generate datasource schema from resource
dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpit().Schema)

dsSchema["project_id"] = &schema.Schema{
Type: schema.TypeString,
Description: "The project_id you want to attach the resource to",
Expand All @@ -25,7 +20,8 @@ func DataSourceCockpit() *schema.Resource {
dsSchema["plan"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The current plan of the cockpit project",
Description: "[DEPRECATED] The current plan of the cockpit project.",
Deprecated: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
}

return &schema.Resource{
Expand All @@ -36,78 +32,25 @@ func DataSourceCockpit() *schema.Resource {
}

func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, err := NewGlobalAPI(m)
if err != nil {
return diag.FromErr(err)
}

regionalAPI, region, err := cockpitAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}

projectID := d.Get("project_id").(string)
if projectID == "" {
projectID, err = getDefaultProjectID(ctx, m)
_, err := getDefaultProjectID(ctx, m)
if err != nil {
return diag.FromErr(err)
}
}

res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{ //nolint:staticcheck
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

_ = d.Set("project_id", d.Get("project_id").(string))
_ = d.Set("plan", res.Name)
_ = d.Set("plan_id", res.Name)
diags := diag.Diagnostics{}

dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
Region: region,
ProjectID: projectID,
Origin: "external",
}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return diag.FromErr(err)
}

grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

if grafana.GrafanaURL == "" {
grafana.GrafanaURL = createGrafanaURL(projectID, region)
}

alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
ProjectID: projectID,
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "Deprecated attribute: 'plan'",
Detail: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
})
if err != nil {
return diag.FromErr(err)
}

alertManagerURL := ""
if alertManager.AlertManagerURL != nil {
alertManagerURL = *alertManager.AlertManagerURL
}

endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)

_ = d.Set("endpoints", endpoints)
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
_ = d.Set("plan", d.Get("plan"))
_ = d.Set("project_id", projectID)
d.SetId(projectID)

return nil
return diags
}
1 change: 0 additions & 1 deletion internal/services/cockpit/cockpit_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestAccDataSourceCockpit_Basic(t *testing.T) {
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan_id", "free"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.logs_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.alertmanager_url"),
Expand Down
3 changes: 0 additions & 3 deletions internal/services/cockpit/cockpit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestAccCockpit_Simple(t *testing.T) {
`,
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"),
),
Expand Down Expand Up @@ -57,7 +56,6 @@ func TestAccCockpit_Basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("scaleway_cockpit.main", "project_id", "scaleway_account_project.project", "id"),
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"),
checkGrafanaURL("scaleway_cockpit.main", "scaleway_account_project.project"),
Expand Down Expand Up @@ -127,7 +125,6 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "premium"),
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan_id", "premium"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.logs_url"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.alertmanager_url"),
Expand Down
Loading
Loading