1/*
2Copyright (c) 2015-2017 VMware, Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package types
18
19import (
20	"reflect"
21	"strings"
22	"time"
23)
24
25func NewBool(v bool) *bool {
26	return &v
27}
28
29func NewInt32(v int32) *int32 {
30	return &v
31}
32
33func NewInt64(v int64) *int64 {
34	return &v
35}
36
37func NewTime(v time.Time) *time.Time {
38	return &v
39}
40
41func NewReference(r ManagedObjectReference) *ManagedObjectReference {
42	return &r
43}
44
45func (r ManagedObjectReference) Reference() ManagedObjectReference {
46	return r
47}
48
49func (r ManagedObjectReference) String() string {
50	return strings.Join([]string{r.Type, r.Value}, ":")
51}
52
53func (r *ManagedObjectReference) FromString(o string) bool {
54	s := strings.SplitN(o, ":", 2)
55
56	if len(s) < 2 {
57		return false
58	}
59
60	r.Type = s[0]
61	r.Value = s[1]
62
63	return true
64}
65
66func (c *PerfCounterInfo) Name() string {
67	return c.GroupInfo.GetElementDescription().Key + "." + c.NameInfo.GetElementDescription().Key + "." + string(c.RollupType)
68}
69
70func defaultResourceAllocationInfo() ResourceAllocationInfo {
71	return ResourceAllocationInfo{
72		Reservation:           NewInt64(0),
73		ExpandableReservation: NewBool(true),
74		Limit: NewInt64(-1),
75		Shares: &SharesInfo{
76			Level: SharesLevelNormal,
77		},
78	}
79}
80
81// DefaultResourceConfigSpec returns a ResourceConfigSpec populated with the same default field values as vCenter.
82// Note that the wsdl marks these fields as optional, but they are required to be set when creating a resource pool.
83// They are only optional when updating a resource pool.
84func DefaultResourceConfigSpec() ResourceConfigSpec {
85	return ResourceConfigSpec{
86		CpuAllocation:    defaultResourceAllocationInfo(),
87		MemoryAllocation: defaultResourceAllocationInfo(),
88	}
89}
90
91func init() {
92	// Known 6.5 issue where this event type is sent even though it is internal.
93	// This workaround allows us to unmarshal and avoid NPEs.
94	t["HostSubSpecificationUpdateEvent"] = reflect.TypeOf((*HostEvent)(nil)).Elem()
95}
96