forked from vmware/vic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vch_delete.go
108 lines (88 loc) · 4.11 KB
/
vch_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017-2018 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handlers
import (
"net/http"
"github.com/go-openapi/runtime/middleware"
"github.com/vmware/vic/lib/apiservers/service/models"
"github.com/vmware/vic/lib/apiservers/service/restapi/handlers/decode"
"github.com/vmware/vic/lib/apiservers/service/restapi/handlers/errors"
"github.com/vmware/vic/lib/apiservers/service/restapi/handlers/target"
"github.com/vmware/vic/lib/apiservers/service/restapi/operations"
"github.com/vmware/vic/lib/install/management"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/version"
)
// VCHDelete is the handler for deleting a VCH without specifying a datacenter
type VCHDelete struct {
vchDelete
}
// VCHDatacenterDelete is the handler for deleting a VCH within a specified datacenter
type VCHDatacenterDelete struct {
vchDelete
}
// vchDelete allows for VCHDelete and VCHDatacenterDelete to share common code without polluting the package
type vchDelete struct{}
// Handle is the handler implementation for deleting a VCH without specifying a datacenter
func (h *VCHDelete) Handle(params operations.DeleteTargetTargetVchVchIDParams, principal interface{}) middleware.Responder {
op := trace.FromContext(params.HTTPRequest.Context(), "VCHDelete: %s", params.VchID)
b := target.Params{
Target: params.Target,
Thumbprint: params.Thumbprint,
VCHID: ¶ms.VchID,
}
err := h.handle(op, b, principal, params.DeletionSpecification)
if err != nil {
return operations.NewDeleteTargetTargetVchVchIDDefault(errors.StatusCode(err)).WithPayload(&models.Error{Message: err.Error()})
}
return operations.NewDeleteTargetTargetVchVchIDAccepted()
}
// Handle is the handler implementation for deleting a VCH within a specified datacenter
func (h *VCHDatacenterDelete) Handle(params operations.DeleteTargetTargetDatacenterDatacenterVchVchIDParams, principal interface{}) middleware.Responder {
op := trace.FromContext(params.HTTPRequest.Context(), "VCHDatacenterDelete: %s", params.VchID)
b := target.Params{
Target: params.Target,
Thumbprint: params.Thumbprint,
Datacenter: ¶ms.Datacenter,
VCHID: ¶ms.VchID,
}
err := h.handle(op, b, principal, params.DeletionSpecification)
if err != nil {
return operations.NewDeleteTargetTargetDatacenterDatacenterVchVchIDDefault(errors.StatusCode(err)).WithPayload(&models.Error{Message: err.Error()})
}
return operations.NewDeleteTargetTargetDatacenterDatacenterVchVchIDAccepted()
}
// handle deletes the VCH described by params based on the preferences expressed by specification, using the credentials
// from principal. If the VCH cannot be found, a 404 is returned. If an error occurs during deletion, a 500 is returned.
func (h *vchDelete) handle(op trace.Operation, params target.Params, principal interface{}, specification *models.DeletionSpecification) error {
d, c, err := target.Validate(op, management.ActionDelete, params, principal)
if err != nil {
return err
}
vchConfig, err := c.GetVCHConfig(op, d)
if err != nil {
return err
}
// compare vch version and vic-machine version
installerBuild := version.GetBuild()
if vchConfig.Version == nil || !installerBuild.Equal(vchConfig.Version) {
op.Debugf("VCH version %q is different than API version %s", vchConfig.Version.ShortVersion(), installerBuild.ShortVersion())
}
deleteContainers, deleteVolumeStores := decode.FromDeletionSpecification(specification)
err = c.Executor().DeleteVCH(vchConfig, deleteContainers, deleteVolumeStores)
if err != nil {
return errors.NewError(http.StatusInternalServerError, "failed to delete VCH: %s", err)
}
return nil
}