Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Implementation of Ignition 2.1 #13

Merged
merged 4 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 45 additions & 35 deletions ignition/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"sync"

"github.com/coreos/go-systemd/unit"
"github.com/coreos/ignition/config/types"
"github.com/coreos/ignition/config/v2_1/types"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
Expand All @@ -24,10 +23,12 @@ var globalCache = &cache{
arrays: make(map[string]*types.Raid, 0),
filesystems: make(map[string]*types.Filesystem, 0),
files: make(map[string]*types.File, 0),
systemdUnits: make(map[string]*types.SystemdUnit, 0),
networkdUnits: make(map[string]*types.NetworkdUnit, 0),
users: make(map[string]*types.User, 0),
groups: make(map[string]*types.Group, 0),
directories: make(map[string]*types.Directory, 0),
links: make(map[string]*types.Link, 0),
systemdUnits: make(map[string]*types.Unit, 0),
networkdUnits: make(map[string]*types.Networkdunit, 0),
users: make(map[string]*types.PasswdUser, 0),
groups: make(map[string]*types.PasswdGroup, 0),
}

func Provider() terraform.ResourceProvider {
Expand All @@ -38,6 +39,8 @@ func Provider() terraform.ResourceProvider {
"ignition_raid": resourceRaid(),
"ignition_filesystem": resourceFilesystem(),
"ignition_file": resourceFile(),
"ignition_directory": resourceDirectory(),
"ignition_link": resourceLink(),
"ignition_systemd_unit": resourceSystemdUnit(),
"ignition_networkd_unit": resourceNetworkdUnit(),
"ignition_user": resourceUser(),
Expand Down Expand Up @@ -89,10 +92,12 @@ type cache struct {
arrays map[string]*types.Raid
filesystems map[string]*types.Filesystem
files map[string]*types.File
systemdUnits map[string]*types.SystemdUnit
networkdUnits map[string]*types.NetworkdUnit
users map[string]*types.User
groups map[string]*types.Group
directories map[string]*types.Directory
links map[string]*types.Link
systemdUnits map[string]*types.Unit
networkdUnits map[string]*types.Networkdunit
users map[string]*types.PasswdUser
groups map[string]*types.PasswdGroup

sync.Mutex
}
Expand Down Expand Up @@ -137,7 +142,27 @@ func (c *cache) addFile(f *types.File) string {
return id
}

func (c *cache) addSystemdUnit(u *types.SystemdUnit) string {
func (c *cache) addDirectory(d *types.Directory) string {
c.Lock()
defer c.Unlock()

id := id(d)
c.directories[id] = d

return id
}

func (c *cache) addLink(l *types.Link) string {
c.Lock()
defer c.Unlock()

id := id(l)
c.links[id] = l

return id
}

func (c *cache) addSystemdUnit(u *types.Unit) string {
c.Lock()
defer c.Unlock()

Expand All @@ -147,7 +172,7 @@ func (c *cache) addSystemdUnit(u *types.SystemdUnit) string {
return id
}

func (c *cache) addNetworkdUnit(u *types.NetworkdUnit) string {
func (c *cache) addNetworkdUnit(u *types.Networkdunit) string {
c.Lock()
defer c.Unlock()

Expand All @@ -157,7 +182,7 @@ func (c *cache) addNetworkdUnit(u *types.NetworkdUnit) string {
return id
}

func (c *cache) addUser(u *types.User) string {
func (c *cache) addUser(u *types.PasswdUser) string {
c.Lock()
defer c.Unlock()

Expand All @@ -167,7 +192,7 @@ func (c *cache) addUser(u *types.User) string {
return id
}

func (c *cache) addGroup(g *types.Group) string {
func (c *cache) addGroup(g *types.PasswdGroup) string {
c.Lock()
defer c.Unlock()

Expand All @@ -193,20 +218,21 @@ func castSliceInterface(i []interface{}) []string {
if value == nil {
continue
}

o = append(o, value.(string))
}

return o
}

func getUInt(d *schema.ResourceData, key string) *uint {
var uid *uint
func getInt(d *schema.ResourceData, key string) *int {
var i *int
if value, ok := d.GetOk(key); ok {
u := uint(value.(int))
uid = &u
n := value.(int)
i = &n
}

return uid
return i
}

var errEmptyUnit = fmt.Errorf("invalid or empty unit content")
Expand All @@ -224,19 +250,3 @@ func validateUnitContent(content string) error {

return nil
}

func buildURL(raw string) (types.Url, error) {
u, err := url.Parse(raw)
if err != nil {
return types.Url{}, err
}

return types.Url(*u), nil
}

func buildHash(raw string) (types.Hash, error) {
h := types.Hash{}
err := h.UnmarshalJSON([]byte(fmt.Sprintf("%q", raw)))

return h, err
}
62 changes: 45 additions & 17 deletions ignition/resource_ignition_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"

"github.com/coreos/ignition/config/types"
"github.com/coreos/ignition/config/v2_1/types"
)

var configReferenceResource = &schema.Resource{
Expand Down Expand Up @@ -49,6 +49,16 @@ func resourceConfig() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"directories": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"links": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"systemd": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -119,7 +129,7 @@ func renderConfig(d *schema.ResourceData, c *cache) (string, error) {
return "", err
}

bytes, err := json.Marshal(i)
bytes, err := json.MarshalIndent(i, " ", " ")

if err != nil {
return "", err
Expand Down Expand Up @@ -163,7 +173,7 @@ func buildIgnition(d *schema.ResourceData) (types.Ignition, error) {
var err error

i := types.Ignition{}
i.Version.UnmarshalJSON([]byte(`"2.0.0"`))
i.Version = types.MaxVersion.String()

rr := d.Get("replace.0").(map[string]interface{})
if len(rr) != 0 {
Expand All @@ -190,20 +200,10 @@ func buildIgnition(d *schema.ResourceData) (types.Ignition, error) {

func buildConfigReference(raw map[string]interface{}) (*types.ConfigReference, error) {
r := &types.ConfigReference{}
r.Source = raw["source"].(string)

src, err := buildURL(raw["source"].(string))
if err != nil {
return nil, err
}

r.Source = src

if raw["verification"].(string) != "" {
hash, err := buildHash(raw["verification"].(string))
if err != nil {
return nil, err
}

hash := raw["verification"].(string)
if hash != "" {
r.Verification.Hash = &hash
}

Expand Down Expand Up @@ -234,7 +234,7 @@ func buildStorage(d *schema.ResourceData, c *cache) (types.Storage, error) {
return storage, fmt.Errorf("invalid raid %q, unknown raid id", id)
}

storage.Arrays = append(storage.Arrays, *a)
storage.Raid = append(storage.Raid, *a)
}

for _, id := range d.Get("filesystems").([]interface{}) {
Expand All @@ -261,6 +261,30 @@ func buildStorage(d *schema.ResourceData, c *cache) (types.Storage, error) {
storage.Files = append(storage.Files, *f)
}

for _, id := range d.Get("directories").([]interface{}) {
if id == nil {
continue
}
f, ok := c.directories[id.(string)]
if !ok {
return storage, fmt.Errorf("invalid file %q, unknown directory id", id)
}

storage.Directories = append(storage.Directories, *f)
}

for _, id := range d.Get("links").([]interface{}) {
if id == nil {
continue
}
f, ok := c.links[id.(string)]
if !ok {
return storage, fmt.Errorf("invalid file %q, unknown link id", id)
}

storage.Links = append(storage.Links, *f)
}

return storage, nil

}
Expand All @@ -272,6 +296,7 @@ func buildSystemd(d *schema.ResourceData, c *cache) (types.Systemd, error) {
if id == nil {
continue
}

u, ok := c.systemdUnits[id.(string)]
if !ok {
return systemd, fmt.Errorf("invalid systemd unit %q, unknown systemd unit id", id)
Expand All @@ -291,6 +316,7 @@ func buildNetworkd(d *schema.ResourceData, c *cache) (types.Networkd, error) {
if id == nil {
continue
}

u, ok := c.networkdUnits[id.(string)]
if !ok {
return networkd, fmt.Errorf("invalid networkd unit %q, unknown networkd unit id", id)
Expand All @@ -309,6 +335,7 @@ func buildPasswd(d *schema.ResourceData, c *cache) (types.Passwd, error) {
if id == nil {
continue
}

u, ok := c.users[id.(string)]
if !ok {
return passwd, fmt.Errorf("invalid user %q, unknown user id", id)
Expand All @@ -321,6 +348,7 @@ func buildPasswd(d *schema.ResourceData, c *cache) (types.Passwd, error) {
if id == nil {
continue
}

g, ok := c.groups[id.(string)]
if !ok {
return passwd, fmt.Errorf("invalid group %q, unknown group id", id)
Expand Down
30 changes: 15 additions & 15 deletions ignition/resource_ignition_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"regexp"
"testing"

"github.com/coreos/ignition/config/types"
"github.com/coreos/ignition/config/v2_1/types"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestIgnitionFileReplace(t *testing.T) {
func TestIngnitionFileReplace(t *testing.T) {
testIgnition(t, `
data "ignition_config" "test" {
replace {
Expand All @@ -25,29 +25,29 @@ func TestIgnitionFileReplace(t *testing.T) {
return fmt.Errorf("unable to find replace config")
}

if r.Source.String() != "foo" {
if r.Source != "foo" {
return fmt.Errorf("config.replace.source, found %q", r.Source)
}

if r.Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
return fmt.Errorf("config.replace.verification, found %q", r.Verification.Hash)
if *r.Verification.Hash != "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
return fmt.Errorf("config.replace.verification, found %q", *r.Verification.Hash)
}

return nil
})
}

func TestIgnitionFileAppend(t *testing.T) {
func TestIngnitionFileAppend(t *testing.T) {
testIgnition(t, `
data "ignition_config" "test" {
append {
source = "foo"
verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
}

append {
source = "foo"
verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
append {
source = "foo"
verification = "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
}
}
`, func(c *types.Config) error {
Expand All @@ -56,12 +56,12 @@ func TestIgnitionFileAppend(t *testing.T) {
return fmt.Errorf("unable to find append config, expected 2")
}

if a[0].Source.String() != "foo" {
if a[0].Source != "foo" {
return fmt.Errorf("config.replace.source, found %q", a[0].Source)
}

if a[0].Verification.Hash.Sum != "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
return fmt.Errorf("config.replace.verification, found %q", a[0].Verification.Hash)
if *a[0].Verification.Hash != "sha512-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" {
return fmt.Errorf("config.replace.verification, found %q", *a[0].Verification.Hash)
}

return nil
Expand All @@ -81,7 +81,7 @@ func TestIngnitionFileReplaceNoVerification(t *testing.T) {
return fmt.Errorf("unable to find replace config")
}

if r.Source.String() != "foo" {
if r.Source != "foo" {
return fmt.Errorf("config.replace.source, found %q", r.Source)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func TestIngnitionFileAppendNoVerification(t *testing.T) {
return fmt.Errorf("unable to find append config, expected 2")
}

if a[0].Source.String() != "foo" {
if a[0].Source != "foo" {
return fmt.Errorf("config.replace.source, found %q", a[0].Source)
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func TestIgnitionConfigArrays(t *testing.T) {
]
}
`, func(c *types.Config) error {
f := c.Storage.Arrays[0]
f := c.Storage.Raid[0]
if f.Name != "data" {
return fmt.Errorf("device, found %q", f.Name)
}
Expand Down
Loading