github.com/vmware/govmomi@v0.37.1/simulator/customization_spec_manager.go (about)

     1  /*
     2  Copyright (c) 2019 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package simulator
    18  
    19  import (
    20  	"fmt"
    21  	"sync/atomic"
    22  	"time"
    23  
    24  	"github.com/vmware/govmomi/vim25/methods"
    25  	"github.com/vmware/govmomi/vim25/mo"
    26  	"github.com/vmware/govmomi/vim25/soap"
    27  	"github.com/vmware/govmomi/vim25/types"
    28  )
    29  
    30  var DefaultCustomizationSpec = []types.CustomizationSpecItem{
    31  	{
    32  		Info: types.CustomizationSpecInfo{
    33  			Name:           "vcsim-linux",
    34  			Description:    "",
    35  			Type:           "Linux",
    36  			ChangeVersion:  "1569965707",
    37  			LastUpdateTime: types.NewTime(time.Now()),
    38  		},
    39  		Spec: types.CustomizationSpec{
    40  			Options: &types.CustomizationLinuxOptions{},
    41  			Identity: &types.CustomizationLinuxPrep{
    42  				CustomizationIdentitySettings: types.CustomizationIdentitySettings{},
    43  				HostName:                      &types.CustomizationVirtualMachineName{},
    44  				Domain:                        "eng.vmware.com",
    45  				TimeZone:                      "Pacific/Apia",
    46  				HwClockUTC:                    types.NewBool(true),
    47  			},
    48  			GlobalIPSettings: types.CustomizationGlobalIPSettings{
    49  				DnsSuffixList: nil,
    50  				DnsServerList: []string{"127.0.1.1"},
    51  			},
    52  			NicSettingMap: []types.CustomizationAdapterMapping{
    53  				{
    54  					MacAddress: "",
    55  					Adapter: types.CustomizationIPSettings{
    56  						Ip:            &types.CustomizationDhcpIpGenerator{},
    57  						SubnetMask:    "",
    58  						Gateway:       nil,
    59  						IpV6Spec:      (*types.CustomizationIPSettingsIpV6AddressSpec)(nil),
    60  						DnsServerList: nil,
    61  						DnsDomain:     "",
    62  						PrimaryWINS:   "",
    63  						SecondaryWINS: "",
    64  						NetBIOS:       "",
    65  					},
    66  				},
    67  			},
    68  			EncryptionKey: nil,
    69  		},
    70  	},
    71  	{
    72  		Info: types.CustomizationSpecInfo{
    73  			Name:           "vcsim-linux-static",
    74  			Description:    "",
    75  			Type:           "Linux",
    76  			ChangeVersion:  "1569969598",
    77  			LastUpdateTime: types.NewTime(time.Now()),
    78  		},
    79  		Spec: types.CustomizationSpec{
    80  			Options: &types.CustomizationLinuxOptions{},
    81  			Identity: &types.CustomizationLinuxPrep{
    82  				CustomizationIdentitySettings: types.CustomizationIdentitySettings{},
    83  				HostName: &types.CustomizationPrefixName{
    84  					CustomizationName: types.CustomizationName{},
    85  					Base:              "vcsim",
    86  				},
    87  				Domain:     "eng.vmware.com",
    88  				TimeZone:   "Africa/Cairo",
    89  				HwClockUTC: types.NewBool(true),
    90  			},
    91  			GlobalIPSettings: types.CustomizationGlobalIPSettings{
    92  				DnsSuffixList: nil,
    93  				DnsServerList: []string{"127.0.1.1"},
    94  			},
    95  			NicSettingMap: []types.CustomizationAdapterMapping{
    96  				{
    97  					MacAddress: "",
    98  					Adapter: types.CustomizationIPSettings{
    99  						Ip:            &types.CustomizationUnknownIpGenerator{},
   100  						SubnetMask:    "255.255.255.0",
   101  						Gateway:       []string{"10.0.0.1"},
   102  						IpV6Spec:      (*types.CustomizationIPSettingsIpV6AddressSpec)(nil),
   103  						DnsServerList: nil,
   104  						DnsDomain:     "",
   105  						PrimaryWINS:   "",
   106  						SecondaryWINS: "",
   107  						NetBIOS:       "",
   108  					},
   109  				},
   110  			},
   111  			EncryptionKey: nil,
   112  		},
   113  	},
   114  	{
   115  		Info: types.CustomizationSpecInfo{
   116  			Name:           "vcsim-windows-static",
   117  			Description:    "",
   118  			Type:           "Windows",
   119  			ChangeVersion:  "1569978029",
   120  			LastUpdateTime: types.NewTime(time.Now()),
   121  		},
   122  		Spec: types.CustomizationSpec{
   123  			Options: &types.CustomizationWinOptions{
   124  				CustomizationOptions: types.CustomizationOptions{},
   125  				ChangeSID:            true,
   126  				DeleteAccounts:       false,
   127  				Reboot:               "",
   128  			},
   129  			Identity: &types.CustomizationSysprep{
   130  				CustomizationIdentitySettings: types.CustomizationIdentitySettings{},
   131  				GuiUnattended: types.CustomizationGuiUnattended{
   132  					Password:       (*types.CustomizationPassword)(nil),
   133  					TimeZone:       2,
   134  					AutoLogon:      false,
   135  					AutoLogonCount: 1,
   136  				},
   137  				UserData: types.CustomizationUserData{
   138  					FullName:     "vcsim",
   139  					OrgName:      "VMware",
   140  					ComputerName: &types.CustomizationVirtualMachineName{},
   141  					ProductId:    "",
   142  				},
   143  				GuiRunOnce: (*types.CustomizationGuiRunOnce)(nil),
   144  				Identification: types.CustomizationIdentification{
   145  					JoinWorkgroup:       "WORKGROUP",
   146  					JoinDomain:          "",
   147  					DomainAdmin:         "",
   148  					DomainAdminPassword: (*types.CustomizationPassword)(nil),
   149  				},
   150  				LicenseFilePrintData: &types.CustomizationLicenseFilePrintData{
   151  					AutoMode:  "perServer",
   152  					AutoUsers: 5,
   153  				},
   154  			},
   155  			GlobalIPSettings: types.CustomizationGlobalIPSettings{},
   156  			NicSettingMap: []types.CustomizationAdapterMapping{
   157  				{
   158  					MacAddress: "",
   159  					Adapter: types.CustomizationIPSettings{
   160  						Ip:            &types.CustomizationUnknownIpGenerator{},
   161  						SubnetMask:    "255.255.255.0",
   162  						Gateway:       []string{"10.0.0.1"},
   163  						IpV6Spec:      (*types.CustomizationIPSettingsIpV6AddressSpec)(nil),
   164  						DnsServerList: nil,
   165  						DnsDomain:     "",
   166  						PrimaryWINS:   "",
   167  						SecondaryWINS: "",
   168  						NetBIOS:       "",
   169  					},
   170  				},
   171  			},
   172  			EncryptionKey: []uint8{0x30},
   173  		},
   174  	},
   175  	{
   176  		Info: types.CustomizationSpecInfo{
   177  			Name:           "vcsim-windows-domain",
   178  			Description:    "",
   179  			Type:           "Windows",
   180  			ChangeVersion:  "1569970234",
   181  			LastUpdateTime: types.NewTime(time.Now()),
   182  		},
   183  		Spec: types.CustomizationSpec{
   184  			Options: &types.CustomizationWinOptions{
   185  				CustomizationOptions: types.CustomizationOptions{},
   186  				ChangeSID:            true,
   187  				DeleteAccounts:       false,
   188  				Reboot:               "",
   189  			},
   190  			Identity: &types.CustomizationSysprep{
   191  				CustomizationIdentitySettings: types.CustomizationIdentitySettings{},
   192  				GuiUnattended: types.CustomizationGuiUnattended{
   193  					Password: &types.CustomizationPassword{
   194  						Value:     "3Gs...==",
   195  						PlainText: false,
   196  					},
   197  					TimeZone:       15,
   198  					AutoLogon:      false,
   199  					AutoLogonCount: 1,
   200  				},
   201  				UserData: types.CustomizationUserData{
   202  					FullName:     "dougm",
   203  					OrgName:      "VMware",
   204  					ComputerName: &types.CustomizationVirtualMachineName{},
   205  					ProductId:    "",
   206  				},
   207  				GuiRunOnce: (*types.CustomizationGuiRunOnce)(nil),
   208  				Identification: types.CustomizationIdentification{
   209  					JoinWorkgroup: "",
   210  					JoinDomain:    "DOMAIN",
   211  					DomainAdmin:   "vcsim",
   212  					DomainAdminPassword: &types.CustomizationPassword{
   213  						Value:     "H3g...==",
   214  						PlainText: false,
   215  					},
   216  				},
   217  				LicenseFilePrintData: &types.CustomizationLicenseFilePrintData{
   218  					AutoMode:  "perServer",
   219  					AutoUsers: 5,
   220  				},
   221  			},
   222  			GlobalIPSettings: types.CustomizationGlobalIPSettings{},
   223  			NicSettingMap: []types.CustomizationAdapterMapping{
   224  				{
   225  					MacAddress: "",
   226  					Adapter: types.CustomizationIPSettings{
   227  						Ip:            &types.CustomizationUnknownIpGenerator{},
   228  						SubnetMask:    "255.255.255.0",
   229  						Gateway:       []string{"10.0.0.1"},
   230  						IpV6Spec:      (*types.CustomizationIPSettingsIpV6AddressSpec)(nil),
   231  						DnsServerList: nil,
   232  						DnsDomain:     "",
   233  						PrimaryWINS:   "",
   234  						SecondaryWINS: "",
   235  						NetBIOS:       "",
   236  					},
   237  				},
   238  			},
   239  			EncryptionKey: []uint8{0x30},
   240  		},
   241  	},
   242  }
   243  
   244  type CustomizationSpecManager struct {
   245  	mo.CustomizationSpecManager
   246  
   247  	items []types.CustomizationSpecItem
   248  }
   249  
   250  func (m *CustomizationSpecManager) init(r *Registry) {
   251  	m.items = DefaultCustomizationSpec
   252  }
   253  
   254  var customizeNameCounter uint64
   255  
   256  func customizeName(vm *VirtualMachine, base types.BaseCustomizationName) string {
   257  	n := atomic.AddUint64(&customizeNameCounter, 1)
   258  
   259  	switch name := base.(type) {
   260  	case *types.CustomizationPrefixName:
   261  		return fmt.Sprintf("%s-%d", name.Base, n)
   262  	case *types.CustomizationCustomName:
   263  		return fmt.Sprintf("%s-%d", name.Argument, n)
   264  	case *types.CustomizationFixedName:
   265  		return name.Name
   266  	case *types.CustomizationUnknownName:
   267  		return ""
   268  	case *types.CustomizationVirtualMachineName:
   269  		return fmt.Sprintf("%s-%d", vm.Name, n)
   270  	default:
   271  		return ""
   272  	}
   273  }
   274  
   275  func (m *CustomizationSpecManager) DoesCustomizationSpecExist(ctx *Context, req *types.DoesCustomizationSpecExist) soap.HasFault {
   276  	exists := false
   277  
   278  	for _, item := range m.items {
   279  		if item.Info.Name == req.Name {
   280  			exists = true
   281  			break
   282  		}
   283  	}
   284  
   285  	return &methods.DoesCustomizationSpecExistBody{
   286  		Res: &types.DoesCustomizationSpecExistResponse{
   287  			Returnval: exists,
   288  		},
   289  	}
   290  }
   291  
   292  func (m *CustomizationSpecManager) GetCustomizationSpec(ctx *Context, req *types.GetCustomizationSpec) soap.HasFault {
   293  	body := new(methods.GetCustomizationSpecBody)
   294  
   295  	for _, item := range m.items {
   296  		if item.Info.Name == req.Name {
   297  			body.Res = &types.GetCustomizationSpecResponse{
   298  				Returnval: item,
   299  			}
   300  			return body
   301  		}
   302  	}
   303  
   304  	body.Fault_ = Fault("", new(types.NotFound))
   305  
   306  	return body
   307  }
   308  
   309  func (m *CustomizationSpecManager) CreateCustomizationSpec(ctx *Context, req *types.CreateCustomizationSpec) soap.HasFault {
   310  	body := new(methods.CreateCustomizationSpecBody)
   311  
   312  	for _, item := range m.items {
   313  		if item.Info.Name == req.Item.Info.Name {
   314  			body.Fault_ = Fault("", &types.AlreadyExists{Name: req.Item.Info.Name})
   315  			return body
   316  		}
   317  	}
   318  
   319  	m.items = append(m.items, req.Item)
   320  	body.Res = new(types.CreateCustomizationSpecResponse)
   321  
   322  	return body
   323  }
   324  
   325  func (m *CustomizationSpecManager) OverwriteCustomizationSpec(ctx *Context, req *types.OverwriteCustomizationSpec) soap.HasFault {
   326  	body := new(methods.OverwriteCustomizationSpecBody)
   327  
   328  	for i, item := range m.items {
   329  		if item.Info.Name == req.Item.Info.Name {
   330  			m.items[i] = req.Item
   331  			body.Res = new(types.OverwriteCustomizationSpecResponse)
   332  			return body
   333  		}
   334  	}
   335  
   336  	body.Fault_ = Fault("", new(types.NotFound))
   337  
   338  	return body
   339  }
   340  
   341  func (m *CustomizationSpecManager) Get() mo.Reference {
   342  	clone := *m
   343  
   344  	for i := range clone.items {
   345  		clone.Info = append(clone.Info, clone.items[i].Info)
   346  	}
   347  
   348  	return &clone
   349  }