From ad6158f6bea8262508fbcd298c69b92cd37e7173 Mon Sep 17 00:00:00 2001 From: Enno Gotthold Date: Fri, 25 Oct 2024 15:14:25 +0200 Subject: [PATCH] Add support for "terraform import" --- cobbler/resource_cobbler_distro.go | 7 ++++++ cobbler/resource_cobbler_distro_test.go | 20 ++++++++++++++++ cobbler/resource_cobbler_profile.go | 3 +++ cobbler/resource_cobbler_profile_test.go | 15 ++++++++++++ cobbler/resource_cobbler_repo.go | 3 +++ cobbler/resource_cobbler_repo_test.go | 10 ++++++++ cobbler/resource_cobbler_snippet.go | 19 +++++++++++++-- cobbler/resource_cobbler_snippet_test.go | 5 ++++ cobbler/resource_cobbler_system.go | 3 +++ cobbler/resource_cobbler_system_test.go | 23 +++++++++++++++++++ cobbler/resource_cobbler_template_file.go | 18 ++++++++++++++- .../resource_cobbler_template_file_test.go | 5 ++++ docs/resources/distro.md | 8 +++++++ docs/resources/profile.md | 8 +++++++ docs/resources/repo.md | 8 +++++++ docs/resources/snippet.md | 8 +++++++ docs/resources/system.md | 8 +++++++ docs/resources/template_file.md | 8 +++++++ examples/resources/cobbler_distro/import.sh | 1 + examples/resources/cobbler_profile/import.sh | 1 + examples/resources/cobbler_repo/import.sh | 1 + examples/resources/cobbler_snippet/import.sh | 1 + examples/resources/cobbler_system/import.sh | 1 + .../resources/cobbler_template_file/import.sh | 1 + 24 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 examples/resources/cobbler_distro/import.sh create mode 100644 examples/resources/cobbler_profile/import.sh create mode 100644 examples/resources/cobbler_repo/import.sh create mode 100644 examples/resources/cobbler_snippet/import.sh create mode 100644 examples/resources/cobbler_system/import.sh create mode 100644 examples/resources/cobbler_template_file/import.sh diff --git a/cobbler/resource_cobbler_distro.go b/cobbler/resource_cobbler_distro.go index 7b4a036..432a75f 100644 --- a/cobbler/resource_cobbler_distro.go +++ b/cobbler/resource_cobbler_distro.go @@ -16,6 +16,9 @@ func resourceDistro() *schema.Resource { ReadContext: resourceDistroRead, UpdateContext: resourceDistroUpdate, DeleteContext: resourceDistroDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "arch": { @@ -206,6 +209,10 @@ func resourceDistroRead(ctx context.Context, d *schema.ResourceData, meta interf } // Set all fields + err = d.Set("name", distro.Name) + if err != nil { + return diag.FromErr(err) + } err = d.Set("arch", distro.Arch) if err != nil { return diag.FromErr(err) diff --git a/cobbler/resource_cobbler_distro_test.go b/cobbler/resource_cobbler_distro_test.go index bcdbdc6..65be60d 100644 --- a/cobbler/resource_cobbler_distro_test.go +++ b/cobbler/resource_cobbler_distro_test.go @@ -22,6 +22,11 @@ func TestAccCobblerDistro_basic(t *testing.T) { testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro), ), }, + { + ResourceName: "cobbler_distro.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -40,6 +45,11 @@ func TestAccCobblerDistro_basic_inherit(t *testing.T) { testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro), ), }, + { + ResourceName: "cobbler_distro.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -64,6 +74,11 @@ func TestAccCobblerDistro_basic_inheritConcrete(t *testing.T) { testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro), ), }, + { + ResourceName: "cobbler_distro.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -88,6 +103,11 @@ func TestAccCobblerDistro_change(t *testing.T) { testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro), ), }, + { + ResourceName: "cobbler_distro.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/cobbler/resource_cobbler_profile.go b/cobbler/resource_cobbler_profile.go index d2aa640..7c84280 100644 --- a/cobbler/resource_cobbler_profile.go +++ b/cobbler/resource_cobbler_profile.go @@ -16,6 +16,9 @@ func resourceProfile() *schema.Resource { ReadContext: resourceProfileRead, UpdateContext: resourceProfileUpdate, DeleteContext: resourceProfileDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "autoinstall": { diff --git a/cobbler/resource_cobbler_profile_test.go b/cobbler/resource_cobbler_profile_test.go index 9156cfb..e6e0ca9 100644 --- a/cobbler/resource_cobbler_profile_test.go +++ b/cobbler/resource_cobbler_profile_test.go @@ -24,6 +24,11 @@ func TestAccCobblerProfile_basic(t *testing.T) { testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile), ), }, + { + ResourceName: "cobbler_profile.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -51,6 +56,11 @@ func TestAccCobblerProfile_change(t *testing.T) { testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile), ), }, + { + ResourceName: "cobbler_profile.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -71,6 +81,11 @@ func TestAccCobblerProfile_withRepo(t *testing.T) { testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile), ), }, + { + ResourceName: "cobbler_profile.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/cobbler/resource_cobbler_repo.go b/cobbler/resource_cobbler_repo.go index 697e915..190c589 100644 --- a/cobbler/resource_cobbler_repo.go +++ b/cobbler/resource_cobbler_repo.go @@ -16,6 +16,9 @@ func resourceRepo() *schema.Resource { ReadContext: resourceRepoRead, UpdateContext: resourceRepoUpdate, DeleteContext: resourceRepoDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "apt_components": { diff --git a/cobbler/resource_cobbler_repo_test.go b/cobbler/resource_cobbler_repo_test.go index 4c094ab..bc0c3dc 100644 --- a/cobbler/resource_cobbler_repo_test.go +++ b/cobbler/resource_cobbler_repo_test.go @@ -24,6 +24,11 @@ func TestAccCobblerRepo_basic(t *testing.T) { testAccCobblerCheckRepoExists("cobbler_repo.foo", &repo), ), }, + { + ResourceName: "cobbler_repo.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -48,6 +53,11 @@ func TestAccCobblerRepo_change(t *testing.T) { testAccCobblerCheckRepoExists("cobbler_repo.foo", &repo), ), }, + { + ResourceName: "cobbler_repo.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/cobbler/resource_cobbler_snippet.go b/cobbler/resource_cobbler_snippet.go index 91ef6a4..6dd3fc0 100644 --- a/cobbler/resource_cobbler_snippet.go +++ b/cobbler/resource_cobbler_snippet.go @@ -16,6 +16,9 @@ func resourceSnippet() *schema.Resource { ReadContext: resourceSnippetRead, UpdateContext: resourceSnippetUpdate, DeleteContext: resourceSnippetDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "name": { @@ -55,8 +58,20 @@ func resourceSnippetCreate(ctx context.Context, d *schema.ResourceData, meta int } func resourceSnippetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - // Since all attributes are required and not computed, - // there's no reason to read. + config := meta.(*Config) + + snippet, err := config.cobblerClient.GetSnippet(d.Id()) + if err != nil { + return diag.Errorf("Cobbler TemplateFile: Error Reading: %s", err) + } + err = d.Set("name", snippet.Name) + if err != nil { + return diag.FromErr(err) + } + err = d.Set("body", snippet.Body) + if err != nil { + return diag.FromErr(err) + } return nil } diff --git a/cobbler/resource_cobbler_snippet_test.go b/cobbler/resource_cobbler_snippet_test.go index e7ccdd2..4374ea5 100644 --- a/cobbler/resource_cobbler_snippet_test.go +++ b/cobbler/resource_cobbler_snippet_test.go @@ -24,6 +24,11 @@ func TestAccCobblerSnippet_basic(t *testing.T) { testAccCobblerCheckSnippetExists("cobbler_snippet.foo", &snippet), ), }, + { + ResourceName: "cobbler_snippet.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/cobbler/resource_cobbler_system.go b/cobbler/resource_cobbler_system.go index e6ea9cd..6b99ad8 100644 --- a/cobbler/resource_cobbler_system.go +++ b/cobbler/resource_cobbler_system.go @@ -25,6 +25,9 @@ func resourceSystem() *schema.Resource { ReadContext: resourceSystemRead, UpdateContext: resourceSystemUpdate, DeleteContext: resourceSystemDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "autoinstall": { diff --git a/cobbler/resource_cobbler_system_test.go b/cobbler/resource_cobbler_system_test.go index a513108..e6da33c 100644 --- a/cobbler/resource_cobbler_system_test.go +++ b/cobbler/resource_cobbler_system_test.go @@ -28,6 +28,11 @@ func TestAccCobblerSystem_basic(t *testing.T) { testAccCobblerCheckSystemExists("cobbler_system.foo", &system), ), }, + { + ResourceName: "cobbler_system.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -50,6 +55,13 @@ func TestAccCobblerSystem_multi(t *testing.T) { testAccCobblerCheckSystemExists("cobbler_system.foo.45", &system), ), }, + { + ResourceName: "cobbler_system.foo[45]", + ImportState: true, + ImportStateId: "foo-44", + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"interface_type"}, + }, }, }) } @@ -80,6 +92,11 @@ func TestAccCobblerSystem_change(t *testing.T) { testAccCobblerCheckSystemExists("cobbler_system.foo", &system), ), }, + { + ResourceName: "cobbler_system.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } @@ -110,6 +127,12 @@ func TestAccCobblerSystem_removeInterface(t *testing.T) { testAccCobblerCheckSystemExists("cobbler_system.foo", &system), ), }, + { + ResourceName: "cobbler_system.foo", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"interface.0.interface_type", "interface.1.interface_type"}, + }, }, }) } diff --git a/cobbler/resource_cobbler_template_file.go b/cobbler/resource_cobbler_template_file.go index 1215350..ea361d5 100644 --- a/cobbler/resource_cobbler_template_file.go +++ b/cobbler/resource_cobbler_template_file.go @@ -16,6 +16,9 @@ func resourceTemplateFile() *schema.Resource { ReadContext: resourceTemplateFileRead, UpdateContext: resourceTemplateFileUpdate, DeleteContext: resourceTemplateFileDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "name": { @@ -55,7 +58,20 @@ func resourceTemplateFileCreate(ctx context.Context, d *schema.ResourceData, met } func resourceTemplateFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - // Since all attributes are required and not computed, there's no reason to read. + config := meta.(*Config) + + templateFile, err := config.cobblerClient.GetTemplateFile(d.Id()) + if err != nil { + return diag.Errorf("Cobbler TemplateFile: Error Reading: %s", err) + } + err = d.Set("name", templateFile.Name) + if err != nil { + return diag.FromErr(err) + } + err = d.Set("body", templateFile.Body) + if err != nil { + return diag.FromErr(err) + } return nil } diff --git a/cobbler/resource_cobbler_template_file_test.go b/cobbler/resource_cobbler_template_file_test.go index 6bc140b..20b9c63 100644 --- a/cobbler/resource_cobbler_template_file_test.go +++ b/cobbler/resource_cobbler_template_file_test.go @@ -24,6 +24,11 @@ func TestAccCobblerTemplateFile_basic(t *testing.T) { testAccCobblerCheckTemplateFileExists("cobbler_template_file.foo", &ks), ), }, + { + ResourceName: "cobbler_template_file.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) } diff --git a/docs/resources/distro.md b/docs/resources/distro.md index afd49bf..30a7686 100644 --- a/docs/resources/distro.md +++ b/docs/resources/distro.md @@ -58,3 +58,11 @@ resource "cobbler_distro" "Ubuntu-2004-x86_64" { ### Read-Only - `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_distro.foo foo +``` diff --git a/docs/resources/profile.md b/docs/resources/profile.md index 66ec36d..0112bfc 100644 --- a/docs/resources/profile.md +++ b/docs/resources/profile.md @@ -80,3 +80,11 @@ resource "cobbler_profile" "my_profile" { ### Read-Only - `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_profile.foo foo +``` diff --git a/docs/resources/repo.md b/docs/resources/repo.md index 706144c..29bd8ae 100644 --- a/docs/resources/repo.md +++ b/docs/resources/repo.md @@ -52,3 +52,11 @@ resource "cobbler_repo" "my_repo" { ### Read-Only - `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_repo.foo foo +``` diff --git a/docs/resources/snippet.md b/docs/resources/snippet.md index db9b888..a5b4f4b 100644 --- a/docs/resources/snippet.md +++ b/docs/resources/snippet.md @@ -30,3 +30,11 @@ resource "cobbler_snippet" "my_snippet" { ### Read-Only - `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_snippet.foo foo +``` diff --git a/docs/resources/system.md b/docs/resources/system.md index 6228ef7..cc7250f 100644 --- a/docs/resources/system.md +++ b/docs/resources/system.md @@ -134,3 +134,11 @@ Optional: - `static` (Boolean) Whether the interface should be static or DHCP. - `static_routes` (List of String) Static routes for the interface. - `virt_bridge` (String) The virtual bridge to attach to. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_system.foo foo +``` diff --git a/docs/resources/template_file.md b/docs/resources/template_file.md index 1de56c0..f06ff80 100644 --- a/docs/resources/template_file.md +++ b/docs/resources/template_file.md @@ -30,3 +30,11 @@ resource "cobbler_template_file" "my_template" { ### Read-Only - `id` (String) The ID of this resource. + +## Import + +Import is supported using the following syntax: + +```shell +terraform import cobbler_template_file.foo foo +``` diff --git a/examples/resources/cobbler_distro/import.sh b/examples/resources/cobbler_distro/import.sh new file mode 100644 index 0000000..645b82b --- /dev/null +++ b/examples/resources/cobbler_distro/import.sh @@ -0,0 +1 @@ +terraform import cobbler_distro.foo foo \ No newline at end of file diff --git a/examples/resources/cobbler_profile/import.sh b/examples/resources/cobbler_profile/import.sh new file mode 100644 index 0000000..3f4f39f --- /dev/null +++ b/examples/resources/cobbler_profile/import.sh @@ -0,0 +1 @@ +terraform import cobbler_profile.foo foo \ No newline at end of file diff --git a/examples/resources/cobbler_repo/import.sh b/examples/resources/cobbler_repo/import.sh new file mode 100644 index 0000000..004ed70 --- /dev/null +++ b/examples/resources/cobbler_repo/import.sh @@ -0,0 +1 @@ +terraform import cobbler_repo.foo foo \ No newline at end of file diff --git a/examples/resources/cobbler_snippet/import.sh b/examples/resources/cobbler_snippet/import.sh new file mode 100644 index 0000000..b82081a --- /dev/null +++ b/examples/resources/cobbler_snippet/import.sh @@ -0,0 +1 @@ +terraform import cobbler_snippet.foo foo \ No newline at end of file diff --git a/examples/resources/cobbler_system/import.sh b/examples/resources/cobbler_system/import.sh new file mode 100644 index 0000000..57e1a84 --- /dev/null +++ b/examples/resources/cobbler_system/import.sh @@ -0,0 +1 @@ +terraform import cobbler_system.foo foo \ No newline at end of file diff --git a/examples/resources/cobbler_template_file/import.sh b/examples/resources/cobbler_template_file/import.sh new file mode 100644 index 0000000..9db7ca4 --- /dev/null +++ b/examples/resources/cobbler_template_file/import.sh @@ -0,0 +1 @@ +terraform import cobbler_template_file.foo foo \ No newline at end of file