Skip to content

Commit

Permalink
feat: add import support for a lot of resources (#390)
Browse files Browse the repository at this point in the history
* add import support for a lot of resources

* fix lints

* set user_id after importing

(cherry picked from commit c3d09ed)

* add tests, fix empty ID

* add import docs

* fix lint
  • Loading branch information
ForsakenHarmony authored Jul 5, 2023
1 parent feac6b0 commit 4147ff6
Show file tree
Hide file tree
Showing 27 changed files with 322 additions and 24 deletions.
12 changes: 12 additions & 0 deletions docs/resources/virtual_environment_cluster_firewall.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ resource "proxmox_virtual_environment_cluster_firewall" "example" {
## Attribute Reference

There are no additional attributes available for this resource.

## Important Notes

Be careful not to use this resource multiple times for the same node.

## Import

Instances can be imported without an ID, but you still need to pass one, e.g.,

```bash
$ terraform import proxmox_virtual_environment_cluster_firewall.example example
```
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ resource "proxmox_virtual_environment_cluster_firewall_security_group" "webserve
- `pos` - Position of the rule in the list.

There are no attribute references available for this resource.

## Import

Instances can be imported using the `name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_cluster_firewall_security_group.webserver webserver
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,11 @@ output "ubuntu_container_public_key" {
## Attribute Reference

There are no additional attributes available for this resource.

## Import

Instances can be imported using the `node_name` and the `vm_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_container.ubuntu_container first-node/1234
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ There are no additional attributes available for this resource.
## Important Notes

Be careful not to use this resource multiple times for the same node.

## Import

Instances can be imported using the `node_name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_dns.first_node first-node
```
10 changes: 9 additions & 1 deletion docs/resources/virtual_environment_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ EOF
- `file_name` - (Optional) The file name to use instead of the source file
name.
- `insecure` - (Optional) Whether to skip the TLS verification step for
HTTPSsources (defaults to `false`).
HTTPS sources (defaults to `false`).
- `path` - (Required) A path to a local file or a URL.
- `source_raw` - (Optional) The raw source (conflicts with `source_file`).
- `data` - (Required) The raw data.
Expand All @@ -93,3 +93,11 @@ file locally before uploading it.
You must ensure that you have at least `Size-in-MB * 2 + 1` MB of storage space
available (twice the size plus overhead because a multipart payload needs to be
created as another temporary file).

## Import

Instances can be imported using the `node_name`, `datastore_id`, `content_type` and the `file_name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_file.cloud_config pve/local/snippets/example.cloud-config.yaml
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ resource "proxmox_virtual_environment_group" "operations_team" {
## Attribute Reference

- `members` - The group members as a list of `username@realm` entries

## Import

Instances can be imported using the `group_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_group.operations_team operations-team
```
12 changes: 12 additions & 0 deletions docs/resources/virtual_environment_hosts.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ resource "proxmox_virtual_environment_hosts" "first_node_host_entries" {
- `entries` - The host entries (conversion of `addresses` and `hostnames` into
objects).
- `hostnames` - The hostnames associated with each of the IP addresses.

## Important Notes

Be careful not to use this resource multiple times for the same node.

## Import

Instances can be imported using the `node_name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_hosts.first_node_host_entries first-node
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ resource "proxmox_virtual_environment_pool" "operations_pool" {
- `node_name` - The node name.
- `type` - The member type.
- `vm_id` - The virtual machine identifier.

## Import

Instances can be imported using the `pool_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_pool.operations_pool operations-pool
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_role.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ resource "proxmox_virtual_environment_role" "operations_monitoring" {
## Attribute Reference

There are no additional attributes available for this resource.

## Import

Instances can be imported using the `role_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_role.operations_monitoring operations-monitoring
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_time.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ resource "proxmox_virtual_environment_time" "first_node_time" {

- `local_time` - The node's local time.
- `utc_time` - The node's local time formatted as UTC.

## Import

Instances can be imported using the `node_name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_dns.first_node first-node
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ resource "proxmox_virtual_environment_role" "operations_monitoring" {
## Attribute Reference

There are no additional attributes available for this resource.

## Import

Instances can be imported using the `user_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_user.operations_automation operations-automation@pve
```
8 changes: 8 additions & 0 deletions docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,11 @@ to force the migration step to migrate all disks to a specific datastore on the
target node. If you need certain disks to be on specific datastores, set
the `datastore_id` argument of the disks in the `disks` block to move the disks
to the correct datastore after the cloning and migrating succeeded.

## Import

Instances can be imported using the `node_name` and the `vm_id`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_vm.ubuntu_vm first-node/4321
```
3 changes: 3 additions & 0 deletions proxmoxtf/resource/cluster/firewall/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func Firewall() *schema.Resource {
ReadContext: selectFirewallAPI(firewallRead),
UpdateContext: selectFirewallAPI(firewallUpdate),
DeleteContext: selectFirewallAPI(firewallDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions proxmoxtf/resource/cluster/firewall/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func SecurityGroup() *schema.Resource {
ReadContext: selectFirewallAPI(SecurityGroupRead),
UpdateContext: selectFirewallAPI(SecurityGroupUpdate),
DeleteContext: selectFirewallAPI(SecurityGroupDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
16 changes: 16 additions & 0 deletions proxmoxtf/resource/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,22 @@ func Container() *schema.Resource {
ReadContext: containerRead,
UpdateContext: containerUpdate,
DeleteContext: containerDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
node, id, err := parseImportIDWithNodeName(d.Id())
if err != nil {
return nil, err
}

d.SetId(id)
err = d.Set(mkResourceVirtualEnvironmentContainerNodeName, node)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

return []*schema.ResourceData{d}, nil
},
},
}
}

Expand Down
14 changes: 14 additions & 0 deletions proxmoxtf/resource/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func DNS() *schema.Resource {
ReadContext: dnsRead,
UpdateContext: dnsUpdate,
DeleteContext: dnsDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
nodeName := d.Id()

err := d.Set(mkResourceVirtualEnvironmentDNSNodeName, nodeName)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

d.SetId(fmt.Sprintf("%s_dns", nodeName))

return []*schema.ResourceData{d}, nil
},
},
}
}

Expand Down
32 changes: 32 additions & 0 deletions proxmoxtf/resource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,39 @@ func File() *schema.Resource {
CreateContext: fileCreate,
ReadContext: fileRead,
DeleteContext: fileDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
node, datastore, volumeID, err := fileParseImportID(d.Id())
if err != nil {
return nil, err
}

d.SetId(volumeID)

err = d.Set(mkResourceVirtualEnvironmentFileNodeName, node)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

err = d.Set(mkResourceVirtualEnvironmentFileDatastoreID, datastore)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

return []*schema.ResourceData{d}, nil
},
},
}
}

func fileParseImportID(id string) (string, string, string, error) {
parts := strings.SplitN(id, "/", 4)

if len(parts) != 4 || parts[0] == "" || parts[1] == "" || parts[2] == "" || parts[3] == "" {
return "", "", "", fmt.Errorf("unexpected format of ID (%s), expected node/datastore_id/content_type/file_name", id)
}

return parts[0], parts[1], strings.Join(parts[2:], "/"), nil
}

func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
39 changes: 39 additions & 0 deletions proxmoxtf/resource/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/require"

"github.com/bpg/terraform-provider-proxmox/proxmoxtf/test"
)
Expand Down Expand Up @@ -98,3 +99,41 @@ func TestFileSchema(t *testing.T) {
mkResourceVirtualEnvironmentFileSourceRawResize: schema.TypeInt,
})
}

func Test_fileParseImportID(t *testing.T) {
t.Parallel()

tests := []struct {
name string
value string
valid bool
expectedNodeName string
expectedDatastoreID string
expectedVolumeID string
}{
{"empty", "", false, "", "", ""},
{"missing slash", "invalid", false, "", "", ""},
{"missing parts", "invalid/invalid/invalid", false, "", "", ""},
{"valid", "node/datastore_id/content_type/file_name", true, "node", "datastore_id", "content_type/file_name"},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
require := require.New(t)

nodeName, datastoreID, volumeID, err := fileParseImportID(tt.value)

if !tt.valid {
require.Error(err)
return
}

require.Nil(err)
require.Equal(tt.expectedNodeName, nodeName)
require.Equal(tt.expectedDatastoreID, datastoreID)
require.Equal(tt.expectedVolumeID, volumeID)
})
}
}
3 changes: 3 additions & 0 deletions proxmoxtf/resource/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func Group() *schema.Resource {
ReadContext: groupRead,
UpdateContext: groupUpdate,
DeleteContext: groupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
14 changes: 14 additions & 0 deletions proxmoxtf/resource/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ func Hosts() *schema.Resource {
ReadContext: hostsRead,
UpdateContext: hostsUpdate,
DeleteContext: hostsDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
nodeName := d.Id()

err := d.Set(mkResourceVirtualEnvironmentHostsNodeName, nodeName)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

d.SetId(fmt.Sprintf("%s_hosts", nodeName))

return []*schema.ResourceData{d}, nil
},
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions proxmoxtf/resource/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func Pool() *schema.Resource {
ReadContext: poolRead,
UpdateContext: poolUpdate,
DeleteContext: poolDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
3 changes: 3 additions & 0 deletions proxmoxtf/resource/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func Role() *schema.Resource {
ReadContext: roleRead,
UpdateContext: roleUpdate,
DeleteContext: roleDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}

Expand Down
14 changes: 14 additions & 0 deletions proxmoxtf/resource/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func Time() *schema.Resource {
ReadContext: timeRead,
UpdateContext: timeUpdate,
DeleteContext: timeDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
nodeName := d.Id()

err := d.Set(mkResourceVirtualEnvironmentTimeNodeName, nodeName)
if err != nil {
return nil, fmt.Errorf("failed setting state during import: %w", err)
}

d.SetId(fmt.Sprintf("%s_time", nodeName))

return []*schema.ResourceData{d}, nil
},
},
}
}

Expand Down
Loading

0 comments on commit 4147ff6

Please sign in to comment.