Skip to content

Commit

Permalink
250: Add bool and string fields in vm.Initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Kapustin Aleksandr <alkapustin@rockitsoft.ru>
  • Loading branch information
Kapustin Aleksandr committed Sep 13, 2023
1 parent a5d2d8c commit 471799d
Show file tree
Hide file tree
Showing 3 changed files with 518 additions and 41 deletions.
263 changes: 256 additions & 7 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,23 @@ type Initialization interface {
CustomScript() string
HostName() string
NicConfiguration() NicConfiguration
ActiveDirectoryOu() string
AuthorizedSshKeys() string
DnsSearch() string
DnsServers() string
Domain() string
InputLocale() string
OrgName() string
RegenerateIds() *bool
RegenerateSshKeys() *bool
RootPassword() string
SetRootPassword(string)
SystemLocale() string
Timezone() string
UiLanguage() string
UserLocale() string
UserName() string
WindowsLicenseKey() string
}

// BuildableInitialization is a buildable version of Initialization.
Expand All @@ -382,24 +399,72 @@ type BuildableInitialization interface {
WithCustomScript(customScript string) BuildableInitialization
WithHostname(hostname string) BuildableInitialization
WithNicConfiguration(nic NicConfiguration) BuildableInitialization
WithActiveDirectoryOu(activeDirectoryOu string) BuildableInitialization
WithAuthorizedSshKeys(authorizedSshKeys string) BuildableInitialization
WithDnsSearch(dnsSearch string) BuildableInitialization
WithDnsServers(dnsServers string) BuildableInitialization
WithDomain(domain string) BuildableInitialization
WithInputLocale(inputLocale string) BuildableInitialization
WithOrgName(orgName string) BuildableInitialization
WithRegenerateIds(regenerateIds bool) BuildableInitialization
WithRegenerateSshKeys(regenerateSshKeys bool) BuildableInitialization
WithRootPassword(rootPassword string) BuildableInitialization
WithSystemLocale(systemLocale string) BuildableInitialization
WithTimezone(timezone string) BuildableInitialization
WithUiLanguage(uiLanguage string) BuildableInitialization
WithUserLocale(userLocale string) BuildableInitialization
WithUserName(userName string) BuildableInitialization
WithWindowsLicenseKey(windowsLicenseKey string) BuildableInitialization
}

// initialization defines to the virtual machine’s initialization configuration.
// customScript - Cloud-init script which will be executed on Virtual Machine when deployed.
// hostname - Hostname to be set to Virtual Machine when deployed.
// nicConfiguration - Optional. The nic configuration used on boot time.
type initialization struct {
customScript string
hostname string
nicConfiguration NicConfiguration
customScript string
hostname string
nicConfiguration NicConfiguration
activeDirectoryOu string
authorizedSshKeys string
dnsSearch string
dnsServers string
domain string
inputLocale string
orgName string
regenerateIds *bool
regenerateSshKeys *bool
rootPassword string
systemLocale string
timezone string
uiLanguage string
userLocale string
userName string
windowsLicenseKey string
}

// NewInitialization creates a new Initialization from the specified parameters.
func NewInitialization(customScript, hostname string) BuildableInitialization {
return &initialization{
customScript: customScript,
hostname: hostname,
nicConfiguration: nil,
customScript: customScript,
hostname: hostname,
nicConfiguration: nil,
activeDirectoryOu: "",
authorizedSshKeys: "",
dnsSearch: "",
dnsServers: "",
domain: "",
inputLocale: "",
orgName: "",
regenerateIds: nil,
regenerateSshKeys: nil,
rootPassword: "",
systemLocale: "",
timezone: "",
uiLanguage: "",
userLocale: "",
userName: "",
windowsLicenseKey: "",
}
}

Expand All @@ -415,6 +480,59 @@ func (i *initialization) NicConfiguration() NicConfiguration {
return i.nicConfiguration
}

func (i *initialization) ActiveDirectoryOu() string {
return i.activeDirectoryOu
}
func (i *initialization) AuthorizedSshKeys() string {
return i.authorizedSshKeys
}
func (i *initialization) DnsSearch() string {
return i.dnsSearch
}
func (i *initialization) DnsServers() string {
return i.dnsServers
}
func (i *initialization) Domain() string {
return i.domain
}
func (i *initialization) InputLocale() string {
return i.inputLocale
}
func (i *initialization) OrgName() string {
return i.orgName
}
func (i *initialization) RegenerateIds() *bool {
return i.regenerateIds
}
func (i *initialization) RegenerateSshKeys() *bool {
return i.regenerateSshKeys
}
func (i *initialization) RootPassword() string {
return i.rootPassword
}
func (i *initialization) SetRootPassword(rootPassword string) {
i.rootPassword = rootPassword
}
func (i *initialization) SystemLocale() string {
return i.systemLocale
}
func (i *initialization) Timezone() string {
return i.timezone
}
func (i *initialization) UiLanguage() string {
return i.uiLanguage
}
func (i *initialization) UserLocale() string {
return i.userLocale
}
func (i *initialization) UserName() string {
return i.userName
}

func (i *initialization) WindowsLicenseKey() string {
return i.windowsLicenseKey
}

func (i *initialization) WithCustomScript(customScript string) BuildableInitialization {
i.customScript = customScript
return i
Expand All @@ -430,6 +548,71 @@ func (i *initialization) WithNicConfiguration(nic NicConfiguration) BuildableIni
return i
}

func (i *initialization) WithActiveDirectoryOu(activeDirectoryOu string) BuildableInitialization {
i.activeDirectoryOu = activeDirectoryOu
return i
}
func (i *initialization) WithAuthorizedSshKeys(authorizedSshKeys string) BuildableInitialization {
i.authorizedSshKeys = authorizedSshKeys
return i
}
func (i *initialization) WithDnsSearch(dnsSearch string) BuildableInitialization {
i.dnsSearch = dnsSearch
return i
}
func (i *initialization) WithDnsServers(dnsServers string) BuildableInitialization {
i.dnsServers = dnsServers
return i
}
func (i *initialization) WithDomain(domain string) BuildableInitialization {
i.domain = domain
return i
}
func (i *initialization) WithInputLocale(inputLocale string) BuildableInitialization {
i.inputLocale = inputLocale
return i
}
func (i *initialization) WithOrgName(orgName string) BuildableInitialization {
i.orgName = orgName
return i
}
func (i *initialization) WithRegenerateIds(regenerateIds bool) BuildableInitialization {
i.regenerateIds = &regenerateIds
return i
}
func (i *initialization) WithRegenerateSshKeys(regenerateSshKeys bool) BuildableInitialization {
i.regenerateSshKeys = &regenerateSshKeys
return i
}
func (i *initialization) WithRootPassword(rootPassword string) BuildableInitialization {
i.rootPassword = rootPassword
return i
}
func (i *initialization) WithSystemLocale(systemLocale string) BuildableInitialization {
i.systemLocale = systemLocale
return i
}
func (i *initialization) WithTimezone(timezone string) BuildableInitialization {
i.timezone = timezone
return i
}
func (i *initialization) WithUiLanguage(uiLanguage string) BuildableInitialization {
i.uiLanguage = uiLanguage
return i
}
func (i *initialization) WithUserLocale(userLocale string) BuildableInitialization {
i.userLocale = userLocale
return i
}
func (i *initialization) WithUserName(userName string) BuildableInitialization {
i.userName = userName
return i
}
func (i *initialization) WithWindowsLicenseKey(windowsLicenseKey string) BuildableInitialization {
i.windowsLicenseKey = windowsLicenseKey
return i
}

type IpVersion string

const (
Expand Down Expand Up @@ -512,7 +695,7 @@ func (i *nicConfiguration) WithIPV6(ip IP) BuildableNicConfiguration {

// convertSDKInitialization converts the initialization of a VM. We keep the error return in case we need it later
// as errors may happen as we extend this function and we don't want to touch other functions.
func convertSDKInitialization(sdkObject *ovirtsdk.Vm) (*initialization, error) { //nolint:unparam
func convertSDKInitialization(sdkObject *ovirtsdk.Vm) (*initialization, error) { //nolint:unparam,funlen
initializationSDK, ok := sdkObject.Initialization()
if !ok {
// This happens for some, but not all API calls if the initialization is not set.
Expand All @@ -532,6 +715,72 @@ func convertSDKInitialization(sdkObject *ovirtsdk.Vm) (*initialization, error) {
if ok && len(nicConfigs.Slice()) >= 1 {
init.nicConfiguration = convertSDKNicConfiguration(nicConfigs.Slice()[0])
}

activeDirectoryOu, ok := initializationSDK.ActiveDirectoryOu()
if ok {
init.activeDirectoryOu = activeDirectoryOu
}
authorizedSshKeys, ok := initializationSDK.AuthorizedSshKeys()
if ok {
init.authorizedSshKeys = authorizedSshKeys
}
dnsSearch, ok := initializationSDK.DnsSearch()
if ok {
init.dnsSearch = dnsSearch
}
dnsServers, ok := initializationSDK.DnsServers()
if ok {
init.dnsServers = dnsServers
}
domain, ok := initializationSDK.Domain()
if ok {
init.domain = domain
}
inputLocale, ok := initializationSDK.InputLocale()
if ok {
init.inputLocale = inputLocale
}
orgName, ok := initializationSDK.OrgName()
if ok {
init.orgName = orgName
}
regenerateIds, ok := initializationSDK.RegenerateIds()
if ok {
init.regenerateIds = &regenerateIds
}
regenerateSshKeys, ok := initializationSDK.RegenerateSshKeys()
if ok {
init.regenerateSshKeys = &regenerateSshKeys
}
rootPassword, ok := initializationSDK.RootPassword()
if ok {
init.rootPassword = rootPassword
}
systemLocale, ok := initializationSDK.SystemLocale()
if ok {
init.systemLocale = systemLocale
}
timezone, ok := initializationSDK.Timezone()
if ok {
init.timezone = timezone
}
uiLanguage, ok := initializationSDK.UiLanguage()
if ok {
init.uiLanguage = uiLanguage
}
userLocale, ok := initializationSDK.UserLocale()
if ok {
init.userLocale = userLocale
}
userName, ok := initializationSDK.UserName()
if ok {
init.userName = userName
}
windowsLicenseKey, ok := initializationSDK.WindowsLicenseKey()
if ok {
init.windowsLicenseKey = windowsLicenseKey
}

return &init, nil
}

Expand Down
55 changes: 54 additions & 1 deletion vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func vmBuilderMemory(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
}
}

func vmBuilderInitialization(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
func vmBuilderInitialization(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) { //nolint:funlen
if params.Initialization() == nil {
return
}
Expand Down Expand Up @@ -104,6 +104,55 @@ func vmBuilderInitialization(params OptionalVMParameters, builder *ovirtsdk.VmBu

initBuilder.NicConfigurationsOfAny(nicBuilder.MustBuild())
}

if init.ActiveDirectoryOu() != "" {
initBuilder.ActiveDirectoryOu(init.ActiveDirectoryOu())
}
if init.AuthorizedSshKeys() != "" {
initBuilder.AuthorizedSshKeys(init.AuthorizedSshKeys())
}
if init.DnsSearch() != "" {
initBuilder.DnsSearch(init.DnsSearch())
}
if init.DnsServers() != "" {
initBuilder.DnsServers(init.DnsServers())
}
if init.Domain() != "" {
initBuilder.Domain(init.Domain())
}
if init.InputLocale() != "" {
initBuilder.InputLocale(init.InputLocale())
}
if init.OrgName() != "" {
initBuilder.OrgName(init.OrgName())
}
if init.RegenerateIds() != nil {
initBuilder.RegenerateIds(*init.RegenerateIds())
}
if init.RegenerateSshKeys() != nil {
initBuilder.RegenerateSshKeys(*init.RegenerateSshKeys())
}
if init.RootPassword() != "" {
initBuilder.RootPassword(init.RootPassword())
}
if init.SystemLocale() != "" {
initBuilder.SystemLocale(init.SystemLocale())
}
if init.Timezone() != "" {
initBuilder.Timezone(init.Timezone())
}
if init.UiLanguage() != "" {
initBuilder.UiLanguage(init.UiLanguage())
}
if init.UserLocale() != "" {
initBuilder.UserLocale(init.UserLocale())
}
if init.UserName() != "" {
initBuilder.UserName(init.UserName())
}
if init.WindowsLicenseKey() != "" {
initBuilder.WindowsLicenseKey(init.WindowsLicenseKey())
}
builder.InitializationBuilder(initBuilder)
}

Expand Down Expand Up @@ -436,6 +485,10 @@ func (m *mockClient) createVM(
init = &initialization{}
}

if init.RootPassword() != "" {
init.SetRootPassword("******")
}

vmType := m.createVMType(params)
console := false
if serialConsole := params.SerialConsole(); serialConsole != nil {
Expand Down
Loading

0 comments on commit 471799d

Please sign in to comment.