From 0fc2a2846954651069f2eef9317d07187c488b7b Mon Sep 17 00:00:00 2001 From: GokceGK <161626272+GokceGK@users.noreply.github.com> Date: Mon, 25 Mar 2024 13:23:06 +0100 Subject: [PATCH] Deprecate ske project resource (#309) * add deprecation message * edit acceptance test * Enable project before creating first cluster // fix typo in function name * add cluster existence check before deleting ske project // add deprecation message to project resource * add region to acceptance test * Update resource error message Co-authored-by: Vicente Pinto * Update resource error message Co-authored-by: Vicente Pinto * Update deprecation message Co-authored-by: Vicente Pinto * Update error message Co-authored-by: Vicente Pinto * remove model id assignment * update error handling for cluster existence check while deleting project * add nil check for clusters during project deletion * add deprecation notice to documentation * Update datasource deprecation message Co-authored-by: Vicente Pinto * improve condition handling --------- Co-authored-by: Vicente Pinto --- docs/data-sources/ske_project.md | 4 ++-- docs/resources/ske_project.md | 4 ++-- .../internal/services/ske/cluster/resource.go | 19 ++++++++++++++++--- .../services/ske/project/datasource.go | 3 ++- .../internal/services/ske/project/resource.go | 17 +++++++++++++++-- stackit/internal/services/ske/ske_acc_test.go | 8 +++++--- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/docs/data-sources/ske_project.md b/docs/data-sources/ske_project.md index 62b40fdd..01e1100f 100644 --- a/docs/data-sources/ske_project.md +++ b/docs/data-sources/ske_project.md @@ -3,12 +3,12 @@ page_title: "stackit_ske_project Data Source - stackit" subcategory: "" description: |- - SKE project data source schema. Must have a region specified in the provider configuration. + SKE project data source schema. Must have a region specified in the provider configuration. Warning: SKE project resource is no longer in use and will be removed with the next release. SKE service enablement is done automatically when a new cluster is created. --- # stackit_ske_project (Data Source) -SKE project data source schema. Must have a `region` specified in the provider configuration. +SKE project data source schema. Must have a `region` specified in the provider configuration. Warning: SKE project resource is no longer in use and will be removed with the next release. SKE service enablement is done automatically when a new cluster is created. ## Example Usage diff --git a/docs/resources/ske_project.md b/docs/resources/ske_project.md index b73121f0..6ad5edc5 100644 --- a/docs/resources/ske_project.md +++ b/docs/resources/ske_project.md @@ -3,12 +3,12 @@ page_title: "stackit_ske_project Resource - stackit" subcategory: "" description: |- - SKE project resource schema. Must have a region specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Deleting this resource will destroy any SKE clusters associated to the project + SKE project resource schema. Must have a region specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Before deleting this resource, all SKE clusters associated to the project must be deleted. Warning: SKE project resource is no longer in use and will be removed with the next minor release. SKE service enablement is done automatically when a new cluster is created. --- # stackit_ske_project (Resource) -SKE project resource schema. Must have a `region` specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Deleting this resource will destroy any SKE clusters associated to the project +SKE project resource schema. Must have a `region` specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Before deleting this resource, all SKE clusters associated to the project must be deleted. Warning: SKE project resource is no longer in use and will be removed with the next minor release. SKE service enablement is done automatically when a new cluster is created. ## Example Usage diff --git a/stackit/internal/services/ske/cluster/resource.go b/stackit/internal/services/ske/cluster/resource.go index 18af6aaa..ddd24839 100644 --- a/stackit/internal/services/ske/cluster/resource.go +++ b/stackit/internal/services/ske/cluster/resource.go @@ -552,7 +552,20 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "name", clusterName) - availableVersions, err := r.loadAvaiableVersions(ctx) + // If SKE functionality is not enabled, enable it + _, err := r.client.EnableService(ctx, projectId).Execute() + if err != nil { + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating cluster", fmt.Sprintf("Calling API to enable SKE: %v", err)) + return + } + + _, err = wait.EnableServiceWaitHandler(ctx, r.client, projectId).WaitWithContext(ctx) + if err != nil { + core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating cluster", fmt.Sprintf("Wait for SKE enablement: %v", err)) + return + } + + availableVersions, err := r.loadAvailableVersions(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating cluster", fmt.Sprintf("Loading available Kubernetes versions: %v", err)) return @@ -572,7 +585,7 @@ func (r *clusterResource) Create(ctx context.Context, req resource.CreateRequest tflog.Info(ctx, "SKE cluster created") } -func (r *clusterResource) loadAvaiableVersions(ctx context.Context) ([]ske.KubernetesVersion, error) { +func (r *clusterResource) loadAvailableVersions(ctx context.Context) ([]ske.KubernetesVersion, error) { c := r.client res, err := c.ListProviderOptions(ctx).Execute() if err != nil { @@ -1423,7 +1436,7 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest ctx = tflog.SetField(ctx, "project_id", projectId) ctx = tflog.SetField(ctx, "name", clName) - availableVersions, err := r.loadAvaiableVersions(ctx) + availableVersions, err := r.loadAvailableVersions(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating cluster", fmt.Sprintf("Loading available Kubernetes versions: %v", err)) return diff --git a/stackit/internal/services/ske/project/datasource.go b/stackit/internal/services/ske/project/datasource.go index bc93c20a..6089190b 100644 --- a/stackit/internal/services/ske/project/datasource.go +++ b/stackit/internal/services/ske/project/datasource.go @@ -74,7 +74,8 @@ func (r *projectDataSource) Configure(ctx context.Context, req datasource.Config // Schema defines the schema for the data source. func (r *projectDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ - Description: "SKE project data source schema. Must have a `region` specified in the provider configuration.", + Description: "SKE project data source schema. Must have a `region` specified in the provider configuration. Warning: SKE project resource is no longer in use and will be removed with the next release. SKE service enablement is done automatically when a new cluster is created.", + DeprecationMessage: "SKE project datasource is no longer in use and will be removed with the next minor release. SKE service enablement is done automatically when a new cluster is created.", Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ Description: "Terraform's internal data source. ID. It is structured as \"`project_id`\".", diff --git a/stackit/internal/services/ske/project/resource.go b/stackit/internal/services/ske/project/resource.go index 87fdfa47..9a99adbe 100644 --- a/stackit/internal/services/ske/project/resource.go +++ b/stackit/internal/services/ske/project/resource.go @@ -86,7 +86,8 @@ func (r *projectResource) Configure(ctx context.Context, req resource.ConfigureR // Schema returns the Terraform schema structure func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ - Description: "SKE project resource schema. Must have a `region` specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Deleting this resource will destroy any SKE clusters associated to the project", + Description: "SKE project resource schema. Must have a `region` specified in the provider configuration. This resource allows you to enable the SKE service and you can only have one per project. Before deleting this resource, all SKE clusters associated to the project must be deleted. Warning: SKE project resource is no longer in use and will be removed with the next minor release. SKE service enablement is done automatically when a new cluster is created.", + DeprecationMessage: "SKE project resource is no longer in use and will be removed with the next minor release. SKE service enablement is done automatically when a new cluster is created.", Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ Description: "Terraform's internal resource ID. It is structured as \"`project_id`\".", @@ -182,7 +183,19 @@ func (r *projectResource) Delete(ctx context.Context, req resource.DeleteRequest ctx = tflog.SetField(ctx, "project_id", projectId) c := r.client - _, err := c.DisableService(ctx, projectId).Execute() + + clusters, err := c.ListClusters(ctx, projectId).Execute() + if err != nil { + core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting project", fmt.Sprintf("Calling API to get the list of clusters: %v", err)) + return + } + + if clusters != nil && len(*clusters.Items) > 0 { + core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting project", fmt.Sprintln("You still have clusters in the project. Please delete them before deleting the project.")) + return + } + + _, err = c.DisableService(ctx, projectId).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting project", fmt.Sprintf("Calling API: %v", err)) return diff --git a/stackit/internal/services/ske/ske_acc_test.go b/stackit/internal/services/ske/ske_acc_test.go index 96a84b9e..39241f9f 100644 --- a/stackit/internal/services/ske/ske_acc_test.go +++ b/stackit/internal/services/ske/ske_acc_test.go @@ -32,8 +32,8 @@ var clusterResource = map[string]string{ "nodepool_name": "np-acc-test", "nodepool_name_min": "np-acc-min-test", "nodepool_machine_type": "b1.2", - "nodepool_os_version": "3602.2.2", - "nodepool_os_version_min": "3602.2.2", + "nodepool_os_version": "3760.2.0", + "nodepool_os_version_min": "3760.2.0", "nodepool_os_name": "flatcar", "nodepool_minimum": "2", "nodepool_maximum": "3", @@ -519,7 +519,9 @@ func testAccCheckSKEDestroy(s *terraform.State) error { var client *ske.APIClient var err error if testutil.SKECustomEndpoint == "" { - client, err = ske.NewAPIClient() + client, err = ske.NewAPIClient( + config.WithRegion("eu01"), + ) } else { client, err = ske.NewAPIClient( config.WithEndpoint(testutil.SKECustomEndpoint),