1/*
2Copyright (c) 2015 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 object
18
19import (
20	"context"
21	"errors"
22
23	"github.com/vmware/govmomi/vim25"
24	"github.com/vmware/govmomi/vim25/methods"
25	"github.com/vmware/govmomi/vim25/types"
26)
27
28type HostStorageSystem struct {
29	Common
30}
31
32func NewHostStorageSystem(c *vim25.Client, ref types.ManagedObjectReference) *HostStorageSystem {
33	return &HostStorageSystem{
34		Common: NewCommon(c, ref),
35	}
36}
37
38func (s HostStorageSystem) RetrieveDiskPartitionInfo(ctx context.Context, devicePath string) (*types.HostDiskPartitionInfo, error) {
39	req := types.RetrieveDiskPartitionInfo{
40		This:       s.Reference(),
41		DevicePath: []string{devicePath},
42	}
43
44	res, err := methods.RetrieveDiskPartitionInfo(ctx, s.c, &req)
45	if err != nil {
46		return nil, err
47	}
48
49	if res.Returnval == nil || len(res.Returnval) == 0 {
50		return nil, errors.New("no partition info")
51	}
52
53	return &res.Returnval[0], nil
54}
55
56func (s HostStorageSystem) ComputeDiskPartitionInfo(ctx context.Context, devicePath string, layout types.HostDiskPartitionLayout) (*types.HostDiskPartitionInfo, error) {
57	req := types.ComputeDiskPartitionInfo{
58		This:       s.Reference(),
59		DevicePath: devicePath,
60		Layout:     layout,
61	}
62
63	res, err := methods.ComputeDiskPartitionInfo(ctx, s.c, &req)
64	if err != nil {
65		return nil, err
66	}
67
68	return &res.Returnval, nil
69}
70
71func (s HostStorageSystem) UpdateDiskPartitionInfo(ctx context.Context, devicePath string, spec types.HostDiskPartitionSpec) error {
72	req := types.UpdateDiskPartitions{
73		This:       s.Reference(),
74		DevicePath: devicePath,
75		Spec:       spec,
76	}
77
78	_, err := methods.UpdateDiskPartitions(ctx, s.c, &req)
79	return err
80}
81
82func (s HostStorageSystem) RescanAllHba(ctx context.Context) error {
83	req := types.RescanAllHba{
84		This: s.Reference(),
85	}
86
87	_, err := methods.RescanAllHba(ctx, s.c, &req)
88	return err
89}
90
91func (s HostStorageSystem) Refresh(ctx context.Context) error {
92	req := types.RefreshStorageSystem{
93		This: s.Reference(),
94	}
95
96	_, err := methods.RefreshStorageSystem(ctx, s.c, &req)
97	return err
98}
99
100func (s HostStorageSystem) RescanVmfs(ctx context.Context) error {
101	req := types.RescanVmfs{
102		This: s.Reference(),
103	}
104
105	_, err := methods.RescanVmfs(ctx, s.c, &req)
106	return err
107}
108
109func (s HostStorageSystem) MarkAsSsd(ctx context.Context, uuid string) (*Task, error) {
110	req := types.MarkAsSsd_Task{
111		This:         s.Reference(),
112		ScsiDiskUuid: uuid,
113	}
114
115	res, err := methods.MarkAsSsd_Task(ctx, s.c, &req)
116	if err != nil {
117		return nil, err
118	}
119
120	return NewTask(s.c, res.Returnval), nil
121}
122
123func (s HostStorageSystem) MarkAsNonSsd(ctx context.Context, uuid string) (*Task, error) {
124	req := types.MarkAsNonSsd_Task{
125		This:         s.Reference(),
126		ScsiDiskUuid: uuid,
127	}
128
129	res, err := methods.MarkAsNonSsd_Task(ctx, s.c, &req)
130	if err != nil {
131		return nil, err
132	}
133
134	return NewTask(s.c, res.Returnval), nil
135}
136
137func (s HostStorageSystem) MarkAsLocal(ctx context.Context, uuid string) (*Task, error) {
138	req := types.MarkAsLocal_Task{
139		This:         s.Reference(),
140		ScsiDiskUuid: uuid,
141	}
142
143	res, err := methods.MarkAsLocal_Task(ctx, s.c, &req)
144	if err != nil {
145		return nil, err
146	}
147
148	return NewTask(s.c, res.Returnval), nil
149}
150
151func (s HostStorageSystem) MarkAsNonLocal(ctx context.Context, uuid string) (*Task, error) {
152	req := types.MarkAsNonLocal_Task{
153		This:         s.Reference(),
154		ScsiDiskUuid: uuid,
155	}
156
157	res, err := methods.MarkAsNonLocal_Task(ctx, s.c, &req)
158	if err != nil {
159		return nil, err
160	}
161
162	return NewTask(s.c, res.Returnval), nil
163}
164
165func (s HostStorageSystem) AttachScsiLun(ctx context.Context, uuid string) error {
166	req := types.AttachScsiLun{
167		This:    s.Reference(),
168		LunUuid: uuid,
169	}
170
171	_, err := methods.AttachScsiLun(ctx, s.c, &req)
172
173	return err
174}
175