-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing of power parameters (#96)
- Loading branch information
1 parent
0aeaa6e
commit a1a74b1
Showing
9 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,10 @@ Provides a resource to manage MAAS machines. | |
```terraform | ||
resource "maas_machine" "virsh_vm1" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "test-vm1" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:89:f5:3e" | ||
} | ||
``` | ||
|
@@ -28,7 +28,7 @@ resource "maas_machine" "virsh_vm1" { | |
|
||
### Required | ||
|
||
- `power_parameters` (Map of String, Sensitive) A map with the parameters specific to the `power_type`. See [Power types](https://maas.io/docs/api#power-types) section for a list of the available power parameters for each power type. | ||
- `power_parameters` (String, Sensitive) Serialized JSON string containing the parameters specific to the `power_type`. See [Power types](https://maas.io/docs/api#power-types) section for a list of the available power parameters for each power type. | ||
- `power_type` (String) A power management type (e.g. `ipmi`). | ||
- `pxe_mac_address` (String) The MAC address of the machine's PXE boot NIC. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ | |
# | ||
resource "maas_machine" "virsh_vm1" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "test-vm1" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:89:f5:3e" | ||
} | ||
|
||
|
@@ -74,10 +74,10 @@ resource "maas_network_interface_link" "virsh_vm1_nic3" { | |
# | ||
resource "maas_machine" "virsh_vm2" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "test-vm2" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:7c:f7:77" | ||
} | ||
|
||
|
@@ -186,10 +186,10 @@ resource "maas_block_device" "vdc" { | |
# | ||
resource "maas_machine" "virsh_vm3" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "machine-01" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:16:78:ec" | ||
} | ||
|
||
|
@@ -198,9 +198,9 @@ resource "maas_machine" "virsh_vm3" { | |
# | ||
resource "maas_machine" "virsh_vm4" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "machine-05" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:c4:74:96" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
resource "maas_machine" "virsh_vm1" { | ||
power_type = "virsh" | ||
power_parameters = { | ||
power_parameters = jsonencode({ | ||
power_address = "qemu+ssh://[email protected]/system" | ||
power_id = "test-vm1" | ||
} | ||
}) | ||
pxe_mac_address = "52:54:00:89:f5:3e" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package maas | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" | ||
) | ||
|
||
func resourceMaasMachineResourceV0() *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"power_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"power_parameters": { | ||
Type: schema.TypeMap, | ||
Required: true, | ||
Sensitive: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"pxe_mac_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"architecture": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "amd64/generic", | ||
}, | ||
"min_hwe_kernel": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"hostname": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"domain": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"zone": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
"pool": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceMaasMachineStateUpgradeV0(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { | ||
// Convert power_parameters from map[string]string to a serialized JSON string. | ||
oldPowerParametersRaw := rawState["power_parameters"].(map[string]interface{}) | ||
flattenedOldPowerParameters, err := structure.FlattenJsonToString(oldPowerParametersRaw) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawState["power_parameters"] = flattenedOldPowerParameters | ||
|
||
return rawState, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package maas | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure" | ||
) | ||
|
||
func testResourceMaasMachineInstanceStateDataV0() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"power_parameters": map[string]interface{}{ | ||
"power_user": "ubuntu", | ||
}, | ||
} | ||
} | ||
|
||
func testResourceMaasMachineInstanceStateDataV1() map[string]interface{} { | ||
flattenedV0, _ := structure.FlattenJsonToString(map[string]interface{}{ | ||
"power_user": "ubuntu", | ||
}) | ||
return map[string]interface{}{"power_parameters": flattenedV0} | ||
} | ||
|
||
func TestResourceMaasMachineInstanceStateUpgradeV0(t *testing.T) { | ||
ctx := context.Background() | ||
expected := testResourceMaasMachineInstanceStateDataV1() | ||
actual, err := resourceMaasMachineStateUpgradeV0(ctx, testResourceMaasMachineInstanceStateDataV0(), nil) | ||
if err != nil { | ||
t.Fatalf("error migrating state: %s", err) | ||
} | ||
|
||
if !reflect.DeepEqual(expected, actual) { | ||
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters