From 471799d4f770218b3faa0161e1c1bff232ce31fb Mon Sep 17 00:00:00 2001 From: Kapustin Aleksandr Date: Mon, 11 Sep 2023 15:37:50 +0300 Subject: [PATCH] 250: Add bool and string fields in vm.Initialization Signed-off-by: Kapustin Aleksandr --- vm.go | 263 +++++++++++++++++++++++++++++++++++++++++++++++++-- vm_create.go | 55 ++++++++++- vm_test.go | 241 +++++++++++++++++++++++++++++++++++++++------- 3 files changed, 518 insertions(+), 41 deletions(-) diff --git a/vm.go b/vm.go index 8d8f90c..0b3eba0 100644 --- a/vm.go +++ b/vm.go @@ -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. @@ -382,6 +399,22 @@ 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. @@ -389,17 +422,49 @@ type BuildableInitialization interface { // 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: "", } } @@ -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 @@ -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 = ®enerateIds + return i +} +func (i *initialization) WithRegenerateSshKeys(regenerateSshKeys bool) BuildableInitialization { + i.regenerateSshKeys = ®enerateSshKeys + 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 ( @@ -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. @@ -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 = ®enerateIds + } + regenerateSshKeys, ok := initializationSDK.RegenerateSshKeys() + if ok { + init.regenerateSshKeys = ®enerateSshKeys + } + 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 } diff --git a/vm_create.go b/vm_create.go index 877ab3d..40d2507 100644 --- a/vm_create.go +++ b/vm_create.go @@ -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 } @@ -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) } @@ -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 { diff --git a/vm_test.go b/vm_test.go index cdd946d..f8db645 100644 --- a/vm_test.go +++ b/vm_test.go @@ -267,47 +267,74 @@ func assertNIC(t *testing.T, nic, shouldBe ovirtclient.NicConfiguration) { } } -func TestVMCreationWithInit(t *testing.T) { //nolint:funlen +func TestVMCreationWithInit(t *testing.T) { //nolint:funlen,gocognit,gocyclo t.Parallel() helper := getHelper(t) testCases := []struct { - name string - customScript string - hostName string - nicConfiguration ovirtclient.NicConfiguration + name string + customScript string + hostName string + optional map[string]interface{} }{ { "only custom script and hostname", - "script-test", - "test-vm", + "customScript", + "hostName", nil, }, + { + "all string and boolean parameters", + "customScript", + "hostName", + map[string]interface{}{ + "activeDirectoryOu": "ActiveDirectoryOu", + "authorizedSshKeys": "AuthorizedSshKeys", + "dnsSearch": "DnsSearch", + "dnsServers": "DnsServers", + "domain": "Domain", + "inputLocale": "InputLocale", + "orgName": "OrgName", + "regenerateIds": false, + "regenerateSshKeys": false, + "rootPassword": "rootpass", + "systemLocale": "SystemLocale", + "timezone": "Timezone", + "uiLanguage": "UiLanguage", + "userLocale": "UserLocale", + "userName": "UserName", + "windowsLicenseKey": "WindowsLicenseKey", + }, + }, { "with ipv4 nic configuration", - "script-test", - "test-vm", - ovirtclient.NewNicConfiguration("custom-nic", ovirtclient.IP{ - Version: ovirtclient.IPVERSION_V4, - Address: "192.168.178.15", - Gateway: "192.168.19.1", - Netmask: "255.255.255.0", - })}, + "customScript", + "hostName", + map[string]interface{}{ + "nicConfiguration": ovirtclient.NewNicConfiguration("custom-nic", ovirtclient.IP{ + Version: ovirtclient.IPVERSION_V4, + Address: "192.168.178.15", + Gateway: "192.168.19.1", + Netmask: "255.255.255.0", + })}, + }, + { "with ipv6 nic configuration", - "script-test", - "test-vm", - ovirtclient.NewNicConfiguration("custom-nic", ovirtclient.IP{ - Version: ovirtclient.IPVERSION_V4, - Address: "192.168.178.15", - Gateway: "192.168.19.1", - Netmask: "255.255.255.0", - }).WithIPV6(ovirtclient.IP{ - Version: ovirtclient.IPVERSION_V6, - Address: "fe80::bfb6:1c6c:f541:1aa564", - Gateway: "fe80::", - Netmask: "64", - }), + "customScript", + "hostName", + map[string]interface{}{ + "nicConfiguration": ovirtclient.NewNicConfiguration("custom-nic", ovirtclient.IP{ + Version: ovirtclient.IPVERSION_V4, + Address: "192.168.178.15", + Gateway: "192.168.19.1", + Netmask: "255.255.255.0", + }).WithIPV6(ovirtclient.IP{ + Version: ovirtclient.IPVERSION_V6, + Address: "fe80::bfb6:1c6c:f541:1aa564", + Gateway: "fe80::", + Netmask: "64", + })}, }, } @@ -321,9 +348,95 @@ func TestVMCreationWithInit(t *testing.T) { //nolint:funlen for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { - init := ovirtclient.NewInitialization(testCase.customScript, testCase.hostName) - if testCase.nicConfiguration != nil { - init = init.WithNicConfiguration(testCase.nicConfiguration) + customScript := testCase.customScript + hostName := testCase.hostName + + init := ovirtclient.NewInitialization(customScript, hostName) + + nicConfiguration, nicConfigurationExists := testCase.optional["nicConfiguration"] + if nicConfigurationExists { + init = init.WithNicConfiguration(nicConfiguration.(ovirtclient.NicConfiguration)) + + } + + activeDirectoryOu, activeDirectoryOuExists := testCase.optional["activeDirectoryOu"] + if activeDirectoryOuExists { + init = init.WithActiveDirectoryOu(activeDirectoryOu.(string)) + } + + authorizedSshKeys, authorizedSshKeysExists := testCase.optional["authorizedSshKeys"] + if authorizedSshKeysExists { + init = init.WithAuthorizedSshKeys(authorizedSshKeys.(string)) + } + + dnsSearch, dnsSearchExists := testCase.optional["dnsSearch"] + if dnsSearchExists { + init = init.WithDnsSearch(dnsSearch.(string)) + } + + dnsServers, dnsServersExists := testCase.optional["dnsServers"] + if dnsServersExists { + init = init.WithDnsServers(dnsServers.(string)) + } + + domain, domainExists := testCase.optional["domain"] + if domainExists { + init = init.WithDomain(domain.(string)) + } + + inputLocale, inputLocaleExists := testCase.optional["inputLocale"] + if inputLocaleExists { + init = init.WithInputLocale(inputLocale.(string)) + } + + orgName, orgNameExists := testCase.optional["orgName"] + if orgNameExists { + init = init.WithOrgName(orgName.(string)) + } + + regenerateIds, regenerateIdsExists := testCase.optional["regenerateIds"] + if regenerateIdsExists { + init = init.WithRegenerateIds(regenerateIds.(bool)) + } + + regenerateSshKeys, regenerateSshKeysExists := testCase.optional["regenerateSshKeys"] + if regenerateSshKeysExists { + init = init.WithRegenerateSshKeys(regenerateSshKeys.(bool)) + } + + rootPassword, rootPasswordExists := testCase.optional["rootPassword"] + if rootPasswordExists { + init = init.WithRootPassword(rootPassword.(string)) + } + + systemLocale, systemLocaleExists := testCase.optional["systemLocale"] + if systemLocaleExists { + init = init.WithSystemLocale(systemLocale.(string)) + } + + timezone, timezoneExists := testCase.optional["timezone"] + if timezoneExists { + init = init.WithTimezone(timezone.(string)) + } + + uiLanguage, uiLanguageExists := testCase.optional["uiLanguage"] + if uiLanguageExists { + init = init.WithUiLanguage(uiLanguage.(string)) + } + + userLocale, userLocaleExists := testCase.optional["userLocale"] + if userLocaleExists { + init = init.WithUserLocale(userLocale.(string)) + } + + userName, userNameExists := testCase.optional["userName"] + if userNameExists { + init = init.WithUserName(userName.(string)) + } + + windowsLicenseKey, windowsLicenseKeyExists := testCase.optional["windowsLicenseKey"] + if windowsLicenseKeyExists { + init = init.WithWindowsLicenseKey(windowsLicenseKey.(string)) } vm2 := assertCanCreateVMFromTemplate( @@ -334,6 +447,8 @@ func TestVMCreationWithInit(t *testing.T) { //nolint:funlen ovirtclient.CreateVMParams().MustWithInitialization(init), ) + // vm2.Initialization().RegenerateIds() always nil in response, so skip this parameter + if vm2.Initialization().CustomScript() != testCase.customScript { t.Fatalf("got Unexpected output from the CustomScript (%s) init field ", vm2.Initialization().CustomScript()) } @@ -342,9 +457,69 @@ func TestVMCreationWithInit(t *testing.T) { //nolint:funlen t.Fatalf("got Unexpected output from the HostName (%s) init field ", vm2.Initialization().HostName()) } - if testCase.nicConfiguration != nil { + if nicConfigurationExists { nic := vm2.Initialization().NicConfiguration() - assertNIC(t, nic, testCase.nicConfiguration) + assertNIC(t, nic, nicConfiguration.(ovirtclient.NicConfiguration)) + } + + if activeDirectoryOuExists && vm2.Initialization().ActiveDirectoryOu() != activeDirectoryOu { + t.Fatalf("got Unexpected output from the ActiveDirectoryOu (%s) init field ", vm2.Initialization().ActiveDirectoryOu()) + } + + if authorizedSshKeysExists && vm2.Initialization().AuthorizedSshKeys() != authorizedSshKeys { + t.Fatalf("got Unexpected output from the AuthorizedSshKeys (%s) init field ", vm2.Initialization().AuthorizedSshKeys()) + } + + if dnsSearchExists && vm2.Initialization().DnsSearch() != dnsSearch { + t.Fatalf("got Unexpected output from the DnsSearch (%s) init field ", vm2.Initialization().DnsSearch()) + } + + if dnsServersExists && vm2.Initialization().DnsServers() != dnsServers { + t.Fatalf("got Unexpected output from the DnsServers (%s) init field ", vm2.Initialization().DnsServers()) + } + + if domainExists && vm2.Initialization().Domain() != domain { + t.Fatalf("got Unexpected output from the Domain (%s) init field ", vm2.Initialization().Domain()) + } + + if inputLocaleExists && vm2.Initialization().InputLocale() != inputLocale { + t.Fatalf("got Unexpected output from the InputLocale (%s) init field ", vm2.Initialization().InputLocale()) + } + + if orgNameExists && vm2.Initialization().OrgName() != orgName { + t.Fatalf("got Unexpected output from the OrgName (%s) init field ", vm2.Initialization().OrgName()) + } + + if regenerateSshKeysExists && *vm2.Initialization().RegenerateSshKeys() != regenerateSshKeys { + t.Fatalf("got Unexpected output from the RegenerateSshKeys (%t)", *vm2.Initialization().RegenerateSshKeys()) + } + + if rootPasswordExists && vm2.Initialization().RootPassword() != "******" { + t.Fatalf("got Unexpected output from the RootPassword (%s) init field ", vm2.Initialization().RootPassword()) + } + + if systemLocaleExists && vm2.Initialization().SystemLocale() != systemLocale { + t.Fatalf("got Unexpected output from the SystemLocale (%s) init field ", vm2.Initialization().SystemLocale()) + } + + if timezoneExists && vm2.Initialization().Timezone() != timezone { + t.Fatalf("got Unexpected output from the Timezone (%s) init field ", vm2.Initialization().Timezone()) + } + + if uiLanguageExists && vm2.Initialization().UiLanguage() != uiLanguage { + t.Fatalf("got Unexpected output from the UiLanguage (%s) init field ", vm2.Initialization().UiLanguage()) + } + + if userLocaleExists && vm2.Initialization().UserLocale() != userLocale { + t.Fatalf("got Unexpected output from the UserLocale (%s) init field ", vm2.Initialization().UserLocale()) + } + + if userNameExists && vm2.Initialization().UserName() != userName { + t.Fatalf("got Unexpected output from the UserName (%s) init field ", vm2.Initialization().UserName()) + } + + if windowsLicenseKeyExists && vm2.Initialization().WindowsLicenseKey() != windowsLicenseKey { + t.Fatalf("got Unexpected output from the WindowsLicenseKey (%s) init field ", vm2.Initialization().WindowsLicenseKey()) } },