-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathvolume_metadata_info.go
More file actions
113 lines (103 loc) · 2.85 KB
/
volume_metadata_info.go
File metadata and controls
113 lines (103 loc) · 2.85 KB
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
109
110
111
112
113
/*
* 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.
*
* Copyright 2025 Red Hat, Inc.
*
*/
package main
import (
"context"
"encoding/json"
"fmt"
"os"
osm_os "vmware-migration-kit/plugins/module_utils/openstack"
)
// Ansible
type ModuleArgs struct {
DstCloud osm_os.DstCloud `json:"dst_cloud"`
VolumeID string `json:"volume_id"`
}
type Response struct {
Msg string `json:"msg"`
Changed bool `json:"changed"`
Failed bool `json:"failed"`
Converted bool `json:"converted"`
}
func ExitJson(responseBody Response) {
returnResponse(responseBody)
}
func FailJson(responseBody Response) {
responseBody.Failed = true
returnResponse(responseBody)
}
func FailWithMessage(msg string) {
response := Response{Msg: msg}
FailJson(response)
}
func returnResponse(responseBody Response) {
var response []byte
var err error
response, err = json.Marshal(responseBody)
if err != nil {
response, _ = json.Marshal(Response{Msg: "Invalid response object"})
}
fmt.Println(string(response))
if responseBody.Failed {
os.Exit(1)
} else {
os.Exit(0)
}
}
func main() {
var response Response
if len(os.Args) != 2 {
response.Msg = "No argument file provided"
FailJson(response)
}
argsFile := os.Args[1]
text, err := os.ReadFile(argsFile)
if err != nil {
response.Msg = "Could not read configuration file: " + argsFile
FailJson(response)
}
var moduleArgs ModuleArgs
err = json.Unmarshal(text, &moduleArgs)
if err != nil {
response.Msg = "Configuration file not valid JSON: " + argsFile
FailJson(response)
}
// Get the volume metadata
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
provider, err := osm_os.OpenstackAuth(ctx, moduleArgs.DstCloud)
if err != nil {
response.Msg = "Failed to authenticate Openstack client: " + err.Error()
FailJson(response)
}
converted, err := osm_os.IsVolumeConverted(provider, moduleArgs.VolumeID)
if err != nil {
response.Msg = "Failed to get volume metadata: " + moduleArgs.VolumeID + " error: " + err.Error()
FailJson(response)
}
if !converted {
converted, err = osm_os.IsVolumeReady(provider, moduleArgs.VolumeID)
if err != nil {
response.Msg = "Failed to get volume state: " + moduleArgs.VolumeID + " error: " + err.Error()
FailJson(response)
}
}
response.Changed = true
response.Msg = "Volume metadata info"
response.Converted = converted
ExitJson(response)
}