Skip to content

Commit

Permalink
Add support for "terraform import"
Browse files Browse the repository at this point in the history
  • Loading branch information
SchoolGuy committed Oct 25, 2024
1 parent 9a73f02 commit ad6158f
Show file tree
Hide file tree
Showing 24 changed files with 182 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cobbler/resource_cobbler_distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions cobbler/resource_cobbler_distro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func TestAccCobblerDistro_basic(t *testing.T) {
testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro),
),
},
{
ResourceName: "cobbler_distro.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -40,6 +45,11 @@ func TestAccCobblerDistro_basic_inherit(t *testing.T) {
testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro),
),
},
{
ResourceName: "cobbler_distro.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -64,6 +74,11 @@ func TestAccCobblerDistro_basic_inheritConcrete(t *testing.T) {
testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro),
),
},
{
ResourceName: "cobbler_distro.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -88,6 +103,11 @@ func TestAccCobblerDistro_change(t *testing.T) {
testAccCobblerCheckDistroExists("cobbler_distro.foo", &distro),
),
},
{
ResourceName: "cobbler_distro.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
3 changes: 3 additions & 0 deletions cobbler/resource_cobbler_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 15 additions & 0 deletions cobbler/resource_cobbler_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func TestAccCobblerProfile_basic(t *testing.T) {
testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile),
),
},
{
ResourceName: "cobbler_profile.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -51,6 +56,11 @@ func TestAccCobblerProfile_change(t *testing.T) {
testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile),
),
},
{
ResourceName: "cobbler_profile.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -71,6 +81,11 @@ func TestAccCobblerProfile_withRepo(t *testing.T) {
testAccCobblerCheckProfileExists("cobbler_profile.foo", &profile),
),
},
{
ResourceName: "cobbler_profile.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
3 changes: 3 additions & 0 deletions cobbler/resource_cobbler_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 10 additions & 0 deletions cobbler/resource_cobbler_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func TestAccCobblerRepo_basic(t *testing.T) {
testAccCobblerCheckRepoExists("cobbler_repo.foo", &repo),
),
},
{
ResourceName: "cobbler_repo.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -48,6 +53,11 @@ func TestAccCobblerRepo_change(t *testing.T) {
testAccCobblerCheckRepoExists("cobbler_repo.foo", &repo),
),
},
{
ResourceName: "cobbler_repo.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
19 changes: 17 additions & 2 deletions cobbler/resource_cobbler_snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions cobbler/resource_cobbler_snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func TestAccCobblerSnippet_basic(t *testing.T) {
testAccCobblerCheckSnippetExists("cobbler_snippet.foo", &snippet),
),
},
{
ResourceName: "cobbler_snippet.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
3 changes: 3 additions & 0 deletions cobbler/resource_cobbler_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 23 additions & 0 deletions cobbler/resource_cobbler_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func TestAccCobblerSystem_basic(t *testing.T) {
testAccCobblerCheckSystemExists("cobbler_system.foo", &system),
),
},
{
ResourceName: "cobbler_system.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -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"},
},
},
})
}
Expand Down Expand Up @@ -80,6 +92,11 @@ func TestAccCobblerSystem_change(t *testing.T) {
testAccCobblerCheckSystemExists("cobbler_system.foo", &system),
),
},
{
ResourceName: "cobbler_system.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -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"},
},
},
})
}
Expand Down
18 changes: 17 additions & 1 deletion cobbler/resource_cobbler_template_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions cobbler/resource_cobbler_template_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions docs/resources/distro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/resources/profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/resources/repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/resources/snippet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/resources/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
8 changes: 8 additions & 0 deletions docs/resources/template_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
1 change: 1 addition & 0 deletions examples/resources/cobbler_distro/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_distro.foo foo
1 change: 1 addition & 0 deletions examples/resources/cobbler_profile/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_profile.foo foo
1 change: 1 addition & 0 deletions examples/resources/cobbler_repo/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_repo.foo foo
1 change: 1 addition & 0 deletions examples/resources/cobbler_snippet/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_snippet.foo foo
1 change: 1 addition & 0 deletions examples/resources/cobbler_system/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_system.foo foo
1 change: 1 addition & 0 deletions examples/resources/cobbler_template_file/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import cobbler_template_file.foo foo

0 comments on commit ad6158f

Please sign in to comment.