Example #1
0
func ConvertAs2_0_0(in Config) (types.Config, error) {
	out := types.Config{
		Ignition: types.Ignition{
			Version: types.IgnitionVersion{Major: 2, Minor: 0},
		},
	}

	for _, ref := range in.Ignition.Config.Append {
		newRef, err := convertConfigReference(ref)
		if err != nil {
			return types.Config{}, err
		}
		out.Ignition.Config.Append = append(out.Ignition.Config.Append, newRef)
	}

	if in.Ignition.Config.Replace != nil {
		newRef, err := convertConfigReference(*in.Ignition.Config.Replace)
		if err != nil {
			return types.Config{}, err
		}
		out.Ignition.Config.Replace = &newRef
	}

	for _, disk := range in.Storage.Disks {
		newDisk := types.Disk{
			Device:    types.Path(disk.Device),
			WipeTable: disk.WipeTable,
		}

		for _, partition := range disk.Partitions {
			size, err := convertPartitionDimension(partition.Size)
			if err != nil {
				return types.Config{}, err
			}
			start, err := convertPartitionDimension(partition.Start)
			if err != nil {
				return types.Config{}, err
			}

			newDisk.Partitions = append(newDisk.Partitions, types.Partition{
				Label:    types.PartitionLabel(partition.Label),
				Number:   partition.Number,
				Size:     size,
				Start:    start,
				TypeGUID: types.PartitionTypeGUID(partition.TypeGUID),
			})
		}

		out.Storage.Disks = append(out.Storage.Disks, newDisk)
	}

	for _, array := range in.Storage.Arrays {
		newArray := types.Raid{
			Name:   array.Name,
			Level:  array.Level,
			Spares: array.Spares,
		}

		for _, device := range array.Devices {
			newArray.Devices = append(newArray.Devices, types.Path(device))
		}

		out.Storage.Arrays = append(out.Storage.Arrays, newArray)
	}

	for _, filesystem := range in.Storage.Filesystems {
		newFilesystem := types.Filesystem{
			Name: filesystem.Name,
			Path: func(p types.Path) *types.Path {
				if p == "" {
					return nil
				}

				return &p
			}(types.Path(filesystem.Path)),
		}

		if filesystem.Mount != nil {
			newFilesystem.Mount = &types.FilesystemMount{
				Device: types.Path(filesystem.Mount.Device),
				Format: types.FilesystemFormat(filesystem.Mount.Format),
			}

			if filesystem.Mount.Create != nil {
				newFilesystem.Mount.Create = &types.FilesystemCreate{
					Force:   filesystem.Mount.Create.Force,
					Options: types.MkfsOptions(filesystem.Mount.Create.Options),
				}
			}
		}

		out.Storage.Filesystems = append(out.Storage.Filesystems, newFilesystem)
	}

	for _, file := range in.Storage.Files {
		newFile := types.File{
			Filesystem: file.Filesystem,
			Path:       types.Path(file.Path),
			Mode:       types.FileMode(file.Mode),
			User:       types.FileUser{Id: file.User.Id},
			Group:      types.FileGroup{Id: file.Group.Id},
		}

		if file.Contents.Inline != "" {
			newFile.Contents = types.FileContents{
				Source: types.Url{
					Scheme: "data",
					Opaque: "," + dataurl.EscapeString(file.Contents.Inline),
				},
			}
		}

		if file.Contents.Remote.Url != "" {
			source, err := url.Parse(file.Contents.Remote.Url)
			if err != nil {
				return types.Config{}, err
			}

			newFile.Contents = types.FileContents{Source: types.Url(*source)}
		}

		if newFile.Contents == (types.FileContents{}) {
			newFile.Contents = types.FileContents{
				Source: types.Url{
					Scheme: "data",
					Opaque: ",",
				},
			}
		}

		newFile.Contents.Compression = types.Compression(file.Contents.Remote.Compression)
		newFile.Contents.Verification = convertVerification(file.Contents.Remote.Verification)

		out.Storage.Files = append(out.Storage.Files, newFile)
	}

	for _, unit := range in.Systemd.Units {
		newUnit := types.SystemdUnit{
			Name:     types.SystemdUnitName(unit.Name),
			Enable:   unit.Enable,
			Mask:     unit.Mask,
			Contents: unit.Contents,
		}

		for _, dropIn := range unit.DropIns {
			newUnit.DropIns = append(newUnit.DropIns, types.SystemdUnitDropIn{
				Name:     types.SystemdUnitDropInName(dropIn.Name),
				Contents: dropIn.Contents,
			})
		}

		out.Systemd.Units = append(out.Systemd.Units, newUnit)
	}

	for _, unit := range in.Networkd.Units {
		out.Networkd.Units = append(out.Networkd.Units, types.NetworkdUnit{
			Name:     types.NetworkdUnitName(unit.Name),
			Contents: unit.Contents,
		})
	}

	for _, user := range in.Passwd.Users {
		newUser := types.User{
			Name:              user.Name,
			PasswordHash:      user.PasswordHash,
			SSHAuthorizedKeys: user.SSHAuthorizedKeys,
		}

		if user.Create != nil {
			newUser.Create = &types.UserCreate{
				Uid:          user.Create.Uid,
				GECOS:        user.Create.GECOS,
				Homedir:      user.Create.Homedir,
				NoCreateHome: user.Create.NoCreateHome,
				PrimaryGroup: user.Create.PrimaryGroup,
				Groups:       user.Create.Groups,
				NoUserGroup:  user.Create.NoUserGroup,
				System:       user.Create.System,
				NoLogInit:    user.Create.NoLogInit,
				Shell:        user.Create.Shell,
			}
		}

		out.Passwd.Users = append(out.Passwd.Users, newUser)
	}

	for _, group := range in.Passwd.Groups {
		out.Passwd.Groups = append(out.Passwd.Groups, types.Group{
			Name:         group.Name,
			Gid:          group.Gid,
			PasswordHash: group.PasswordHash,
			System:       group.System,
		})
	}

	if err := out.AssertValid(); err != nil {
		return types.Config{}, err
	}

	return out, nil
}
func buildFile(d *schema.ResourceData, c *cache) (string, error) {
	_, hasContent := d.GetOk("content")
	_, hasSource := d.GetOk("source")
	if hasContent && hasSource {
		return "", fmt.Errorf("content and source options are incompatible")
	}

	if !hasContent && !hasSource {
		return "", fmt.Errorf("content or source options must be present")
	}

	var compression types.Compression
	var source types.Url
	var hash *types.Hash
	var err error

	if hasContent {
		source, err = encodeDataURL(
			d.Get("content.0.mime").(string),
			d.Get("content.0.content").(string),
		)

		if err != nil {
			return "", err
		}
	}

	if hasSource {
		source, err = buildURL(d.Get("source.0.source").(string))
		if err != nil {
			return "", err
		}

		compression = types.Compression(d.Get("source.0.compression").(string))
		h, err := buildHash(d.Get("source.0.verification").(string))
		if err != nil {
			return "", err
		}

		hash = &h
	}

	return c.addFile(&types.File{
		Filesystem: d.Get("filesystem").(string),
		Path:       types.Path(d.Get("path").(string)),
		Contents: types.FileContents{
			Compression: compression,
			Source:      source,
			Verification: types.Verification{
				Hash: hash,
			},
		},
		User: types.FileUser{
			Id: d.Get("uid").(int),
		},
		Group: types.FileGroup{
			Id: d.Get("gid").(int),
		},
		Mode: types.FileMode(d.Get("mode").(int)),
	}), nil
}