Skip to content

Commit 38f40cc

Browse files
committed
Move WMI service functions to cim package
1 parent dac0282 commit 38f40cc

File tree

3 files changed

+114
-83
lines changed

3 files changed

+114
-83
lines changed

pkg/cim/system.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import (
1010
"github.com/microsoft/wmi/server2019/root/cimv2"
1111
)
1212

13+
var (
14+
BIOSSelectorList = []string{"SerialNumber"}
15+
ServiceSelectorList = []string{"DisplayName", "State", "StartMode"}
16+
)
17+
18+
type ServiceInterface interface {
19+
GetPropertyName() (string, error)
20+
GetPropertyDisplayName() (string, error)
21+
GetPropertyState() (string, error)
22+
GetPropertyStartMode() (string, error)
23+
GetDependents() ([]ServiceInterface, error)
24+
StartService() (result uint32, err error)
25+
StopService() (result uint32, err error)
26+
}
27+
1328
// QueryBIOSElement retrieves the BIOS element.
1429
//
1530
// The equivalent WMI query is:
@@ -33,6 +48,11 @@ func QueryBIOSElement(selectorList []string) (*cimv2.CIM_BIOSElement, error) {
3348
return bios, err
3449
}
3550

51+
// GetBIOSSerialNumber returns the BIOS serial number.
52+
func GetBIOSSerialNumber(bios *cimv2.CIM_BIOSElement) (string, error) {
53+
return bios.GetPropertySerialNumber()
54+
}
55+
3656
// QueryServiceByName retrieves a specific service by its name.
3757
//
3858
// The equivalent WMI query is:
@@ -55,3 +75,60 @@ func QueryServiceByName(name string, selectorList []string) (*cimv2.Win32_Servic
5575

5676
return service, err
5777
}
78+
79+
// GetServiceName returns the name of a service.
80+
func GetServiceName(service ServiceInterface) (string, error) {
81+
return service.GetPropertyName()
82+
}
83+
84+
// GetServiceDisplayName returns the display name of a service.
85+
func GetServiceDisplayName(service ServiceInterface) (string, error) {
86+
return service.GetPropertyDisplayName()
87+
}
88+
89+
// GetServiceState returns the state of a service.
90+
func GetServiceState(service ServiceInterface) (string, error) {
91+
return service.GetPropertyState()
92+
}
93+
94+
// GetServiceStartMode returns the start mode of a service.
95+
func GetServiceStartMode(service ServiceInterface) (string, error) {
96+
return service.GetPropertyStartMode()
97+
}
98+
99+
// Win32Service wraps the WMI class Win32_Service (mainly for testing)
100+
type Win32Service struct {
101+
*cimv2.Win32_Service
102+
}
103+
104+
func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
105+
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
var result []ServiceInterface
111+
for _, coll := range collection {
112+
service, err := cimv2.NewWin32_ServiceEx1(coll)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
result = append(result, &Win32Service{
118+
service,
119+
})
120+
}
121+
return result, nil
122+
}
123+
124+
type Win32ServiceFactory struct {
125+
}
126+
127+
func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
128+
service, err := QueryServiceByName(name, ServiceSelectorList)
129+
if err != nil {
130+
return nil, err
131+
}
132+
133+
return &Win32Service{Win32_Service: service}, nil
134+
}

pkg/os/system/api.go

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/kubernetes-csi/csi-proxy/pkg/cim"
88
"github.com/kubernetes-csi/csi-proxy/pkg/server/system/impl"
9-
"github.com/microsoft/wmi/server2019/root/cimv2"
109
"github.com/pkg/errors"
1110
)
1211

@@ -26,8 +25,8 @@ type ServiceInfo struct {
2625
Status uint32 `json:"Status"`
2726
}
2827

29-
type stateCheckFunc func() (bool, string, ServiceInterface, error)
30-
type stateTransitionFunc func(ServiceInterface) error
28+
type stateCheckFunc func() (bool, string, cim.ServiceInterface, error)
29+
type stateTransitionFunc func(cim.ServiceInterface) error
3130

3231
const (
3332
// startServiceErrorCodeAccepted indicates the request is accepted
@@ -82,23 +81,13 @@ func serviceState(status string) uint32 {
8281
return stateMappings[status]
8382
}
8483

85-
type ServiceInterface interface {
86-
GetPropertyName() (string, error)
87-
GetPropertyDisplayName() (string, error)
88-
GetPropertyState() (string, error)
89-
GetPropertyStartMode() (string, error)
90-
GetDependents() ([]ServiceInterface, error)
91-
StartService() (result uint32, err error)
92-
StopService() (result uint32, err error)
93-
}
94-
9584
type ServiceManager interface {
9685
WaitUntilServiceState(stateTransition stateTransitionFunc, stateCheck stateCheckFunc, interval time.Duration, timeout time.Duration) (string, error)
9786
GetDependentsForService(name string) ([]string, error)
9887
}
9988

10089
type ServiceFactory interface {
101-
GetService(name string) (ServiceInterface, error)
90+
GetService(name string) (cim.ServiceInterface, error)
10291
}
10392

10493
type APIImplementor struct {
@@ -107,7 +96,7 @@ type APIImplementor struct {
10796
}
10897

10998
func New() APIImplementor {
110-
serviceFactory := Win32ServiceFactory{}
99+
serviceFactory := cim.Win32ServiceFactory{}
111100
return APIImplementor{
112101
serviceFactory: serviceFactory,
113102
serviceManager: ServiceManagerImpl{
@@ -117,36 +106,36 @@ func New() APIImplementor {
117106
}
118107

119108
func (APIImplementor) GetBIOSSerialNumber() (string, error) {
120-
bios, err := cim.QueryBIOSElement([]string{"SerialNumber"})
109+
bios, err := cim.QueryBIOSElement(cim.BIOSSelectorList)
121110
if err != nil {
122111
return "", fmt.Errorf("failed to get BIOS element: %w", err)
123112
}
124113

125-
sn, err := bios.GetPropertySerialNumber()
114+
sn, err := cim.GetBIOSSerialNumber(bios)
126115
if err != nil {
127116
return "", fmt.Errorf("failed to get BIOS serial number property: %w", err)
128117
}
129118

130119
return sn, nil
131120
}
132121

133-
func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
134-
service, err := cim.QueryServiceByName(name, []string{"DisplayName", "State", "StartMode"})
122+
func (impl APIImplementor) GetService(name string) (*ServiceInfo, error) {
123+
service, err := impl.serviceFactory.GetService(name)
135124
if err != nil {
136125
return nil, fmt.Errorf("failed to get service %s: %w", name, err)
137126
}
138127

139-
displayName, err := service.GetPropertyDisplayName()
128+
displayName, err := cim.GetServiceDisplayName(service)
140129
if err != nil {
141130
return nil, fmt.Errorf("failed to get displayName property of service %s: %w", name, err)
142131
}
143132

144-
state, err := service.GetPropertyState()
133+
state, err := cim.GetServiceState(service)
145134
if err != nil {
146135
return nil, fmt.Errorf("failed to get state property of service %s: %w", name, err)
147136
}
148137

149-
startMode, err := service.GetPropertyStartMode()
138+
startMode, err := cim.GetServiceStartMode(service)
150139
if err != nil {
151140
return nil, fmt.Errorf("failed to get startMode property of service %s: %w", name, err)
152141
}
@@ -159,20 +148,20 @@ func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
159148
}
160149

161150
func (impl APIImplementor) StartService(name string) error {
162-
startService := func(service ServiceInterface) error {
151+
startService := func(service cim.ServiceInterface) error {
163152
retVal, err := service.StartService()
164153
if err != nil || (retVal != startServiceErrorCodeAccepted && retVal != startServiceErrorCodeAlreadyRunning) {
165154
return fmt.Errorf("error starting service name %s. return value: %d, error: %v", name, retVal, err)
166155
}
167156
return nil
168157
}
169-
serviceRunningCheck := func() (bool, string, ServiceInterface, error) {
158+
serviceRunningCheck := func() (bool, string, cim.ServiceInterface, error) {
170159
service, err := impl.serviceFactory.GetService(name)
171160
if err != nil {
172161
return false, "", nil, err
173162
}
174163

175-
state, err := service.GetPropertyState()
164+
state, err := cim.GetServiceState(service)
176165
if err != nil {
177166
return false, state, service, err
178167
}
@@ -194,7 +183,7 @@ func (impl APIImplementor) StartService(name string) error {
194183

195184
func (impl APIImplementor) stopSingleService(name string) (bool, error) {
196185
var dependentRunning bool
197-
stopService := func(service ServiceInterface) error {
186+
stopService := func(service cim.ServiceInterface) error {
198187
retVal, err := service.StopService()
199188
if err != nil || (retVal != stopServiceErrorCodeAccepted && retVal != stopServiceErrorCodeStopPending) {
200189
if retVal == stopServiceErrorCodeDependentRunning {
@@ -205,13 +194,13 @@ func (impl APIImplementor) stopSingleService(name string) (bool, error) {
205194
}
206195
return nil
207196
}
208-
serviceStoppedCheck := func() (bool, string, ServiceInterface, error) {
197+
serviceStoppedCheck := func() (bool, string, cim.ServiceInterface, error) {
209198
service, err := impl.serviceFactory.GetService(name)
210199
if err != nil {
211200
return false, "", nil, err
212201
}
213202

214-
state, err := service.GetPropertyState()
203+
state, err := cim.GetServiceState(service)
215204
if err != nil {
216205
return false, state, service, err
217206
}
@@ -252,42 +241,6 @@ func (impl APIImplementor) StopService(name string, force bool) error {
252241
return nil
253242
}
254243

255-
type Win32Service struct {
256-
*cimv2.Win32_Service
257-
}
258-
259-
func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
260-
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
261-
if err != nil {
262-
return nil, err
263-
}
264-
265-
var result []ServiceInterface
266-
for _, coll := range collection {
267-
service, err := cimv2.NewWin32_ServiceEx1(coll)
268-
if err != nil {
269-
return nil, err
270-
}
271-
272-
result = append(result, &Win32Service{
273-
service,
274-
})
275-
}
276-
return result, nil
277-
}
278-
279-
type Win32ServiceFactory struct {
280-
}
281-
282-
func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
283-
service, err := cim.QueryServiceByName(name, nil)
284-
if err != nil {
285-
return nil, err
286-
}
287-
288-
return &Win32Service{Win32_Service: service}, nil
289-
}
290-
291244
type ServiceManagerImpl struct {
292245
serviceFactory ServiceFactory
293246
}
@@ -330,7 +283,7 @@ func (impl ServiceManagerImpl) WaitUntilServiceState(stateTransition stateTransi
330283

331284
func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, error) {
332285
var serviceNames []string
333-
var servicesToCheck []ServiceInterface
286+
var servicesToCheck []cim.ServiceInterface
334287
servicesByName := map[string]string{}
335288

336289
service, err := impl.serviceFactory.GetService(name)
@@ -344,12 +297,12 @@ func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, e
344297
service = servicesToCheck[i]
345298
i += 1
346299

347-
serviceName, err := service.GetPropertyName()
300+
serviceName, err := cim.GetServiceName(service)
348301
if err != nil {
349302
return serviceNames, err
350303
}
351304

352-
currentState, err := service.GetPropertyState()
305+
currentState, err := cim.GetServiceState(service)
353306
if err != nil {
354307
return serviceNames, err
355308
}

0 commit comments

Comments
 (0)