Skip to content

Commit

Permalink
provider/ignition: migration from resources to data resources (#11851)
Browse files Browse the repository at this point in the history
* provider/ignition: migration from resources to data resources

* website: provider/ignition documention updated to data resources

* provider/ignition: backwards compatibility support for old resources
  • Loading branch information
mcuadros authored and stack72 committed Mar 6, 2017
1 parent 22f69b1 commit b58709a
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 195 deletions.
40 changes: 39 additions & 1 deletion builtin/providers/ignition/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
DataSourcesMap: map[string]*schema.Resource{
"ignition_config": resourceConfig(),
"ignition_disk": resourceDisk(),
"ignition_raid": resourceRaid(),
Expand All @@ -28,6 +28,44 @@ func Provider() terraform.ResourceProvider {
"ignition_user": resourceUser(),
"ignition_group": resourceGroup(),
},
ResourcesMap: map[string]*schema.Resource{
"ignition_config": schema.DataSourceResourceShim(
"ignition_config",
resourceConfig(),
),
"ignition_disk": schema.DataSourceResourceShim(
"ignition_disk",
resourceDisk(),
),
"ignition_raid": schema.DataSourceResourceShim(
"ignition_raid",
resourceRaid(),
),
"ignition_filesystem": schema.DataSourceResourceShim(
"ignition_filesystem",
resourceFilesystem(),
),
"ignition_file": schema.DataSourceResourceShim(
"ignition_file",
resourceFile(),
),
"ignition_systemd_unit": schema.DataSourceResourceShim(
"ignition_systemd_unit",
resourceSystemdUnit(),
),
"ignition_networkd_unit": schema.DataSourceResourceShim(
"ignition_networkd_unit",
resourceNetworkdUnit(),
),
"ignition_user": schema.DataSourceResourceShim(
"ignition_user",
resourceUser(),
),
"ignition_group": schema.DataSourceResourceShim(
"ignition_group",
resourceGroup(),
),
},
ConfigureFunc: func(*schema.ResourceData) (interface{}, error) {
return &cache{
disks: make(map[string]*types.Disk, 0),
Expand Down
14 changes: 1 addition & 13 deletions builtin/providers/ignition/resource_ignition_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ var configReferenceResource = &schema.Resource{

func resourceConfig() *schema.Resource {
return &schema.Resource{
Create: resourceIgnitionFileCreate,
Update: resourceIgnitionFileCreate,
Delete: resourceIgnitionFileDelete,
Exists: resourceIgnitionFileExists,
Read: resourceIgnitionFileRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -93,7 +90,7 @@ func resourceConfig() *schema.Resource {
}
}

func resourceIgnitionFileCreate(d *schema.ResourceData, meta interface{}) error {
func resourceIgnitionFileRead(d *schema.ResourceData, meta interface{}) error {
rendered, err := renderConfig(d, meta.(*cache))
if err != nil {
return err
Expand All @@ -107,11 +104,6 @@ func resourceIgnitionFileCreate(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceIgnitionFileDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func resourceIgnitionFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
rendered, err := renderConfig(d, meta.(*cache))
if err != nil {
Expand All @@ -121,10 +113,6 @@ func resourceIgnitionFileExists(d *schema.ResourceData, meta interface{}) (bool,
return hash(rendered) == d.Id(), nil
}

func resourceIgnitionFileRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func renderConfig(d *schema.ResourceData, c *cache) (string, error) {
i, err := buildConfig(d, c)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions builtin/providers/ignition/resource_ignition_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestIngnitionFileReplace(t *testing.T) {
testIgnition(t, `
resource "ignition_config" "test" {
data "ignition_config" "test" {
replace {
source = "foo"
verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
Expand All @@ -38,7 +38,7 @@ func TestIngnitionFileReplace(t *testing.T) {

func TestIngnitionFileAppend(t *testing.T) {
testIgnition(t, `
resource "ignition_config" "test" {
data "ignition_config" "test" {
append {
source = "foo"
verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
Expand Down Expand Up @@ -95,7 +95,7 @@ var testTemplate = `
%s
output "rendered" {
value = "${ignition_config.test.rendered}"
value = "${data.ignition_config.test.rendered}"
}
`
13 changes: 1 addition & 12 deletions builtin/providers/ignition/resource_ignition_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

func resourceDisk() *schema.Resource {
return &schema.Resource{
Create: resourceDiskCreate,
Delete: resourceDiskDelete,
Exists: resourceDiskExists,
Read: resourceDiskRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -60,7 +58,7 @@ func resourceDisk() *schema.Resource {
}
}

func resourceDiskCreate(d *schema.ResourceData, meta interface{}) error {
func resourceDiskRead(d *schema.ResourceData, meta interface{}) error {
id, err := buildDisk(d, meta.(*cache))
if err != nil {
return err
Expand All @@ -70,11 +68,6 @@ func resourceDiskCreate(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceDiskDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func resourceDiskExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id, err := buildDisk(d, meta.(*cache))
if err != nil {
Expand All @@ -84,10 +77,6 @@ func resourceDiskExists(d *schema.ResourceData, meta interface{}) (bool, error)
return id == d.Id(), nil
}

func resourceDiskRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func buildDisk(d *schema.ResourceData, c *cache) (string, error) {
var partitions []types.Partition
for _, raw := range d.Get("partition").([]interface{}) {
Expand Down
31 changes: 27 additions & 4 deletions builtin/providers/ignition/resource_ignition_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestIngnitionDisk(t *testing.T) {
testIgnition(t, `
resource "ignition_disk" "foo" {
data "ignition_disk" "foo" {
device = "/foo"
partition {
label = "qux"
Expand All @@ -18,10 +18,10 @@ func TestIngnitionDisk(t *testing.T) {
type_guid = "01234567-89AB-CDEF-EDCB-A98765432101"
}
}
resource "ignition_config" "test" {
data "ignition_config" "test" {
disks = [
"${ignition_disk.foo.id}",
"${data.ignition_disk.foo.id}",
]
}
`, func(c *types.Config) error {
Expand Down Expand Up @@ -58,3 +58,26 @@ func TestIngnitionDisk(t *testing.T) {
return nil
})
}

func TestIngnitionDiskResource(t *testing.T) {
testIgnition(t, `
resource "ignition_disk" "foo" {
device = "/foo"
partition {
label = "qux"
}
}
data "ignition_config" "test" {
disks = [
"${ignition_disk.foo.id}",
]
}
`, func(c *types.Config) error {
if len(c.Storage.Disks) != 1 {
return fmt.Errorf("disks, found %d", len(c.Storage.Disks))
}

return nil
})
}
13 changes: 1 addition & 12 deletions builtin/providers/ignition/resource_ignition_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

func resourceFile() *schema.Resource {
return &schema.Resource{
Create: resourceFileCreate,
Delete: resourceFileDelete,
Exists: resourceFileExists,
Read: resourceFileRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -91,7 +89,7 @@ func resourceFile() *schema.Resource {
}
}

func resourceFileCreate(d *schema.ResourceData, meta interface{}) error {
func resourceFileRead(d *schema.ResourceData, meta interface{}) error {
id, err := buildFile(d, meta.(*cache))
if err != nil {
return err
Expand All @@ -101,11 +99,6 @@ func resourceFileCreate(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceFileDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func resourceFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id, err := buildFile(d, meta.(*cache))
if err != nil {
Expand All @@ -115,10 +108,6 @@ func resourceFileExists(d *schema.ResourceData, meta interface{}) (bool, error)
return id == d.Id(), nil
}

func resourceFileRead(d *schema.ResourceData, metacontent interface{}) error {
return nil
}

func buildFile(d *schema.ResourceData, c *cache) (string, error) {
_, hasContent := d.GetOk("content")
_, hasSource := d.GetOk("source")
Expand Down
12 changes: 6 additions & 6 deletions builtin/providers/ignition/resource_ignition_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestIngnitionFile(t *testing.T) {
testIgnition(t, `
resource "ignition_file" "foo" {
data "ignition_file" "foo" {
filesystem = "foo"
path = "/foo"
content {
Expand All @@ -19,8 +19,8 @@ func TestIngnitionFile(t *testing.T) {
uid = 42
gid = 84
}
resource "ignition_file" "qux" {
data "ignition_file" "qux" {
filesystem = "qux"
path = "/qux"
source {
Expand All @@ -30,10 +30,10 @@ func TestIngnitionFile(t *testing.T) {
}
}
resource "ignition_config" "test" {
data "ignition_config" "test" {
files = [
"${ignition_file.foo.id}",
"${ignition_file.qux.id}",
"${data.ignition_file.foo.id}",
"${data.ignition_file.qux.id}",
]
}
`, func(c *types.Config) error {
Expand Down
13 changes: 1 addition & 12 deletions builtin/providers/ignition/resource_ignition_filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

func resourceFilesystem() *schema.Resource {
return &schema.Resource{
Create: resourceFilesystemCreate,
Delete: resourceFilesystemDelete,
Exists: resourceFilesystemExists,
Read: resourceFilesystemRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -59,7 +57,7 @@ func resourceFilesystem() *schema.Resource {
}
}

func resourceFilesystemCreate(d *schema.ResourceData, meta interface{}) error {
func resourceFilesystemRead(d *schema.ResourceData, meta interface{}) error {
id, err := buildFilesystem(d, meta.(*cache))
if err != nil {
return err
Expand All @@ -69,11 +67,6 @@ func resourceFilesystemCreate(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceFilesystemDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func resourceFilesystemExists(d *schema.ResourceData, meta interface{}) (bool, error) {
id, err := buildFilesystem(d, meta.(*cache))
if err != nil {
Expand All @@ -83,10 +76,6 @@ func resourceFilesystemExists(d *schema.ResourceData, meta interface{}) (bool, e
return id == d.Id(), nil
}

func resourceFilesystemRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func buildFilesystem(d *schema.ResourceData, c *cache) (string, error) {
var mount *types.FilesystemMount
if _, ok := d.GetOk("mount"); ok {
Expand Down
14 changes: 7 additions & 7 deletions builtin/providers/ignition/resource_ignition_filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (

func TestIngnitionFilesystem(t *testing.T) {
testIgnition(t, `
resource "ignition_filesystem" "foo" {
data "ignition_filesystem" "foo" {
name = "foo"
path = "/foo"
}
resource "ignition_filesystem" "qux" {
data "ignition_filesystem" "qux" {
name = "qux"
mount {
device = "/qux"
format = "ext4"
}
}
resource "ignition_filesystem" "bar" {
data "ignition_filesystem" "bar" {
name = "bar"
mount {
device = "/bar"
Expand All @@ -32,11 +32,11 @@ func TestIngnitionFilesystem(t *testing.T) {
}
}
resource "ignition_config" "test" {
data "ignition_config" "test" {
filesystems = [
"${ignition_filesystem.foo.id}",
"${ignition_filesystem.qux.id}",
"${ignition_filesystem.bar.id}",
"${data.ignition_filesystem.foo.id}",
"${data.ignition_filesystem.qux.id}",
"${data.ignition_filesystem.bar.id}",
]
}
`, func(c *types.Config) error {
Expand Down
Loading

0 comments on commit b58709a

Please sign in to comment.